|
-
Dec 13th, 2002, 05:11 AM
#1
Thread Starter
Addicted Member
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
-
Dec 13th, 2002, 06:15 AM
#2
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.
-
Dec 13th, 2002, 06:54 AM
#3
Thread Starter
Addicted Member
-
Dec 13th, 2002, 07:02 AM
#4
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.
-
Dec 13th, 2002, 12:58 PM
#5
transcendental analytic
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.
-
Dec 13th, 2002, 01:26 PM
#6
Frenzied Member
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.
-
Dec 13th, 2002, 03:18 PM
#7
transcendental analytic
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.
-
Dec 13th, 2002, 04:00 PM
#8
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.
-
Dec 13th, 2002, 04:02 PM
#9
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.
-
Dec 13th, 2002, 04:04 PM
#10
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.
-
Dec 13th, 2002, 04:14 PM
#11
transcendental analytic
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.
-
Dec 13th, 2002, 04:16 PM
#12
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.
-
Dec 13th, 2002, 04:17 PM
#13
transcendental analytic
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.
-
Dec 13th, 2002, 04:25 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|