My Project
StringBuilder.h
1 #pragma once
2 
3 #include "NPLMemPool.h"
4 
5 namespace ParaEngine
6 {
7  // forward declare
8  template <typename UserAllocator = ParaEngine::CNPLPool_Char_allocator >
10 
13 
30  template <typename UserAllocator>
31  class StringBuilderT
32  {
33  public:
35  typedef std::string string_type;
36  typedef UserAllocator user_allocator;
37  typedef char Char;
38 
41  StringBuilderT(const string_type& sz);
42 
46  StringBuilderT(const Char* sz);
47 
51  StringBuilderT(Char ch, size_t count);
52 
56  StringBuilderT(size_t reserved);
57 
60 
64 
68  void reserve(size_t length);
69 
71  void resize(size_t length);
72 
75  inline size_t length() const { return m_size; }
76  inline size_t size() const { return m_size; }
77 
80  void clear();
81 
86  inline bool empty() const { return m_size == 0; }
87 
90  void append(Char c);
91 
94  void append(const string_type& sz);
95 
99  void append(const Char* sz);
100 
105  void append(const Char* sz, size_t len);
106 
109  void append(const StringBuilderT<UserAllocator>& b);
110 
114  void append(int32 i);
115  void append(uint32 i);
116  void append(uint64 i);
118  void appendBinary(uint16 i);
119  void appendBinary(uint32 i);
125  void appendHex(uint8 i);
126  template <class T> inline void appendHex(const T& i) { for (size_t j = 0; j < sizeof(T); ++j) appendHex(*(((uint8*)&i) + j)); }
128 #if defined(WIN32)
129  inline void append(int i) { append((int32) i); }
130 #ifdef _W64
131  inline void append(_W64 unsigned int i) { append((uint32) i); }
132 #else
133  inline void append(unsigned int i) { append((uint32) i); }
134 #endif
135 #else
136  // inline void append(unsigned long i) { append((uint32) i); } /**< See above. */
137  inline void append(long i) { append((int32) i); }
138 #endif // platform-switch.
139 
146  void append(float f);
147  void append(double f);
150  template<typename TYPE>
151  void WriteAt(int nIndex, const TYPE& val){
152  // *((TYPE*)(m_buffer + nIndex)) = val;
153  memcpy(m_buffer + nIndex, &val, sizeof(TYPE)); // fix byte alignment issue
154  }
155 
156  void WriteAt(int nIndex, const Char* data, size_t nSize);
157 
158 
163  template<typename TYPE> inline StringBuilderT& operator+=(const TYPE& val) { append(val); return *this; }
164 
169  void appendLine(const Char* sz);
170 
175  void remove(int32 start);
176 
183  void remove(int32 start, int32 end);
184 
186  inline void pop_back() { if (m_size > 0) --m_size; }
187 
191  template<typename TYPE> inline void set(const TYPE& val) { clear(); append(val); }
192  template<typename TYPE> inline StringBuilderT& operator=(const TYPE& val) { clear(); append(val); return *this; }
196  string_type ToString() const { return string_type(c_str(), length()); };
197 
202  const Char* c_str() const;
203  inline operator const Char*() const { return c_str(); }
206  Char* str() { return m_buffer; };
207 
209  Char& operator[] (const int nIndex) { return *(m_buffer+nIndex); };
210 
216  int32 index(Char c) const;
217 
223  int32 rindex(Char c) const;
224 
227  Char back() const { assert(m_size > 0); return m_buffer[m_size-1]; }
228  private:
229  void enlarge(size_t minimum);
230 
231  private:
232  Char* m_buffer;
233  size_t m_reserved;
234  size_t m_size;
235  };
236 
237  template <typename UserAllocator>
239  {
240  append((const char*)(&i), 4);
241  }
242 
243  template <typename UserAllocator>
245  {
246  append((const char*)(&i), 2);
247  }
248 
249 }
250 
251 // required by DLL interface
252 // EXPIMP_TEMPLATE template class PE_CORE_DECL ParaEngine::StringBuilderT< ParaEngine::CNPLPool_Char_allocator >;
StringBuilderT()
Creates a new builder with an empty buffer.
Definition: StringBuilder.hpp:221
void appendHex(uint8 i)
Appends the integer value, after converting it to a fm::string, in hexadecimal, to the content of the...
Definition: StringBuilder.hpp:388
int32 index(Char c) const
Retrieves the index of the first character within the content of the builder that is equivalent to th...
Definition: StringBuilder.hpp:429
string_type ToString() const
Converts the content of the builder to a standard string.
Definition: StringBuilder.h:196
void appendHex(const T &i)
See above.
Definition: StringBuilder.h:126
StringBuilderT & operator=(const TYPE &val)
See above.
Definition: StringBuilder.h:192
different physics engine has different winding order.
Definition: EventBinding.h:32
void append(long i)
See above.
Definition: StringBuilder.h:137
int32 rindex(Char c) const
Retrieves the index of the last character within the content of the builder that is equivalent to the...
Definition: StringBuilder.hpp:443
void resize(size_t length)
resize the buffer
Definition: StringBuilder.hpp:247
Char * str()
get raw string
Definition: StringBuilder.h:206
void clear()
Clears the content of the builder.
Definition: StringBuilder.hpp:241
const Char * c_str() const
Converts the content of the builder to a character array.
Definition: StringBuilder.hpp:420
std::string string_type
The standard string object which correspond to the builder.
Definition: StringBuilder.h:35
StringBuilderT< ParaEngine::CNPLPool_Char_allocator > StringBuilder
the string builder class using CNPLPool_Char_alloc
Definition: StringBuilder.h:9
void append(Char c)
Appends a character to the content of the builder.
Definition: StringBuilder.hpp:257
void WriteAt(int nIndex, const TYPE &val)
this is useful for writing to a previous cached location.
Definition: StringBuilder.h:151
size_t length() const
Retrieves the length of the content within the builder.
Definition: StringBuilder.h:75
Char & operator[](const int nIndex)
overload the operator [].
Definition: StringBuilder.h:209
void reserve(size_t length)
Reserves a given number of character slots.
Definition: StringBuilder.hpp:480
A NON-thread-safe, mutable sequence of characters(Binary is also possible).
Definition: StringBuilder.h:9
~StringBuilderT()
Deletes the builder.
Definition: StringBuilder.hpp:229
bool empty() const
Retrieves whether the builder is empty.
Definition: StringBuilder.h:86
StringBuilderT & operator+=(const TYPE &val)
Appends a value to the content of the builder.
Definition: StringBuilder.h:163
void pop_back()
Removes the last character of the content of the builder.
Definition: StringBuilder.h:186
void appendLine(const Char *sz)
Appends a character array to the content of the builder.
Definition: StringBuilder.hpp:381
Char back() const
Retrieves the last character within the content of the builder.
Definition: StringBuilder.h:227