Bug 19911 - Using std::string find the n field always causes nothing to be found
Summary: Using std::string find the n field always causes nothing to be found
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.3.2
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-02-11 20:31 UTC by Greg Rogers
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Greg Rogers 2005-02-11 20:31:54 UTC
This is for all flavors of find.

According to the documentation I can find.

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv)
{
    string test("this should be a bunch of words");
    size_t p = test.find("s");  // correct at 3
    cerr << "p = " << p << ", npos = " << string::npos << endl;
    p = test.find("s", 5);      // correct at 5
    cerr << "p = " << p << ", npos = " << string::npos << endl;
    p = test.find("s", 5, 5);   // incorrect, should be 5 is npos
    cerr << "p = " << p << ", npos = " << string::npos << endl;
}
Comment 1 Andrew Pinski 2005-02-11 20:46:09 UTC
No string.find (const char *s, size_type pos, size_t n) returns the same as:
string.find(string(s, n), pos)
which means you are allocating a string with length 5 but the string is really 
only of lenght 1 so this will not be able to find the string.

This documented by 21.3.6.1 of the C++ standard.
Comment 2 Greg Rogers 2005-02-11 20:59:16 UTC
Subject: Re:  Using std::string find the n field always
 causes nothing to be found

Okay, doesn't make much sense.

The docs I found on the web all had it described as n being the number 
of characters past pos to search.

pinskia at gcc dot gnu dot org wrote:
> ------- Additional Comments From pinskia at gcc dot gnu dot org  2005-02-11 20:46 -------
> No string.find (const char *s, size_type pos, size_t n) returns the same as:
> string.find(string(s, n), pos)
> which means you are allocating a string with length 5 but the string is really 
> only of lenght 1 so this will not be able to find the string.
> 
> This documented by 21.3.6.1 of the C++ standard.
> 

Comment 3 Paolo Carlini 2005-02-11 21:11:59 UTC
I'm maintaining the string class, and Andrew is 100% right: please browse the
standard (or any good book, such as Josuttis, if you like) instead of trusting
"docs on the web" ;)