🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3807

Re: strstr - zero-length s2

#3807
From: Barry Margolin
Date: Mon, 21 Aug 1995 18:33
34 lines
2078 bytes
In article <bnelsonDDo6u7.F01@netcom.com> bnelson@netcom.com (Bob Nelson) writes:
>2). Was there perhaps any fleeting thought given (while designing
>    strstr) to have the function instead return a pointer to the
>    _end_ of s1 if s2 were a zero-length string? What were some of
>    the things the Committee had to consider in coming up with
>    the s2 as zero-length string behavior for strstr? What are the
>    advantages (in terms of usage) of the current design
>    as opposed to the alternative I mentioned?

I'm not, nor have I ever been, a member of the ANSI/ISO C committee.  But
their design seems completely logical to me.  It's consistent with many
languages that came before, such as PL/I and Lisp.  And it's
self-consistent: strstr() returns a pointer to the first occurrence of s2
in s1, and in the case of a zero-length s2, the first occurrence is at the
beginning of s1.

>3). Plauger, "The Standard C Library", in the section "Implementing
>    <string.h>" says: "The function treats an empty string s2 as
>    a special case. It matches the implicit empty string at the start
>    of s1."

>    Might one of the c.s.c participants expand on what Plauger means
>    by this "implicit empty string" and contrast this phrase with
>    the null string (null string used to denote "")?

Normally, a string is an array of characters whose end is marked with '\0',
so a null string is a character array whose first element is '\0'.
The return value of strstr() doesn't normally point to a string that exactly
matches s2, just one with a prefix that matches s2.
-- 
Barry Margolin
BBN PlaNET Corporation, Cambridge, MA
barmar@bbnplanet.com
Phone (617) 873-3126 - Fax (617) 873-5124
Technical Details
Message-ID: <41aji3$f0q@tools.near.net>
Path: arc1-ca.pugleaf.net!tools.near.net!not-for-mail
References: <bnelsonDDo6u7.F01@netcom.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!tools.near.net!not-for-mail
From: barmar@nic.near.net (Barry Margolin)
Newsgroups: comp.std.c
Subject: Re: strstr - zero-length s2
Date: 21 Aug 1995 14:33:39 -0400
Organization: BBN Planet Corp., Cambridge, MA
Message-ID: <41aji3$f0q@tools.near.net>
References: <bnelsonDDo6u7.F01@netcom.com>
NNTP-Posting-Host: tools.near.net