Results 1 to 14 of 14

Thread: basic_string comparison

  1. #1

    Thread Starter
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196

    basic_string comparison

    Is there a way to compare strings that is case insensitive? I know I can compare them using stricmp, but is there an inbuilt method or technique?

    Thanks in advance

    HD

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, not really. You can use the Traits template argument to create your own basic_string specialization that only does case insensitive comparisons but if you want to do it just for a moment you'll have to use stricmp or memicmp.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    OK. Cheers

    HD

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And you can't simply assign from a case sensitive basic_string to a insensitive basic_string, you'll have to use iterators (src.begin() and back_inserter(target)) and the copy algorithm.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    hmm, this is the second time I think STL isn't generic enough
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by kedaman
    hmm, this is the second time I think STL isn't generic enough
    You are looking at it from the wrong angle...

    Code:
    #define lcase(x) (((x >='A') && (x <= 'Z')) ? x-'A' + 'a' : x)
    template<class t=char>
    class ci_char {
    private:
        t m_c;
    public:
       ci_char& operator =(t c){m_c = c;}
       bool operator ==(t& c) { return lcase(m_c)==lcase(c); }
       bool operator ==(ci_char& c){ return lcase(m_c)==lcase(c.m_c);}
       ...
    };
    A couple more operators are needed, of course, but you get the idea. Then you just use ci_char instead of char in std::string... you get case insensitive comparisons, but you also conserve case when copying.

    Z.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Z: no I meant, strings shouldn't be inherently case sensitive or case insensitive, the comparation should be. Data should be independent of algoritms, even Stepanov had OOP on his mind. Pure abstractuib lies in functional languages.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No operators for the char_traits, only static member functions. Best idea is to take most from char_traits:
    Code:
    template<typename C>
    struct ic_char_traits : public char_traits<C>
    {
      static int compare(const char_type *s1, const char_type *s2, size_t num) {
        int i=0;
        for(;i<num;++i) {
          if(!eq(*s1, *s2)) {
            if(lt(*s1, *s2)) {
              return -1;
            } else {
              return 1;
            }
          }
        }
        return 0;
      }
    
      static bool eq(const char_type &ch1, const char_type &ch2) {
        locale loc;
        return tolower(ch1, loc) == tolower(ch2, loc);
      }
    
      static bool eq_int_type(const int_type &ch1, const int_type &ch2) {
        locale loc;
        return tolower((char_type)ch1, loc) == tolower((char_type)ch2, loc);
      }
    
      struct eq_pred {
        bool operator()(C ch1, C ch2) {
          locale loc;
          return tolower(ch1, loc) == tolower(ch2, loc);
        }
      };
    
      static const char_type * find(const char_type *s, size_t num, const char_type &c) {
        const char_type *res = search_n(s, s+num, 1, c);
        return (res < s+num)?res:NULL;
      }
    
      static bool lt(const char_type &ch1, const char_type &ch2) {
        locale loc;
        return char_traits<char_type>::lt(tolower(ch1, loc), tolower(ch2, loc));
      }
    };
    That's it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    typedef ci_string basic_string<char, ci_char_traits<char>, allocator<char> >;
    typedef ci_wstring basic_string<wchar_t, ci_char_traits<wchar_t>, allocator<wchar_t> >;
    typedef ci_tstring basic_string<tchar_t, ci_char_traits<tchar_t>, allocator<tchar_t> >;

    tchar_t is an invention of mine, defined equivalent to TCHAR or _TCHAR. But I like the name more.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Keda: you mean the char_traits template parameter should be passed to the comparison function? They probably left that out because template methods of template classes are a very new thing and not supported when the basic_string class was standardized.

    When was the first time?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    template methods of template classes weren't available when STL was made? That still isn't enough reason to skip genericity, and put static type methods in the classes, you could go with the old function outher, class inner
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What would you have done then?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    remove all methods from the classes, and go with global template functions and structs with public data all the way
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh, well...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width