string.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "stddef.h"
  29. #include "hash.h"
  30. namespace tinystl {
  31. template<typename Alloc>
  32. class stringT {
  33. public:
  34. stringT();
  35. stringT(const stringT<Alloc>& other);
  36. stringT(const char* sz);
  37. stringT(const char* sz, size_t len);
  38. ~stringT();
  39. stringT<Alloc>& operator=(const stringT<Alloc>& other);
  40. const char* c_str() const;
  41. size_t size() const;
  42. bool empty() const;
  43. void reserve(size_t size);
  44. void resize(size_t size);
  45. void append(const char* first, const char* last);
  46. void append(const char* str);
  47. void swap(stringT<Alloc>& other);
  48. private:
  49. typedef char* pointer;
  50. pointer m_first;
  51. pointer m_last;
  52. pointer m_capacity;
  53. static const size_t c_nbuffer = 12;
  54. char m_buffer[12];
  55. };
  56. typedef stringT<TINYSTL_ALLOCATOR> string;
  57. template<typename Alloc>
  58. inline stringT<Alloc>::stringT()
  59. : m_first(m_buffer)
  60. , m_last(m_buffer)
  61. , m_capacity(m_buffer + c_nbuffer)
  62. {
  63. resize(0);
  64. }
  65. template<typename Alloc>
  66. inline stringT<Alloc>::stringT(const stringT<Alloc>& other)
  67. : m_first(m_buffer)
  68. , m_last(m_buffer)
  69. , m_capacity(m_buffer + c_nbuffer)
  70. {
  71. reserve(other.size());
  72. append(other.m_first, other.m_last);
  73. }
  74. template<typename Alloc>
  75. inline stringT<Alloc>::stringT(const char* sz)
  76. : m_first(m_buffer)
  77. , m_last(m_buffer)
  78. , m_capacity(m_buffer + c_nbuffer)
  79. {
  80. size_t len = 0;
  81. for (const char* it = sz; *it; ++it)
  82. ++len;
  83. reserve(len);
  84. append(sz, sz + len);
  85. }
  86. template<typename Alloc>
  87. inline stringT<Alloc>::stringT(const char* sz, size_t len)
  88. : m_first(m_buffer)
  89. , m_last(m_buffer)
  90. , m_capacity(m_buffer + c_nbuffer)
  91. {
  92. reserve(len);
  93. append(sz, sz + len);
  94. }
  95. template<typename Alloc>
  96. inline stringT<Alloc>::~stringT() {
  97. if (m_first != m_buffer)
  98. Alloc::static_deallocate(m_first, m_capacity - m_first);
  99. }
  100. template<typename Alloc>
  101. inline stringT<Alloc>& stringT<Alloc>::operator=(const stringT<Alloc>& other) {
  102. stringT<Alloc>(other).swap(*this);
  103. return *this;
  104. }
  105. template<typename Alloc>
  106. inline const char* stringT<Alloc>::c_str() const {
  107. return m_first;
  108. }
  109. template<typename Alloc>
  110. inline size_t stringT<Alloc>::size() const
  111. {
  112. return (size_t)(m_last - m_first);
  113. }
  114. template<typename Alloc>
  115. inline bool stringT<Alloc>::empty() const
  116. {
  117. return 0 == size();
  118. }
  119. template<typename Alloc>
  120. inline void stringT<Alloc>::reserve(size_t capacity) {
  121. if (m_first + capacity + 1 <= m_capacity) {
  122. return;
  123. }
  124. const size_t size = (size_t)(m_last - m_first);
  125. pointer newfirst = (pointer)Alloc::static_allocate(capacity + 1);
  126. for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit) {
  127. *newit = *it;
  128. }
  129. if (m_first != m_buffer) {
  130. Alloc::static_deallocate(m_first, m_capacity - m_first);
  131. }
  132. m_first = newfirst;
  133. m_last = newfirst + size;
  134. m_capacity = m_first + capacity;
  135. }
  136. template<typename Alloc>
  137. inline void stringT<Alloc>::resize(size_t size) {
  138. reserve(size);
  139. for (pointer it = m_last, end = m_first + size + 1; it < end; ++it)
  140. *it = 0;
  141. m_last += size;
  142. }
  143. template<typename Alloc>
  144. inline void stringT<Alloc>::append(const char* first, const char* last) {
  145. const size_t newsize = (size_t)((m_last - m_first) + (last - first) + 1);
  146. if (m_first + newsize > m_capacity)
  147. reserve((newsize * 3) / 2);
  148. for (; first != last; ++m_last, ++first)
  149. *m_last = *first;
  150. *m_last = 0;
  151. }
  152. template<typename Alloc>
  153. inline void stringT<Alloc>::append(const char* str) {
  154. append(str, str + strlen(str) );
  155. }
  156. template<typename Alloc>
  157. inline void stringT<Alloc>::swap(stringT<Alloc>& other) {
  158. const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity;
  159. m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity;
  160. other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity;
  161. char tbuffer[c_nbuffer];
  162. if (m_first == other.m_buffer)
  163. for (pointer it = other.m_buffer, end = m_last, out = tbuffer; it != end; ++it, ++out)
  164. *out = *it;
  165. if (other.m_first == m_buffer) {
  166. other.m_last = other.m_last - other.m_first + other.m_buffer;
  167. other.m_first = other.m_buffer;
  168. other.m_capacity = other.m_buffer + c_nbuffer;
  169. for (pointer it = other.m_first, end = other.m_last, in = m_buffer; it != end; ++it, ++in)
  170. *it = *in;
  171. *other.m_last = 0;
  172. }
  173. if (m_first == other.m_buffer) {
  174. m_last = m_last - m_first + m_buffer;
  175. m_first = m_buffer;
  176. m_capacity = m_buffer + c_nbuffer;
  177. for (pointer it = m_first, end = m_last, in = tbuffer; it != end; ++it, ++in)
  178. *it = *in;
  179. *m_last = 0;
  180. }
  181. }
  182. template<typename Alloc>
  183. inline bool operator==(const stringT<Alloc>& lhs, const stringT<Alloc>& rhs) {
  184. typedef const char* pointer;
  185. const size_t lsize = lhs.size(), rsize = rhs.size();
  186. if (lsize != rsize)
  187. return false;
  188. pointer lit = lhs.c_str(), rit = rhs.c_str();
  189. pointer lend = lit + lsize;
  190. while (lit != lend)
  191. if (*lit++ != *rit++)
  192. return false;
  193. return true;
  194. }
  195. template<typename Alloc>
  196. static inline size_t hash(const stringT<Alloc>& value) {
  197. return hash_string(value.c_str(), value.size());
  198. }
  199. }
  200. #endif