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

Discussion in 'C++' started by Adrian, Jul 11, 2007.

  1. Adrian

    Adrian Guest

    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;
    }
     
    Adrian, Jul 11, 2007
    #1
    1. Advertising

  2. Adrian

    Marcus Kwok Guest

    Adrian <> wrote:
    > 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
     
    Marcus Kwok, Jul 11, 2007
    #2
    1. Advertising

  3. Adrian

    Adrian Guest

    On Jul 11, 11:32 am, (Marcus Kwok) wrote:
    > Adrian <> wrote:
    > > 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
     
    Adrian, Jul 11, 2007
    #3
  4. Adrian

    Kai-Uwe Bux Guest

    Adrian wrote:

    > On Jul 11, 11:32 am, (Marcus Kwok) wrote:
    >> Adrian <> wrote:
    >> > 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
     
    Kai-Uwe Bux, Jul 11, 2007
    #4
  5. Adrian

    Adrian Guest

    On Jul 11, 1:01 pm, Kai-Uwe Bux <> wrote:
    > > - 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
     
    Adrian, Jul 11, 2007
    #5
  6. Re: Why does string.find(x, npos) search from the begining of thestring

    On 2007-07-11 22:36, Adrian wrote:
    > On Jul 11, 1:01 pm, Kai-Uwe Bux <> wrote:
    >> > - 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 11, 2007
    #6
  7. Adrian

    Kai-Uwe Bux Guest

    Adrian wrote:

    > On Jul 11, 1:01 pm, Kai-Uwe Bux <> wrote:
    >> > - 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
     
    Kai-Uwe Bux, Jul 11, 2007
    #7
  8. Adrian

    Adrian Guest

    Adrian, Jul 12, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek

    string::npos + 1 ???

    Derek, Apr 12, 2004, in forum: C++
    Replies:
    8
    Views:
    4,701
    Jorge Rivera
    Apr 15, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,948
    Smokey Grindel
    Dec 2, 2006
  3. Marcus Kwok
    Replies:
    7
    Views:
    423
    Duane Hebert
    Jan 27, 2006
  4. Christopher Pisz
    Replies:
    11
    Views:
    536
    James Kanze
    Jan 13, 2008
  5. Evertjan.

    Begining asp begining

    Evertjan., Apr 4, 2010, in forum: ASP General
    Replies:
    1
    Views:
    719
    Haziq
    Apr 5, 2010
Loading...

Share This Page