Plasma Engine  2.0
Loading...
Searching...
No Matches
StringUtils_inl.h
1#pragma once
2
3PL_ALWAYS_INLINE plInt32 plStringUtils::CompareChars(plUInt32 uiCharacter1, plUInt32 uiCharacter2)
4{
5 return (plInt32)uiCharacter1 - (plInt32)uiCharacter2;
6}
7
8inline plInt32 plStringUtils::CompareChars_NoCase(plUInt32 uiCharacter1, plUInt32 uiCharacter2)
9{
10 return (plInt32)ToUpperChar(uiCharacter1) - (plInt32)ToUpperChar(uiCharacter2);
11}
12
13inline plInt32 plStringUtils::CompareChars(const char* szUtf8Char1, const char* szUtf8Char2)
14{
16}
17
18inline plInt32 plStringUtils::CompareChars_NoCase(const char* szUtf8Char1, const char* szUtf8Char2)
19{
21}
22
23template <typename T>
24PL_ALWAYS_INLINE constexpr bool plStringUtils::IsNullOrEmpty(const T* pString)
25{
26 return (pString == nullptr) || (pString[0] == '\0');
27}
28
29template <typename T>
30PL_ALWAYS_INLINE bool plStringUtils::IsNullOrEmpty(const T* pString, const T* pStringEnd)
31{
32 return (pString == nullptr) || (pString[0] == '\0') || pString == pStringEnd;
33}
34
35template <typename T>
36PL_ALWAYS_INLINE void plStringUtils::UpdateStringEnd(const T* pStringStart, const T*& ref_pStringEnd)
37{
38 if (ref_pStringEnd != plUnicodeUtils::GetMaxStringEnd<T>())
39 return;
40
41 ref_pStringEnd = pStringStart + GetStringElementCount(pStringStart, plUnicodeUtils::GetMaxStringEnd<T>());
42}
43
44template <typename T>
45constexpr plUInt32 plStringUtils::GetStringElementCount(const T* pString)
46{
47 // can't use strlen here as long as it's not constexpr (C++ 23)
48
49 if (pString == nullptr)
50 return 0;
51
52 plUInt32 uiCount = 0;
53 while (*pString != '\0')
54 {
55 ++pString;
56 ++uiCount;
57 }
58
59 return uiCount;
60}
61
62template <typename T>
63plUInt32 plStringUtils::GetStringElementCount(const T* pString, const T* pStringEnd)
64{
65 if (IsNullOrEmpty(pString))
66 return 0;
67
68 if (pStringEnd != plUnicodeUtils::GetMaxStringEnd<T>())
69 return (plUInt32)(pStringEnd - pString);
70
71 plUInt32 uiCount = 0;
72 while ((*pString != '\0') && (pString < pStringEnd))
73 {
74 ++pString;
75 ++uiCount;
76 }
77
78 return uiCount;
79}
80
81inline plUInt32 plStringUtils::GetCharacterCount(const char* szUtf8, const char* pStringEnd)
82{
83 if (IsNullOrEmpty(szUtf8))
84 return 0;
85
86 plUInt32 uiCharacters = 0;
87
88 while ((*szUtf8 != '\0') && (szUtf8 < pStringEnd))
89 {
90 // skip all the Utf8 continuation bytes
92 ++uiCharacters;
93
94 ++szUtf8;
95 }
96
97 return uiCharacters;
98}
99
100inline void plStringUtils::GetCharacterAndElementCount(const char* szUtf8, plUInt32& ref_uiCharacterCount, plUInt32& ref_uiElementCount, const char* pStringEnd)
101{
102 ref_uiCharacterCount = 0;
103 ref_uiElementCount = 0;
104
105 if (IsNullOrEmpty(szUtf8))
106 return;
107
108 while (szUtf8 < pStringEnd)
109 {
110 char uiByte = *szUtf8;
111 if (uiByte == '\0')
112 {
113 break;
114 }
115
116 // skip all the Utf8 continuation bytes
118 ++ref_uiCharacterCount;
119
120 ++szUtf8;
121 ++ref_uiElementCount;
122 }
123}
124
125PL_ALWAYS_INLINE bool plStringUtils::IsEqual(const char* pString1, const char* pString2, const char* pString1End, const char* pString2End)
126{
127 return plStringUtils::Compare(pString1, pString2, pString1End, pString2End) == 0;
128}
129
130PL_ALWAYS_INLINE bool plStringUtils::IsEqualN(
131 const char* pString1, const char* pString2, plUInt32 uiCharsToCompare, const char* pString1End, const char* pString2End)
132{
133 return plStringUtils::CompareN(pString1, pString2, uiCharsToCompare, pString1End, pString2End) == 0;
134}
135
136PL_ALWAYS_INLINE bool plStringUtils::IsEqual_NoCase(const char* pString1, const char* pString2, const char* pString1End, const char* pString2End)
137{
138 return plStringUtils::Compare_NoCase(pString1, pString2, pString1End, pString2End) == 0;
139}
140
141PL_ALWAYS_INLINE bool plStringUtils::IsEqualN_NoCase(
142 const char* pString1, const char* pString2, plUInt32 uiCharsToCompare, const char* pString1End, const char* pString2End)
143{
144 return plStringUtils::CompareN_NoCase(pString1, pString2, uiCharsToCompare, pString1End, pString2End) == 0;
145}
146
147PL_ALWAYS_INLINE bool plStringUtils::IsDecimalDigit(plUInt32 uiChar)
148{
149 return (uiChar >= '0' && uiChar <= '9');
150}
151
152PL_ALWAYS_INLINE bool plStringUtils::IsHexDigit(plUInt32 uiChar)
153{
154 return IsDecimalDigit(uiChar) || (uiChar >= 'A' && uiChar <= 'F') || (uiChar >= 'a' && uiChar <= 'f');
155}
static bool IsEqual(const char *pString1, const char *pString2, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true, if the two given strings are identical (bitwise).
Definition StringUtils_inl.h:125
static plInt32 Compare_NoCase(const char *pString1, const char *pString2, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Compares two strings for equality, case-insensitive.
Definition StringUtils.cpp:286
static plInt32 CompareChars_NoCase(plUInt32 uiCharacter1, plUInt32 uiCharacter2)
Compares the two code points for equality, case-insensitive.
Definition StringUtils_inl.h:8
static plInt32 CompareN(const char *pString1, const char *pString2, plUInt32 uiCharsToCompare, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Compares the first uiCharsToCompare characters of the two strings for equality.
Definition StringUtils.cpp:247
static plInt32 CompareN_NoCase(const char *pString1, const char *pString2, plUInt32 uiCharsToCompare, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Compares the first uiCharsToCompare characters of the two strings for equality, case-insensitive.
Definition StringUtils.cpp:320
static bool IsEqual_NoCase(const char *pString1, const char *pString2, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true, if the two given strings are identical (case-insensitive).
Definition StringUtils_inl.h:136
static void UpdateStringEnd(const T *pStringStart, const T *&ref_pStringEnd)
Recomputes the end pointer of a string (szStringEnd), if that is currently set to plMaxStringEnd....
static plInt32 CompareChars(plUInt32 uiCharacter1, plUInt32 uiCharacter2)
Compares the two code points for equality.
Definition StringUtils_inl.h:3
static bool IsDecimalDigit(plUInt32 uiChar)
A decimal digit from 0..9.
Definition StringUtils_inl.h:147
static plInt32 Compare(const char *pString1, const char *pString2, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Compares two strings for equality.
Definition StringUtils.cpp:218
static bool IsHexDigit(plUInt32 uiChar)
A hexadecimal digit from 0..F.
Definition StringUtils_inl.h:152
static bool IsEqualN_NoCase(const char *pString1, const char *pString2, plUInt32 uiCharsToCompare, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true, if the two given strings are identical (case-insensitive) up to the n-th character.
Definition StringUtils_inl.h:141
static constexpr plUInt32 GetStringElementCount(const T *pString)
Returns the number of elements of type T that the string contains, until it hits an element that is z...
Definition StringUtils_inl.h:45
static bool IsEqualN(const char *pString1, const char *pString2, plUInt32 uiCharsToCompare, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true, if the two given strings are identical (bitwise) up to the n-th character.
Definition StringUtils_inl.h:130
static plUInt32 GetCharacterCount(const char *szUtf8, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns the number of characters (not Bytes!) in a Utf8 string (excluding the zero terminator),...
Definition StringUtils_inl.h:81
static plUInt32 ToUpperChar(plUInt32 uiChar)
Returns the upper case code point for uiChar.
Definition StringUtils.cpp:10
static void GetCharacterAndElementCount(const char *szUtf8, plUInt32 &ref_uiCharacterCount, plUInt32 &ref_uiElementCount, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns both the number of characters and the number of bytes in a Utf8 string, until it hits zero or...
Definition StringUtils_inl.h:100
static constexpr bool IsNullOrEmpty(const T *pString)
Returns true, if the given string is a nullptr pointer or a string that immediately terminates with a...
static constexpr T * GetMaxStringEnd()
[internal] Returns the max string end pointer for the given type
Definition UnicodeUtils_inl.h:261
static plUInt32 ConvertUtf8ToUtf32(const char *pFirstChar)
Converts the UTF-8 character that starts at pFirstChar into a UTF-32 character.
Definition UnicodeUtils_inl.h:124
static bool IsUtf8ContinuationByte(char iByte)
Checks whether the given byte is a byte in a UTF-8 multi-byte sequence.
Definition UnicodeUtils_inl.h:17