Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode Processing in C++

Tags:

c++

unicode

What is the best practice of Unicode processing in C++?

like image 254
Fortepianissimo Avatar asked Sep 11 '08 01:09

Fortepianissimo


2 Answers

  • Use ICU for dealing with your data (or a similar library)
  • In your own data store, make sure everything is stored in the same encoding
  • Make sure you are always using your unicode library for mundane tasks like string length, capitalization status, etc. Never use standard library builtins like is_alpha unless that is the definition you want.
  • I can't say it enough: never iterate over the indices of a string if you care about correctness, always use your unicode library for this.
like image 65
hazzen Avatar answered Oct 14 '22 18:10

hazzen


If you don't care about backwards compatibility with previous C++ standards, the current C++11 standard has built in Unicode support: http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2011/n3242.pdf

So the truly best practice for Unicode processing in C++ would be to use the built in facilities for it. That isn't always a possibility with older code bases though, with the standard being so new at present.

EDIT: To clarify, C++11 is Unicode aware in that it now has support for Unicode literals and Unicode strings. However, the standard library has only limited support for Unicode processing and conversion. For your current needs this may be enough. However, if you need to do a large amount of heavy lifting right now then you may still need to use something like ICU for more in-depth processing. There are some proposals currently in the works to include more robust support for text conversion between different encodings. My guess (and hope) is that this will be part of the next technical report.

like image 34
eestrada Avatar answered Oct 14 '22 17:10

eestrada