Board index » delphi » I can't explain this, has anyone else seen it?

I can't explain this, has anyone else seen it?

I'm using Borland 5.02.  I've tried this on Win95 and 2000.  I know there are many other ways to do this, but why does this way not work?

The following code will replace S2 with "wow".

string S1( "%SOLDADR1" );
string S2( "21M0606B5" );

size_t S1Size = S2.find( S1 );
if( S1Size != NPOS )
   S2.replace( S1Size, S1.length(), string( "wow" ));

 

Re:I can't explain this, has anyone else seen it?


On 7 Dec 2001 07:42:13 -0800, "Chad" <cdb...@yahoo.com> wrote:

Quote
>why does this way not work?

Define "not work". Do you get compile errors? Link errors?
Runtime errors? Does it run OK but fails to do what you expect?
If the latter, what does it actually do and what are your expectations?

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:I can't explain this, has anyone else seen it?


Quote
Wayne A. King wrote:
> On 7 Dec 2001 07:42:13 -0800, "Chad" <cdb...@yahoo.com> wrote:

> Define "not work".

He did, maybe not very clearly. He (and I) didn't expect the expression
'S1Size != NPOS' to evaluate to true, but it did.

Re:I can't explain this, has anyone else seen it?


Quote
On Sat, 08 Dec 2001 09:09:56 +0100, Thomas Maeder <mae...@glue.ch> wrote:
>> Define "not work".

>He did, maybe not very clearly. He (and I) didn't expect the expression
>'S1Size != NPOS' to evaluate to true, but it did.

It's not immediately clear from the snippet exactly *which* string class
is being used. Based on the use of NPOS rather than npos I presume
that the string class from cstring.h is being used, and not the one from
<string>? If so, the problem may be related to that class's use of hash
values for comparing when doing a find. This can lead to false matches
in rare cases. To force an actual byte by byte comparison, you have
to use set_paranoid_check().

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:I can't explain this, has anyone else seen it?


wak...@idirect.com (Wayne A. King) wrote:

Quote
>On Sat, 08 Dec 2001 09:09:56 +0100, Thomas Maeder <mae...@glue.ch> wrote:

>>> Define "not work".

>>He did, maybe not very clearly. He (and I) didn't expect the expression
>>'S1Size != NPOS' to evaluate to true, but it did.

>It's not immediately clear from the snippet exactly *which* string class
>is being used. Based on the use of NPOS rather than npos I presume
>that the string class from cstring.h is being used, and not the one from
><string>? If so, the problem may be related to that class's use of hash
>values for comparing when doing a find. This can lead to false matches
>in rare cases. To force an actual byte by byte comparison, you have
>to use set_paranoid_check().

>--
>Wayne A. King
>(ba...@torfree.net, wayne.k...@ablelink.org,
> wak...@idirect.com, Wayne_A_K...@compuserve.com)

I was indeed using cstring.h.  I am sorry for being so unclear.

Thanks for the help, set_paranoid_check() fixed it right up.

Other Threads