Skip to content

Commit

Permalink
Implement sf::String in terms of std::u32string
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Apr 4, 2023
1 parent ebe4b3c commit 95c3470
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 64 deletions.
28 changes: 14 additions & 14 deletions include/SFML/System/String.hpp
Expand Up @@ -48,15 +48,15 @@ class SFML_SYSTEM_API String
////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////
using Iterator = std::basic_string<std::uint32_t>::iterator; //!< Iterator type
using ConstIterator = std::basic_string<std::uint32_t>::const_iterator; //!< Read-only iterator type
using Iterator = std::u32string::iterator; //!< Iterator type
using ConstIterator = std::u32string::const_iterator; //!< Read-only iterator type

////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
// NOLINTBEGIN(readability-identifier-naming)
/// Represents an invalid position in the string
static const std::size_t InvalidPos{std::basic_string<std::uint32_t>::npos};
static const std::size_t InvalidPos{std::u32string::npos};
// NOLINTEND(readability-identifier-naming)

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -93,7 +93,7 @@ class SFML_SYSTEM_API String
/// \param utf32Char UTF-32 character to convert
///
////////////////////////////////////////////////////////////
String(std::uint32_t utf32Char);
String(char32_t utf32Char);

////////////////////////////////////////////////////////////
/// \brief Construct from a null-terminated C-style ANSI string and a locale
Expand Down Expand Up @@ -141,15 +141,15 @@ class SFML_SYSTEM_API String
/// \param utf32String UTF-32 string to assign
///
////////////////////////////////////////////////////////////
String(const std::uint32_t* utf32String);
String(const char32_t* utf32String);

////////////////////////////////////////////////////////////
/// \brief Construct from an UTF-32 string
///
/// \param utf32String UTF-32 string to assign
///
////////////////////////////////////////////////////////////
String(const std::basic_string<std::uint32_t>& utf32String);
String(const std::u32string& utf32String);

////////////////////////////////////////////////////////////
/// \brief Copy constructor
Expand Down Expand Up @@ -203,8 +203,8 @@ class SFML_SYSTEM_API String
/// \brief Create a new sf::String from a UTF-32 encoded string
///
/// This function is provided for consistency, it is equivalent to
/// using the constructors that takes a const std::uint32_t* or
/// a std::basic_string<std::uint32_t>.
/// using the constructors that takes a const char32_t* or
/// a std::u32string.
///
/// \param begin Forward iterator to the beginning of the UTF-32 sequence
/// \param end Forward iterator to the end of the UTF-32 sequence
Expand Down Expand Up @@ -297,7 +297,7 @@ class SFML_SYSTEM_API String
/// \see toUtf8, toUtf32
///
////////////////////////////////////////////////////////////
std::basic_string<std::uint16_t> toUtf16() const;
std::u16string toUtf16() const;

////////////////////////////////////////////////////////////
/// \brief Convert the Unicode string to a UTF-32 string
Expand All @@ -310,7 +310,7 @@ class SFML_SYSTEM_API String
/// \see toUtf8, toUtf16
///
////////////////////////////////////////////////////////////
std::basic_string<std::uint32_t> toUtf32() const;
std::u32string toUtf32() const;

////////////////////////////////////////////////////////////
/// \brief Overload of assignment operator
Expand Down Expand Up @@ -343,7 +343,7 @@ class SFML_SYSTEM_API String
/// \return Character at position \a index
///
////////////////////////////////////////////////////////////
std::uint32_t operator[](std::size_t index) const;
char32_t operator[](std::size_t index) const;

////////////////////////////////////////////////////////////
/// \brief Overload of [] operator to access a character by its position
Expand All @@ -356,7 +356,7 @@ class SFML_SYSTEM_API String
/// \return Reference to the character at position \a index
///
////////////////////////////////////////////////////////////
std::uint32_t& operator[](std::size_t index);
char32_t& operator[](std::size_t index);

////////////////////////////////////////////////////////////
/// \brief Clear the string
Expand Down Expand Up @@ -480,7 +480,7 @@ class SFML_SYSTEM_API String
/// \return Read-only pointer to the array of characters
///
////////////////////////////////////////////////////////////
const std::uint32_t* getData() const;
const char32_t* getData() const;

