• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision124 (tree)
Time2018-09-10 18:06:11
Authorquiret

Log Message

- refactored some eir::String code
- optimized the Insert method by only copying up to insertPos old bytes (if it is ever done) to the new buffer from the old buffer

Change Summary

Incremental Difference

--- common/sdk/String.h (revision 123)
+++ common/sdk/String.h (revision 124)
@@ -132,6 +132,50 @@
132132 this->release_data();
133133 }
134134
135+private:
136+ AINLINE static void expand_buffer( void *objMem, charType *oldCharBuf, size_t oldCharCount, size_t oldCharCopyCount, size_t newCharCount, charType*& useBufOut, bool& isBufNewOut )
137+ {
138+ size_t newRequiredCharsSize = sizeof(charType) * ( newCharCount + 1 );
139+
140+ charType *useBuf = nullptr;
141+ bool isBufNew;
142+
143+ if ( oldCharBuf && oldCharCount > 0 )
144+ {
145+ bool couldResize = allocatorType::Resize( objMem, oldCharBuf, newRequiredCharsSize );
146+
147+ if ( couldResize )
148+ {
149+ useBuf = oldCharBuf;
150+ isBufNew = false;
151+ }
152+ }
153+
154+ if ( useBuf == nullptr )
155+ {
156+ useBuf = (charType*)allocatorType::Allocate( objMem, newRequiredCharsSize, alignof(charType) );
157+
158+ if ( useBuf == nullptr )
159+ {
160+ throw eir_exception();
161+ }
162+
163+ // Guarranteed to throw no exception due to the trivial charType.
164+ if ( oldCharCopyCount > 0 )
165+ {
166+ // Copy over the characters.
167+ FSDataUtil::copy_impl( oldCharBuf, oldCharBuf + oldCharCopyCount, useBuf );
168+ }
169+
170+ isBufNew = true;
171+ }
172+
173+ // Return the data.
174+ isBufNewOut = isBufNew;
175+ useBufOut = useBuf;
176+ }
177+
178+public:
135179 // *** Modification functions.
136180
137181 inline void Assign( const charType *theChars, size_t copyCharCount )
@@ -150,36 +194,11 @@
150194 charType *useBuf = nullptr;
151195 bool isBufNew;
152196
153- size_t charDataSize = sizeof(charType) * ( copyCharCount + 1 );
154-
155197 charType *oldCharBuf = this->char_data;
156198 size_t oldCharCount = this->num_chars;
157199
158- if ( oldCharBuf && oldCharCount > 0 )
159- {
160- bool resizeSuccess = allocatorType::Resize( this, oldCharBuf, charDataSize );
200+ expand_buffer( this, oldCharBuf, oldCharCount, 0, copyCharCount, useBuf, isBufNew );
161201
162- if ( resizeSuccess )
163- {
164- useBuf = oldCharBuf;
165- isBufNew = false;
166- }
167- }
168-
169- // If we failed to retake our old buffer then we just allocate a new one.
170- if ( useBuf == nullptr )
171- {
172- // It is ok to cast to charType immediatly because charType is trivial.
173- useBuf = (charType*)allocatorType::Allocate( this, charDataSize, alignof(charType) );
174-
175- if ( !useBuf )
176- {
177- throw eir_exception();
178- }
179-
180- isBufNew = true;
181- }
182-
183202 // Granted because charType is trivial.
184203 //noexcept
185204 {
@@ -222,51 +241,6 @@
222241 return *this;
223242 }
224243
225-private:
226- AINLINE static void expand_buffer( void *objMem, charType *oldCharBuf, size_t oldCharCount, size_t newCharCount, charType*& useBufOut, bool& isBufNewOut )
227- {
228- size_t newRequiredCharsSize = sizeof(charType) * ( newCharCount + 1 );
229-
230- charType *useBuf = nullptr;
231- bool isBufNew;
232-
233- if ( oldCharBuf && oldCharCount > 0 )
234- {
235- bool couldResize = allocatorType::Resize( objMem, oldCharBuf, newRequiredCharsSize );
236-
237- if ( couldResize )
238- {
239- useBuf = oldCharBuf;
240- isBufNew = false;
241- }
242- }
243-
244- if ( useBuf == nullptr )
245- {
246- useBuf = (charType*)allocatorType::Allocate( objMem, newRequiredCharsSize, alignof(charType) );
247-
248- if ( useBuf == nullptr )
249- {
250- throw eir_exception();
251- }
252-
253- // Guarranteed due to the trivial charType.
254- // But we like to make sure anyway.
255- //noexcept
256- {
257- // Copy over the characters.
258- FSDataUtil::copy_impl( oldCharBuf, oldCharBuf + oldCharCount, useBuf );
259-
260- isBufNew = true;
261- }
262- }
263-
264- // Return the data.
265- isBufNewOut = isBufNew;
266- useBufOut = useBuf;
267- }
268-
269-public:
270244 // Append characters to the end of this string.
271245 inline void Append( const charType *charsToAppend, size_t charsToAppendCount )
272246 {
@@ -285,7 +259,7 @@
285259 charType *useBuf;
286260 bool isBufNew;
287261
288- expand_buffer( this, oldCharBuf, num_chars, newCharCount, useBuf, isBufNew );
262+ expand_buffer( this, oldCharBuf, num_chars, num_chars, newCharCount, useBuf, isBufNew );
289263
290264 // Guarranteed due to trivial charType.
291265 //noexcept
@@ -326,7 +300,7 @@
326300 charType *useBuf;
327301 bool isBufNew;
328302
329- expand_buffer( this, oldCharBuf, oldCharCount, newCharCount, useBuf, isBufNew );
303+ expand_buffer( this, oldCharBuf, oldCharCount, insertPos, newCharCount, useBuf, isBufNew );
330304
331305 //noexcept
332306 {
Show on old repository browser