From 76e96b06c797dafa48847949ff9edc6650c8f030 Mon Sep 17 00:00:00 2001 From: Redstone1024 <2824517378@qq.com> Date: Fri, 8 Nov 2024 17:55:35 +0800 Subject: [PATCH] refactor(string): refactor string find family to return INDEX_NONE without check(false) when index is invalid --- .../Source/Public/String/StringView.h | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/Redcraft.Utility/Source/Public/String/StringView.h b/Redcraft.Utility/Source/Public/String/StringView.h index f98a813..5be2ebb 100644 --- a/Redcraft.Utility/Source/Public/String/StringView.h +++ b/Redcraft.Utility/Source/Public/String/StringView.h @@ -220,7 +220,7 @@ public: /** @return The index of the first occurrence of the given substring, or INDEX_NONE if not found. */ NODISCARD constexpr size_t Find(TStringView View, size_t Index = 0) const { - checkf(Index < this->Num(), TEXT("Illegal index. Please check Index.")); + if (Index >= this->Num()) return INDEX_NONE; if (View.Num() > this->Num()) return INDEX_NONE; @@ -240,7 +240,7 @@ public: /** @return The index of the first occurrence of the given character, or INDEX_NONE if not found. */ NODISCARD constexpr size_t Find(ElementType Char, size_t Index = 0) const { - checkf(Index < this->Num(), TEXT("Illegal index. Please check Index.")); + if (Index >= this->Num()) return INDEX_NONE; for (; Index != this->Num(); ++Index) { @@ -257,7 +257,7 @@ public: template F> NODISCARD constexpr size_t Find(F&& InPredicate, size_t Index = 0) const { - checkf(Index < this->Num(), TEXT("Illegal index. Please check Index.")); + if (Index >= this->Num()) return INDEX_NONE; for (; Index != this->Num(); ++Index) { @@ -273,7 +273,7 @@ public: /** @return The index of the last occurrence of the given substring, or INDEX_NONE if not found. */ NODISCARD constexpr size_t RFind(TStringView View, size_t Index = INDEX_NONE) const { - checkf(Index == INDEX_NONE || Index < this->Num(), TEXT("Illegal index. Please check Index.")); + if (Index != INDEX_NONE && Index >= this->Num()) return INDEX_NONE; if (View.Num() > this->Num()) return INDEX_NONE; @@ -295,7 +295,7 @@ public: /** @return The index of the last occurrence of the given character, or INDEX_NONE if not found. */ NODISCARD constexpr size_t RFind(ElementType Char, size_t Index = INDEX_NONE) const { - checkf(Index == INDEX_NONE || Index < this->Num(), TEXT("Illegal index. Please check Index.")); + if (Index != INDEX_NONE && Index >= this->Num()) return INDEX_NONE; if (Index == INDEX_NONE) Index = this->Num(); @@ -314,7 +314,7 @@ public: template F> NODISCARD constexpr size_t RFind(F&& InPredicate, size_t Index = INDEX_NONE) const { - checkf(Index == INDEX_NONE || Index < this->Num(), TEXT("Illegal index. Please check Index.")); + if (Index != INDEX_NONE && Index >= this->Num()) return INDEX_NONE; if (Index == INDEX_NONE) Index = this->Num(); @@ -332,64 +332,48 @@ public: /** @return The index of the first occurrence of the character contained in the given view, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindFirstOf(TStringView View, size_t Index = 0) const { - checkf(Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return Find([View](ElementType Char) { return View.Contains(Char); }, Index); } /** @return The index of the first occurrence of the given character, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindFirstOf(ElementType Char, size_t Index = 0) const { - checkf(Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return Find(Char, Index); } /** @return The index of the last occurrence of the character contained in the given view, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindLastOf(TStringView View, size_t Index = INDEX_NONE) const { - checkf(Index == INDEX_NONE || Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return RFind([View](ElementType Char) { return View.Contains(Char); }, Index); } /** @return The index of the last occurrence of the given character, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindLastOf(ElementType Char, size_t Index = INDEX_NONE) const { - checkf(Index == INDEX_NONE || Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return RFind(Char, Index); } /** @return The index of the first absence of the character contained in the given view, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindFirstNotOf(TStringView View, size_t Index = 0) const { - checkf(Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return Find([View](ElementType Char) { return !View.Contains(Char); }, Index); } /** @return The index of the first absence of the given character, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindFirstNotOf(ElementType Char, size_t Index = 0) const { - checkf(Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return Find([Char](ElementType C) { return C != Char; }, Index); } /** @return The index of the last absence of the character contained in the given view, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindLastNotOf(TStringView View, size_t Index = INDEX_NONE) const { - checkf(Index == INDEX_NONE || Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return RFind([View](ElementType Char) { return !View.Contains(Char); }, Index); } /** @return The index of the last absence of the given character, or INDEX_NONE if not found. */ NODISCARD FORCEINLINE constexpr size_t FindLastNotOf(ElementType Char, size_t Index = INDEX_NONE) const { - checkf(Index == INDEX_NONE || Index < this->Num(), TEXT("Illegal index. Please check Index.")); - return RFind([Char](ElementType C) { return C != Char; }, Index); }