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