how to compare unicode chars? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

how to compare unicode chars?

Post by MoonKid » Mon Mar 26, 2007 5:28 pm

This code in a unicode application:

Code: Select all

wxString strCurr(_t("TEST"));

if (strCurr[1] == ':')
   doSomething();
Is it right that I compare a unicode-character (wxString[]) here with a char (':') ? But it compile without errors. Why?
How can I code it a right way?

geralds
I live to help wx-kind
I live to help wx-kind
Posts: 186
Joined: Tue Nov 01, 2005 9:22 am
Contact:

Post by geralds » Mon Mar 26, 2007 9:33 pm

Can't test this right now, but I seem to remember L':' works ok. I've certainly used L"foobar" for Unicode string comparisons in the past.

User avatar
tierra
Site Admin
Site Admin
Posts: 1343
Joined: Sun Aug 29, 2004 7:14 pm
Location: Salt Lake City, Utah, USA
Contact:

Re: how to compare unicode chars?

Post by tierra » Mon Mar 26, 2007 9:49 pm

MoonKid wrote:How can I code it a right way?
You should use the appropriate wxString methods, which will keep your code compatible whether it's built with Unicode support or in ANSI mode.

Code: Select all

wxString strCurr(_T("TEST"));
if ( strCurr.StartsWith(_T(":")) )
   doSomething();

maro.gach
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Mar 10, 2007 1:44 pm
Contact:

Post by maro.gach » Mon Mar 26, 2007 9:52 pm

If you code unicode application strCurr is an array of wchar_t encapsulated into some wxString object.

The main question is, if you compare the second character of string with ':' it can, but I'm not shure cause problems.

This ':' is probably char. So the main problem is(I think) that you are comparing char with wchar_t.

It will work fine, but not allways!

The main problem is that, wchar_t is 16bits long and char is 8bits long. As i think it will work, because comparing it, you compare low byte of that wchar_t with char. Due to backward compatibility of Unicode characters, it will work fine. But remember, if you compare something, that is not 8bit-ASCII it won't work!

So the best way is to compare like this:

Code: Select all

wxString strCurr(_t("TEST"));

if (strCurr[1] == _(":"))
   doSomething();
that _(":") macro will substitute something like this L":" in unicode build. So now you are comparing with wchar_t (with high byte set to 0x20, thats ' '). As you can see, it will work now. Because in your code above in that compare only low byte was compared. So whatever was in that strCurr[1] in high 8bits it doesn't matters, because you compared only with low byte. So everything with 8bit-ASCII code of ':' in low byte returned true in comparision. So it returned true in 2^8-1 cases as you can see... :D

Mark.

[EDIT] heheh, question was already answered while I was writing.
Marek Gach
BUT Brno, Czech Republic

RomanV
Knows some wx things
Knows some wx things
Posts: 32
Joined: Tue Mar 27, 2007 10:29 am
Contact:

Re: how to compare unicode chars?

Post by RomanV » Thu Mar 29, 2007 6:47 am

MoonKid wrote:This code in a unicode application:

Is it right that I compare a unicode-character (wxString[]) here with a char (':') ? But it compile without errors. Why?
How can I code it a right way?
According to help/manual of wxWidgets (see "Unicode support in wxWidgets")
You should use wxT() around chars and strings. So it's better to use the following code:

Code: Select all

wxString strCurr(wxT"TEST"));

if (strCurr[1] ==wxT(':'))
   doSomething();

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Thu Mar 29, 2007 7:06 am

Actually your code poses no problems because your compiler will convert both to int then compare them. I'm not sure if it is in the standard, but I certainly know that in C (and hence probably C++) the standard library functions isalpha and friends take an int for a parameter rather than a char.

Joel

Post Reply