Skip to content

Commit

Permalink
(v2) Adding support for std::string_view to sf::String
Browse files Browse the repository at this point in the history
Cleaned up formatting and added comments
  • Loading branch information
khatharr committed Mar 15, 2023
1 parent 641cf54 commit 485d904
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
29 changes: 29 additions & 0 deletions include/SFML/System/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <locale>
#include <string>
#include <string_view>


namespace sf
Expand Down Expand Up @@ -107,6 +108,18 @@ class SFML_SYSTEM_API String
////////////////////////////////////////////////////////////
String(const char* ansiString, const std::locale& locale = std::locale());

////////////////////////////////////////////////////////////
/// \brief Construct from an ANSI string and a locale
///
/// The source string_view is converted to UTF-32 according
/// to the given locale.
///
/// \param ansiString ANSI string to convert
/// \param locale Locale to use for conversion
///
////////////////////////////////////////////////////////////
String(std::string_view ansiString, const std::locale& locale = std::locale());

////////////////////////////////////////////////////////////
/// \brief Construct from an ANSI string and a locale
///
Expand All @@ -127,6 +140,14 @@ class SFML_SYSTEM_API String
////////////////////////////////////////////////////////////
String(const wchar_t* wideString);

////////////////////////////////////////////////////////////
/// \brief Construct from a wide string_view
///
/// \param wideString Wide string to convert
///
////////////////////////////////////////////////////////////
String(std::wstring_view wideString);

////////////////////////////////////////////////////////////
/// \brief Construct from a wide string
///
Expand All @@ -143,6 +164,14 @@ class SFML_SYSTEM_API String
////////////////////////////////////////////////////////////
String(const std::uint32_t* utf32String);

////////////////////////////////////////////////////////////
/// \brief Construct from an UTF-32 string_view
///
/// \param utf32String UTF-32 string to assign
///
////////////////////////////////////////////////////////////
String(std::basic_string_view<std::uint32_t> utf32String);

////////////////////////////////////////////////////////////
/// \brief Construct from an UTF-32 string
///
Expand Down
22 changes: 19 additions & 3 deletions src/SFML/System/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ String::String(const char* ansiString, const std::locale& locale)


////////////////////////////////////////////////////////////
String::String(const std::string& ansiString, const std::locale& locale)
String::String(std::string_view ansiString, const std::locale& locale)
{
m_string.reserve(ansiString.length() + 1);
Utf32::fromAnsi(ansiString.begin(), ansiString.end(), std::back_inserter(m_string), locale);
}


////////////////////////////////////////////////////////////
String::String(const std::string& ansiString, const std::locale& locale) : String(std::string_view(ansiString), locale)
{
}

////////////////////////////////////////////////////////////
String::String(const wchar_t* wideString)
{
Expand All @@ -98,13 +103,19 @@ String::String(const wchar_t* wideString)


////////////////////////////////////////////////////////////
String::String(const std::wstring& wideString)
String::String(std::wstring_view wideString)
{
m_string.reserve(wideString.length() + 1);
Utf32::fromWide(wideString.begin(), wideString.end(), std::back_inserter(m_string));
}


////////////////////////////////////////////////////////////
String::String(const std::wstring& wideString) : String(std::wstring_view(wideString))
{
}


////////////////////////////////////////////////////////////
String::String(const std::uint32_t* utf32String)
{
Expand All @@ -114,11 +125,16 @@ String::String(const std::uint32_t* utf32String)


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


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

////////////////////////////////////////////////////////////
String::String(const String& copy) = default;

Expand Down

0 comments on commit 485d904

Please sign in to comment.