🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #189

Re: Taming The Switch-Statement

#189
From: Janis Papanagnou
Date: Sun, 11 Oct 2026 08:49
68 lines
5098 bytes
On 2026-10-11 10:27, Janis Papanagnou wrote:
> 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 just looked it up in an old book; you could (in Pascal) also
compare "strings" (PACKED ARRAY OF TEXT) by the six comparison
operators (where other general array type objects needed an
element-wise comparison). So you could at least compare strings
of type TEXT directly with IF (and need no function, as in "C").

> 
> 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: <11afiji$286mm$3@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> <11afh96$286mm$2@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: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 10:49:54 +0200
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <11afiji$286mm$3@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> <11afh96$286mm$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 08:49:54 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2366166"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/xpOJVxK8T1I8fzjsRrOGP"; posting-host="dc047fa64def26b023d01b53ecdcc125"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JvaX0F9vL/5JUEEz3sU13ggM4WI= sha256:jL+7PbNhuykEFgwZ5yxYiy63jbuwfdwMeZYHRksnTzg=
	sha1:CK3hL9t+7N1WsYgG/Khl4dYMH1c= sha256:sMpfysl0pQx2EZAvo/ckZ6T1/RHou1fXQi12fe4KKGg=
Content-Language: en-US
In-Reply-To: <11afh96$286mm$2@dont-email.me>