20 #ifndef STATICSTRING_H 21 #define STATICSTRING_H 23 #include "pedigree/kernel/compiler.h" 24 #include "pedigree/kernel/processor/types.h" 25 #include "pedigree/kernel/utilities/assert.h" 26 #include "pedigree/kernel/utilities/utility.h" 32 #define STATICSTRING_ASSERTS 0 34 #if STATICSTRING_ASSERTS 35 #define STATICSTRING_ASSERT(...) assert(__VA_ARGS__) 37 #define STATICSTRING_ASSERT(...) 46 template <
unsigned int N>
65 explicit StaticString(
const char *pSrc) : m_Length(StringLength(pSrc)), m_Hash(0), m_AllowHashes(false)
75 StaticString(
const char *pSrc,
size_t len) : m_Length(0), m_Hash(0), m_AllowHashes(false)
84 template <
unsigned int N2>
87 operator const char *()
const 92 template <
unsigned int N2>
105 template <
typename T>
120 void assign(
const char *str,
size_t len=0)
124 len = min(N - 1, StringLength(str));
128 len = min(len, N - 1);
132 ForwardMemoryCopy(m_pData, str, len);
141 template <
unsigned int N2>
144 assign(other, other.length());
155 if (StringLength(pStr) != length())
160 return StringCompareN(m_pData, pStr, length()) == 0;
163 template <
unsigned int N2>
166 if (other.length() != length())
170 else if ((m_AllowHashes && other.m_AllowHashes) && (m_Hash != other.hash()))
175 return StringCompareN(m_pData, other.
m_pData, length()) == 0;
178 int last(
const char search)
const 180 for (
int i = length(); i >= 0; i--)
181 if (m_pData[i] == search)
186 int first(
const char search)
const 188 for (
size_t i = 0; i < length(); i++)
189 if (m_pData[i] == search)
198 m_pData[--m_Length] =
'\0';
203 bool contains(
const char *other)
const 205 return contains(m_pData, other, length(), StringLength(other));
208 template <
unsigned int N2>
211 return contains(m_pData, other.
m_pData, length(), other.length());
214 int intValue(
int nBase = 0)
const 217 int ret = StringToUnsignedLong(m_pData, &pEnd, nBase);
224 uintptr_t uintptrValue(
int nBase = 0)
const 227 uintptr_t ret = StringToUnsignedLong(m_pData, &pEnd, nBase);
234 void truncate(
size_t len)
265 for (i = n; m_pData[i] !=
'\0'; i++)
266 m_pData[i - n] = m_pData[i];
267 m_pData[i - n] =
'\0';
275 template <
typename T>
282 void append(
char Char,
size_t nLen = 0,
char c =
'0')
284 char Characters[] = {Char,
'\0'};
285 append(Characters, nLen, c);
288 void append(
short nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
295 append(static_cast<unsigned short>(nInt), nRadix, nLen, c);
298 void append(
int nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
305 append(static_cast<unsigned int>(nInt), nRadix, nLen, c);
308 void append(
long nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
315 append(static_cast<unsigned long>(nInt), nRadix, nLen, c);
319 append(
long long nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
326 append(static_cast<unsigned long long>(nInt), nRadix, nLen, c);
330 unsigned char nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
332 appendInteger<sizeof(char)>(nInt, nRadix, nLen, c);
336 unsigned short nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
338 appendInteger<sizeof(short)>(nInt, nRadix, nLen, c);
342 append(
unsigned int nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
344 appendInteger<sizeof(int)>(nInt, nRadix, nLen, c);
348 unsigned long nInt,
size_t nRadix = 10,
size_t nLen = 0,
char c =
'0')
350 appendInteger<sizeof(long)>(nInt, nRadix, nLen, c);
354 unsigned long long nInt,
size_t nRadix = 10,
size_t nLen = 0,
357 appendInteger<sizeof(long long)>(nInt, nRadix, nLen, c);
360 template <
unsigned int size,
typename T>
361 void appendInteger(T nInt,
size_t nRadix,
size_t nLen,
char c)
369 char pStr[size * 8 + 1];
373 size_t tmp = nInt % nRadix;
376 pStr[index++] =
'0' + tmp;
378 pStr[index++] =
'a' + (tmp - 10);
381 for (
size_t i = 0; i < (index / 2); i++)
384 pStr[i] = pStr[index - i - 1];
385 pStr[index - i - 1] = tmp;
390 append(pStr, nLen, c);
393 void append(
const char *str,
size_t nLen = 0,
char c =
' ')
401 if (nLen == 0 && length() == 0)
412 size_t length2 = min(nLen, StringLength(str));
418 for (i = 0; i < nLen - length2; i++)
420 m_pData[i + length()] = c;
422 m_pData[i + length()] =
'\0';
423 m_Length += nLen - length2;
435 while ((i <= N) && (appended++ < nLen) && *str)
437 m_pData[i++] = *str++;
445 size_t otherLen = StringLength(str);
446 size_t copyLen = min(N - length(), otherLen);
449 ForwardMemoryCopy(m_pData + length(), str, copyLen);
452 m_pData[m_Length] = 0;
460 void appendBytes(
const char *bytes,
size_t numBytes)
468 for (
size_t i = 0; i < numBytes; ++i)
471 if ((c < -1) || (c >= 0x20 && c != 0x7f))
480 append(static_cast<unsigned int>(c), 16, 2);
485 template <
unsigned int N2>
494 if (nLen == 0 && length() == 0)
501 if (nLen > str.length())
504 for (i = 0; i < nLen - str.length(); i++)
506 m_pData[i + length()] = c;
508 m_pData[i + length()] =
'\0';
509 m_Length += nLen - str.length();
514 ForwardMemoryCopy(m_pData + length(), str, N - length());
515 m_Length += str.length();
516 m_pData[m_Length] = 0;
523 void pad(
size_t nLen,
char c =
' ')
535 for (i = 0; i < nLen - length(); i++)
537 m_pData[i + length()] = c;
539 m_pData[i + length()] =
'\0';
540 m_Length += nLen - length();
546 size_t length()
const 551 uint64_t hash()
const 565 m_AllowHashes =
true;
575 m_AllowHashes =
false;
579 static bool contains(
const char *a,
const char *b,
size_t alen,
size_t blen)
581 return StringContainsN(a, alen, b, blen) == 1;
589 STATICSTRING_ASSERT(StringLength(m_pData) == m_Length);
591 m_Hash = spookyHash(m_pData, m_Length);
599 m_pData[N - 1] =
'\0';
604 bool canAppend()
const 606 return m_Length < (N - 1);
void assign(const char *str, size_t len=0)
StaticString right(int n) const
void allowHashing(bool computeNow=false)
StaticString(const char *pSrc, size_t len)
StaticString(const char *pSrc)
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
StaticString(const StaticString< N2 > &src)
void append(const char *str, size_t nLen=0, char c= ' ')