Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Passing a std::string of unicode characters (including chars outside of ASCII)  (Read 5944 times)

0 Members and 1 Guest are viewing this topic.

Tamico

  • Newbie
  • *
  • Posts: 12
    • View Profile
This helped me a lot, and it could be useful to someone. I've adapted the solution proposed on another forum post (here's a link).

Here's the solution I used:
// Takes sf::String (UTF-32) and returns std::string (UTF-8)
std::string to_std_string(const sf::String& original)
{
    // The final UTF-8 string
    std::string str;
    // Worst case scenario: 4 bytes per codepoint
    str.resize(original.getSize() * 4);
    std::string::iterator last = sf::Utf<32>::toUtf8( original.getData(),
        original.getData() + original.getSize(), str.begin() );
    str.resize(last - str.begin());
    return str;
}

This will take any sf::String containing unicode codepoint and pass it to a std::string that can be used to output to anywhere. You can even use this std::string for filenames.

NOTE: I've tested this with the main Linux distros (Debian, OpenSUSE, Arch, Fedora, etc.) and Windows XP/Vista/7. The behavior appears to be consistent, but it may not be that way for all machines.

You can set a unicode character by using its codepoint:
// Codepoint in decimal
sf::Uint32 unicode_char = 12354;
// Append unicode char to an sf::String
sf::String unicode_string += unicode_char;

This will create the character '0x3042' (or '12354' in decimal) which is the hiragana character 'あ'. For reference, here's a list of unicode characters along with their codepoint.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
What about sf::String::toUtf8()? :P
Laurent Gomila - SFML developer

Tamico

  • Newbie
  • *
  • Posts: 12
    • View Profile
What about sf::String::toUtf8()? :P

I've encountered situations where that doesn't work. Namely, two separate Windows machines that had different versions of standard string libraries. There's also an Ubuntu-based distro that comes with a version of the libraries that behaved differently.

I don't know what causes it, but the solution above seems to work everywhere. :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
But yet this function uses the same code as you. If it really doesn't work, it would be great if you could post a proper bug report.
Laurent Gomila - SFML developer

Tamico

  • Newbie
  • *
  • Posts: 12
    • View Profile
After some trial and error, turns out that I'm an idiot and just needed to recompile SFML on those machines. Oops!

(Also, thank you for producing SFML!)

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
turns out that I'm an idiot
The main cause of our bugs. =)

 

anything