473,480 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Lowercase std::string compare?

Is there any builtin lowercase std::string compare? Right now I'm doing
this:

if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 )
AmmoType = Item_Ammo_GunBullet;

Is there anything the standard library to do this? I'm not interested in
Boost until it becomes part of the standard.
May 11 '06 #1
4 24861

Jim Langston wrote:
Is there any builtin lowercase std::string compare? Right now I'm doing
this:

if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 )
AmmoType = Item_Ammo_GunBullet;

Is there anything the standard library to do this? I'm not interested in
Boost until it becomes part of the standard.


I usually do it like:

string tmp = strToCompare;
transform(tmp.begin(), tmp.end(), tolower);
if(tmp == "gunbullet")
{
//more codes...
}

Which I agree is probably not the most efficient solution. I'd be happy
to know how I can improve that.

May 11 '06 #2
Jim Langston wrote:
Is there any builtin lowercase std::string compare? Right now I'm doing
this:

if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 )
AmmoType = Item_Ammo_GunBullet;

Is there anything the standard library to do this? I'm not interested in
Boost until it becomes part of the standard.


Unfortunately, std::string lacks case insensitive compare.
Case-insensitive operations are locale dependant, and therefore require
a more complex functionality then the one described above.See:

http://www.gotw.ca/gotw/029.htm and
http://gcc.gnu.org/onlinedocs/libstd...ngs/howto.html

Assuming you limit yourself to single-byte (char) ASCII characters and
you don't change LOCALE settings, _stricmp is fine. If you do need
locale depend functions, take a look at the Strinx library:

http://strinx.sourceforge.net/strinx...ith-std-locale
which provides a set of locale-dependant functions for strings.

Good luck,
SS.

May 11 '06 #3

shablool wrote:
Unfortunately, std::string lacks case insensitive compare.
Case-insensitive operations are locale dependant, and therefore require
a more complex functionality then the one described above.See:


Actually it's the char_traits that performs the comparison, not the
std::basic_string class.

You can use a specific char_traits in your strings to do such
comparisons.

transforming the whole string looks wrong algorithmically, as it means
you have "worst case scenario" on every string, i.e. the slowest result
of the comparison is either when the strings match or they differ on
the last character, in which case you need N comparisons, but a lot of
the time the comparison will fail on the first character. By
transforming the entire string to lowercase (or uppercase) your
algorithm will be O(N) every time (well you will be performing an
action on every character).

It would be therefore better to provide a single-character comparison
and use that.

I would like to see a case-insensitive comparison function in <locale>
which is where I think it belongs. There isn't one, although you can
write one based on the toupper that lives in locale. You can do it
based on single-character comparison (call the locale toupper or
tolower on both and compare them) and then you can do another one for a
sequence (std::compare with your predicate).

Difficult to know what header it should be in - it is both an algorithm
and locale based. You wouldn't want to put it in <algortihm> because it
has then has a dependency on <locale>. You could do it the other way if
you rewrite the compare and not use std::compare. The other alternative
would be to give it its own header so you only include what you use.

May 11 '06 #4
Relying on char_traits to implement a case insensitive string is wrong.
See: http://lafstern.org/matt/col2_new.pdf

Here is an implementation (from the Strinx library) which uses
std::locale, but not char_traits (although there is an assumption that
there exists operator== for CharT, which is obviously true for built-in
types char and wchar_t):

#include <locale>

// Case-insensitive compare of the first n characters of s1 and s2,
// using std::locale.
// Note: s1 and s2 are not necessarily null terminated strings.
template <typename CharT>
int icompare(const CharT* s1, const CharT* s2, size_type n, const
std::locale& loc)
{
const std::ctype<CharT>& facet = std::use_facet<std::ctype<CharT>
(loc);

const CharT* end = s1+n;
for (const CharT *p = s1, *q = s2; p != end; ++p, ++q)
{
const CharT c1 = *p;
const CharT c2 = *q;
if ( !(c1 == c2) && !(facet.toupper(c1) == facet.toupper(c2)) )
{
return (int(c1 < c2) - int(c2 < c1));
}
}
return 0;
}

May 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2596
by: Kian Goh | last post by:
Hello, I would like to replace all the occurrence of "(" and ")" in C++ std::string to "\(" and "\)". For example: string s = "(abc)|(toto)|(lala)" will be become "\(abc\)|\(toto\)|\(lala\)"
2
1785
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
3
17067
by: jl_post | last post by:
Hi, I recently wrote two benchmark programs that compared if two strings were equal: one was a C program that used C char arrays with strcmp(), and the other was a C++ program that used...
8
4788
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
15
6873
by: roberts.noah | last post by:
Are there any decent benchmarks of the difference between using c strings and std::string out there? Google isn't being friendly about it. Obviously this would be dependant on different...
10
9030
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use...
4
11209
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
14
24237
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
16
3641
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7110
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.