Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with CString::Left()

402 views
Skip to first unread message

Martin Bischoff

unread,
Sep 4, 2002, 9:52:16 AM9/4/02
to
I have a problem with the implementations of CString::Left() and
CString::Right() in MFC 7:

With previous versions of MFC, CString::Left(-1) returned an empty string.
It was therefore possible to do the following (which was very practical):

CString s1 = "hello-hello";
CString s2 = "hello hello";
CString sSub = "-";
CString sRes1 = s1.Right( s1.Find(sSubString) ); // Find() returns 5,
sRes1 == "hello"
CString sRes2 = s2.Right( s2.Find(sSubString) ); // Find() returns -1,
sRes2 == ""

In MFC 7 this (the results in an exception (resp. ASSERT in debug builds).
Is this a bug in MFC/CString or is it by design?


Thanks for your comments,
Martin Bischoff

--
To send an eMail remove NO SPAM

Martin Bischoff

unread,
Sep 4, 2002, 10:13:38 AM9/4/02
to
Sorry there were some errors in the example. Here's a corrected version:

CString s1, s2, sSub, sRes1, sRes2;
s1 = "hello-hello";
s2 = "hello hello";
sSub = "-";

sRes1 = s1.Left( s1.Find(sSubString) );
/* s1.Find() returns 5, sRes1 == "hello" */

sRes2 = s2.Left( s2.Find(sSubString) );
/* s2.Find() returns -1, sRes2 is now "" */

Thanks,

Heinz Ozwirk

unread,
Sep 4, 2002, 10:58:02 AM9/4/02
to
"Martin Bischoff" <martin....@nogmxspam.net> wrote in message
news:al54e7$c5g$1...@rex.ip-plus.net...

> Sorry there were some errors in the example. Here's a corrected version:

Not really, there is still no declaration for sSubString, but let me assume,
that should be sSub...

> CString s1, s2, sSub, sRes1, sRes2;
> s1 = "hello-hello";
> s2 = "hello hello";
> sSub = "-";
>
> sRes1 = s1.Left( s1.Find(sSubString) );
> /* s1.Find() returns 5, sRes1 == "hello" */
>
> sRes2 = s2.Left( s2.Find(sSubString) );
> /* s2.Find() returns -1, sRes2 is now "" */

Where should Find() find a dash in "hello hello"?

Heinz


Martin Bischoff

unread,
Sep 4, 2002, 12:51:13 PM9/4/02
to
Heinz Ozwirk wrote:
> "Martin Bischoff" <martin....@nogmxspam.net> wrote in message
> news:al54e7$c5g$1...@rex.ip-plus.net...
>
>>Sorry there were some errors in the example. Here's a corrected version:
>
>
> Not really, there is still no declaration for sSubString, but let me assume,
> that should be sSub...
>
Your assumption was correct :-)

>
>>CString s1, s2, sSub, sRes1, sRes2;
>>s1 = "hello-hello";
>>s2 = "hello hello";
>>sSub = "-";
>>

>>sRes1 = s1.Left( s1.Find(sSub) );


>>/* s1.Find() returns 5, sRes1 == "hello" */
>>

>>sRes2 = s2.Left( s2.Find(sSub) );


>>/* s2.Find() returns -1, sRes2 is now "" */
>
>
> Where should Find() find a dash in "hello hello"?
>


Find will return 5 in the first case and -1 in the second (because the
substring can't be found). No problem so far.

with MFC prior to version 7:
s1.Left(5); // --> returns "hello"
s2.Left(-1); // --> returns "" !!

with MFC 7 (here comes the problem):
s1.Left(5); // --> still returns "hello"
s2.Left(-1); // --> ASSERT / unhandled exception !!


Thanks for your comments,

Jon Sturgeon

unread,
Sep 4, 2002, 5:19:50 PM9/4/02
to
On Wed, 04 Sep 2002 18:51:13 +0200, Martin Bischoff
<martin....@nogmxspam.net> wrote:

>with MFC prior to version 7:
>s1.Left(5); // --> returns "hello"
>s2.Left(-1); // --> returns "" !!
>
>with MFC 7 (here comes the problem):
>s1.Left(5); // --> still returns "hello"
>s2.Left(-1); // --> ASSERT / unhandled exception !!

It seems to me that you are/were relying upon an implementation detail/bug for
the correct behavior of your program. Nowhere in the CString::Left()
documentation do I see its behavior defined when the parameter is negative.

Is there any particular reason you can't write your code according to the
documented class interface?

Jon

Martin Bischoff

unread,
Sep 4, 2002, 6:43:43 PM9/4/02
to
No particular reason. But I have to port a lot of code from VC6 to VC7,
which contains quite a few lines like:

s2 = s1.Left(s1.Find(s));
-or-
s2 = s1.Right(s1.Find(s));

In alle these lines, s1.Find() might return -1, which means that I have
to check all occurrences :-(

Martin

--
To send an e-mail remove NO SPAM

0 new messages