Solved

strcmpi for wstring that has suport for internationalization

Posted on 2002-03-03
8
466 Views
Last Modified: 2013-12-14
Is there a function like strcmpi that works with wstring and string, and has suport for internationalization?

If not, does anyone have code for case insensitive strcmp function for both string and wstring that uses locale?
0
Comment
Question by:GoodInfo4117
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Expert Comment

by:gandalf79
ID: 6837842
I assume you are trying to use STL since you mentioned the wstring and string variable names. The nice thing about STL is you can use any character varialbe without having to worry about writing a hole new sting class. this includes wchar_t as seen below.

typedef basic_string<wchar_t> wstring;

if I understand what you want to do corrctly this should do something similar to what you want.

void main() {
     std::wstring str1, str2;
     wchar_t* wcharStr1 = new wchar_t[10];
     wchar_t* wcharStr2 = new wchar_t[10];

     wcharStr1 = (wchar_t*)"xxxx";
     wcharStr2 = (wchar_t*)"yyyy";

     str1.assign(wcharStr1);
     str2.assign(wcharStr2);

     std::wcout << str1 << ":" << str2 << std::endl;

     str2.assign(str1);

     std::wcout << str1 << ":" << str2 << std::endl;
}

i am not sure of the characters placed in wcharStr1 and wcharStr2 because my computer does not support wide characters in this way, so I am unable to over come this obstical. However if you can get passed this it should work for you.

You can also use any other functions from the basic_string class for wstring. hope this works for you.
0
 
LVL 30

Accepted Solution

by:
Axter earned 300 total points
ID: 6838074
>>Is there a function like strcmpi that works with wstring
>>and string, and has suport for internationalization?

No.

You can try the following class and function.
They don't take locale as a variable, but instead they take the string used to create a locale.


#include <iostream>
#include <algorithm>
#include <string>
#include <locale>

class StrICmp
{
public:
     StrICmp(const std::string &Lang = "english") : m_locE(Lang.c_str())
     {
     }
     class CharLessI
     {
     public:
          CharLessI(std::locale &locE) : m_locE(locE)
          {
          }
          template<typename T>
               bool operator()(T c1, T c2)
          {
               return std::tolower(c1, m_locE) < std::tolower(c2, m_locE);
          }
     private:
          std::locale &m_locE;
     };
     template<typename T>
          int operator()(const T &s1, const T &s2)
     {
          if (std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), CharLessI(m_locE)))
               return -1;
          if (std::lexicographical_compare(s2.begin(), s2.end(), s1.begin(), s1.end(), CharLessI(m_locE)))
               return 1;
          return 0;
     }
private:
     std::locale m_locE;
};

template<typename T>
int StrCmpI(const T &s1, const T &s2, const std::string &Lang = "english")
{
     return StrICmp(Lang)(s1, s2);
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 6838076
Example Usage:

void Test1(void)
{
     const std::string Src_set1a = "This Is A Test";
     const std::string Src_set1b = "this is a tesT";
     int test1 = StrICmp()(Src_set1a, Src_set1b);
     
     const std::string Src_set2a = "This Is A Test";
     const std::string Src_set2b = "this is a tesTa";
     int test2 = StrCmpI(Src_set2a, Src_set2b);
     
     const std::string Src_set3a = "This Is A Testa";
     const std::string Src_set3b = "this is a tesT";
     int test3 = StrICmp()(Src_set3a, Src_set3b);
     
     std::cout << " test1 = " << test1 << " test2 = " << test2 << " test3 = " << test3 << std::endl;
     
     test1 = strcmpi(Src_set1a.c_str(), Src_set1b.c_str());
     test2 = strcmpi(Src_set2a.c_str(), Src_set2b.c_str());
     test3 = strcmpi(Src_set3a.c_str(), Src_set3b.c_str());
     
     std::cout << " test1 = " << test1 << " test2 = " << test2 << " test3 = " << test3 << std::endl;
}
void Test2(void)
{
     const std::wstring Src_set1a = L"This Is A Test";
     const std::wstring Src_set1b = L"this is a tesT";
     int test1 = StrICmp("english")(Src_set1a, Src_set1b);
     
     const std::wstring Src_set2a = L"This Is A Test";
     const std::wstring Src_set2b = L"this is a tesTa";
     int test2 = StrICmp("english")(Src_set2a, Src_set2b);
     
     const std::wstring Src_set3a = L"This Is A Testa";
     const std::wstring Src_set3b = L"this is a tesT";
     int test3 = StrICmp("english")(Src_set3a, Src_set3b);
     
     std::cout << " test1 = " << test1 << " test2 = " << test2 << " test3 = " << test3 << std::endl;
}

int main(int argc, char* argv[])
{
     Test1();
     Test2();
     system("pause");
     return 0;
}

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 5

Expert Comment

by:yonat
ID: 6838578
or you can write your own char_traits class, say case_insensitive_char_traits, and then use basic_string<char, case_insensitive_char_traits<char> > or basic_string<wchar_t, case_insensitive_char_traits<wchar_t> >.
0
 

Author Comment

by:GoodInfo4117
ID: 6838831
gandalf79,
I'm sorry, but I have no idea how your comment is related to my question.


Axter,
Your method is functional.  But I would think there would be a simpler method for this.  If I don't get a better answer, I'll accept yours.


yonat,
Would this method require less code then Axter's method?
If so, can you post a working example?
0
 
LVL 5

Expert Comment

by:yonat
ID: 6840613
It's not much code, and the usage is nicer IMHO. Here is code to do it:
http://www.josuttis.com/libbook/string/icstring.hpp.html

And here is code + good explanation:
http://www.gotw.ca/gotw/029.htm
0
 
LVL 30

Expert Comment

by:Axter
ID: 6841536
yonat,
Nice links
0
 

Author Comment

by:GoodInfo4117
ID: 6843302
yonat,
Thanks for the link, but I can't use it.  It doesn't support locale.
I need a method that can support any major language.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question