string.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*-
  2. * Copyright 2012 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef TINYSTL_STRING_H
  27. #define TINYSTL_STRING_H
  28. #include <string.h> // strlen
  29. #include "stddef.h"
  30. #include "hash.h"
  31. namespace tinystl {
  32. template<typename Alloc>
  33. class stringT {
  34. public:
  35. stringT();
  36. stringT(const stringT<Alloc>& other);
  37. stringT(const char* sz);
  38. stringT(const char* sz, size_t len);
  39. ~stringT();
  40. stringT<Alloc>& operator=(const stringT<Alloc>& other);
  41. const char* c_str() const;
  42. size_t size() const;
  43. bool empty() const;
  44. void reserve(size_t _size);
  45. void resize(size_t _size);
  46. void append(const char* first, const char* last);
  47. void append(const char* str);
  48. void swap(stringT<Alloc>& other);
  49. private:
  50. typedef char* pointer;
  51. pointer m_first;
  52. pointer m_last;
  53. pointer m_capacity;
  54. static const size_t c_nbuffer = 12;
  55. char m_buffer[12];
  56. };
  57. typedef stringT<TINYSTL_ALLOCATOR> string;
  58. template<typename Alloc>
  59. inline stringT<Alloc>::stringT()
  60. : m_first(m_buffer)
  61. , m_last(m_buffer)
  62. , m_capacity(m_buffer + c_nbuffer)
  63. {
  64. resize(0);
  65. }
  66. template<typename Alloc>
  67. inline stringT<Alloc>::stringT(const stringT<Alloc>& other)
  68. : m_first(m_buffer)
  69. , m_last(m_buffer)
  70. , m_capacity(m_buffer + c_nbuffer)
  71. {
  72. reserve(other.size());
  73. append(other.m_first, other.m_last);
  74. }
  75. template<typename Alloc>
  76. inline stringT<Alloc>::stringT(const char* sz)
  77. : m_first(m_buffer)
  78. , m_last(m_buffer)
  79. , m_capacity(m_buffer + c_nbuffer)
  80. {
  81. size_t len = 0;
  82. for (const char* it = sz; *it; ++it)
  83. ++len;
  84. reserve(len);
  85. append(sz, sz + len);
  86. }
  87. template<typename Alloc>
  88. inline stringT<Alloc>::stringT(const char* sz, size_t len)
  89. : m_first(m_buffer)
  90. , m_last(m_buffer)
  91. , m_capacity(m_buffer + c_nbuffer)
  92. {
  93. reserve(len);
  94. append(sz, sz + len);
  95. }
  96. template<typename Alloc>
  97. inline stringT<Alloc>::~stringT() {
  98. if (m_first != m_buffer)
  99. Alloc::static_deallocate(m_first, m_capacity - m_first);
  100. }
  101. template<typename Alloc>
  102. inline stringT<Alloc>& stringT<Alloc>::operator=(const stringT<Alloc>& other) {
  103. stringT<Alloc>(other).swap(*this);
  104. return *this;
  105. }
  106. template<typename Alloc>
  107. inline const char* stringT<Alloc>::c_str() const {
  108. return m_first;
  109. }
  110. template<typename Alloc>
  111. inline size_t stringT<Alloc>::size() const
  112. {
  113. return (size_t)(m_last - m_first);
  114. }
  115. template<typename Alloc>
  116. inline bool stringT<Alloc>::empty() const
  117. {
  118. return 0 == size();
  119. }
  120. template<typename Alloc>
  121. inline void stringT<Alloc>::reserve(size_t capacity) {
  122. if (m_first + capacity + 1 <= m_capacity) {
  123. return;
  124. }
  125. const size_t _size = (size_t)(m_last - m_first);
  126. pointer newfirst = (pointer)Alloc::static_allocate(capacity + 1);
  127. for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit) {
  128. *newit = *it;
  129. }
  130. if (m_first != m_buffer) {
  131. Alloc::static_deallocate(m_first, m_capacity - m_first);
  132. }
  133. m_first = newfirst;
  134. m_last = newfirst + _size;
  135. m_capacity = m_first + capacity;
  136. }
  137. template<typename Alloc>
  138. inline void stringT<Alloc>::resize(size_t _size) {
  139. reserve(_size);
  140. for (pointer it = m_last, end = m_first + _size + 1; it < end; ++it)
  141. *it = 0;
  142. m_last += _size;
  143. }
  144. template<typename Alloc>
  145. inline void stringT<Alloc>::append(const char* first, const char* last) {
  146. const size_t newsize = (size_t)((m_last - m_first) + (last - first) + 1);
  147. if (m_first + newsize > m_capacity)
  148. reserve((newsize * 3) / 2);
  149. for (; first != last; ++m_last, ++first)
  150. *m_last = *first;
  151. *m_last = 0;
  152. }
  153. template<typename Alloc>
  154. inline void stringT<Alloc>::append(const char* str) {
  155. append(str, str + strlen(str) );
  156. }
  157. template<typename Alloc>
  158. inline void stringT<Alloc>::swap(stringT<Alloc>& other) {
  159. const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity;
  160. m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity;
  161. other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity;
  162. char tbuffer[c_nbuffer];
  163. if (m_first == other.m_buffer)
  164. for (pointer it = other.m_buffer, end = m_last, out = tbuffer; it != end; ++it, ++out)
  165. *out = *it;
  166. if (other.m_first == m_buffer) {
  167. other.m_last = other.m_last - other.m_first + other.m_buffer;
  168. other.m_first = other.m_buffer;
  169. other.m_capacity = other.m_buffer + c_nbuffer;
  170. for (pointer it = other.m_first, end = other.m_last, in = m_buffer; it != end; ++it, ++in)
  171. *it = *in;
  172. *other.m_last = 0;
  173. }
  174. if (m_first == other.m_buffer) {
  175. m_last = m_last - m_first + m_buffer;
  176. m_first = m_buffer;
  177. m_capacity = m_buffer + c_nbuffer;
  178. for (pointer it = m_first, end = m_last, in = tbuffer; it != end; ++it, ++in)
  179. *it = *in;
  180. *m_last = 0;
  181. }
  182. }
  183. template<typename Alloc>
  184. inline bool operator==(const stringT<Alloc>& lhs, const stringT<Alloc>& rhs) {
  185. typedef const char* pointer;
  186. const size_t lsize = lhs.size(), rsize = rhs.size();
  187. if (lsize != rsize)
  188. return false;
  189. pointer lit = lhs.c_str(), rit = rhs.c_str();
  190. pointer lend = lit + lsize;
  191. while (lit != lend)
  192. if (*lit++ != *rit++)
  193. return false;
  194. return true;
  195. }
  196. template<typename Alloc>
  197. static inline size_t hash(const stringT<Alloc>& value) {
  198. return hash_string(value.c_str(), value.size());
  199. }
  200. }
  201. #endif