🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #186

Re: Taming The Switch-Statement

#186
From: Janis Papanagnou
Date: Sun, 11 Oct 2026 08:27
59 lines
4614 bytes
On 2026-10-11 10:00, Waldek Hebisch wrote:
> 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.

That is absolutely correct; about the PACKED ARRAY [1..10] OF CHAR
on these CDCs, and about what the Pascal language's CASE allowed
per language definition, which was any enumerable type, and strings
were, being arrays, not enumerable.

I seem to recall that operations on the Pascal's TEXT type were not
very elaborated, though. And assignments were possible for any type
not restricted to primitive types or the TEXT type.

My point was, as you also wrote, that there's possibilities to handle
a short string at least as an atom for comparisons in principle (if
the language definition allows that, if it's appropriately defined).

> 
> 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.

Yes, probably. - There's so many things you cannot do now, decades
after the base of "C" has been defined.

Janis
Technical Details
Message-ID: <11afh96$286mm$2@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-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> <11affmm$1m9m7$1@paganini.bofh.team>

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: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 10:27:18 +0200
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <11afh96$286mm$2@dont-email.me>
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>
 <11affmm$1m9m7$1@paganini.bofh.team>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 11 Oct 2026 08:27:19 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2366166"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/QP/5wY4KnI+8NLvKQM7/0"; posting-host="dc047fa64def26b023d01b53ecdcc125"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:YIeDQPbKF7GwfgClk7Sr+jNJR0A= sha256:G6LjZ5XkzaUSSdtNqorkeYWTG1cMt4jkUsR+7H1JRsc=
	sha1:Gc6uz9XUxe84L3k3Dme+hJQ5O2A= sha256:GK3pu0uHSdYRM0rT9+a5zO07VfTTgurfPh7gasdDkxg=
In-Reply-To: <11affmm$1m9m7$1@paganini.bofh.team>
Content-Language: en-US