Compare commits

...

3 Commits

5 changed files with 207 additions and 200 deletions

View File

@ -33,208 +33,65 @@ void TestChar()
always_check(CCharType<u32char>);
always_check(CCharType<unicodechar>);
}
auto Test = []<typename T>(TInPlaceType<T>)
{
always_check(FChar::IsAlnum(TEXT('0')));
always_check(FChar::IsAlpha(TEXT('A')));
always_check(FChar::IsLower(TEXT('a')));
always_check(FChar::IsUpper(TEXT('A')));
always_check(FChar::IsDigit(TEXT('0')));
always_check(FChar::IsCntrl(TEXT('\n')));
always_check(FChar::IsGraph(TEXT('!')));
always_check(FChar::IsSpace(TEXT('\t')));
always_check(FChar::IsBlank(TEXT(' ')));
always_check(FChar::IsPrint(TEXT('#')));
always_check(FChar::IsPunct(TEXT('[')));
}
always_check(TChar<T>::IsASCII(LITERAL(T, '0')));
always_check(TChar<T>::IsASCII(LITERAL(T, 'A')));
always_check(TChar<T>::IsASCII(LITERAL(T, 'a')));
always_check(TChar<T>::IsASCII(LITERAL(T, 'A')));
always_check(TChar<T>::IsASCII(LITERAL(T, '0')));
always_check(TChar<T>::IsASCII(LITERAL(T, '\n')));
always_check(TChar<T>::IsASCII(LITERAL(T, '!')));
always_check(TChar<T>::IsASCII(LITERAL(T, '\t')));
always_check(TChar<T>::IsASCII(LITERAL(T, ' ')));
always_check(TChar<T>::IsASCII(LITERAL(T, '#')));
always_check(TChar<T>::IsASCII(LITERAL(T, '[')));
{
always_check(FWChar::IsAlnum(WTEXT('0')));
always_check(FWChar::IsAlpha(WTEXT('A')));
always_check(FWChar::IsLower(WTEXT('a')));
always_check(FWChar::IsUpper(WTEXT('A')));
always_check(FWChar::IsDigit(WTEXT('0')));
always_check(FWChar::IsCntrl(WTEXT('\n')));
always_check(FWChar::IsGraph(WTEXT('!')));
always_check(FWChar::IsSpace(WTEXT('\t')));
always_check(FWChar::IsBlank(WTEXT(' ')));
always_check(FWChar::IsPrint(WTEXT('#')));
always_check(FWChar::IsPunct(WTEXT('[')));
}
always_check(TChar<T>::IsAlnum(LITERAL(T, '0')));
always_check(TChar<T>::IsAlpha(LITERAL(T, 'A')));
always_check(TChar<T>::IsLower(LITERAL(T, 'a')));
always_check(TChar<T>::IsUpper(LITERAL(T, 'A')));
always_check(TChar<T>::IsDigit(LITERAL(T, '0')));
always_check(TChar<T>::IsCntrl(LITERAL(T, '\n')));
always_check(TChar<T>::IsGraph(LITERAL(T, '!')));
always_check(TChar<T>::IsSpace(LITERAL(T, '\t')));
always_check(TChar<T>::IsBlank(LITERAL(T, ' ')));
always_check(TChar<T>::IsPrint(LITERAL(T, '#')));
always_check(TChar<T>::IsPunct(LITERAL(T, '[')));
{
always_check(FU8Char::IsAlnum(U8TEXT('0')));
always_check(FU8Char::IsAlpha(U8TEXT('A')));
always_check(FU8Char::IsLower(U8TEXT('a')));
always_check(FU8Char::IsUpper(U8TEXT('A')));
always_check(FU8Char::IsDigit(U8TEXT('0')));
always_check(FU8Char::IsCntrl(U8TEXT('\n')));
always_check(FU8Char::IsGraph(U8TEXT('!')));
always_check(FU8Char::IsSpace(U8TEXT('\t')));
always_check(FU8Char::IsBlank(U8TEXT(' ')));
always_check(FU8Char::IsPrint(U8TEXT('#')));
always_check(FU8Char::IsPunct(U8TEXT('[')));
}
always_check(!TChar<T>::IsAlnum(LITERAL(T, '$')));
always_check(!TChar<T>::IsAlpha(LITERAL(T, '0')));
always_check(!TChar<T>::IsLower(LITERAL(T, 'A')));
always_check(!TChar<T>::IsUpper(LITERAL(T, 'a')));
always_check(!TChar<T>::IsDigit(LITERAL(T, 'I')));
always_check(!TChar<T>::IsCntrl(LITERAL(T, '_')));
always_check(!TChar<T>::IsGraph(LITERAL(T, ' ')));
always_check(!TChar<T>::IsSpace(LITERAL(T, '=')));
always_check(!TChar<T>::IsBlank(LITERAL(T, '+')));
always_check(!TChar<T>::IsPrint(LITERAL(T, '\n')));
always_check(!TChar<T>::IsPunct(LITERAL(T, 'H')));
{
always_check(FU16Char::IsAlnum(U16TEXT('0')));
always_check(FU16Char::IsAlpha(U16TEXT('A')));
always_check(FU16Char::IsLower(U16TEXT('a')));
always_check(FU16Char::IsUpper(U16TEXT('A')));
always_check(FU16Char::IsDigit(U16TEXT('0')));
always_check(FU16Char::IsCntrl(U16TEXT('\n')));
always_check(FU16Char::IsGraph(U16TEXT('!')));
always_check(FU16Char::IsSpace(U16TEXT('\t')));
always_check(FU16Char::IsBlank(U16TEXT(' ')));
always_check(FU16Char::IsPrint(U16TEXT('#')));
always_check(FU16Char::IsPunct(U16TEXT('[')));
}
always_check( TChar<T>::IsDigit(LITERAL(T, 'F'), 16));
always_check(!TChar<T>::IsDigit(LITERAL(T, 'G'), 16));
{
always_check(FU32Char::IsAlnum(U32TEXT('0')));
always_check(FU32Char::IsAlpha(U32TEXT('A')));
always_check(FU32Char::IsLower(U32TEXT('a')));
always_check(FU32Char::IsUpper(U32TEXT('A')));
always_check(FU32Char::IsDigit(U32TEXT('0')));
always_check(FU32Char::IsCntrl(U32TEXT('\n')));
always_check(FU32Char::IsGraph(U32TEXT('!')));
always_check(FU32Char::IsSpace(U32TEXT('\t')));
always_check(FU32Char::IsBlank(U32TEXT(' ')));
always_check(FU32Char::IsPrint(U32TEXT('#')));
always_check(FU32Char::IsPunct(U32TEXT('[')));
}
always_check(TChar<T>::ToLower(LITERAL(T, 'i')) == LITERAL(T, 'i'));
always_check(TChar<T>::ToUpper(LITERAL(T, 'l')) == LITERAL(T, 'L'));
{
always_check(!FChar::IsAlnum(TEXT('$')));
always_check(!FChar::IsAlpha(TEXT('0')));
always_check(!FChar::IsLower(TEXT('A')));
always_check(!FChar::IsUpper(TEXT('a')));
always_check(!FChar::IsDigit(TEXT('I')));
always_check(!FChar::IsCntrl(TEXT('_')));
always_check(!FChar::IsGraph(TEXT(' ')));
always_check(!FChar::IsSpace(TEXT('=')));
always_check(!FChar::IsBlank(TEXT('+')));
always_check(!FChar::IsPrint(TEXT('\n')));
always_check(!FChar::IsPunct(TEXT('H')));
}
always_check(0x0 == TChar<T>::ToDigit(LITERAL(T, '0')));
always_check(0xF == TChar<T>::ToDigit(LITERAL(T, 'f')));
always_check(0xF == TChar<T>::ToDigit(LITERAL(T, 'F')));
{
always_check(!FWChar::IsAlnum(WTEXT('$')));
always_check(!FWChar::IsAlpha(WTEXT('0')));
always_check(!FWChar::IsLower(WTEXT('A')));
always_check(!FWChar::IsUpper(WTEXT('a')));
always_check(!FWChar::IsDigit(WTEXT('I')));
always_check(!FWChar::IsCntrl(WTEXT('_')));
always_check(!FWChar::IsGraph(WTEXT(' ')));
always_check(!FWChar::IsSpace(WTEXT('=')));
always_check(!FWChar::IsBlank(WTEXT('+')));
always_check(!FWChar::IsPrint(WTEXT('\n')));
always_check(!FWChar::IsPunct(WTEXT('H')));
}
always_check(LITERAL(T, '0') == TChar<T>::FromDigit(0x0));
always_check(LITERAL(T, 'f') != TChar<T>::FromDigit(0xF));
always_check(LITERAL(T, 'F') == TChar<T>::FromDigit(0xF));
};
{
always_check(!FU8Char::IsAlnum(U8TEXT('$')));
always_check(!FU8Char::IsAlpha(U8TEXT('0')));
always_check(!FU8Char::IsLower(U8TEXT('A')));
always_check(!FU8Char::IsUpper(U8TEXT('a')));
always_check(!FU8Char::IsDigit(U8TEXT('I')));
always_check(!FU8Char::IsCntrl(U8TEXT('_')));
always_check(!FU8Char::IsGraph(U8TEXT(' ')));
always_check(!FU8Char::IsSpace(U8TEXT('=')));
always_check(!FU8Char::IsBlank(U8TEXT('+')));
always_check(!FU8Char::IsPrint(U8TEXT('\n')));
always_check(!FU8Char::IsPunct(U8TEXT('H')));
}
{
always_check(!FU16Char::IsAlnum(U16TEXT('$')));
always_check(!FU16Char::IsAlpha(U16TEXT('0')));
always_check(!FU16Char::IsLower(U16TEXT('A')));
always_check(!FU16Char::IsUpper(U16TEXT('a')));
always_check(!FU16Char::IsDigit(U16TEXT('I')));
always_check(!FU16Char::IsCntrl(U16TEXT('_')));
always_check(!FU16Char::IsGraph(U16TEXT(' ')));
always_check(!FU16Char::IsSpace(U16TEXT('=')));
always_check(!FU16Char::IsBlank(U16TEXT('+')));
always_check(!FU16Char::IsPrint(U16TEXT('\n')));
always_check(!FU16Char::IsPunct(U16TEXT('H')));
}
{
always_check(!FU32Char::IsAlnum(U32TEXT('$')));
always_check(!FU32Char::IsAlpha(U32TEXT('0')));
always_check(!FU32Char::IsLower(U32TEXT('A')));
always_check(!FU32Char::IsUpper(U32TEXT('a')));
always_check(!FU32Char::IsDigit(U32TEXT('I')));
always_check(!FU32Char::IsCntrl(U32TEXT('_')));
always_check(!FU32Char::IsGraph(U32TEXT(' ')));
always_check(!FU32Char::IsSpace(U32TEXT('=')));
always_check(!FU32Char::IsBlank(U32TEXT('+')));
always_check(!FU32Char::IsPrint(U32TEXT('\n')));
always_check(!FU32Char::IsPunct(U32TEXT('H')));
}
{
always_check( FChar::IsDigit(TEXT('F'), 16));
always_check(!FChar::IsDigit(TEXT('G'), 16));
always_check( FWChar::IsDigit(WTEXT('F'), 16));
always_check(!FWChar::IsDigit(WTEXT('G'), 16));
always_check( FU8Char::IsDigit(U8TEXT('F'), 16));
always_check(!FU8Char::IsDigit(U8TEXT('G'), 16));
always_check( FU16Char::IsDigit(U16TEXT('F'), 16));
always_check(!FU16Char::IsDigit(U16TEXT('G'), 16));
always_check( FU32Char::IsDigit(U32TEXT('F'), 16));
always_check(!FU32Char::IsDigit(U32TEXT('G'), 16));
}
{
always_check(FChar::ToLower(TEXT('i')) == TEXT('i'));
always_check(FChar::ToUpper(TEXT('l')) == TEXT('L'));
always_check(FWChar::ToUpper(WTEXT('l')) == WTEXT('L'));
always_check(FWChar::ToLower(WTEXT('i')) == WTEXT('i'));
always_check(FU8Char::ToLower(U8TEXT('i')) == U8TEXT('i'));
always_check(FU8Char::ToUpper(U8TEXT('l')) == U8TEXT('L'));
always_check(FU16Char::ToLower(U16TEXT('i')) == U16TEXT('i'));
always_check(FU16Char::ToUpper(U16TEXT('l')) == U16TEXT('L'));
always_check(FU32Char::ToLower(U32TEXT('i')) == U32TEXT('i'));
always_check(FU32Char::ToUpper(U32TEXT('l')) == U32TEXT('L'));
}
{
always_check(0x0 == FChar::ToDigit(TEXT('0')));
always_check(0xF == FChar::ToDigit(TEXT('f')));
always_check(0xF == FChar::ToDigit(TEXT('F')));
always_check(0x0 == FWChar::ToDigit(WTEXT('0')));
always_check(0xF == FWChar::ToDigit(WTEXT('f')));
always_check(0xF == FWChar::ToDigit(WTEXT('F')));
always_check(0x0 == FU8Char::ToDigit(U8TEXT('0')));
always_check(0xF == FU8Char::ToDigit(U8TEXT('f')));
always_check(0xF == FU8Char::ToDigit(U8TEXT('F')));
always_check(0x0 == FU16Char::ToDigit(U16TEXT('0')));
always_check(0xF == FU16Char::ToDigit(U16TEXT('f')));
always_check(0xF == FU16Char::ToDigit(U16TEXT('F')));
always_check(0x0 == FU16Char::ToDigit(U16TEXT('0')));
always_check(0xF == FU16Char::ToDigit(U16TEXT('f')));
always_check(0xF == FU16Char::ToDigit(U16TEXT('F')));
}
{
always_check(TEXT('0') == FChar::FromDigit(0x0));
always_check(TEXT('f') != FChar::FromDigit(0xF));
always_check(TEXT('F') == FChar::FromDigit(0xF));
always_check(WTEXT('0') == FWChar::FromDigit(0x0));
always_check(WTEXT('f') != FWChar::FromDigit(0xF));
always_check(WTEXT('F') == FWChar::FromDigit(0xF));
always_check(U8TEXT('0') == FU8Char::FromDigit(0x0));
always_check(U8TEXT('f') != FU8Char::FromDigit(0xF));
always_check(U8TEXT('F') == FU8Char::FromDigit(0xF));
always_check(U16TEXT('0') == FU16Char::FromDigit(0x0));
always_check(U16TEXT('f') != FU16Char::FromDigit(0xF));
always_check(U16TEXT('F') == FU16Char::FromDigit(0xF));
always_check(U16TEXT('0') == FU16Char::FromDigit(0x0));
always_check(U16TEXT('f') != FU16Char::FromDigit(0xF));
always_check(U16TEXT('F') == FU16Char::FromDigit(0xF));
}
Test(InPlaceType<char>);
Test(InPlaceType<wchar>);
Test(InPlaceType<u8char>);
Test(InPlaceType<u16char>);
Test(InPlaceType<u32char>);
Test(InPlaceType<unicodechar>);
}
void TestStringView()
@ -317,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

