Article View: comp.lang.c
Article #97Re: Taming The Switch-Statement
From: Lawrence D’Oliveiro
Date: Sat, 10 Oct 2026 00:25
Date: Sat, 10 Oct 2026 00:25
74 lines
4380 bytes
4380 bytes
For comparison, here is an example of Python’s match-statement. Like
all the good (i.e. non-C-based) designs, the designers saw no point in
having fall-through.
This is a very simple example, involving straight matching of values
against instances of an enum. There are also more complex
pattern-matching options available.
for b in sb :
self.chars_seen.append(b)
match self.parse_state :
case PARSE_STATE.EXPECT_LEN :
if ord("0") <= b <= ord("9") :
digit = b - ord("0")
if digit == 0 and len(self.chars_seen) == 2 and self.len == 0 :
raise NetstringSyntaxError \
(
"length must not have leading zero: %s"
%
bytes(self.chars_seen).decode()
)
#end if
if (
self.len > MAX_NETSTRING_LEN // 10
or
self.len == MAX_NETSTRING_LEN // 10
and
digit > MAX_NETSTRING_LEN % 10
) :
raise NetstringSyntaxError \
("length too large: %s" % bytes(self.chars_seen).decode())
#end if
self.len = self.len * 10 + digit
elif b == ord(":") :
if len(self.chars_seen) < 2 :
raise NetstringSyntaxError \
("missing length prefix: %s" % repr(bytes(self.chars_seen)))
#end if
if self.len != 0 :
self.parse_state = PARSE_STATE.EXPECT_CHARS
else :
self.parse_state = PARSE_STATE.EXPECT_END
#end if
self.content = []
else :
raise NetstringSyntaxError \
(
"expecting digits of length or colon: %s"
%
repr(bytes(self.chars_seen))
)
#end if
#end case
case PARSE_STATE.EXPECT_CHARS :
self.content.append(b)
self.len -= 1
if self.len == 0 :
self.parse_state = PARSE_STATE.EXPECT_END
#end if
#end case
case PARSE_STATE.EXPECT_END :
if b == ord(",") :
self.parse_state = PARSE_STATE.PARSE_DONE
else :
raise NetstringSyntaxError \
("expecting comma after netstring: %s" % repr(bytes(self.chars_seen)))
#end if
#end case
case PARSE_STATE.PARSE_DONE :
raise NetstringSyntaxError \
("junk after complete netstring: %s" % repr(bytes(self.chars_seen)))
#end case
#end match
#end for
Technical Details
Message-ID:
<11ac0mi$19kfd$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Lawrence D’Oliveiro <ldo@nz.invalid> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sat, 10 Oct 2026 00:25:54 -0000 (UTC) Organization: A noiseless patient Spider Lines: 74 Message-ID: <11ac0mi$19kfd$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Sat, 10 Oct 2026 00:25:54 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1364461"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZmX8t4e9UfAYRfqCc9JiG"; posting-host="b79ec01f0c353e9abab4d37c266d4491" User-Agent: Pan/0.165 (Kostiantynivka) Cancel-Lock: sha1:8HTbvBUVOdyhZnbX2l6EB+j+Ukk= sha256:g70iJLFQzpXWZwYacet/EJr/wL9cnzjZDEWAXNrH7fQ= sha1:InJmFLycgkN3zrQCjOGUpzj1Ylc= sha256:/D2O4yXEXgSg2cKelCAhD24BnxNBfbA9xmRyforLLCQ=