Skip to content

Commit 5c5dbf5

Browse files
committed
Add string append
1 parent e50c38b commit 5c5dbf5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

include/cppcore/Common/TStringBase.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ class TStringBase {
5151
void clear();
5252
void reset();
5353
size_t size() const;
54+
void resize(size_t size);
5455
size_t capacity() const;
5556
const T *c_str() const;
57+
void append(const T *ptr, size_t size);
5658

5759
/// @brief Helper method to copy data into the string.
5860
/// @param base [inout] The string data to copy in.
@@ -101,6 +103,28 @@ inline size_t TStringBase<T>::size() const {
101103
return mSize;
102104
}
103105

106+
template <class T>
107+
inline void TStringBase<T>::resize(size_t size) {
108+
if (size <= mCapacity) {
109+
return;
110+
}
111+
112+
if (mStringBuffer == nullptr) {
113+
mStringBuffer = new T[size];
114+
mCapacity = size;
115+
if (mSize > 0) {
116+
memcpy(mStringBuffer, mBuffer, size());
117+
}
118+
} else {
119+
T *ptr = new T[size];
120+
mCapacity = size;
121+
if (mSize > 0) {
122+
memcpy(ptr, mBuffer, size());
123+
}
124+
mStringBuffer = ptr
125+
}
126+
}
127+
104128
template <class T>
105129
inline size_t TStringBase<T>::capacity() const {
106130
return mCapacity;
@@ -115,6 +139,18 @@ inline const T *TStringBase<T>::c_str() const {
115139
return mBuffer;
116140
}
117141

142+
template <class T>
143+
inline void TStringBase<T>::append(const T* ptr, size_t size) {
144+
if (ptr == nullptr) {
145+
return;
146+
}
147+
148+
size_t newLen = mSize + size;
149+
if (newLen > mCapacity) {
150+
151+
}
152+
}
153+
118154
template <class T>
119155
inline void TStringBase<T>::clear() {
120156
if (mStringBuffer != nullptr) {

0 commit comments

Comments
 (0)