string.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*-
  2. * Copyright 2012-2018 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 <tinystl/allocator.h>
  29. #include <tinystl/stddef.h>
  30. #include <tinystl/hash.h>
  31. namespace tinystl {
  32. template<typename Allocator>
  33. class basic_string {
  34. public:
  35. basic_string();
  36. basic_string(const basic_string& other);
  37. basic_string(basic_string&& other);
  38. basic_string(const char* sz);
  39. basic_string(const char* sz, size_t len);
  40. ~basic_string();
  41. basic_string& operator=(const basic_string& other);
  42. basic_string& operator=(basic_string&& other);
  43. const char* c_str() const;
  44. size_t size() const;
  45. void reserve(size_t size);
  46. void resize(size_t size);
  47. void clear();
  48. void append(const char* first, const char* last);
  49. void assign(const char* s, size_t n);
  50. void shrink_to_fit();
  51. void swap(basic_string& other);
  52. private:
  53. typedef char* pointer;
  54. pointer m_first;
  55. pointer m_last;
  56. pointer m_capacity;
  57. static const size_t c_nbuffer = 12;
  58. char m_buffer[12]{0};
  59. };
  60. template<typename allocator>
  61. inline basic_string<allocator>::basic_string()
  62. : m_first(m_buffer)
  63. , m_last(m_buffer)
  64. , m_capacity(m_buffer + c_nbuffer)
  65. {
  66. resize(0);
  67. }
  68. template<typename allocator>
  69. inline basic_string<allocator>::basic_string(const basic_string& other)
  70. : m_first(m_buffer)
  71. , m_last(m_buffer)
  72. , m_capacity(m_buffer + c_nbuffer)
  73. {
  74. reserve(other.size());
  75. append(other.m_first, other.m_last);
  76. }
  77. template<typename allocator>
  78. inline basic_string<allocator>::basic_string(basic_string&& other)
  79. {
  80. if (other.m_first == other.m_buffer) {
  81. m_first = m_buffer;
  82. m_last = m_buffer;
  83. m_capacity = m_buffer + c_nbuffer;
  84. reserve(other.size());
  85. append(other.m_first, other.m_last);
  86. } else {
  87. m_first = other.m_first;
  88. m_last = other.m_last;
  89. m_capacity = other.m_capacity;
  90. }
  91. other.m_first = other.m_last = other.m_buffer;
  92. other.m_capacity = other.m_buffer + c_nbuffer;
  93. other.resize(0);
  94. }
  95. template<typename allocator>
  96. inline basic_string<allocator>::basic_string(const char* sz)
  97. : m_first(m_buffer)
  98. , m_last(m_buffer)
  99. , m_capacity(m_buffer + c_nbuffer)
  100. {
  101. size_t len = 0;
  102. for (const char* it = sz; *it; ++it)
  103. ++len;
  104. reserve(len);
  105. append(sz, sz + len);
  106. }
  107. template<typename allocator>
  108. inline basic_string<allocator>::basic_string(const char* sz, size_t len)
  109. : m_first(m_buffer)
  110. , m_last(m_buffer)
  111. , m_capacity(m_buffer + c_nbuffer)
  112. {
  113. reserve(len);
  114. append(sz, sz + len);
  115. }
  116. template<typename allocator>
  117. inline basic_string<allocator>::~basic_string() {
  118. if (m_first != m_buffer)
  119. allocator::static_deallocate(m_first, m_capacity - m_first);
  120. }
  121. template<typename allocator>
  122. inline basic_string<allocator>& basic_string<allocator>::operator=(const basic_string& other) {
  123. basic_string(other).swap(*this);
  124. return *this;
  125. }
  126. template<typename allocator>
  127. inline basic_string<allocator>& basic_string<allocator>::operator=(basic_string&& other) {
  128. basic_string(static_cast<basic_string&&>(other)).swap(*this);
  129. return *this;
  130. }
  131. template<typename allocator>
  132. inline const char* basic_string<allocator>::c_str() const {
  133. return m_first;
  134. }
  135. template<typename allocator>
  136. inline size_t basic_string<allocator>::size() const
  137. {
  138. ptrdiff_t size = m_last - m_first;
  139. return (size_t)size;
  140. }
  141. template<typename allocator>
  142. inline void basic_string<allocator>::reserve(size_t capacity) {
  143. if (m_first + capacity + 1 <= m_capacity)
  144. return;
  145. const ptrdiff_t size = m_last - m_first;
  146. pointer newfirst = (pointer)allocator::static_allocate(capacity + 1);
  147. for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit)
  148. *newit = *it;
  149. if (m_first != m_buffer)
  150. allocator::static_deallocate(m_first, m_capacity - m_first);
  151. m_first = newfirst;
  152. m_last = newfirst + size;
  153. m_capacity = m_first + capacity;
  154. }
  155. template<typename allocator>
  156. inline void basic_string<allocator>::resize(size_t size) {
  157. const ptrdiff_t prevSize = m_last-m_first;
  158. reserve(size);
  159. if (size > (size_t)prevSize)
  160. for (pointer it = m_last, end = m_first + size + 1; it < end; ++it)
  161. *it = 0;
  162. else if (m_last != m_first)
  163. m_first[size] = 0;
  164. m_last = m_first + size;
  165. }
  166. template<typename allocator>
  167. inline void basic_string<allocator>::clear() {
  168. resize(0);
  169. }
  170. template<typename allocator>
  171. inline void basic_string<allocator>::append(const char* first, const char* last) {
  172. const ptrdiff_t newsize = ((m_last - m_first) + (last - first) + 1);
  173. if (m_first + newsize > m_capacity)
  174. reserve((newsize * 3) / 2);
  175. for (; first != last; ++m_last, ++first)
  176. *m_last = *first;
  177. *m_last = 0;
  178. }
  179. template<typename allocator>
  180. inline void basic_string<allocator>::assign(const char* sz, size_t n) {
  181. clear();
  182. append(sz, sz+n);
  183. }
  184. template<typename allocator>
  185. inline void basic_string<allocator>::shrink_to_fit() {
  186. if (m_first == m_buffer) {
  187. } else if (m_last == m_first) {
  188. const ptrdiff_t capacity = m_capacity - m_first;
  189. if (capacity)
  190. allocator::static_deallocate(m_first, capacity+1);
  191. m_capacity = m_first;
  192. } else if (m_capacity != m_last) {
  193. const ptrdiff_t size = m_last - m_first;
  194. char* newfirst = (pointer)allocator::static_allocate(size+1);
  195. for (pointer in = m_first, out = newfirst; in != m_last + 1; ++in, ++out)
  196. *out = *in;
  197. if (m_first != m_capacity)
  198. allocator::static_deallocate(m_first, m_capacity+1-m_first);
  199. m_first = newfirst;
  200. m_last = newfirst+size;
  201. m_capacity = m_last;
  202. }
  203. }
  204. template<typename allocator>
  205. inline void basic_string<allocator>::swap(basic_string& other) {
  206. {
  207. const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity;
  208. m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity;
  209. other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity;
  210. }
  211. for (size_t i = 0; i < c_nbuffer; ++i) {
  212. const char temp = m_buffer[i];
  213. m_buffer[i] = other.m_buffer[i];
  214. other.m_buffer[i] = temp;
  215. }
  216. if (m_first == other.m_buffer) {
  217. ptrdiff_t len = m_last - m_first;
  218. m_first = m_buffer;
  219. m_last = m_buffer + len;
  220. m_capacity = m_buffer + c_nbuffer;
  221. }
  222. if (other.m_first == m_buffer) {
  223. ptrdiff_t len = other.m_last - other.m_first;
  224. other.m_first = other.m_buffer;
  225. other.m_last = other.m_buffer + len;
  226. other.m_capacity = other.m_buffer + c_nbuffer;
  227. }
  228. }
  229. template<typename allocatorl, typename allocatorr>
  230. inline bool operator==(const basic_string<allocatorl>& lhs, const basic_string<allocatorr>& rhs) {
  231. typedef const char* pointer;
  232. const size_t lsize = lhs.size(), rsize = rhs.size();
  233. if (lsize != rsize)
  234. return false;
  235. pointer lit = lhs.c_str(), rit = rhs.c_str();
  236. pointer lend = lit + lsize;
  237. while (lit != lend)
  238. if (*lit++ != *rit++)
  239. return false;
  240. return true;
  241. }
  242. template<typename allocator>
  243. static inline size_t hash(const basic_string<allocator>& value) {
  244. return hash_string(value.c_str(), value.size());
  245. }
  246. typedef basic_string<TINYSTL_ALLOCATOR> string;
  247. }
  248. #endif