string.h 5.6 KB

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