From 09bbcecc2856275c99c9892291280ac971293756 Mon Sep 17 00:00:00 2001 From: Redstone1024 <2824517378@qq.com> Date: Sun, 10 Nov 2024 19:24:40 +0800 Subject: [PATCH] feat(string): add functions to categorize strings --- .../Source/Private/Testing/StringTesting.cpp | 9 ++++ .../Source/Public/String/Conversion.h.inl | 4 +- .../Source/Public/String/String.h | 26 +++++++++++ .../Source/Public/String/StringView.h | 46 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/Redcraft.Utility/Source/Private/Testing/StringTesting.cpp b/Redcraft.Utility/Source/Private/Testing/StringTesting.cpp index 010d96c..6c2761d 100644 --- a/Redcraft.Utility/Source/Private/Testing/StringTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/StringTesting.cpp @@ -174,6 +174,15 @@ void TestStringView() always_check(View.FindLastNotOf(LITERAL(T, "Hello! Goodbye!")) == 25); always_check(View.FindLastNotOf(LITERAL(T, '!')) == 27); } + + { + always_check( LITERAL_VIEW(T, "012345678900").IsASCII()); + always_check(!LITERAL_VIEW(T, "\u4E38\u8FA3").IsASCII()); + always_check( LITERAL_VIEW(T, "012345678900").IsNumeric()); + always_check(!LITERAL_VIEW(T, "\u4E38\u8FA3").IsNumeric()); + always_check(!LITERAL_VIEW(T, "0123456789AB").IsNumeric()); + always_check( LITERAL_VIEW(T, "0123456789AB").IsNumeric(16)); + } }; Test(InPlaceType); diff --git a/Redcraft.Utility/Source/Public/String/Conversion.h.inl b/Redcraft.Utility/Source/Public/String/Conversion.h.inl index 935d83b..22907ff 100644 --- a/Redcraft.Utility/Source/Public/String/Conversion.h.inl +++ b/Redcraft.Utility/Source/Public/String/Conversion.h.inl @@ -49,7 +49,7 @@ struct TStringHelper if constexpr (CArithmetic) { constexpr const T* DigitToChar = LITERAL(T, "9876543210123456789"); - constexpr size_t ZeroIndex = 9; + constexpr size_t ZeroIndex = 9; if constexpr (CSameAs) { @@ -452,7 +452,7 @@ struct TStringHelper if (IndexLength != 0) { - if (PlaceholderIndex.FindFirstNotOf(LITERAL(T, "0123456789")) != INDEX_NONE) + if (!PlaceholderIndex.IsNumeric()) { checkf(false, TEXT("Invalid placeholder index.")); diff --git a/Redcraft.Utility/Source/Public/String/String.h b/Redcraft.Utility/Source/Public/String/String.h index 0a1f297..b6bf97b 100644 --- a/Redcraft.Utility/Source/Public/String/String.h +++ b/Redcraft.Utility/Source/Public/String/String.h @@ -1019,6 +1019,32 @@ public: return AsConst(*this).GetData(); } +public: + + /** @return true if the string only contains valid characters, false otherwise. */ + NODISCARD FORCEINLINE constexpr bool IsValid() const + { + return TStringView(*this).IsValid(); + } + + /** @return true if the string only contains ASCII characters, false otherwise. */ + NODISCARD FORCEINLINE constexpr bool IsASCII() const + { + return TStringView(*this).IsASCII(); + } + + /** @return true if the string only contains numeric characters, false otherwise. */ + NODISCARD FORCEINLINE constexpr bool IsNumeric() const + { + return TStringView(*this).IsNumeric(); + } + + /** @return true if the string only contains numeric characters, false otherwise. */ + NODISCARD FORCEINLINE constexpr bool IsNumeric(unsigned Base) const + { + return TStringView(*this).IsNumeric(Base); + } + public: /** diff --git a/Redcraft.Utility/Source/Public/String/StringView.h b/Redcraft.Utility/Source/Public/String/StringView.h index 820269f..132f76f 100644 --- a/Redcraft.Utility/Source/Public/String/StringView.h +++ b/Redcraft.Utility/Source/Public/String/StringView.h @@ -377,6 +377,52 @@ public: return RFind([Char](ElementType C) { return C != Char; }, Index); } +public: + + /** @return true if the string only contains valid characters, false otherwise. */ + NODISCARD constexpr bool IsValid() const + { + for (ElementType Char : *this) + { + if (!TChar::IsValid(Char)) return false; + } + + return true; + } + + /** @return true if the string only contains ASCII characters, false otherwise. */ + NODISCARD constexpr bool IsASCII() const + { + for (ElementType Char : *this) + { + if (!TChar::IsASCII(Char)) return false; + } + + return true; + } + + /** @return true if the string only contains numeric characters, false otherwise. */ + NODISCARD constexpr bool IsNumeric() const + { + for (ElementType Char : *this) + { + if (!TChar::IsDigit(Char)) return false; + } + + return true; + } + + /** @return true if the string only contains numeric characters, false otherwise. */ + NODISCARD constexpr bool IsNumeric(unsigned Base) const + { + for (ElementType Char : *this) + { + if (!TChar::IsDigit(Char, Base)) return false; + } + + return true; + } + public: /**