////////////////////////////////////////////////////////////
/// \brief Return an iterator to the beginning of the string
Expand Down Expand Up @@ -537,7 +537,7 @@ class SFML_SYSTEM_API String
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::basic_string<std::uint32_t> m_string; //!< Internal string of UTF-32 characters
std::u32string m_string; //!< Internal string of UTF-32 characters
};

////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Network/Packet.cpp
Expand Up @@ -381,7 +381,7 @@ Packet& Packet::operator>>(String& data)
{
std::uint32_t character = 0;
*this >> character;
data += character;
data += static_cast<char32_t>(character);
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/SFML/System/String.cpp
Expand Up @@ -57,7 +57,7 @@ String::String(wchar_t wideChar)


////////////////////////////////////////////////////////////
String::String(std::uint32_t utf32Char)
String::String(char32_t utf32Char)
{
m_string += utf32Char;
}
Expand Down Expand Up @@ -110,15 +110,15 @@ String::String(const std::wstring& wideString)


////////////////////////////////////////////////////////////
String::String(const std::uint32_t* utf32String)
String::String(const char32_t* utf32String)
{
if (utf32String)
m_string = utf32String;
}


////////////////////////////////////////////////////////////
String::String(const std::basic_string<std::uint32_t>& utf32String) : m_string(utf32String)
String::String(const std::u32string& utf32String) : m_string(utf32String)
{
}

Expand Down Expand Up @@ -192,10 +192,10 @@ std::basic_string<std::uint8_t> String::toUtf8() const


////////////////////////////////////////////////////////////
std::basic_string<std::uint16_t> String::toUtf16() const
std::u16string String::toUtf16() const
{
// Prepare the output string
std::basic_string<std::uint16_t> output;
std::u16string output;
output.reserve(m_string.length());

// Convert
Expand All @@ -206,7 +206,7 @@ std::basic_string<std::uint16_t> String::toUtf16() const


////////////////////////////////////////////////////////////
std::basic_string<std::uint32_t> String::toUtf32() const
std::u32string String::toUtf32() const
{
return m_string;
}
Expand All @@ -225,14 +225,14 @@ String& String::operator+=(const String& right)


////////////////////////////////////////////////////////////
std::uint32_t String::operator[](std::size_t index) const
char32_t String::operator[](std::size_t index) const
{
return m_string[index];
}


////////////////////////////////////////////////////////////
std::uint32_t& String::operator[](std::size_t index)
char32_t& String::operator[](std::size_t index)
{
return m_string[index];
}
Expand Down Expand Up @@ -311,7 +311,7 @@ String String::substring(std::size_t position, std::size_t length) const


////////////////////////////////////////////////////////////
const std::uint32_t* String::getData() const
const char32_t* String::getData() const
{
return m_string.c_str();
}
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Window/OSX/HIDInputManager.mm
Expand Up @@ -690,7 +690,7 @@
// Phase 2: Try to convert the key to unicode
UniChar unicode = toUnicode(localize(code));
if (unicode != 0x00)
return String(static_cast<std::uint32_t>(unicode));
return String(static_cast<char32_t>(unicode));
}

// Phase 3: Return final fallback
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Window/Unix/KeySymToUnicodeMapping.cpp
Expand Up @@ -30,7 +30,7 @@
namespace sf::priv
{

std::uint32_t keysymToUnicode(KeySym keysym)
char32_t keysymToUnicode(KeySym keysym)
{
// clang-format off
switch (keysym)
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Window/Unix/KeySymToUnicodeMapping.hpp
Expand Up @@ -49,6 +49,6 @@ namespace sf::priv
/// \return corresponding UTF-32
///
////////////////////////////////////////////////////////////
std::uint32_t keysymToUnicode(KeySym keysym);
char32_t keysymToUnicode(KeySym keysym);

} // namespace sf::priv
4 changes: 2 additions & 2 deletions src/SFML/Window/Unix/KeyboardImpl.cpp
Expand Up @@ -671,8 +671,8 @@ String KeyboardImpl::getDescription(Keyboard::Scancode code)

if (checkInput)
{
KeySym keysym = scancodeToKeySym(code);
std::uint32_t unicode = keysymToUnicode(keysym);
KeySym keysym = scancodeToKeySym(code);
char32_t unicode = keysymToUnicode(keysym);

if (unicode != 0)
return String(unicode);
Expand Down

0 comments on commit 95c3470

Please sign in to comment.