Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Adrian
Guest
 
Posts: n/a
#1: Jul 11 '07
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;
}

Marcus Kwok
Guest
 
Posts: n/a
#2: Jul 11 '07

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


Adrian <nntp@bluedreamer.comwrote:
Quote:
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).
Quote:
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]
Quote:
>
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Adrian
Guest
 
Posts: n/a
#3: Jul 11 '07

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


On Jul 11, 11:32 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Quote:
Adrian <n...@bluedreamer.comwrote:
Quote:
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).
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

Kai-Uwe Bux
Guest
 
Posts: n/a
#4: Jul 11 '07

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


Adrian wrote:
Quote:
On Jul 11, 11:32 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Quote:
>Adrian <n...@bluedreamer.comwrote:
Quote:
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).
>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
It appears to be fixed in 4.2.0.
Quote:
- 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
Adrian
Guest
 
Posts: n/a
#5: Jul 11 '07

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


On Jul 11, 1:01 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Quote:
Quote:
- 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?=
Guest
 
Posts: n/a
#6: Jul 11 '07

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


On 2007-07-11 22:36, Adrian wrote:
Quote:
On Jul 11, 1:01 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Quote:
Quote:
- 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.

--
Erik Wikström
Kai-Uwe Bux
Guest
 
Posts: n/a
#7: Jul 11 '07

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


Adrian wrote:
Quote:
On Jul 11, 1:01 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Quote:
Quote:
- 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?

Quote:
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
Adrian
Guest
 
Posts: n/a
#8: Jul 12 '07

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


This is a bug in GCC

Fixed 4.2

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31401


Adrian

Closed Thread


Similar C / C++ bytes