@@ -23,30 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
#pragma once
24
24
25
25
#include " string.h"
26
+ #include < cppcore/Common/Hash.h>
26
27
#include < cppcore/CPPCoreCommon.h>
27
- #include < malloc.h>
28
28
29
29
namespace cppcore {
30
30
31
- template <class T >
32
- struct Allocator {
33
- inline T *alloc (size_t size, size_t alignment) {
34
- return (T*) _aligned_malloc (size, alignment);
35
- }
36
-
37
- inline void dealloc (T *ptr) {
38
- _aligned_free (ptr);
39
- }
40
-
41
- inline static size_t countChars (const T *ptr) {
42
- if (nullptr != ptr) {
43
- return 0 ;
44
- }
45
-
46
- return (::strlen (ptr));
47
- }
48
- };
49
-
50
31
// -------------------------------------------------------------------------------------------------
51
32
// / @class TStringBase
52
33
// / @ingroup CPPCore
@@ -57,135 +38,122 @@ template <class T>
57
38
class TStringBase {
58
39
public:
59
40
// / @brief The default class constructor.
60
- TStringBase () noexcept ;
41
+ TStringBase () noexcept = default ;
61
42
62
43
// / @brief The class constructor with a pointer showing to the data buffer.
63
44
// / @param pPtr [in] The data buffer.
64
- TStringBase (const T *pPtr);
45
+ TStringBase (const T *pPtr, size_t size );
65
46
66
47
// / @brief The class destructor.
67
48
~TStringBase ();
68
49
69
- void set (const T *ptr);
50
+ void set (const T *ptr, size_t size);
51
+ void clear ();
52
+ void reset ();
53
+ size_t size () const ;
54
+ size_t capacity () const ;
55
+ const T *c_str () const ;
70
56
71
57
// / @brief Helper method to copy data into the string.
72
58
// / @param base [inout] The string data to copy in.
73
59
// / @param pPtr [in] The data source.
74
- static void copyFrom (TStringBase<T> &base, const T *pPtr);
60
+ static void copyFrom (TStringBase<T> &base, const T *pPtr, size_t size );
75
61
76
62
bool operator == (const TStringBase<T> &rhs) const ;
77
63
bool operator != (const TStringBase<T> &rhs) const ;
78
64
79
- T *m_pStringBuffer;
80
- size_t m_size;
81
- size_t m_capacity;
82
- Allocator<T> mAllocator ;
65
+ private:
66
+ static constexpr size_t InitSize = 256 ;
67
+ T mBuffer [InitSize] = {' \0 ' };
68
+ T *mStringBuffer {nullptr };
69
+ size_t mSize {0 };
70
+ size_t mCapacity {256 };
71
+ HashId mHashId {0 };
83
72
};
84
73
85
74
template <class T >
86
- inline TStringBase<T>::TStringBase() noexcept :
87
- m_pStringBuffer (nullptr ),
88
- m_size(0 ),
89
- m_capacity(0 ),
90
- mAllocator() {
91
- // empty
75
+ inline TStringBase<T>::TStringBase(const T *pPtr, size_t size) {
76
+ copyFrom (*this , pPtr, size);
77
+ mHashId = THash<HashId>::toHash (pPtr, mSize );
92
78
}
93
79
94
80
template <class T >
95
- inline TStringBase<T>::TStringBase(const T *pPtr) :
96
- m_pStringBuffer (nullptr ),
97
- m_size(0 ),
98
- m_capacity(0 ),
99
- mAllocator() {
100
- copyFrom (*this , pPtr);
81
+ inline TStringBase<T>::~TStringBase () {
82
+ clear ();
101
83
}
102
84
103
85
template <class T >
104
- inline TStringBase<T>::~TStringBase ( ) {
105
- if (m_pStringBuffer) {
106
- mAllocator . dealloc (m_pStringBuffer);
107
- m_pStringBuffer = nullptr ;
86
+ inline void TStringBase<T>::set( const T *ptr, size_t size ) {
87
+ void reset ();
88
+ if ( nullptr != ptr) {
89
+ copyFrom (* this , ptr, size) ;
108
90
}
109
91
}
110
92
111
93
template <class T >
112
- inline void TStringBase<T>::set(const T *ptr) {
113
- mAllocator .dealloc (m_pStringBuffer);
114
- if (nullptr != ptr) {
115
- copyFrom (*this , ptr);
116
- }
94
+ inline void TStringBase<T>::reset() {
95
+ mSize = 0u ;
117
96
}
118
97
119
98
template <class T >
120
- inline void TStringBase<T>::copyFrom(TStringBase<T> &base, const T *ptr) {
121
- if (nullptr != ptr) {
122
- base.m_size = Allocator<T>::countChars (ptr);
123
- if (base.m_size ) {
124
- base.m_capacity = base.m_size + 1 ;
125
- base.m_pStringBuffer = base.mAllocator .alloc (base.m_capacity , 16 );
126
- #ifdef CPPCORE_WINDOWS
127
- ::strncpy_s (base.m_pStringBuffer, base.m_capacity, ptr, base.m_size);
128
- #else
129
- ::strncpy (base.m_pStringBuffer, ptr, base.m_size);
130
- #endif
131
- base.m_pStringBuffer [base.m_size ] = ' \0 ' ;
132
- }
133
- }
99
+ inline size_t TStringBase<T>::size() const {
100
+ return mSize ;
134
101
}
135
102
136
103
template <class T >
137
- inline bool TStringBase<T>::operator ==( const TStringBase<T> &rhs ) const {
138
- if (rhs.m_size != m_size) {
139
- return false ;
140
- }
104
+ inline size_t TStringBase<T>::capacity() const {
105
+ return mCapacity ;
106
+ }
141
107
142
- for ( size_t i = 0 ; i < m_size; ++i) {
143
- if (rhs. m_pStringBuffer [i] != m_pStringBuffer[i]) {
144
- return false ;
145
- }
108
+ template < class T >
109
+ inline const T *TStringBase<T>::c_str() const {
110
+ if ( mStringBuffer != nullptr ) {
111
+ return mStringBuffer ;
146
112
}
147
113
148
- return true ;
114
+ return mBuffer ;
149
115
}
150
116
151
117
template <class T >
152
- inline bool TStringBase<T>::operator !=(const TStringBase<T> &rhs) const {
153
- return !(*this == rhs);
118
+ inline void TStringBase<T>::clear() {
119
+ if (mStringBuffer != nullptr ) {
120
+ delete [] mStringBuffer ;
121
+ mStringBuffer = nullptr ;
122
+ mCapacity = InitSize;
123
+ }
124
+ mSize = 0 ;
154
125
}
155
126
156
- template <class TCharType >
157
- class TStringView {
158
- public:
159
- TStringView (TCharType *ptr);
160
- ~TStringView ();
161
- size_t size () const ;
162
- TCharType *data () const ;
163
-
164
- private:
165
- TCharType *mPtr ;
166
- size_t mLen ;
167
- };
168
-
169
- template <class TCharType >
170
- inline TStringView<TCharType>::TStringView(TCharType *ptr) :
171
- mPtr (ptr),
172
- mLen(0 ) {
173
- if (nullptr != mPtr ) {
174
- mLen = strlen (ptr);
127
+ template <class T >
128
+ inline void TStringBase<T>::copyFrom(TStringBase<T> &base, const T *ptr, size_t size) {
129
+ if (ptr != nullptr ) {
130
+ T *targetPtr = base.mBuffer ;
131
+
132
+ if (size > 0 ) {
133
+ if (size > base.mCapacity ) {
134
+ base.mStringBuffer = new T[size];
135
+ base.mCapacity = size;
136
+ targetPtr = base.mStringBuffer ;
137
+ }
138
+ memcpy (targetPtr, ptr, size);
139
+ base.mSize = size;
140
+ }
175
141
}
176
142
}
177
143
178
- template <class TCharType >
179
- inline TStringView<TCharType>::~TStringView () {
180
- // empty
181
- }
144
+ template <class T >
145
+ inline bool TStringBase<T>::operator == (const TStringBase<T> &rhs) const {
146
+ if (rhs.mSize != mSize ) {
147
+ return false ;
148
+ }
182
149
183
- template <class TCharType >
184
- inline size_t TStringView<TCharType>::size() const {
185
- return mLen ;
150
+
151
+ return mHashId == rhs.mHashId ;
186
152
}
187
153
188
- template <class TCharType >
189
- inline TCharType *TStringView<TCharType>::data() const {
154
+ template <class T >
155
+ inline bool TStringBase<T>::operator != (const TStringBase<T> &rhs) const {
156
+ return !(*this == rhs);
190
157
}
191
158
159
+ } // namespace cppcore
0 commit comments