Why does string.find(x, npos) search from the begining of the string


A

Adrian

Why does std::strings find search from the begining of the string when
pos >= (std::string::npos-3)

I cant find anything in the standard that says what find should do if
pos==npos in find

I tried it on a few platforms (all with gcc unfortunaley) and its
seems to be consistent.

Adrian


Linux 64 bit:
g++=4.1.1
Flags=-Wall -ansi -pedantic
Result:
x=18446744073709551615 Found:a test
x=18446744073709551614 Found:a test
x=18446744073709551613 Found:a test
x=18446744073709551612 Not found
x=18446744073709551611 Not found
x=18446744073709551610 Not found
x=18446744073709551609 Not found
x=18446744073709551608 Not found
x=18446744073709551607 Not found
x=18446744073709551606 Not found

Linux 32 bit:
g++=4.1.1
Flags=-Wall -ansi -pedantic
Result:
x=4294967295 Found:a test
x=4294967294 Found:a test
x=4294967293 Found:a test
x=4294967292 Not found
x=4294967291 Not found
x=4294967290 Not found
x=4294967289 Not found
x=4294967288 Not found
x=4294967287 Not found
x=4294967286 Not found

Unixware 7.1.1
g++=2.95.2
Flags=-Wall -ansi -pedantic
Result:
x=4294967295 Found:a test
x=4294967294 Found:a test
x=4294967293 Found:a test
x=4294967292 Not found
x=4294967291 Not found
x=4294967290 Not found
x=4294967289 Not found
x=4294967288 Not found
x=4294967287 Not found
x=4294967286 Not found

FreeBSD 5.4
g++=4.1.2
Flags=-Wall -ansi -pedantic
Result:
x=4294967295 Found:a test
x=4294967294 Found:a test
x=4294967293 Found:a test
x=4294967292 Not found
x=4294967291 Not found
x=4294967290 Not found
x=4294967289 Not found
x=4294967288 Not found
x=4294967287 Not found
x=4294967286 Not found

Example Code:
#include <iostream>
#include <string>
#include <climits>

int main(int argc, char *argv[])
{
std::string a("this is a test");

std::string b("a t");

for(unsigned long x=ULONG_MAX; x>ULONG_MAX-10; --x)
{
std::string::size_type y=a.find(b, x);

if(y==std::string::npos)
{
std::cout << "x=" << x << " Not found\n";
}
else
{
std::cout << "x=" << x << " Found:" << a.substr(y) <<
std::endl;
}
}

return 0;
}
 
Ad

Advertisements

M

Marcus Kwok

Adrian said:
Why does std::strings find search from the begining of the string when
pos >= (std::string::npos-3)

It does not on my implementation (see below).
I cant find anything in the standard that says what find should do if
pos==npos in find

I tried it on a few platforms (all with gcc unfortunaley) and its
seems to be consistent.
[snip examples]

Example Code:
#include <iostream>
#include <string>
#include <climits>

int main(int argc, char *argv[])
{
std::string a("this is a test");

std::string b("a t");

for(unsigned long x=ULONG_MAX; x>ULONG_MAX-10; --x)
{
std::string::size_type y=a.find(b, x);

if(y==std::string::npos)
{
std::cout << "x=" << x << " Not found\n";
}
else
{
std::cout << "x=" << x << " Found:" << a.substr(y) << std::endl;
}
}

return 0;
}

Running the above code on Windows, compiled with VS 2005:

x=4294967295 Not found
x=4294967294 Not found
x=4294967293 Not found
x=4294967292 Not found
x=4294967291 Not found
x=4294967290 Not found
x=4294967289 Not found
x=4294967288 Not found
x=4294967287 Not found
x=4294967286 Not found

so maybe it is a quirk in the GCC Standard Library implementation.
 
A

Adrian

It does not on my implementation (see below).
Running the above code on Windows, compiled with VS 2005:

x=4294967295 Not found
x=4294967294 Not found
x=4294967293 Not found
x=4294967292 Not found
x=4294967291 Not found
x=4294967290 Not found
x=4294967289 Not found
x=4294967288 Not found
x=4294967287 Not found
x=4294967286 Not found

