string.h 5.5 KB

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