[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

strcmpi for wstring that has suport for internationalization

Posted on 2002-03-03
8
Medium Priority
?
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

649 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