feat(string): add functions to categorize strings

This commit is contained in:
2024-11-10 19:24:40 +08:00
parent 7a80a80a12
commit 09bbcecc28
4 changed files with 83 additions and 2 deletions

View File

@ -49,7 +49,7 @@ struct TStringHelper
if constexpr (CArithmetic<U>)
{
constexpr const T* DigitToChar = LITERAL(T, "9876543210123456789");
constexpr size_t ZeroIndex = 9;
constexpr size_t ZeroIndex = 9;
if constexpr (CSameAs<U, bool>)
{
@ -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."));

View File

@ -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<ElementType>(*this).IsValid();
}
/** @return true if the string only contains ASCII characters, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsASCII() const
{
return TStringView<ElementType>(*this).IsASCII();
}
/** @return true if the string only contains numeric characters, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsNumeric() const
{
return TStringView<ElementType>(*this).IsNumeric();
}
/** @return true if the string only contains numeric characters, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsNumeric(unsigned Base) const
{
return TStringView<ElementType>(*this).IsNumeric(Base);
}
public:
/**

View File

@ -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<ElementType>::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<ElementType>::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<ElementType>::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<ElementType>::IsDigit(Char, Base)) return false;
}
return true;
}
public:
/**