🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #13383

Re: String Processing Instruction

#13383
From: john
Date: Tue, 31 Mar 1987 23:45
66 lines
3133 bytes
> Well, John Mashey's numbers were certainly counter-intuitive to ME...

Really sophisticated programs spend most of their time doing really
sophisticated things; simple grunt routines like str*() aren't universally
applicable.

> When were nroff/grep/yacc written compared to when str* was standardized
> in the library?  After all, the str* libraries didn't spring full-blown on
> day one...

I don't know (offhand) about these, but I recall once being curious about
why lex was slow.  It turned out to use subroutines for isalpha(), isspace(),
isdigit(), etc, instead of the (presumably newer) ctype.h macros.  HOWEVER,
even after making that change to a version of lex, it didn't help all that
much, because lex has so much else to do.

> Even USING the libraries, sometimes instead of:
> 	strcpy(buf, "piece");
> 	strcat(buf, "another piece");
> 	strcat(buf, "still another piece");
> I find myself doing:
> 	char *p = &buf[0];
> 	int n;
> 	strcpy(p, "piece");		p += strlen(p);
> 	strcpy(p, "another piece");	p += strlen(p);
> 	sprintf(p, "format%d%s", args...); p+= strlen(p);
> 	strcpy(p, "still another piece"); p += strlen(p);
> 	etc...
> 
WHOOPS!  That's about as much work (first you copy the string, then
you step over it) as the use of strcat().  If you don't mind being
non-portable to oddball architectures, you can write a routine like
"strcatl()", which takes a destination buffer as a first argument, and
a NULL-terminated list of strings to concatenate into that buffer (or
you can be slightly portable and do it with <varargs.h>).   The easiest
way might be, however, to use:

	char *strcpya(p1, p2)	/* strcat and return end address */
	char *p1, *p2;
	{
		while (*p1++ = *p2++) /**/;
		return p1-1;
	}

and then
	strcpya(strcpya(strcpya(buf, "first piece"), " second piece"),
		" and the last");
or if you prefer
	char *p,buf[127 /* a prime number*/ ];
	p = strcpya(buf, "first piece");
	p = strcpya(p, " second piece");
	p = strcpya(p, " third piece");

Either way, you only touch each byte once.

Sometimes, the question you need to ask yourself about library routines is
not "Could they be better coded with this screwball assembly instruction?"
but rather "Does it *really* do what I want?".

--
John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, ...!mit-eddie!jfw, jfw%mit-ccc@MIT-XX.ARPA

"Happiness is the planet Earth
	in your rear-view mirror."
		- Sam Hurt, in "Eyebeam, Therefore I Am"
Technical Details
Message-ID: <1304@frog.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watnot!watmath!clyde!rutgers!mit-eddie!cybvax0!frog!john
References: <15292@amdcad.UUCP> <978@ames.UUCP> <15304@amdcad.UUCP> <232@winchester.mips.UUCP> <15317@amdcad.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watnot!watmath!clyde!rutgers!mit-eddie!cybvax0!frog!john
From: john@frog.UUCP
Newsgroups: comp.arch,comp.lang.c
Subject: Re: String Processing Instruction
Message-ID: <1304@frog.UUCP>
Date: Tue, 31-Mar-87 18:45:00 EST
Article-I.D.: frog.1304
Posted: Tue Mar 31 18:45:00 1987
Date-Received: Sat, 4-Apr-87 15:16:59 EST
References: <15292@amdcad.UUCP> <978@ames.UUCP> <15304@amdcad.UUCP> <232@winchester.mips.UUCP> <15317@amdcad.UUCP>
Distribution: na
Organization: Superfrog Heaven [ CRDS, Framingham MA ]
Lines: 66