🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3616

Re: Switch switch

#3616
From: Lars Wirzenius
Date: Wed, 02 Aug 1995 11:36
57 lines
2368 bytes
jim.fleming@bytes.com (Jim Fleming):
> Does anyone want to be able to write....???
> 
> 	switch (aString) {
> 		...
> 	case "hello":

Would this be equivalent to

	aString == "hello"	(compare pointers)
	strcmp(aString, "hello") == 0	(compare strings)
	strcasecmp(aString, "hello") == 0	(case in-sensitive comparison)

or something else?

If it is anything but the first one, the potential for hidden costs
is fairly high.  Also, a mere strcmp is not versatile enough to be 
worth it, you really need to be able to do strcasecmp (yes, I know
it isn't a standard function), strncmp, strncasecmp, strnumcmp, 
memcmp, ....

If you want to make switch more versatile, do it properly and allow
arbitrary conditions:

	caselist {
	case strcmp(aString, "hello") == 0:
	case strcmp(aString, "hi there") == 0:
		printf("hello, sailor\n");
		break;
	case rand() < RAND_MAX/2:
		printf("whaddaya want?\n");
		break;
	default:
		printf("how may I serve you, sir?\n");
		break;
	}

The conditions would be evaluated one by one, top to bottom, until
one is satisfied.  The optional "default" would be evaluated only
if no other condition would be true.

Of course, this is just syntactic sugar for a few nested if-else's,
but in some cases it would be much clearer.

But I'm not advocating this, since I'd prefer to see C stay as
unchanging as possible.  It's now a nice, almost universally
portable language (not counting a few systems that _still_ haven't
got a c89 compiler or libraries).  Any new standard has a high
probability of destroying this, again.

(I'm not agains minor additions, such as // comments or enhancements
to the library, but I think the language part should stay the same
as much as possible.)

-- 
Lars.Wirzenius@helsinki.fi  (finger wirzeniu@klaava.helsinki.fi)
Publib version 0.5: ftp://ftp.cs.helsinki.fi/pub/Software/Local/Publib/
Technical Details
Message-ID: <3vno05$il7@kruuna.helsinki.fi>
Path: arc1-ca.pugleaf.net!news.helsinki.fi!not-for-mail
References: <3vn92m$oqt@news1.mcs.net>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.helsinki.fi!not-for-mail
From: wirzeniu@cc.Helsinki.FI (Lars Wirzenius)
Newsgroups: comp.std.c
Subject: Re: Switch switch
Date: 2 Aug 1995 11:36:37 -0000
Organization: University of Helsinki
Sender: wirzeniu@cc.helsinki.fi
Message-ID: <3vno05$il7@kruuna.helsinki.fi>
References: <3vn92m$oqt@news1.mcs.net>
NNTP-Posting-Host: kruuna.helsinki.fi
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit