cm_string_view.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.hxx"
  4. #ifndef CMake_HAVE_CXX_STRING_VIEW
  5. # include "cm_kwiml.h"
  6. # include <algorithm>
  7. # include <ostream>
  8. # include <stdexcept>
  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, size_type pos) const
  71. 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, size_type pos) const
  94. 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, size_type pos) const
  131. 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, size_type pos) const
  159. 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(string_view v,
  174. size_type pos) const
  175. noexcept
  176. {
  177. for (; pos < size_; ++pos) {
  178. if (!traits_type::find(v.data_, v.size_, data_[pos])) {
  179. return pos;
  180. }
  181. }
  182. return npos;
  183. }
  184. string_view::size_type string_view::find_first_not_of(char c,
  185. size_type pos) const
  186. noexcept
  187. {
  188. return find_first_not_of(string_view(&c, 1), pos);
  189. }
  190. string_view::size_type string_view::find_first_not_of(const char* s,
  191. size_type pos,
  192. size_type count) const
  193. {
  194. return find_first_not_of(string_view(s, count), pos);
  195. }
  196. string_view::size_type string_view::find_first_not_of(const char* s,
  197. size_type pos) const
  198. {
  199. return find_first_not_of(string_view(s), pos);
  200. }
  201. string_view::size_type string_view::find_last_not_of(string_view v,
  202. size_type pos) const
  203. noexcept
  204. {
  205. if (size_ > 0) {
  206. for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) {
  207. --pos;
  208. if (!traits_type::find(v.data_, v.size_, data_[pos])) {
  209. return pos;
  210. }
  211. }
  212. }
  213. return npos;
  214. }
  215. string_view::size_type string_view::find_last_not_of(char c,
  216. size_type pos) const
  217. noexcept
  218. {
  219. return find_last_not_of(string_view(&c, 1), pos);
  220. }
  221. string_view::size_type string_view::find_last_not_of(const char* s,
  222. size_type pos,
  223. size_type count) const
  224. {
  225. return find_last_not_of(string_view(s, count), pos);
  226. }
  227. string_view::size_type string_view::find_last_not_of(const char* s,
  228. size_type pos) const
  229. {
  230. return find_last_not_of(string_view(s), pos);
  231. }
  232. std::ostream& operator<<(std::ostream& o, string_view v)
  233. {
  234. return o.write(v.data(), v.size());
  235. }
  236. std::string& operator+=(std::string& s, string_view v)
  237. {
  238. s.append(v.data(), v.size());
  239. return s;
  240. }
  241. }
  242. std::hash<cm::string_view>::result_type std::hash<cm::string_view>::operator()(
  243. argument_type const& s) const noexcept
  244. {
  245. // FNV-1a hash.
  246. static KWIML_INT_uint64_t const fnv_offset_basis = 0xcbf29ce484222325;
  247. static KWIML_INT_uint64_t const fnv_prime = 0x100000001b3;
  248. KWIML_INT_uint64_t h = fnv_offset_basis;
  249. for (char const& c : s) {
  250. h = h ^ KWIML_INT_uint64_t(KWIML_INT_uint8_t(c));
  251. h = h * fnv_prime;
  252. }
  253. return result_type(h);
  254. }
  255. #else
  256. // Avoid empty translation unit.
  257. void cm_string_view_cxx()
  258. {
  259. }
  260. #endif