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

Author Topic: std::istream& operator >> for sf::String  (Read 437 times)

0 Members and 1 Guest are viewing this topic.

Garwin

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
std::istream& operator >> for sf::String
« on: December 02, 2023, 11:52:54 am »
I overloaded operator >> for std::istream and sf::String to handle UTF8 encoding.
However I can see there are a lot of copies there:
- from stream to std::string (UTF8)
- from std::string (UTF8) to std::basic_string<sf::Uint32> using sfml function decode
- from std::basic_string<sf::Uint32> to sf::String

I need to use the middle step through std::basic_string<sf::Uint32> as there is no std::back_inserter for sf::String. Is there possibility to decrease number of steps.
note: SFML 2.6.1

Some ideas I have:
- making std::basic_string<sf::Uint32> static and add clear at the end of overload so there is no need to for memory allocation each time

std::istream& operator>> (std::istream& in, sf::String& string)
{
    std::string u8string;
    in >> u8string;
    auto begin = u8string.begin();
    auto end = u8string. end();
    std::basic_string<sf::Uint32> stringUint32;
    auto output = std::back_inserter(stringUint32);
    while (begin < end)
    {
        sf::Uint32 codepoint;
        begin = sf::Utf<8>::decode(begin, end, codepoint);
        *output++ = codepoint;
    }
    string = stringUint32;
    return in;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10895
    • View Profile
    • development blog
    • Email
Re: std::istream& operator >> for sf::String
« Reply #1 on: December 06, 2023, 01:55:05 pm »
Can't you use sf::String::fromUtf8 to at least work around one of the copies?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Garwin

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: std::istream& operator >> for sf::String
« Reply #2 on: December 09, 2023, 06:54:28 pm »
Thanks, in this case, where I parse whole string, I can.