Topic: string.find()


Author: kanze@gabi-soft.de
Date: 2000/06/18
Raw View
Anthony Williams <anthony_w@my-deja.com> writes:

|>  I have two implementations of the C++ standard library, which I am
|>  comparing. The following piece of code behaves differently in each
|>  case:

|>  #include <iostream>
|>  #include <string>

|>  int main()
|>  {
|>      std::string a("hello");
|>      std::string b;

|>      for(unsigned i=3D0;i<=3Da.size();++i)
|>          std::cout<<a.find(b,i)<<std::endl;
|>  }

|>  With library 1 it prints
|>  0
|>  1
|>  2
|>  3
|>  4
|>  4294967295

This looks correct, with the exception of the last results.  If I
interpret the standard correctly, the last test should return 5.

|>  with library 2 it prints
|>  5
|>  5
|>  5
|>  5
|>  5
|>  5

This is certainly wrong.  If there is no match, the results should be
npos.  If there is a match, the index of the match.  The match is over
the length of the first parameter, and an empty string always matches an
empty string.

|>  Which is correct? (4294967295 is std::string::npos)

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: kanze@gabi-soft.de
Date: 2000/06/19
Raw View
"Jeff Peil" <jpeil@bigfoot.com> writes:

|>  After careful reading of 21.3.6.1 (basic_string::find) I'd conclude
|>  library 1's implementations is the correct.  Specifically 21.3.6.1
|>  requires the following given 2 strings, str1 and str2, if you
|>  execute "xpos =3D str1.find(str2, pos)"

|>  1) xpos is the lowest possible position that meets the other criteria=
, such
|>  a position exists, otherwise it is npos
|>  2) pos <=3D xpos
|>  3) xpos + str2.size() <=3D str1.size()
|>  4) xpos must either equal npos or str1.at(xpos+i) =3D=3D str2.at(i) m=
ust return
|>  true where i is a valid index into str2.

|>  With an empty string as str2 the clause requiring indexing via the
|>  at() operator is effectively void (since there are no valid elements
|>  in the string to compare against.)  Thus xpos must be equal to pos
|>  in all cases where pos refers to a element in the string (in the
|>  last iteration of your loop, i doesn't refer to a valid position in
|>  string a, so npos is the correct return.)

Where is the requirement that pos refer to an element in the string?

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Anthony Williams <anthony_w@my-deja.com>
Date: 2000/06/07
Raw View
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I have two implementations of the C++ standard library, which I am
comparing. The following piece of code behaves differently in each
case:

#include <iostream>
#include <string>

int main()
{
    std::string a("hello");
    std::string b;

    for(unsigned i=0;i<=a.size();++i)
        std::cout<<a.find(b,i)<<std::endl;
}

With library 1 it prints
0
1
2
3
4
4294967295

with library 2 it prints
5
5
5
5
5
5

Which is correct? (4294967295 is std::string::npos)

- --
alink@anthonyw.cjb.net -- Questions relating to ALINK
anthony@anthonyw.cjb.net  -- Non-ALINK questions
anthonyw.cjb.net -- ALINK home page
PGP Key at: i3.yimg.com/3/c7e5ee24/g/68fc2307.asc

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.1 for non-commercial use <http://www.pgp.com>
Comment: PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc

iQA/AwUBOT0AdJvw+P4cG5rVEQJejACePtTRHeevv0244S0C4D0QKMRugD0AmwVI
tP7RKgZohyzlpTPRbdBzuk8B
=Zp8z
-----END PGP SIGNATURE-----


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jeff Peil" <jpeil@bigfoot.com>
Date: 2000/06/07
Raw View
Anthony,

After careful reading of 21.3.6.1 (basic_string::find) I'd conclude library
1's implementations is the correct.  Specifically 21.3.6.1 requires the
following given 2 strings, str1 and str2, if you execute "xpos =
str1.find(str2, pos)"
1) xpos is the lowest possible position that meets the other criteria, such
a position exists, otherwise it is npos
2) pos <= xpos
3) xpos + str2.size() <= str1.size()
4) xpos must either equal npos or str1.at(xpos+i) == str2.at(i) must return
true where i is a valid index into str2.

With an empty string as str2 the clause requiring indexing via the at()
operator is effectively void (since there are no valid elements in the
string to compare against.)  Thus xpos must be equal to pos in all cases
where pos refers to a element in the string (in the last iteration of your
loop, i doesn't refer to a valid position in string a, so npos is the
correct return.)


"Anthony Williams" <anthony_w@my-deja.com> wrote in message
news:8hivcv$vdn$1@nnrp1.deja.com...
> I have two implementations of the C++ standard library, which I am
> comparing. The following piece of code behaves differently in each
> case:
>
> #include <iostream>
> #include <string>
>
> int main()
> {
>     std::string a("hello");
>     std::string b;
>
>     for(unsigned i=0;i<=a.size();++i)
>         std::cout<<a.find(b,i)<<std::endl;
> }
>
> With library 1 it prints
> 0
> 1
> 2
> 3
> 4
> 4294967295
>
> with library 2 it prints
> 5
> 5
> 5
> 5
> 5
> 5
>
> Which is correct? (4294967295 is std::string::npos)



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]