@ -60,6 +60,11 @@ NAMESPACE_PRIVATE_END
/** Templated literal struct to allow selection of string literals based on the character type provided, and not on compiler switches. */
#define LITERAL(CharType, StringLiteral) NAMESPACE_PRIVATE::TLiteral<CharType>::Select(TEXT(StringLiteral), WTEXT(StringLiteral), U8TEXT(StringLiteral), U16TEXT(StringLiteral), U32TEXT(StringLiteral))
static_assert(CUnsigned<u8char>, "TChar assumes u8char is an unsigned integer");
static_assert(CUnsigned<u16char>, "TChar assumes u16char is an unsigned integer");
static_assert(CUnsigned<u32char>, "TChar assumes u32char is an unsigned integer");
static_assert(CUnsigned<unicodechar>, "TChar assumes unicodechar is an unsigned integer");
/** Set of utility functions operating on a single character. Implemented based on user-preferred locale and ISO 30112 "i18n". */
template <CCharType T>
struct TChar
@ -156,6 +161,70 @@ struct TChar
return false;
}
NODISCARD FORCEINLINE static constexpr bool IsASCII(CharType InChar)
{
if constexpr (CSameAs<CharType, char>)
{
constexpr bool ASCIICompatible = []() -> bool
{
constexpr char ASCIITable[] =
TEXT("\u0000\u0001\u0002\u0003\u0004\u0005\u0006")
TEXT("\a\b\t\n\v\f\r")
TEXT("\u000E\u000F")
TEXT("\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017")
TEXT("\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F")
TEXT(" !\"#$%&'()*+,-./0123456789:;<=>?")
TEXT("@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_")
TEXT("`abcdefghijklmnopqrstuvwxyz{|}~\u007F");
for (size_t Index = 0; Index <= 0x7F; ++Index)
{
if (ASCIITable[Index] != static_cast<char>(Index)) return false;
}
return true;
}
();
return ASCIICompatible && 0x00 <= InChar && InChar <= 0x7F;
}
else if constexpr (CSameAs<CharType, wchar>)
{
constexpr bool ASCIICompatible = []() -> bool
{
constexpr wchar ASCIITable[] =
WTEXT("\u0000\u0001\u0002\u0003\u0004\u0005\u0006")
WTEXT("\a\b\t\n\v\f\r")
WTEXT("\u000E\u000F")
WTEXT("\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017")
WTEXT("\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F")
WTEXT(" !\"#$%&'()*+,-./0123456789:;<=>?")
WTEXT("@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_")
WTEXT("`abcdefghijklmnopqrstuvwxyz{|}~\u007F");
for (size_t Index = 0; Index <= 0x7F; ++Index)
{
if (ASCIITable[Index] != static_cast<wchar>(Index)) return false;
}
return true;
}
();
return ASCIICompatible && 0x00 <= InChar && InChar <= 0x7F;
}
else if constexpr (CSameAs<CharType, u8char> || CSameAs<CharType, u16char> || CSameAs<CharType, u32char> || CSameAs<CharType, unicodechar>)
{
return InChar <= 0x7F;
}
else static_assert(sizeof(CharType) == -1, "Unsupported character type");
return false;
}
NODISCARD FORCEINLINE static constexpr bool IsAlnum(CharType InChar)
{
if constexpr (CSameAs<CharType, char> || CSameAs<CharType, wchar>)
@ -294,15 +363,15 @@ struct TChar
return (InChar >= LITERAL(CharType, '0') && InChar <= LITERAL(CharType, '9'));
}
NODISCARD FORCEINLINE static constexpr bool IsDigit(CharType InChar, int Base)
NODISCARD FORCEINLINE static constexpr bool IsDigit(CharType InChar, unsigned Base)
{
checkf(Base >= 2 && Base <= 36, TEXT("Base must be in the range [2, 36]."));
/* <U0030>..<U0039>;<U0041>..<U0046>;<U0061>..<U0066>; */
return
(InChar >= LITERAL(CharType, '0') && InChar < LITERAL(CharType, '0') + Base ) ||
(InChar >= LITERAL(CharType, 'a') && InChar < LITERAL(CharType, 'a') + Base - 10) ||
(InChar >= LITERAL(CharType, 'A') && InChar < LITERAL(CharType, 'A') + Base - 10);
(InChar >= LITERAL(CharType, '0') && InChar < LITERAL(CharType, '0') + static_cast<signed>(Base) ) ||
(InChar >= LITERAL(CharType, 'a') && InChar < LITERAL(CharType, 'a') + static_cast<signed>(Base) - 10) ||
(InChar >= LITERAL(CharType, 'A') && InChar < LITERAL(CharType, 'A') + static_cast<signed>(Base) - 10);
}
NODISCARD FORCEINLINE static constexpr bool IsCntrl(CharType InChar)

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