string_view.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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(char const* s) const
  58. {
  59. return compare(string_view(s));
  60. }
  61. int string_view::compare(size_type pos1, size_type count1, char const* s) const
  62. {
  63. return substr(pos1, count1).compare(string_view(s));
  64. }
  65. int string_view::compare(size_type pos1, size_type count1, char const* 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. if (pos < size_) {
  74. for (; pos + v.size_ <= size_; ++pos) {
  75. if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) ==
  76. 0) {
  77. return pos;
  78. }
  79. }
  80. }
  81. return npos;
  82. }
  83. string_view::size_type string_view::find(char c, size_type pos) const noexcept
  84. {
  85. return find(string_view(&c, 1), pos);
  86. }
  87. string_view::size_type string_view::find(char const* s, size_type pos,
  88. size_type count) const
  89. {
  90. return find(string_view(s, count), pos);
  91. }
  92. string_view::size_type string_view::find(char const* s, size_type pos) const
  93. {
  94. return find(string_view(s), pos);
  95. }
  96. string_view::size_type string_view::rfind(string_view v,
  97. size_type pos) const noexcept
  98. {
  99. if (size_ >= v.size_) {
  100. for (pos = std::min(pos, size_ - v.size_) + 1; pos > 0;) {
  101. --pos;
  102. if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) ==
  103. 0) {
  104. return pos;
  105. }
  106. }
  107. }
  108. return npos;
  109. }
  110. string_view::size_type string_view::rfind(char c, size_type pos) const noexcept
  111. {
  112. return rfind(string_view(&c, 1), pos);
  113. }
  114. string_view::size_type string_view::rfind(char const* s, size_type pos,
  115. size_type count) const
  116. {
  117. return rfind(string_view(s, count), pos);
  118. }
  119. string_view::size_type string_view::rfind(char const* s, size_type pos) const
  120. {
  121. return rfind(string_view(s), pos);
  122. }
  123. string_view::size_type string_view::find_first_of(string_view v,
  124. size_type pos) const noexcept
  125. {
  126. for (; pos < size_; ++pos) {
  127. if (traits_type::find(v.data_, v.size_, data_[pos])) {
  128. return pos;
  129. }
  130. }
  131. return npos;
  132. }
  133. string_view::size_type string_view::find_first_of(char c,
  134. size_type pos) const noexcept
  135. {
  136. return find_first_of(string_view(&c, 1), pos);
  137. }
  138. string_view::size_type string_view::find_first_of(char const* s, size_type pos,
  139. size_type count) const
  140. {
  141. return find_first_of(string_view(s, count), pos);
  142. }
  143. string_view::size_type string_view::find_first_of(char const* s,
  144. size_type pos) const
  145. {
  146. return find_first_of(string_view(s), pos);
  147. }
  148. string_view::size_type string_view::find_last_of(string_view v,
  149. size_type pos) const noexcept
  150. {
  151. if (size_ > 0) {
  152. for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) {
  153. --pos;
  154. if (traits_type::find(v.data_, v.size_, data_[pos])) {
  155. return pos;
  156. }
  157. }
  158. }
  159. return npos;
  160. }
  161. string_view::size_type string_view::find_last_of(char c,
  162. size_type pos) const noexcept
  163. {
  164. return find_last_of(string_view(&c, 1), pos);
  165. }
  166. string_view::size_type string_view::find_last_of(char const* s, size_type pos,
  167. size_type count) const
  168. {
  169. return find_last_of(string_view(s, count), pos);
  170. }
  171. string_view::size_type string_view::find_last_of(char const* s,
  172. size_type pos) const
  173. {
  174. return find_last_of(string_view(s), pos);
  175. }
  176. string_view::size_type string_view::find_first_not_of(
  177. string_view v, size_type pos) const noexcept
  178. {
  179. for (; pos < size_; ++pos) {
  180. if (!traits_type::find(v.data_, v.size_, data_[pos])) {
  181. return pos;
  182. }
  183. }
  184. return npos;
  185. }
  186. string_view::size_type string_view::find_first_not_of(
  187. char c, size_type pos) const noexcept
  188. {
  189. return find_first_not_of(string_view(&c, 1), pos);
  190. }
  191. string_view::size_type string_view::find_first_not_of(char const* s,
  192. size_type pos,
  193. size_type count) const
  194. {
  195. return find_first_not_of(string_view(s, count), pos);
  196. }
  197. string_view::size_type string_view::find_first_not_of(char const* s,
  198. size_type pos) const
  199. {
  200. return find_first_not_of(string_view(s), pos);
  201. }
  202. string_view::size_type string_view::find_last_not_of(
  203. string_view v, size_type pos) const 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(
  216. char c, size_type pos) const noexcept
  217. {
  218. return find_last_not_of(string_view(&c, 1), pos);
  219. }
  220. string_view::size_type string_view::find_last_not_of(char const* s,
  221. size_type pos,
  222. size_type count) const
  223. {
  224. return find_last_not_of(string_view(s, count), pos);
  225. }
  226. string_view::size_type string_view::find_last_not_of(char const* s,
  227. size_type pos) const
  228. {
  229. return find_last_not_of(string_view(s), pos);
  230. }
  231. std::ostream& operator<<(std::ostream& o, string_view v)
  232. {
  233. return o.write(v.data(), v.size());
  234. }
  235. std::string& operator+=(std::string& s, string_view v)
  236. {
  237. s.append(v.data(), v.size());
  238. return s;
  239. }
  240. }
  241. std::hash<cm::string_view>::result_type std::hash<cm::string_view>::operator()(
  242. argument_type const& s) const noexcept
  243. {
  244. // FNV-1a hash.
  245. static KWIML_INT_uint64_t const fnv_offset_basis = 0xcbf29ce484222325;
  246. static KWIML_INT_uint64_t const fnv_prime = 0x100000001b3;
  247. KWIML_INT_uint64_t h = fnv_offset_basis;
  248. for (char const& c : s) {
  249. h = h ^ KWIML_INT_uint64_t(KWIML_INT_uint8_t(c));
  250. h = h * fnv_prime;
  251. }
  252. return result_type(h);
  253. }
  254. #else
  255. // Avoid empty translation unit.
  256. void cm_string_view_cxx()
  257. {
  258. }
  259. #endif