Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to make cross-platform locale-independent string code in C++ without using ILU
    primarykey
    data
    text
    <p>Background (through it's not that important for the scope of problem): I am working on game framework in C++ based on SDL that will be compiled on different platforms (Win/Lin/Mac/iOS/Android/etc.) and I need good, cross-platform way to store locale-dependent string which is not so bloated like ICU library is</p> <p>The wchar_t is not an option here, because of it's platform dependence. You can't (for example) save a game on Linux (wchar_t is 4 bytes long) and then load in on Windows (because wchar_t is 2 bytes long).</p> <p>So, my idea is to make universal character string (UCS-2) as standard inside my framework and games made atop of it. I want to do simple typedef in core header:</p> <pre><code>typedef unsigned short uchar typedef std::basic_string&lt;uchar&gt; ustring </code></pre> <p>The problem is that many underlying libraries uses different string encoding. So I need couple of functions:</p> <pre><code>std::string UStrToAscii(const ustring &amp; str); ustring AsciiToUStr(const char * str); std::string UStrToUtf8(const ustring &amp; str); ustring Utf8ToUStr(const char * str); std::wstring UStrToWide(const ustring &amp; str); ustring WideToUStr(const wchar_t * str); // etc. </code></pre> <p>I am returning STL object, because I don't need to worry about their lifetime and time/memory cost is pretty small.</p> <p>Questions:</p> <ul> <li><p>Is it "right track" to do locale/platform independent strings? Or perhaps there is much easier solution I missed on Google?</p></li> <li><p>How should I define string in code (for example to be used in Logger)?</p></li> </ul> <p>My idea is to use macro like this:</p> <pre><code>#define _U(str) WideToUStr(L##str) // Then in code: _U("Hello World zażółć gęślą jaźń"); // some polish special chars </code></pre> <p>But I don't know if it's a right track (is it cross-platform? Could it be completed easier?)</p> <ul> <li>Second problem: it's clear that I cannot rely on sprintf. My idea is to write my own print formatted text function, but perhaps there is some easier way?</li> </ul> <p>Ah, and I don't want to use UTF-8 as native format in my framework - it's way too compilcated to do simple task on strings (like substrings, get char from index, etc - you have to traverse entire string and make sure that the byte at picked index is actually a char, not an entity of other char, etc.)</p> <p><strong>EDIT</strong></p> <p>To be clear, the not-UTF8 rule is not my deal-breaker, it's merely discouraged because of it's limitations. But if the only right way to do is is by UTF-8 (pros strongly beats cons), then it's acceptable as answer</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload