Skip to content

Commit

Permalink
Support for string_view
Browse files Browse the repository at this point in the history
Adds support to sf::String to allow construction from std::string_view and variants thereof
  • Loading branch information
khatharr committed Apr 13, 2023
1 parent 82b9821 commit 5c1c795
Show file tree
Hide file tree
Showing 2 changed files with 49 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_view and a locale
///
/// The source string_view is converted to UTF-32 according
/// to the given locale.
///
/// \param ansiString ANSI string_view 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_view 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 char32_t* utf32String);

////////////////////////////////////////////////////////////
/// \brief Construct from an UTF-32 string_view
///
/// \param utf32String UTF-32 string_view to assign
///
////////////////////////////////////////////////////////////
String(std::u32string_view utf32String);

////////////////////////////////////////////////////////////
/// \brief Construct from an UTF-32 string
///
Expand Down
23 changes: 20 additions & 3 deletions src/SFML/System/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,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 @@ -102,13 +107,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 char32_t* utf32String)
{
Expand All @@ -118,7 +129,13 @@ String::String(const char32_t* utf32String)


////////////////////////////////////////////////////////////
String::String(const std::u32string& utf32String) : m_string(utf32String)
String::String(std::u32string_view utf32String) : m_string(utf32String)
{
}


////////////////////////////////////////////////////////////
String::String(const std::u32string& utf32String) : String(std::u32string_view(utf32String))
{
}

Expand Down

0 comments on commit 5c1c795

Please sign in to comment.