so maybe it is a quirk in the GCC Standard Library implementation.

Your result is what I would expect.

To me common sense says this is a bug in gcc - but is there anything
in the standard to support that.

Adrian
 
K

Kai-Uwe Bux

Adrian said:
Your result is what I would expect.

To me common sense says this is a bug in gcc

It appears to be fixed in 4.2.0.
- but is there anything in the standard to support that.

Sure [21.3.6.1/1-3]

basic_string::find [lib.string::find]

size_type
find( const basic_string<charT,traits,Allocator>& str,
size_type pos = 0) const;

Effects: Determines the lowest position xpos, if possible, such that both
of the following conditions obtain:
? pos <= xpos and xpos + str.size() <= size();
? at(xpos+I) == str.at(I) for all elements I of the
string controlled by str.

Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.

Notes: Uses traits::eq().

That determines the return-value uniquely for all possible inputs since
size_type is an unsigned type.


Best

Kai-Uwe Bux
 
A

Adrian

- but is there anything in the standard to support that.

Sure [21.3.6.1/1-3]

basic_string::find [lib.string::find]

size_type
find( const basic_string<charT,traits,Allocator>& str,
size_type pos = 0) const;

Effects: Determines the lowest position xpos, if possible, such that both
of the following conditions obtain:
? pos <= xpos and xpos + str.size() <= size();
? at(xpos+I) == str.at(I) for all elements I of the
string controlled by str.

Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.

Notes: Uses traits::eq().

That determines the return-value uniquely for all possible inputs since
size_type is an unsigned type.

As far as The standard places no requirement for pos to be in bounds
of the string.

I have discovered that length of the search string overflows pos the
the string is found because it is search from the begining.

Lots of other sections for string have requirements of the type pos <
size().

Does this mean ommission of the requirement from the standard means
pos can be any valid value of size_type.


Adrian
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

- but is there anything in the standard to support that.

Sure [21.3.6.1/1-3]

basic_string::find [lib.string::find]

size_type
find( const basic_string<charT,traits,Allocator>& str,
size_type pos = 0) const;

Effects: Determines the lowest position xpos, if possible, such that both
of the following conditions obtain:
? pos <= xpos and xpos + str.size() <= size();
? at(xpos+I) == str.at(I) for all elements I of the
string controlled by str.

Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.

Notes: Uses traits::eq().

That determines the return-value uniquely for all possible inputs since
size_type is an unsigned type.

As far as The standard places no requirement for pos to be in bounds
of the string.

I have discovered that length of the search string overflows pos the
the string is found because it is search from the begining.

Lots of other sections for string have requirements of the type pos <
size().

Does this mean ommission of the requirement from the standard means
pos can be any valid value of size_type.

Technically, it seems so, but if pos > size() then find should return
npos since the requirements specified can't be fulfilled.
 
Ad

Advertisements

K

Kai-Uwe Bux

Adrian said:
- but is there anything in the standard to support that.

Sure [21.3.6.1/1-3]

basic_string::find [lib.string::find]

size_type
find( const basic_string<charT,traits,Allocator>& str,
size_type pos = 0) const;

Effects: Determines the lowest position xpos, if possible, such that
both
of the following conditions obtain:
? pos <= xpos and xpos + str.size() <= size();
? at(xpos+I) == str.at(I) for all elements I of the
string controlled by str.

Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.

Notes: Uses traits::eq().

That determines the return-value uniquely for all possible inputs since
size_type is an unsigned type.

As far as The standard places no requirement for pos to be in bounds
of the string.

Where would you see such a requirement?

I have discovered that length of the search string overflows pos the
the string is found because it is search from the begining.

Lots of other sections for string have requirements of the type pos <
size().

Does this mean ommission of the requirement from the standard means
pos can be any valid value of size_type.

That's the way I read it. Any value for the pos parameter is fine, and the
return value xpos is then determined accordingly.


Best

Kai-Uwe Bux
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top