diff options
Diffstat (limited to 'include/cru/common')
-rw-r--r-- | include/cru/common/Exception.hpp | 5 | ||||
-rw-r--r-- | include/cru/common/Range.hpp | 43 | ||||
-rw-r--r-- | include/cru/common/String.hpp | 14 | ||||
-rw-r--r-- | include/cru/common/StringUtil.hpp | 6 |
4 files changed, 61 insertions, 7 deletions
diff --git a/include/cru/common/Exception.hpp b/include/cru/common/Exception.hpp index 861bf5e9..8864f4df 100644 --- a/include/cru/common/Exception.hpp +++ b/include/cru/common/Exception.hpp @@ -18,4 +18,9 @@ class CRU_BASE_API Exception { private: String message_; }; + +class CRU_BASE_API TextEncodeException : public Exception { + public: + using Exception::Exception; +}; } // namespace cru diff --git a/include/cru/common/Range.hpp b/include/cru/common/Range.hpp new file mode 100644 index 00000000..ecc61243 --- /dev/null +++ b/include/cru/common/Range.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "Base.hpp" + +namespace cru { +struct Range final { + constexpr static Range FromTwoSides(gsl::index start, gsl::index end) { + return Range(start, end - start); + } + + constexpr static Range FromTwoSides(gsl::index start, gsl::index end, + gsl::index offset) { + return Range(start + offset, end - start); + } + + constexpr Range() = default; + constexpr Range(const gsl::index position, const gsl::index count = 0) + : position(position), count(count) {} + + gsl::index GetStart() const { return position; } + gsl::index GetEnd() const { return position + count; } + + void ChangeEnd(gsl::index new_end) { count = new_end - position; } + + Range Normalize() const { + auto result = *this; + if (result.count < 0) { + result.position += result.count; + result.count = -result.count; + } + return result; + } + + Range CoerceInto(gsl::index min, gsl::index max) const { + auto coerce = [min, max](gsl::index index) { + return index > max ? max : (index < min ? min : index); + }; + return Range::FromTwoSides(coerce(GetStart()), coerce(GetEnd())); + } + + gsl::index position = 0; + gsl::index count = 0; +}; +} // namespace cru diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 563e1606..4211a160 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -1,7 +1,9 @@ #pragma once - #include "Base.hpp" +#include "StringUtil.hpp" +#include "Range.hpp" + #include <cstdint> #include <iterator> @@ -127,6 +129,16 @@ class CRU_BASE_API String { } public: + Utf16CodePointIterator CodePointIterator() const { + return Utf16CodePointIterator( + std::u16string_view(reinterpret_cast<char16_t*>(buffer_), size_)); + } + + Index IndexFromCodeUnitToCodePoint(Index code_unit_index) const; + Index IndexFromCodePointToCodeUnit(Index code_point_index) const; + Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const; + Range RangeFromCodePointToCodeUnit(Range code_point_range) const; + const char16_t* Char16CStr() const { return reinterpret_cast<const char16_t*>(c_str()); } diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index 985f0032..5c230898 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -1,6 +1,5 @@ #pragma once #include "Base.hpp" -#include "Exception.hpp" #include <functional> #include <string> @@ -10,11 +9,6 @@ namespace cru { using CodePoint = std::int32_t; constexpr CodePoint k_invalid_code_point = -1; -class CRU_BASE_API TextEncodeException : public Exception { - public: - using Exception::Exception; -}; - inline bool IsUtf16SurrogatePairCodeUnit(char16_t c) { return c >= 0xD800 && c <= 0xDFFF; } |