The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] origin of string.h and ctype.h
@ 2017-08-13  1:58 Doug McIlroy
  2017-08-13  3:39 ` Steve Johnson
  0 siblings, 1 reply; 18+ messages in thread
From: Doug McIlroy @ 2017-08-13  1:58 UTC (permalink / raw)


Jon Steinhart <jon at fourwinds.com> asked this question (not on tuhs).
It highlights some less well known contributors to research Unix.

> I'm trying to find out who came up with strcmp and the idea of
> returning -1,0,1 for a string comparison.  I can see that it's not in
> my V6 manual but is in V7.  Don't see anything in Algol, PL/I, BCPL, or B

The -1,0,1 return from comparisons stems from the interface of qsort,
which was written by Lee McMahon. As far as I know, the interface for
the comparison-function parameter originated with him, but conceivably
he borrowed it from some other sort utility. The negative-zero-positive
convention for the return value encouraged (and perhaps was motivated by)
this trivial comparison function for integers
	int compar(a,b) { return(a-b); }
This screws up on overflow, so cautious folks would write it with
comparisons. And -1,0,1 were the easiest conventional values to return:
	int compar(a,b) {
		if(a<b) return(-1);
		if(a>b) return(1);
		return(0);
	}
qsort was in v2. In v3 a string-comparison routine called "compar"
appeared, with a man page titled "string comparison for sort". So the
convention was established early on.

Compar provided the model for strcmp, one of a package of basic string
operations that came much later, in v7, under the banner of string.h
and ctype.h.

These packages were introduced at the urging of Nils-Peter Nelson, a
good friend of the Unix lab, who was in the Bell Labs comp center.
Here's the story in his own words.

I wrote a memo to dmr with some suggestions for additions to C.  I asked
for the str... because the mainframes had single instructions to implement
them. I know for sure I had a blindingly fast implementation of isupper,
ispunct, etc. I had a table of length 128 integers for the ascii character
set; I assigned bits for upper, lower, numeric, punct, control, etc. So
ispunct(c) became

	#define PUNCT 0400
	return(qtable[c]&PUNCT)
instead of
	if(c==':' || c ==';' || ...
[or
	switch(c) {
		default:
			return 0;
		case ':':
		case ';':
		...
		return 1;
	}
MDM]
dmr argued people could easily write their own but when I showed
him my qtable was 20 times faster he gave in.  I also asked for type
logical which dmr implemented as unsigned, which was especially useful
when bitfields were implemented (a 2 bit int would have values -2, -1,
0, 1 instead of 0, 1, 2, 3).  I requested a way to interject assembler,
which became asm() (yes, a bad idea).


^ permalink raw reply	[flat|nested] 18+ messages in thread
[parent not found: <mailman.919.1502645045.3779.tuhs@minnie.tuhs.org>]
[parent not found: <mailman.1.1502762401.21962.tuhs@minnie.tuhs.org>]

end of thread, other threads:[~2017-08-15  4:13 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-13  1:58 [TUHS] origin of string.h and ctype.h Doug McIlroy
2017-08-13  3:39 ` Steve Johnson
2017-08-13  4:26   ` Dave Horsfall
2017-08-13  5:27     ` Steve Johnson
2017-08-13  5:43       ` Dave Horsfall
2017-08-13 17:24         ` Warner Losh
2017-08-13 18:23           ` Steve Nickolas
2017-08-13  8:42       ` arnold
2017-08-14 11:08     ` Mutiny 
     [not found] <mailman.919.1502645045.3779.tuhs@minnie.tuhs.org>
2017-08-13 21:55 ` Johnny Billquist
2017-08-13 22:26   ` Charles Anthony
2017-08-14  0:02 ` Paul McJones
2017-08-14 16:09   ` Paul Winalski
2017-08-14 17:16     ` Paul McJones
2017-08-15  3:57       ` Greg 'groggy' Lehey
2017-08-15  4:13         ` Paul McJones
2017-08-15  0:37     ` Dave Horsfall
     [not found] <mailman.1.1502762401.21962.tuhs@minnie.tuhs.org>
2017-08-15  3:06 ` Paul McJones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).