🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #185

Re: Taming The Switch-Statement

#185
From: Waldek Hebisch
Date: Sun, 11 Oct 2026 08:00
41 lines
3393 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 2026-10-10 22:09, bart wrote:
>> On 10/10/2026 19:08, Janis Papanagnou wrote:
>>> [*] In the GNU version of Awk - a much simpler but higher abstracted
>>> language - you can nicely compare strings, and even regexps.
>> 
>> I have a narrow view of SWITCH, which is specifically for use with 
>> integer values. That allows comparison of N unique possibilities 
>> simultaneously, or in O(1) time.
>> So if you're comparing strings, then it is not SWITCH IMO, unless those 
>> strings are perhaps interned.
> 
> A CDC 175/176 carried a Pascal TEXT object in one machine word and
> could compare it as atom. Newer standard PC CPUs have more bits in
> a word than those former supercomputers. - But the point here was
> that internals should be abstracted by a higher level programming
> language. - And compilers have principally various possibilities to
> support some "atomization" of string comparisons with compile-time
> string constants. - As said, it would have been useful in many of
> my application cases. (No more, no less.)

In C there are multi-character "characters".  Details are implementation
defined, but AFAICS on 64-bit machine you can pack 8 bytes into single
machine word and use that in a switch.  CDC used 6 bit characters, so
it could cram 10 characters into a single word.  Corresponding comparison
of strings would execute as a single machine instruction.  But AFAIK
Pascal did not allow to use strings in 'case' statement.

In principle C compiler could generate matching code for strings,
basically if all cases correspond to short strings it could pack
characters into a single machine word and then reuse integer
case.  For longer strings it could generate either tree of
comparisons (IIUC some sparse switches use this) or use hashing
to find correct case.  That would be modest extention of current
switch (with sparse case labels one needs to handle most of it
anyway).  I would guess that main counterargument to such a switch
is C++ compatibility: clearly C++ would want such things for classes
and it opens a can of worms.

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11affmm$1m9m7$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11aduuk$cjlv$1@dont-email.me> <11ae62d$221po$1@dont-email.me> <11ae9r3$cjlv$7@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!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 08:00:24 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11affmm$1m9m7$1@paganini.bofh.team>
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11aduuk$cjlv$1@dont-email.me> <11ae62d$221po$1@dont-email.me> <11ae9r3$cjlv$7@dont-email.me>
Injection-Date: Sun, 11 Oct 2026 08:00:24 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="1779399"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2