29template<
typename CharType,
int BUFSIZE = 512>
class CharBuffer
34 CharBuffer() =
default;
36 CharBuffer(
const CharBuffer& other)
41 CharBuffer(CharBuffer&& other)
noexcept
43 if (other.buffer == other.smallBuffer)
45 memcpy(this->smallBuffer, other.smallBuffer, BUFSIZE);
46 this->buffer = this->smallBuffer;
50 this->buffer = other.buffer;
52 this->capacity = other.capacity;
53 other.buffer = other.smallBuffer;
54 other.capacity = BUFSIZE;
58 CharBuffer& operator=(
const CharBuffer& other)
64 CharBuffer& operator=(CharBuffer&& other)
noexcept
69 if (other.buffer == other.smallBuffer)
71 memcpy(this->smallBuffer, other.smallBuffer, BUFSIZE);
72 this->buffer = this->smallBuffer;
76 this->buffer = other.buffer;
78 this->capacity = other.capacity;
79 other.buffer = other.smallBuffer;
80 other.capacity = BUFSIZE;
86 virtual ~CharBuffer()
noexcept
92 catch (
const std::exception&)
97 explicit CharBuffer(
const char* s)
102 explicit CharBuffer(
const char16_t* s)
107 explicit CharBuffer(
const wchar_t* s)
112 explicit CharBuffer(
const std::basic_string<char>& other)
117 explicit CharBuffer(
const std::basic_string<char16_t>& other)
122 explicit CharBuffer(
const std::basic_string<wchar_t>& other)
127 explicit CharBuffer(std::size_t n)
133 void Set(
const CharBuffer& other)
137 Reserve(other.capacity);
138 memcpy(this->buffer, other.buffer, other.capacity *
sizeof(CharType));
142 template<
typename OtherCharType>
void Set(
const OtherCharType* s)
151 std::size_t requiredSize;
152 if (
sizeof(CharType) <
sizeof(OtherCharType))
154 requiredSize = StrLen(s) * 4 + 1;
158 requiredSize = StrLen(s) + 1;
160 Reserve(requiredSize);
161 StringUtil::CopyCeeString(buffer, GetCapacity(), s);
165 template<
typename OtherCharType>
void Set(
const std::basic_string<OtherCharType>& s)
170 void Append(
const std::basic_string<CharType>& s)
172 Reserve(GetLength() + s.length() + 1);
173 StringUtil::AppendCeeString(buffer, GetCapacity(), s.c_str());
176 void Append(
const CharType* s)
179 Reserve(GetLength() + StrLen(s) + 1);
180 StringUtil::AppendCeeString(buffer, GetCapacity(), s);
183 void Append(
const CharType* s, std::size_t len)
185 std::size_t idx = GetLength();
186 Reserve(idx + len + 1);
187 for (; len > 0; --len, ++idx, ++s)
194 void Append(CharType ch)
196 std::size_t len = GetLength();
211 return buffer[0] == 0;
216 if (buffer != smallBuffer)
219 buffer = smallBuffer;
225 void Reserve(std::size_t newSize)
227 if (newSize > BUFSIZE && newSize > capacity)
229 CharType* newBuffer =
new CharType[newSize];
230 memcpy(newBuffer, buffer, capacity *
sizeof(CharType));
231 if (buffer != smallBuffer)
244 return std::basic_string<CharType>(buffer);
247 const CharType* GetData()
const
257 std::size_t GetLength()
const
260 for (idx = 0; idx < capacity && buffer[idx] != 0; ++idx)
267 std::size_t GetCapacity()
const
272 const CharType& operator[](std::size_t idx)
const
278 CharType& operator[](std::size_t idx)
284 template<
typename OtherCharType> CharBuffer& operator=(
const OtherCharType* s)
290 template<
typename OtherCharType> CharBuffer& operator=(
const std::basic_string<OtherCharType>& s)
296 CharBuffer& operator+=(
const CharType* s)
302 CharBuffer& operator+=(
const std::basic_string<CharType>& s)
308 CharBuffer& operator+=(CharType ch)
316 CharType* buffer = smallBuffer;
317 std::size_t capacity = BUFSIZE;
318 CharType smallBuffer[BUFSIZE] = { 0 };