Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for std::string_view in sf::String #2517

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
ChrisThrasher marked this conversation as resolved.
Show resolved Hide resolved
{
}


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

Expand Down