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

Author Topic: How to initialize sf::String with UTF-8-encoded data?  (Read 10313 times)

0 Members and 1 Guest are viewing this topic.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
How to initialize sf::String with UTF-8-encoded data?
« on: April 02, 2012, 08:11:35 pm »
Hey,

is it possible to initialize sf::String directly with UTF-8-encoded data?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #1 on: April 02, 2012, 08:37:36 pm »
No, you must convert to UTF-32 first and then initialize your sf::String.
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #2 on: April 02, 2012, 08:43:49 pm »
It's not a big deal, but is it somehow possible to provide a proper constructor or utility function? I have to do a lot of conversions from and to UTF-32 and UTF-8 and it's a little bit annoying to always use a temporary variable, like:

std::string utf8_source = "...";
std::basic_string<sf::Uint32> tmp;

sf::Uint8::toUtf32( source.begin(), source.end(), std::back_inserter( tmp ) );

sf::String final = tmp; // Avoid me :-(
 

I know I can write a little function doing exactly that, however with an additional constructor it can directly store to m_string of sf::String.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #3 on: April 02, 2012, 08:48:47 pm »
...how about providing functions like in the cute sf::Time? ;)

sf::String string = sf::String::fromUtfX();
string.asUtfX();

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #4 on: April 02, 2012, 08:57:03 pm »
I can provide fromUtfX functions with templated iterators, but asUtfX() functions are more complicated since I have to choose a type to return, which is not obvious.
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #5 on: April 02, 2012, 09:08:47 pm »
The fromUtfX with iterators sounds perfect. Issue added ;) https://github.com/SFML/SFML/issues/196

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #6 on: April 02, 2012, 10:31:09 pm »
Thanks :)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #7 on: April 03, 2012, 02:31:34 pm »
Quote
I can provide fromUtfX functions with templated iterators, but asUtfX() functions are more complicated since I have to choose a type to return, which is not obvious.
Couldn't you just require the template argument explicitly, i.e. string.asUtfX<32>()?

However I wouldn't mix "to" and "as", you already use toAnsiString() and toWideString().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #8 on: April 03, 2012, 02:51:40 pm »
Quote
Couldn't you just require the template argument explicitly, i.e. string.asUtfX<32>()?
I think you misunderstood the point. When calling, for example, string.asUtf8(), what should be returned? Maybe the user will need a std::basic_string<sf::Uint32>, or a QString, or a GString, or even a MyCustomUtf8String, etc. I can handle the first one, but not the others.
However there's a chance that the target type has a generic fromUtf32 function.

Quote
However I wouldn't mix "to" and "as", you already use toAnsiString() and toWideString().
True. The inconsistency with sf::Time will have to wait for SFML 3 ;D
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #9 on: April 04, 2012, 10:06:50 am »
Ok. One possibility is to use a function template with an output iterator, analog to fromUtfX(), but with T& instead of const T&. Or you directly use iterator ranges which are more flexible.

Anyway, I think there are more important things to implement in sf::String before, for example a substr() function (issue #21) or a constructor taking iterators (issue #89). The latter provides the base for many other use cases (among which is substr() and maybe also the UTF issue).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #10 on: April 04, 2012, 12:17:15 pm »
#89 got closed, probably Laurent forgot about it? ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #11 on: April 04, 2012, 12:22:56 pm »
Completely ;D
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #12 on: April 05, 2012, 09:18:45 am »
lol I knew it. Will setup a cronjob that reopens all my issues in time. ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to initialize sf::String with UTF-8-encoded data?
« Reply #13 on: April 05, 2012, 09:27:58 am »
I closed it because it was a pull request, a new clean issue should be created (that links to #89).
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email

 

anything