UStringTest.h 8.9 KB

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