473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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;
}

Jul 11 '07 #1
7 2415
Adrian <nn**@bluedreamer.comwrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 11 '07 #2
On Jul 11, 11:32 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Adrian <n...@bluedreamer.comwrote:
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

Jul 11 '07 #3
Adrian wrote:
On Jul 11, 11:32 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>Adrian <n...@bluedreamer.comwrote:
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.
- 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
Jul 11 '07 #4
On Jul 11, 1:01 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
- 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

Jul 11 '07 #5
On 2007-07-11 22:36, Adrian wrote:
On Jul 11, 1:01 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
- 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
Jul 11 '07 #6
Adrian wrote:
On Jul 11, 1:01 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
- 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
Jul 11 '07 #7
This is a bug in GCC

Fixed 4.2

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

Jul 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
1
by: Derek | last post by:
A common technique for trimming leading and trailing spaces from std::string is the following: string s(" blah blah blah "); const char* ws= " \t\r"; string::size_type not_white; // trim...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
33
by: Bertram Trabant | last post by:
Hello, Im working on a little LAN game in the style of old text-only MUD's and would need to find a way to search for a string in a text file (for example for usernames). I know it works in the way...
10
by: B. Williams | last post by:
I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called...
8
by: Marcus Kwok | last post by:
Is std::string::npos portably able to be incremented? For example, I want to insert some text into a string. If a certain character is found, I want to insert this text immediately after this...
11
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr....
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.