Skip to content

Commit cee59bd

Browse files
committed
rework
1 parent 5c10a83 commit cee59bd

File tree

5 files changed

+176
-129
lines changed

5 files changed

+176
-129
lines changed

include/cppcore/Common/TStringBase.h

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,42 @@ class TStringBase {
4747
/// @brief The class destructor.
4848
~TStringBase();
4949

50+
/// @brief Will set a new string.
51+
/// @param ptr Pointer to the buffer.
52+
/// @param size Size of the buffer.
5053
void set(const T *ptr, size_t size);
54+
55+
/// @brief Will clear the string buffer.
5156
void clear();
57+
58+
/// @brief Will reset the string buffer, not internal storage will be released.
5259
void reset();
60+
61+
/// @brief Returns the size of the string buffer.
62+
/// @return The size of the string buffer.
5363
size_t size() const;
54-
void resize(size_t size);
64+
65+
/// @brief Will return true, if the string is empty.
66+
/// @return true for empty.
67+
bool isEmpty() const;
68+
69+
/// @brief Will return the whole capacity of the string.
70+
/// @return The capacity of the string
5571
size_t capacity() const;
72+
73+
/// @brief Will return the string pointer.
74+
/// @return The string pointer.
5675
const T *c_str() const;
57-
void append(const T *ptr, size_t size);
5876

5977
/// @brief Helper method to copy data into the string.
6078
/// @param base [inout] The string data to copy in.
6179
/// @param pPtr [in] The data source.
6280
static void copyFrom(TStringBase<T> &base, const T *pPtr, size_t size);
6381

82+
/// @brief Compare operator.
6483
bool operator == (const TStringBase<T> &rhs) const;
84+
85+
/// @brief Not equal operator.
6586
bool operator != (const TStringBase<T> &rhs) const;
6687

6788
private:
@@ -104,25 +125,8 @@ inline size_t TStringBase<T>::size() const {
104125
}
105126

106127
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-
}
128+
inline bool TStringBase<T>::isEmpty() const {
129+
return mSize == 0;
126130
}
127131

128132
template <class T>
@@ -139,18 +143,6 @@ inline const T *TStringBase<T>::c_str() const {
139143
return mBuffer;
140144
}
141145

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-
154146
template <class T>
155147
inline void TStringBase<T>::clear() {
156148
if (mStringBuffer != nullptr) {

include/cppcore/Common/TStringView.h

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,53 @@ namespace cppcore {
3030
/// @class TStringView
3131
/// @ingroup CPPCore
3232
///
33-
/// @brief
33+
/// @brief
3434
//-------------------------------------------------------------------------------------------------
3535
template <class T>
3636
class TStringView {
3737
public:
3838
using const_iterator = const T*;
3939

40+
/// @brief The default constructor.
41+
TStringView() = default;
42+
43+
/// @brief The constructor with the buffer.
44+
/// @param ptr Pointer to the buffer
45+
/// @param len The buffer size
4046
TStringView(const T *ptr, size_t len);
47+
48+
/// @brief The class destructor.
4149
~TStringView() = default;
50+
51+
/// @brief Will return the size of the view.
52+
/// @return The size of the view
4253
size_t size() const;
43-
const T *data() const;
54+
55+
/// @brief Will return the data with an offset, if given.
56+
/// @param offset The offset in the view.
57+
/// @return Pointer to the data
58+
const T *data(size_t offset = 0) const;
59+
60+
/// @brief Will return true, if there is no string to view.
61+
/// @return true for empty
4462
bool isEmpty() const;
63+
64+
/// @brief Will return the first entry.
65+
/// @return The first entry
4566
const_iterator begin() const;
67+
68+
/// @brief Will return the end entry.
69+
/// @return The end entry
4670
const_iterator end() const;
4771

4872
private:
49-
const T *mPtr;
50-
size_t mLen;
73+
const T *mPtr = nullptr;
74+
size_t mLen = 0;
5175
};
5276

5377
template <class T>
5478
inline TStringView<T>::TStringView(const T *ptr, size_t len) :
55-
mPtr(ptr),
56-
mLen(len) {
79+
mPtr(ptr), mLen(len) {
5780
// empty
5881
}
5982

@@ -63,8 +86,11 @@ inline size_t TStringView<T>::size() const {
6386
}
6487

6588
template <class T>
66-
inline const T *TStringView<T>::data() const {
67-
return mPtr;
89+
inline const T *TStringView<T>::data(size_t offset) const {
90+
if (offset > mLen) {
91+
return nullptr;
92+
}
93+
return mPtr + offset;
6894
}
6995

7096
template <class T>

0 commit comments

Comments
 (0)