versadac  1
versadac - Scalable Recorder Firmware
StringTest.h
1 #ifndef STRING_TEST_H
2 #define STRING_TEST_H
3 
4 /*
5  * Developer note: This is not a fully functionaly string and is not meant to be used as such.
6  * It is merely to serve as a testing module
7  */
8 
9 #include <cstring>
10 #include <cstdlib>
11 
12 typedef char mychar;
13 
14 static size_t mystrlen(const mychar * str){
15  unsigned int i = 0;
16  for(const mychar * it = str; *it; ++it, ++i){
17  //dummy
18  }
19  return i;
20 }
21 
22 class json_string {
23 public:
24  struct const_iterator {
25  inline const_iterator& operator ++(void) { ++it; return *this; }
26  inline const_iterator& operator --(void) { --it; return *this; }
27  inline const_iterator& operator +=(long i) { it += i; return *this; }
28  inline const_iterator& operator -=(long i) { it -= i; return *this; }
29  inline const_iterator operator ++(int) {
30  const_iterator result(*this);
31  ++it;
32  return result;
33  }
34  inline const_iterator operator --(int) {
35  const_iterator result(*this);
36  --it;
37  return result;
38  }
39  inline const_iterator operator +(long i) const {
40  const_iterator result(*this);
41  result.it += i;
42  return result;
43  }
44  inline const_iterator operator -(long i) const {
45  const_iterator result(*this);
46  result.it -= i;
47  return result;
48  }
49  inline size_t operator -(const_iterator other) const {
50  return it - other.it;
51  }
52  inline mychar & operator [](size_t pos) const { return it[pos]; };
53  inline mychar & operator *(void) const { return *it; }
54  inline bool operator == (const const_iterator & other) const { return it == other.it; }
55  inline bool operator != (const const_iterator & other) const { return it != other.it; }
56  inline bool operator > (const const_iterator & other) const { return it > other.it; }
57  inline bool operator >= (const const_iterator & other) const { return it >= other.it; }
58  inline bool operator < (const const_iterator & other) const { return it < other.it; }
59  inline bool operator <= (const const_iterator & other) const { return it <= other.it; }
60  inline const_iterator & operator = (const const_iterator & orig) { it = orig.it; return *this; }
61  const_iterator (const const_iterator & orig) : it(orig.it) {}
62  const_iterator (const mychar * place) : it((mychar*)place) {}
63  const_iterator(void) : it(0) {};
64 
65  mychar * it;
66  };
67 
68  struct iterator {
69  inline iterator& operator ++(void) { ++it; return *this; }
70  inline iterator& operator --(void) { --it; return *this; }
71  inline iterator& operator +=(long i) { it += i; return *this; }
72  inline iterator& operator -=(long i) { it -= i; return *this; }
73  inline iterator operator ++(int) {
74  iterator result(*this);
75  ++it;
76  return result;
77  }
78  inline iterator operator --(int) {
79  iterator result(*this);
80  --it;
81  return result;
82  }
83  inline iterator operator +(long i) const {
84  iterator result(*this);
85  result.it += i;
86  return result;
87  }
88  inline iterator operator -(long i) const {
89  iterator result(*this);
90  result.it -= i;
91  return result;
92  }
93  inline mychar & operator [](size_t pos) const { return it[pos]; };
94  inline mychar & operator *(void) const { return *it; }
95  inline bool operator == (const iterator & other) const { return it == other.it; }
96  inline bool operator != (const iterator & other) const { return it != other.it; }
97  inline bool operator > (const iterator & other) const { return it > other.it; }
98  inline bool operator >= (const iterator & other) const { return it >= other.it; }
99  inline bool operator < (const iterator & other) const { return it < other.it; }
100  inline bool operator <= (const iterator & other) const { return it <= other.it; }
101  inline iterator & operator = (const iterator & orig) { it = orig.it; return *this; }
102  inline operator const_iterator() const json_nothrow { return const_iterator(it); }
103  iterator (const iterator & orig) : it(orig.it) {}
104  iterator (const mychar * place) : it((mychar*)place) {}
105 
106  mychar * it;
107  };
108 
109 
110 
111  const static size_t npos = 0xFFFFFFFF;
112  json_string(void) : len(0), str(0){
113  setToCStr("", 0);
114  }
115 
116  json_string(const mychar * meh) : len(0), str(0){
117  setToCStr(meh, mystrlen(meh));
118  }
119 
120  json_string(const mychar * meh, size_t l) : len(l), str(0){
121  setToCStr(meh, l);
122  str[len] = '\0';
123  }
124 
125  json_string(const iterator & beg, const iterator & en) : len(0), str(0){
126  setToCStr(beg.it, en.it - beg.it);
127  str[len] = '\0';
128  }
129 
130  json_string(const const_iterator & beg, const const_iterator & en) : len(0), str(0){
131  setToCStr(beg.it, en.it - beg.it);
132  str[len] = '\0';
133  }
134 
135  json_string(const json_string & meh) : len(0), str(0){
136  setToCStr(meh.c_str(), meh.len);
137  }
138 
139  ~json_string(void){ std::free(str); };
140 
141  json_string(unsigned int l, mychar meh) : len(0), str(0){
142  str = (mychar*)std::malloc((l + 1) * sizeof(mychar));
143  len = l;
144  for (unsigned int i = 0; i < l; ++i){
145  str[i] = meh;
146  }
147  str[l] = '\0';
148  }
149 
150  void swap(json_string & meh){
151  size_t _len = len;
152  mychar * _str = str;
153  len = meh.len;
154  str = meh.str;
155  meh.len = _len;
156  meh.str = _str;
157  }
158 
159  iterator begin(void){ return iterator(str); };
160  iterator end(void){ return iterator(str + length()); };
161  const iterator begin(void) const { return iterator(str); };
162  const iterator end(void) const { return iterator(str + length()); };
163  void assign(const iterator & beg, const iterator & en){
164  json_string(beg, en).swap(*this);
165  }
166  json_string & append(const iterator & beg, const iterator & en){
167  json_string temp(beg, en);
168  return *this += temp;
169  }
170 
171  const mychar * c_str(void) const { return str; };
172  const mychar * data(void) const { return str; };
173  size_t length(void) const { return len; };
174  size_t capacity(void) const { return len; };
175  bool empty(void) const { return len == 0; };
176 
177  bool operator ==(const json_string & other) const {
178  if (len != other.len) return false;
179  return memcmp(str, other.str, len * sizeof(mychar)) == 0;
180  }
181 
182  bool operator !=(const json_string & other) const {
183  return !(*this == other);
184  }
185 
186  const char & operator[] (size_t pos) const { return str[pos]; }
187  char & operator[] ( size_t pos ){ return str[pos]; }
188 
189  json_string & operator = (const json_string & meh) {
190  std::free(str);
191  setToCStr(meh.c_str(), meh.len);
192  return *this;
193  }
194 
195  json_string & operator = (const mychar * meh) {
196  std::free(str);
197  setToCStr(meh, mystrlen(meh));
198  return *this;
199  }
200 
201  json_string & operator += (const json_string & other) {
202  size_t newlen = len + other.len;
203  mychar * newstr = (mychar*)std::malloc((newlen + 1) * sizeof(mychar));
204  std::memcpy(newstr, str, len * sizeof(mychar));
205  std::memcpy(newstr + len, other.str, (other.len + 1) * sizeof(mychar));
206  len = newlen;
207  std::free(str);
208  str = newstr;
209  return *this;
210  }
211 
212  const json_string operator + (const json_string & other) const {
213  json_string result = *this;
214  result += other;
215  return result;
216  }
217 
218  json_string & operator += (const mychar other) {
219  mychar temp[2] = {other, '\0'};
220  json_string temp_s(temp);
221  return (*this) += temp_s;
222  }
223 
224  const json_string operator + (const mychar other) const {
225  json_string result = *this;
226  result += other;
227  return result;
228  }
229 
230  void reserve(size_t){}; //noop, its just a test
231  void clear(void){setToCStr("", 0);}
232 
233  json_string substr(size_t pos = 0, size_t n = npos) const {
234  json_string res(false, false, false);
235  if (n > len) n = len;
236  if (n + pos > len) n = len - pos;
237  res.setToCStr(str + pos, n);
238  res.str[n] = L'\0';
239  return res;
240  }
241 
242 
243  size_t find ( mychar c, size_t pos = 0 ) const {
244  if (pos > len) return npos;
245  for(mychar * i = str + pos; *i; ++i){
246  if (*i == c) return i - str;
247  }
248  return npos;
249  }
250 
251  size_t find_first_not_of ( const mychar* s, size_t pos = 0 ) const {
252  if (pos > len) return npos;
253  for(mychar * i = str + pos; *i; ++i){
254  bool found = false;
255  for(const mychar * k = s; *k; ++k){
256  if (*i == *k){
257  found = true;
258  break;
259  }
260  }
261  if (!found) return i - str;
262  }
263  return npos;
264  }
265 
266  size_t find_first_of ( const mychar* s, size_t pos = 0 ) const {
267  if (pos > len) return npos;
268  for(mychar * i = str + pos; *i; ++i){
269  for(const mychar * k = s; *k; ++k){
270  if (*i == *k){
271  return i - str;
272  }
273  }
274  }
275  return npos;
276  }
277 
278  iterator erase(iterator it, iterator it2){
279  size_t mov = it2.it - it.it;
280  std::memmove(str, it2.it, (len - mov + 1) * sizeof(mychar)); //+1 for null terminator
281  len -= mov;
282  return it;
283  }
284 private:
285  json_string(bool, bool, bool) : len(0), str(0){};
286 
287  void setToCStr(const mychar * st, size_t l){
288  len = l;
289  str = (mychar*)std::memcpy(std::malloc((len + 1) * sizeof(mychar)), st, (len + 1) * sizeof(mychar));
290  }
291 
292  size_t len;
293  mychar * str;
294 
295 };
296 
297 #endif
Definition: StringTest.h:68
Definition: StringTest.h:24
Definition: StringTest.h:22