cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TStringBase.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
25#include "string.h"
26#include <cppcore/Common/Hash.h>
28
29namespace cppcore {
30
31//-------------------------------------------------------------------------------------------------
36//-------------------------------------------------------------------------------------------------
37template <class T>
39public:
41 TStringBase() noexcept = default;
42
46 TStringBase(const T *ptr, size_t size);
47
50
54 void set(const T *ptr, size_t size);
55
57 void clear();
58
60 void reset();
61
64 size_t size() const;
65
68 bool isEmpty() const;
69
72 size_t capacity() const;
73
76 const T *c_str() const;
77
81 static void copyFrom(TStringBase<T> &base, const T *pPtr, size_t size);
82
84 bool operator == (const TStringBase<T> &rhs) const;
85
87 bool operator != (const TStringBase<T> &rhs) const;
88
89 TStringBase<T> &operator += (const TStringBase<T> &rhs);
90 TStringBase<T> &operator+=(char c);
91 T operator[](size_t index) const;
92
93private:
94 static constexpr size_t InitSize = 512;
95 T mBuffer[InitSize] = {'\0'};
96 T *mStringBuffer{nullptr};
97 size_t mSize{0};
98 size_t mCapacity{InitSize};
99 HashId mHashId{0};
100};
101
102template <class T>
103inline TStringBase<T>::TStringBase(const T *ptr, size_t size) {
104 copyFrom(*this, ptr, size);
105 mHashId = THash<HashId>::toHash(c_str(), size);
106}
107
108template <class T>
110 clear();
111}
112
113template <class T>
114inline void TStringBase<T>::set(const T *ptr, size_t size) {
115 reset();
116 if (nullptr != ptr) {
117 copyFrom(*this, ptr, size);
118 }
119}
120
121template <class T>
123 mSize = 0u;
124 mHashId = 0;
125}
126
127template <class T>
128inline size_t TStringBase<T>::size() const {
129 return mSize;
130}
131
132template <class T>
133inline bool TStringBase<T>::isEmpty() const {
134 return mSize == 0;
135}
136
137template <class T>
138inline size_t TStringBase<T>::capacity() const {
139 return mCapacity;
140}
141
142template <class T>
143inline const T *TStringBase<T>::c_str() const {
144 if (mStringBuffer != nullptr) {
145 return mStringBuffer;
146 }
147
148 return mBuffer;
149}
150
151template <class T>
153 if (mStringBuffer != nullptr) {
154 delete [] mStringBuffer;
155 mStringBuffer = nullptr;
156 mCapacity = InitSize;
157 }
158 reset();
159}
160
161template <class T>
162inline void TStringBase<T>::copyFrom(TStringBase<T> &base, const T *ptr, size_t size) {
163 if (ptr == nullptr) {
164 return;
165 }
166
167 T *targetPtr = base.mBuffer;
168 if (size == 0) {
169 return;
170 }
171
172 if (size > base.mCapacity) {
173 if (base.mStringBuffer != nullptr) {
174 delete [] base.mStringBuffer;
175 }
176 base.mStringBuffer = new T[size + 1];
177 base.mCapacity = size + 1;
178 targetPtr = base.mStringBuffer;
179 }
180 memcpy(targetPtr, ptr, size * sizeof(T));
181 targetPtr[size] = '\0';
182 base.mSize = size;
183}
184
185template <class T>
186inline bool TStringBase<T>::operator == (const TStringBase<T> &rhs) const {
187 if (rhs.mSize != mSize) {
188 return false;
189 }
190
191 if (mHashId != rhs.mHashId) {
192 return false;
193 }
194
195 // Fallback to actual comparison in case of hash collision
196 return memcmp(c_str(), rhs.c_str(), mSize * sizeof(T)) == 0;
197}
198
199template <class T>
200inline bool TStringBase<T>::operator != (const TStringBase<T> &rhs) const {
201 return !(*this == rhs);
202}
203
204template <class T>
206 if (rhs.isEmpty()) {
207 return *this;
208 }
209
210 copyFrom(*this, rhs.c_str(), rhs.size());
211
212 return *this;
213}
214
215template <class T>
217 copyFrom(*this, &c, 1);
218 return *this;
219}
220
221template <class T>
222inline T TStringBase<T>::operator[](size_t index) const {
223 if (index >= mSize) {
224 return 0;
225 }
226 return mBuffer[index];
227}
228
229} // namespace cppcore
static T toHash(const char *buffer, T base)
Computes the hash value for a given character buffer.
Definition Hash.h:94
const char * c_str() const
void set(const char *ptr, size_t size)
bool operator==(const TStringBase< T > &rhs) const
Compare operator.
static void copyFrom(TStringBase< char > &base, const char *pPtr, size_t size)
bool operator!=(const TStringBase< T > &rhs) const
Not equal operator.
TStringBase< T > & operator+=(const TStringBase< T > &rhs)
~TStringBase()
The class destructor.
TStringBase() noexcept=default
The default class constructor.
T operator[](size_t index) const
uint64_t HashId