Solved

strcmpi for wstring that has suport for internationalization

Posted on 2002-03-03
8
472 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ reading data from file into two dimensional array 3 129
Syntax Error 2 91
Finding Divisors 5 35
print bytes of an integer 6 21
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

735 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