Lowercase std::string compare?

Discussion in 'C++' started by Jim Langston, May 11, 2006.

  1. Jim Langston

    Jim Langston Guest

    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.
     
    Jim Langston, May 11, 2006
    #1
    1. Advertising

  2. Jim Langston

    Guest

    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, 2006
    #2
    1. Advertising

  3. Jim Langston

    shablool Guest

    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/libstdc /21_strings/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.html#string-parsing-with-std-locale


    which provides a set of locale-dependant functions for strings.

    Good luck,
    SS.
     
    shablool, May 11, 2006
    #3
  4. Jim Langston

    Earl Purple Guest

    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.
     
    Earl Purple, May 11, 2006
    #4
  5. Jim Langston

    shablool Guest

    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;
    }
     
    shablool, May 11, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zombie

    <string> to lowercase

    Zombie, Aug 18, 2004, in forum: C++
    Replies:
    38
    Views:
    25,288
    davitz38
    Dec 25, 2009
  2. Peter Jansson
    Replies:
    5
    Views:
    6,323
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    23
    Views:
    1,193
  4. Fei Liu
    Replies:
    9
    Views:
    447
  5. Jeffrey Walton
    Replies:
    10
    Views:
    944
    Mathias Gaunard
    Nov 26, 2006
Loading...

Share This Page