string_view.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <cm/string_view> // IWYU pragma: associated
  4. #ifndef CMake_HAVE_CXX_STRING_VIEW
  5. # include <algorithm>
  6. # include <ostream>
  7. # include <stdexcept>
  8. # include <cm3p/kwiml/int.h>
  9. namespace cm {
  10. string_view::const_reference string_view::at(size_type pos) const
  11. {
  12. if (pos >= size_) {
  13. throw std::out_of_range("Index out of range in string_view::at");
  14. }
  15. return data_[pos];
  16. }
  17. string_view::size_type string_view::copy(char* dest, size_type count,
  18. size_type pos) const
  19. {
  20. if (pos > size_) {
  21. throw std::out_of_range("Index out of range in string_view::copy");
  22. }
  23. size_type const rcount = std::min(count, size_ - pos);
  24. traits_type::copy(dest, data_ + pos, rcount);
  25. return rcount;
  26. }
  27. string_view string_view::substr(size_type pos, size_type count) const
  28. {
  29. if (pos > size_) {
  30. throw std::out_of_range("Index out of range in string_view::substr");
  31. }
  32. size_type const rcount = std::min(count, size_ - pos);
  33. return string_view(data_ + pos, rcount);
  34. }
  35. int string_view::compare(string_view v) const noexcept
  36. {
  37. size_type const rlen = std::min(size_, v.size_);
  38. int c = traits_type::compare(data_, v.data_, rlen);
  39. if (c == 0) {
  40. if (size_ < v.size_) {
  41. c = -1;
  42. } else if (size_ > v.size_) {
  43. c = 1;
  44. }
  45. }
  46. return c;
  47. }
  48. int string_view::compare(size_type pos1, size_type count1, string_view v) const
  49. {
  50. return substr(pos1, count1).compare(v);
  51. }
  52. int string_view::compare(size_type pos1, size_type count1, string_view v,
  53. size_type pos2, size_type count2) const
  54. {
  55. return substr(pos1, count1).compare(v.substr(pos2, count2));
  56. }
  57. int string_view::compare(const char* s) const
  58. {
  59. return compare(string_view(s));
  60. }
  61. int string_view::compare(size_type pos1, size_type count1, const char* s) const
  62. {
  63. return substr(pos1, count1).compare(string_view(s));
  64. }
  65. int string_view::compare(size_type pos1, size_type count1, const char* s,
  66. size_type count2) const
  67. {
  68. return substr(pos1, count1).compare(string_view(s, count2));
  69. }
  70. string_view::size_type string_view::find(string_view v,
  71. size_type pos) const noexcept
  72. {
  73. for (; pos + v.size_ <= size_; ++pos) {
  74. if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) == 0) {
  75. return pos;
  76. }
  77. }
  78. return npos;
  79. }
  80. string_view::size_type string_view::find(char c, size_type pos) const noexcept
  81. {
  82. return find(string_view(&c, 1), pos);
  83. }
  84. string_view::size_type string_view::find(const char* s, size_type pos,
  85. size_type count) const
  86. {
  87. return find(string_view(s, count), pos);
  88. }
  89. string_view::size_type string_view::find(const char* s, size_type pos) const
  90. {
  91. return find(string_view(s), pos);
  92. }
  93. string_view::size_type string_view::rfind(string_view v,
  94. size_type pos) const noexcept
  95. {
  96. if (size_ >= v.size_) {
  97. for (pos = std::min(pos, size_ - v.size_) + 1; pos > 0;) {
  98. --pos;
  99. if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) ==
  100. 0) {
  101. return pos;
  102. }
  103. }
  104. }
  105. return npos;
  106. }
  107. string_view::size_type string_view::rfind(char c, size_type pos) const noexcept
  108. {
  109. return rfind(string_view(&c, 1), pos);
  110. }
  111. string_view::size_type string_view::rfind(const char* s, size_type pos,
  112. size_type count) const
  113. {
  114. return rfind(string_view(s, count), pos);
  115. }
  116. string_view::size_type string_view::rfind(const char* s, size_type pos) const
  117. {
  118. return rfind(string_view(s), pos);
  119. }
  120. string_view::size_type string_view::find_first_of(string_view v,
  121. size_type pos) const noexcept
  122. {
  123. for (; pos < size_; ++pos) {
  124. if (traits_type::find(v.data_, v.size_, data_[pos])) {
  125. return pos;
  126. }
  127. }
  128. return npos;
  129. }
  130. string_view::size_type string_view::find_first_of(char c,
  131. size_type pos) const noexcept
  132. {
  133. return find_first_of(string_view(&c, 1), pos);
  134. }
  135. string_view::size_type string_view::find_first_of(const char* s, size_type pos,
  136. size_type count) const
  137. {
  138. return find_first_of(string_view(s, count), pos);
  139. }
  140. string_view::size_type string_view::find_first_of(const char* s,
  141. size_type pos) const
  142. {
  143. return find_first_of(string_view(s), pos);
  144. }
  145. string_view::size_type string_view::find_last_of(string_view v,
  146. size_type pos) const noexcept
  147. {
  148. if (size_ > 0) {
  149. for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) {
  150. --pos;
  151. if (traits_type::find(v.data_, v.size_, data_[pos])) {
  152. return pos;
  153. }
  154. }
  155. }
  156. return npos;
  157. }
  158. string_view::size_type string_view::find_last_of(char c,
  159. size_type pos) const noexcept
  160. {
  161. return find_last_of(string_view(&c, 1), pos);
  162. }
  163. string_view::size_type string_view::find_last_of(const char* s, size_type pos,
  164. size_type count) const
  165. {
  166. return find_last_of(string_view(s, count), pos);
  167. }
  168. string_view::size_type string_view::find_last_of(const char* s,
  169. size_type pos) const
  170. {
  171. return find_last_of(string_view(s), pos);
  172. }
  173. string_view::size_type string_view::find_first_not_of(
  174. string_view v, size_type pos) const noexcept
  175. {
  176. for (; pos < size_; ++pos) {
  177. if (!traits_type::find(v.data_, v.size_, data_[pos])) {
  178. return pos;
  179. }
  180. }
  181. return npos;
  182. }
  183. string_view::size_type string_view::find_first_not_of(
  184. char c, size_type pos) const noexcept
  185. {
  186. return find_first_not_of(string_view(&c, 1), pos);
  187. }
  188. string_view::size_type string_view::find_first_not_of(const char* s,
  189. size_type pos,
  190. size_type count) const
  191. {
  192. return find_first_not_of(string_view(s, count), pos);
  193. }
  194. string_view::size_type string_view::find_first_not_of(const char* s,
  195. size_type pos) const
  196. {
  197. return find_first_not_of(string_view(s), pos);
  198. }
  199. string_view::size_type string_view::find_last_not_of(
  200. string_view v, size_type pos) const noexcept
  201. {
  202. if (size_ > 0) {
  203. for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) {
  204. --pos;
  205. if (!traits_type::find(v.data_, v.size_, data_[pos])) {
  206. return pos;
  207. }
  208. }
  209. }
  210. return npos;
  211. }
  212. string_view::size_type string_view::find_last_not_of(
  213. char c, size_type pos) const noexcept
  214. {
  215. return find_last_not_of(string_view(&c, 1), pos);
  216. }
  217. string_view::size_type string_view::find_last_not_of(const char* s,
  218. size_type pos,
  219. size_type count) const
  220. {
  221. return find_last_not_of(string_view(s, count), pos);
  222. }
  223. string_view::size_type string_view::find_last_not_of(const char* s,
  224. size_type pos) const
  225. {
  226. return find_last_not_of(string_view(s), pos);
  227. }
  228. std::ostream& operator<<(std::ostream& o, string_view v)
  229. {
  230. return o.write(v.data(), v.size());
  231. }
  232. std::string& operator+=(std::string& s, string_view v)
  233. {
  234. s.append(v.data(), v.size());
  235. return s;
  236. }
  237. }
  238. std::hash<cm::string_view>::result_type std::hash<cm::string_view>::operator()(
  239. argument_type const& s) const noexcept
  240. {
  241. // FNV-1a hash.
  242. static KWIML_INT_uint64_t const fnv_offset_basis = 0xcbf29ce484222325;
  243. static KWIML_INT_uint64_t const fnv_prime = 0x100000001b3;
  244. KWIML_INT_uint64_t h = fnv_offset_basis;
  245. for (char const& c : s) {
  246. h = h ^ KWIML_INT_uint64_t(KWIML_INT_uint8_t(c));
  247. h = h * fnv_prime;
  248. }
  249. return result_type(h);
  250. }
  251. #else
  252. // Avoid empty translation unit.
  253. void cm_string_view_cxx()
  254. {
  255. }
  256. #endif