feat(string): add functions to categorize strings

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

View File

@ -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<char>);

View File

@ -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:
/**