From d740efdf068b87e88c72839fb70e1fa32d4d90f9 Mon Sep 17 00:00:00 2001 From: Doyle T Date: Mon, 11 Jun 2018 18:03:18 +1000 Subject: [PATCH] Compact operator overloads for FixedString --- dqn.h | 59 +++++++++++++++-------------------------------------------- 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/dqn.h b/dqn.h index 064e280..470a7e1 100644 --- a/dqn.h +++ b/dqn.h @@ -2011,10 +2011,19 @@ struct DqnSmartString : public DqnString ~DqnSmartString() { this->Free(); } }; +DQN_FILE_SCOPE DqnString DqnString_(DqnMemAPI *const api = DQN_DEFAULT_HEAP_ALLOCATOR); +DQN_FILE_SCOPE DqnString DqnString_(DqnMemStack *const stack); + +// NOTE: First level of indirection needs to turn the combined dqnstring_(guid) into a name. Otherwise +// each use of literalVarName will increment __COUNTER__ +#define DQN_STRING_LITERAL_INTERNAL(srcVariable, literal, literalVarName) \ + {}; \ + char literalVarName[] = literal; \ + srcVariable.InitLiteralNoAlloc(literalVarName, DQN_CHAR_COUNT(literalVarName)) // #DqnFixedString Public API - Fixed sized strings at compile time // ================================================================================================= -int DqnFixedString__Append(char *dest, int destSize, char const *src, int len = -1); +FILE_SCOPE int DqnFixedString__Append(char *dest, int destSize, char const *src, int len = -1); template struct DqnFixedString @@ -2033,10 +2042,10 @@ struct DqnFixedString DqnFixedString &operator+=(DqnSlice const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; } DqnFixedString &operator+=(DqnFixedString const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.str, other.len); return *this; } - DqnFixedString operator+ (char const *other); - DqnFixedString operator+ (DqnSlice const &other); - DqnFixedString operator+ (DqnSlice const &other); - DqnFixedString operator+ (DqnFixedString const &other); + DqnFixedString operator+ (char const *other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other); return result; } + DqnFixedString operator+ (DqnSlice const &other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.data, other.len); return result; } + DqnFixedString operator+ (DqnSlice const &other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.data, other.len); return result; } + DqnFixedString operator+ (DqnFixedString const &other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.str, other.len); return result; } // Xprintf functions always modifies buffer and null-terminates even with insufficient buffer size. // Asserts on failure if DQN_ASSERT is defined. @@ -2050,33 +2059,6 @@ struct DqnFixedString void Clear (Dqn::ZeroClear clear = Dqn::ZeroClear::False) { if (clear == Dqn::ZeroClear::True) DqnMem_Set(str, 0, MAX); this = {}; } }; -template -DqnFixedString DqnFixedString::operator+(char const *other) -{ - DqnFixedString result = {}; - result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, this->str, this->len); - result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other); - return result; -} - -template -DqnFixedString DqnFixedString::operator+(DqnSlice const &other) -{ - DqnFixedString result = {}; - result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, this->str, this->len); - result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.data, other.len); - return result; -} - -template -DqnFixedString DqnFixedString::operator+(DqnFixedString const &other) -{ - DqnFixedString result = {}; - result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, this->str, this->len); - result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.str, other.len); - return result; -} - template FILE_SCOPE bool DqnFixedString__VSprintf(DqnFixedString *me, char const *fmt, va_list argList, int bufOffset) @@ -2137,17 +2119,6 @@ bool DqnFixedString::SprintfAppend(char const *fmt, ...) return result; } - -DQN_FILE_SCOPE DqnString DqnString_(DqnMemAPI *const api = DQN_DEFAULT_HEAP_ALLOCATOR); -DQN_FILE_SCOPE DqnString DqnString_(DqnMemStack *const stack); - -// NOTE: First level of indirection needs to turn the combined dqnstring_(guid) into a name. Otherwise -// each use of literalVarName will increment __COUNTER__ -#define DQN_STRING_LITERAL_INTERNAL(srcVariable, literal, literalVarName) \ - {}; \ - char literalVarName[] = literal; \ - srcVariable.InitLiteralNoAlloc(literalVarName, DQN_CHAR_COUNT(literalVarName)) - // TODO(doyle): Load factor // #DqnHashTable API // ================================================================================================= @@ -6465,7 +6436,7 @@ wchar_t *DqnString::ToWChar(DqnMemAPI *api) const // #DqnFixedString Implementation // ================================================================================================= // return: The number of bytes written to dest -int DqnFixedString__Append(char *dest, int destSize, char const *src, int len) +FILE_SCOPE int DqnFixedString__Append(char *dest, int destSize, char const *src, int len) { if (len <= -1) {