Core/StringExt: Add Substring family of function (unicode-aware)
This commit is contained in:
parent
1f34bb58ea
commit
625b4f2d9f
|
|
@ -48,6 +48,9 @@ namespace Nz
|
|||
template<typename T> std::basic_string<T>& ReplaceStr(std::basic_string<T>& str, const T* from, const T* to);
|
||||
template<typename T> std::basic_string<T>& ReplaceStr(std::basic_string<T>& str, std::basic_string_view<T> from, std::basic_string_view<T> to);
|
||||
|
||||
inline std::string_view Substring(std::string_view str, std::size_t index, UnicodeAware);
|
||||
NAZARA_CORE_API std::string_view Substring(std::string_view str, std::size_t index, std::size_t count, UnicodeAware);
|
||||
|
||||
inline bool StartsWith(std::string_view str, std::string_view s);
|
||||
NAZARA_CORE_API bool StartsWith(std::string_view lhs, std::string_view rhs, CaseIndependent);
|
||||
NAZARA_CORE_API bool StartsWith(std::string_view lhs, std::string_view rhs, UnicodeAware);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -109,6 +110,11 @@ namespace Nz
|
|||
return str;
|
||||
}
|
||||
|
||||
std::string_view Substring(std::string_view str, std::size_t index, UnicodeAware)
|
||||
{
|
||||
return Substring(str, index, std::numeric_limits<std::size_t>::max(), UnicodeAware{});
|
||||
}
|
||||
|
||||
inline bool StartsWith(std::string_view str, std::string_view s)
|
||||
{
|
||||
#if NAZARA_CPP_VER >= NAZARA_CPP20
|
||||
|
|
|
|||
|
|
@ -389,6 +389,35 @@ namespace Nz
|
|||
return str;
|
||||
}
|
||||
|
||||
std::string_view Substring(std::string_view str, std::size_t index, std::size_t count, UnicodeAware)
|
||||
{
|
||||
const char* start = str.data();
|
||||
const char* end = start + str.size();
|
||||
try
|
||||
{
|
||||
utf8::advance(start, index, end);
|
||||
}
|
||||
catch (const utf8::not_enough_room&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
if (count == std::numeric_limits<std::size_t>::max())
|
||||
return str.substr(start - str.data());
|
||||
|
||||
const char* to = start;
|
||||
try
|
||||
{
|
||||
utf8::advance(to, count, end);
|
||||
}
|
||||
catch (const utf8::not_enough_room&)
|
||||
{
|
||||
return str.substr(start - str.data());
|
||||
}
|
||||
|
||||
return std::string_view(start, to - start);
|
||||
}
|
||||
|
||||
bool StartsWith(std::string_view lhs, std::string_view rhs, CaseIndependent)
|
||||
{
|
||||
NAZARA_USE_ANONYMOUS_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -112,6 +112,15 @@ SCENARIO("String", "[CORE][STRING]")
|
|||
REQUIRE(Nz::ReplaceStr(str, "Unreal Reality", "Ungine") == "Ungine");
|
||||
}
|
||||
|
||||
WHEN("Getting substring of unicode strings")
|
||||
{
|
||||
std::string_view str = "\u00E0\u00E9\u00E7\u0153\u00C2\u5B98";
|
||||
CHECK(Nz::Substring(str, 0, 150, Nz::UnicodeAware{}) == "\u00E0\u00E9\u00E7\u0153\u00C2\u5B98");
|
||||
CHECK(Nz::Substring(str, 1, Nz::UnicodeAware{}) == "\u00E9\u00E7\u0153\u00C2\u5B98");
|
||||
CHECK(Nz::Substring(str, 1, 2, Nz::UnicodeAware{}) == "\u00E9\u00E7");
|
||||
CHECK(Nz::Substring(str, 1, 10, Nz::UnicodeAware{}) == "\u00E9\u00E7\u0153\u00C2\u5B98");
|
||||
}
|
||||
|
||||
WHEN("Checking if string starts with")
|
||||
{
|
||||
CHECK(Nz::StartsWith("Nazara Engine", ""));
|
||||
|
|
|
|||
Loading…
Reference in New Issue