From: Ulrich Eckhardt on
Greetings!

We have some problems concerning the behaviour of this piece of code:

string s = "aoeui";
string::size_type pos = s.find("");

There is one opinion that says that 'pos' must be 'string::npos', because
you can never find an empty string anywhere. There is another opinion that
says that it matches any position, so the first matching position would be
zero. The discussion is still taking place online at [1], though it started
at [2], in case you are interested.


As another corner case, the question came up how a search for an empty
string within an empty string should behave, i.e. whether it should return
0 or npos.


Uli

[1]
https://sourceforge.net/tracker/?func=detail&atid=766244&aid=1872656&group_id=146814
[2]
https://sourceforge.net/forum/message.php?msg_id=4723253

--
Sator Laser GmbH
Gesch�ftsf�hrer: Michael W�hrmann, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Linonut on
* Ulrich Eckhardt peremptorily fired off this memo:

> Greetings!
>
> We have some problems concerning the behaviour of this piece of code:
>
> string s = "aoeui";
> string::size_type pos = s.find("");
>
> There is one opinion that says that 'pos' must be 'string::npos', because
> you can never find an empty string anywhere. There is another opinion that
> says that it matches any position, so the first matching position would be
> zero.

I think it should be s.length(), since "" is just a null terminator, and
s.length() is where you find it.

Is that to C'ish? <grin>

> As another corner case, the question came up how a search for an empty
> string within an empty string should behave, i.e. whether it should return
> 0 or npos.

Maybe another value is needed. I propose std::bogus.

Seriously, though, I don't see any way of eliminating having the caller
check either the search string or the searchee for validity before
proceeding. You know, the old input-validation thing. Pick an answer
and stick with it.

Chris Ahlstrom

(Still need to set up slrn to use my real name here.)

--
You dialed 5483.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jerry Coffin on
In article <fkg365-reo.ln1(a)satorlaser.homedns.org>,
eckhardt(a)satorlaser.com says...
> Greetings!
>
> We have some problems concerning the behaviour of this piece of code:
>
> string s = "aoeui";
> string::size_type pos = s.find("");
>
> There is one opinion that says that 'pos' must be 'string::npos', because
> you can never find an empty string anywhere. There is another opinion that
> says that it matches any position, so the first matching position would be
> zero. The discussion is still taking place online at [1], though it started
> at [2], in case you are interested.
>
>
> As another corner case, the question came up how a search for an empty
> string within an empty string should behave, i.e. whether it should return
> 0 or npos.

I think it should be npos. The requirement is:

- at(xpos+I) == str.at(I) for all elements I of the string
controlled by str.

For an empty string, str.at(I) must throw for all possible values of I,
therefore there cannot be any value of I for which the == can be true.
This being the case, it must return npos.

The same is true for searching for an empty string within an empty
string. Since there is no value of I for which the comparison can be
true, it should return npos.

It's certainly true that requirements could be cleaned up a bit in this
regard though. I don't see the issue in the commitee's C++ library
active isuses list, and the specification in N2461 seems to be the same
as in the current standard. IMO, this should be posted to comp.std.c++
with a suggestion that: the specification in [lib.string::find] have the
Returns clause changed as follows:

Returns: xpos if !str.empty() and the function can determine
such a value for xpos. Otherwise, returns npos.

This would cover both cases cited above, since the added condition
depends only upon whether the second string is empty.

--
Later,
Jerry.

The universe is a figment of its own imagination.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ron Natalie on
Linonut wrote:

> I think it should be s.length(), since "" is just a null terminator, and
> s.length() is where you find it.

I realize you aren't serious, but let me amplify before some newbie
misunderstands you.

"" it converts to a zero length string. A std::string doesn't
implicitly have a null in it. In the case above s has length 5
with 5 characters in it.=

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Chris Ahlstrom wrote:
> * Ulrich Eckhardt peremptorily fired off this memo:
>> We have some problems concerning the behaviour of this piece of code:
>>
>> string s = "aoeui";
>> string::size_type pos = s.find("");
>>
>> There is one opinion that says that 'pos' must be 'string::npos', because
>> you can never find an empty string anywhere. There is another opinion
>> that says that it matches any position, so the first matching position
>> would be zero.
>
> I think it should be s.length(), since "" is just a null terminator, and
> s.length() is where you find it.

Nope, s is not guaranteed to even be NUL-terminated. Rather, a std::string
is even allowed to contain NUL characters in the middle!

> Is that to C'ish? <grin>

I'd rather say the opposite:

char* s = "aoeui";
char* p = strstr( s, "");
assert(p==s);

IOW, strstr() is documented to return the passed string ('s' in this case)
when the searched string is empty.

cheers

Uli

--
Sator Laser GmbH
Gesch�ftsf�hrer: Michael W�hrmann, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]