StringTest.h 8.9 KB

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