Article View: comp.lang.c
Article #98Re: Taming The Switch-Statement
From: bart
Date: Sat, 10 Oct 2026 00:58
Date: Sat, 10 Oct 2026 00:58
38 lines
2566 bytes
2566 bytes
On 10/10/2026 01:25, Lawrence D’Oliveiro wrote:
> 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.
C, doesn't specifically have fallthrough. It's just that C's switch is
so bizarre, 'fallthrough' is a consequence of it being so dumb it
doesn't know it's still inside a switch statement; it is just code that
it carries ploughing through until it hits more control flow.
> 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 :
I think Python 'match' is just linear testing (I didn't notice any
speed-ups when I tried it).
Real 'switch' allows for a jump-table and O(1) parallel matching against
N unique possibilities.
> case PARSE_STATE.EXPECT_LEN :
A jump-table would need all these expressions to be known at
compile-time, and to be immutable. (This is why Python can't have nice
things.)
> if ord("0") <= b <= ord("9") :
Why not:
if b in digits: # digits is range(ord("0"), ord("9")+1)
Technical Details
Message-ID:
<11ac2jv$1a84t$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> <11ac0mi$19kfd$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: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sat, 10 Oct 2026 01:58:38 +0100 Organization: A noiseless patient Spider Lines: 38 Message-ID: <11ac2jv$1a84t$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 10 Oct 2026 00:58:39 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1384605"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+nH9uyLyilwglqVZuZhzH7"; posting-host="8d9c8b578a5ce6a1f662bc81cc091c91" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:deEDfNwHO4eyCp6tLZUWHPZQGCk= sha256:5HseEpCIyYq/URFaTu94HColZzjCa5HloZaQdfMVkMk= sha1:kNhXLa+1XjkHp7DrAH7e00/PPaE= sha256:+kCsFNkzEQHIbwMFk3tnZ6FSPIj2H0aEnr4JAakSyB0= In-Reply-To: <11ac0mi$19kfd$1@dont-email.me> Content-Language: en-GB