cmString.hxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <algorithm>
  6. #include <cstddef>
  7. #include <initializer_list>
  8. #include <memory>
  9. #include <ostream>
  10. #include <string>
  11. #include <type_traits>
  12. #include <utility>
  13. #include <cm/string_view>
  14. #include <cmext/string_view>
  15. namespace cm {
  16. class String;
  17. /**
  18. * Trait to convert type T into a String.
  19. * Implementations must derive from 'std::true_type'
  20. * and define an 'into_string' member that accepts
  21. * type T (by value or reference) and returns one of:
  22. *
  23. * - 'std::string' to construct an owned instance.
  24. * - 'cm::string_view' to construct a borrowed or null instances.
  25. * The buffer from which the view is borrowed must outlive
  26. * all copies of the resulting String, e.g. static storage.
  27. * - 'cm::String' for already-constructed instances.
  28. */
  29. template <typename T>
  30. struct IntoString : std::false_type
  31. {
  32. };
  33. template <typename T>
  34. struct IntoString<T&> : IntoString<T>
  35. {
  36. };
  37. template <typename T>
  38. struct IntoString<T const> : IntoString<T>
  39. {
  40. };
  41. template <typename T>
  42. struct IntoString<T const*> : IntoString<T*>
  43. {
  44. };
  45. template <typename T, std::string::size_type N>
  46. struct IntoString<T const[N]> : IntoString<T[N]>
  47. {
  48. };
  49. template <>
  50. struct IntoString<char*> : std::true_type
  51. {
  52. static String into_string(char const* s);
  53. };
  54. template <>
  55. struct IntoString<std::nullptr_t> : std::true_type
  56. {
  57. static string_view into_string(std::nullptr_t) { return string_view(); }
  58. };
  59. template <std::string::size_type N>
  60. struct IntoString<char[N]> : std::true_type
  61. {
  62. static std::string into_string(char const (&s)[N])
  63. {
  64. return std::string(s, N - 1);
  65. }
  66. };
  67. template <>
  68. struct IntoString<std::string> : std::true_type
  69. {
  70. static std::string into_string(std::string s) { return s; }
  71. };
  72. template <>
  73. struct IntoString<char> : std::true_type
  74. {
  75. static std::string into_string(char c) { return std::string(1, c); }
  76. };
  77. /**
  78. * Trait to convert type T into a 'cm::string_view'.
  79. * Implementations must derive from 'std::true_type' and
  80. * define a 'view' member that accepts type T (by reference)
  81. * and returns a 'cm::string_view'.
  82. */
  83. template <typename T>
  84. struct AsStringView : std::false_type
  85. {
  86. };
  87. template <typename T>
  88. struct AsStringView<T&> : AsStringView<T>
  89. {
  90. };
  91. template <typename T>
  92. struct AsStringView<T const> : AsStringView<T>
  93. {
  94. };
  95. template <typename T>
  96. struct AsStringView<T const*> : AsStringView<T*>
  97. {
  98. };
  99. template <typename T, std::string::size_type N>
  100. struct AsStringView<T const[N]> : AsStringView<T[N]>
  101. {
  102. };
  103. template <>
  104. struct AsStringView<char*> : std::true_type
  105. {
  106. static string_view view(char const* s) { return s; }
  107. };
  108. template <std::string::size_type N>
  109. struct AsStringView<char[N]> : std::true_type
  110. {
  111. static string_view view(char const (&s)[N]) { return string_view(s, N - 1); }
  112. };
  113. template <>
  114. struct AsStringView<std::string> : std::true_type
  115. {
  116. static string_view view(std::string const& s) { return s; }
  117. };
  118. template <>
  119. struct AsStringView<char> : std::true_type
  120. {
  121. static string_view view(
  122. char const& s) // clazy:exclude=function-args-by-value
  123. {
  124. return string_view(&s, 1);
  125. }
  126. };
  127. template <>
  128. struct AsStringView<string_view> : std::true_type
  129. {
  130. static string_view view(string_view s) { return s; }
  131. };
  132. template <>
  133. struct AsStringView<static_string_view> : std::true_type
  134. {
  135. static string_view view(
  136. static_string_view const& s) // clazy:exclude=function-args-by-value
  137. {
  138. return s;
  139. }
  140. };
  141. template <>
  142. struct AsStringView<String> : std::true_type
  143. {
  144. static string_view view(String const& s);
  145. };
  146. /**
  147. * \class String
  148. *
  149. * A custom string type that holds a view of a string buffer
  150. * and optionally shares ownership of the buffer. Instances
  151. * may have one of the following states:
  152. *
  153. * - null: views and owns nothing.
  154. * Conversion to 'bool' is 'false'.
  155. * 'data()' and 'c_str()' return nullptr.
  156. * 'size()' returns 0.
  157. * 'str()' returns an empty string.
  158. *
  159. * - borrowed: views a string but does not own it. This is used
  160. * to bind to static storage (e.g. string literals) or for
  161. * temporary instances that do not outlive the borrowed buffer.
  162. * Copies and substrings still borrow the original buffer.
  163. * Mutation allocates a new internal string and converts to
  164. * the 'owned' state.
  165. * Conversion to 'bool' is 'true'.
  166. * 'c_str()' may internally mutate to the 'owned' state.
  167. * 'str()' internally mutates to the 'owned' state.
  168. *
  169. * - owned: views an immutable 'std::string' instance owned internally.
  170. * Copies and substrings share ownership of the internal string.
  171. * Mutation allocates a new internal string.
  172. * Conversion to 'bool' is 'true'.
  173. */
  174. class String
  175. {
  176. enum class Private
  177. {
  178. };
  179. public:
  180. using traits_type = std::string::traits_type;
  181. using value_type = string_view::value_type;
  182. using pointer = string_view::pointer;
  183. using const_pointer = string_view::const_pointer;
  184. using reference = string_view::reference;
  185. using const_reference = string_view::const_reference;
  186. using const_iterator = string_view::const_iterator;
  187. using iterator = string_view::const_iterator;
  188. using const_reverse_iterator = string_view::const_reverse_iterator;
  189. using reverse_iterator = string_view::const_reverse_iterator;
  190. using difference_type = string_view::difference_type;
  191. using size_type = string_view::size_type;
  192. static size_type const npos = string_view::npos;
  193. /** Construct a null string. */
  194. String() = default;
  195. /** Construct from any type implementing the IntoString trait. */
  196. template <typename T,
  197. typename = typename std::enable_if<IntoString<T>::value>::type>
  198. String(T&& s)
  199. : String(IntoString<T>::into_string(std::forward<T>(s)), Private())
  200. {
  201. }
  202. /**
  203. * Construct via static_string_view constructor.
  204. * explicit is required to avoid ambiguous overloaded operators (i.e ==,
  205. * etc...) with the ones provided by string_view.
  206. */
  207. explicit String(static_string_view s)
  208. : String(s, Private())
  209. {
  210. }
  211. /**
  212. * Construct via string_view constructor.
  213. * explicit is required to avoid ambiguous overloaded operators (i.e ==,
  214. * etc...) with the ones provided by string_view.
  215. */
  216. explicit String(string_view s)
  217. : String(std::string(s), Private())
  218. {
  219. }
  220. /** Construct via std::string initializer list constructor. */
  221. String(std::initializer_list<char> il)
  222. : String(std::string(il))
  223. {
  224. }
  225. /** Construct by copying the specified buffer. */
  226. String(char const* d, size_type s)
  227. : String(std::string(d, s))
  228. {
  229. }
  230. /** Construct by copying from input iterator range. */
  231. template <typename InputIterator>
  232. String(InputIterator first, InputIterator last)
  233. : String(std::string(first, last))
  234. {
  235. }
  236. /** Construct a string with 'n' copies of character 'c'. */
  237. String(size_type n, char c)
  238. : String(std::string(n, c))
  239. {
  240. }
  241. /** Construct from a substring of another String instance.
  242. This shares ownership of the other string's buffer
  243. but views only a substring. */
  244. String(String const& s, size_type pos, size_type count = npos)
  245. : string_(s.string_)
  246. , view_(s.data() + pos, std::min(count, s.size() - pos))
  247. {
  248. }
  249. /** Construct by moving from another String instance.
  250. The other instance is left as a null string. */
  251. String(String&& s) noexcept
  252. : string_(std::move(s.string_))
  253. , view_(s.view_)
  254. {
  255. s.view_ = string_view();
  256. }
  257. /** Construct by copying from another String instance.
  258. This shares ownership of the other string's buffer. */
  259. String(String const&) noexcept = default;
  260. ~String() = default;
  261. /** Construct by borrowing an externally-owned buffer. The buffer
  262. must outlive the returned instance and all copies of it. */
  263. static String borrow(string_view v) { return String(v, Private()); }
  264. /** Assign by moving from another String instance.
  265. The other instance is left as a null string. */
  266. String& operator=(String&& s) noexcept
  267. {
  268. this->string_ = std::move(s.string_);
  269. this->view_ = s.view_;
  270. s.view_ = string_view();
  271. return *this;
  272. }
  273. /** Assign by copying from another String instance.
  274. This shares ownership of the other string's buffer. */
  275. String& operator=(String const&) noexcept = default;
  276. String& operator=(static_string_view s)
  277. {
  278. *this = String(s);
  279. return *this;
  280. }
  281. String& operator=(string_view s)
  282. {
  283. *this = String(s);
  284. return *this;
  285. }
  286. /** Assign from any type implementing the IntoString trait. */
  287. template <typename T>
  288. typename // NOLINT(*)
  289. std::enable_if<IntoString<T>::value, String&>::type
  290. operator=(T&& s)
  291. {
  292. *this = String(std::forward<T>(s));
  293. return *this;
  294. }
  295. /** Assign via std::string initializer list constructor. */
  296. String& operator=(std::initializer_list<char> il)
  297. {
  298. *this = String(il);
  299. return *this;
  300. }
  301. /** Return true if the instance is not a null string. */
  302. explicit operator bool() const noexcept { return this->data() != nullptr; }
  303. /** Return a view of the string. */
  304. string_view view() const noexcept { return this->view_; }
  305. operator string_view() const noexcept { return this->view(); }
  306. /** Return true if the instance is an empty stringn or null string. */
  307. bool empty() const noexcept { return this->view_.empty(); }
  308. /** Return a pointer to the start of the string. */
  309. char const* data() const noexcept { return this->view_.data(); }
  310. /** Return the length of the string in bytes. */
  311. size_type size() const noexcept { return this->view_.size(); }
  312. size_type length() const noexcept { return this->view_.length(); }
  313. /** Return the character at the given position.
  314. No bounds checking is performed. */
  315. char operator[](size_type pos) const noexcept { return this->view_[pos]; }
  316. /** Return the character at the given position.
  317. If the position is out of bounds, throws std::out_of_range. */
  318. char at(size_type pos) const { return this->view_.at(pos); }
  319. char front() const noexcept { return this->view_.front(); }
  320. char back() const noexcept { return this->view_.back(); }
  321. /** Return true if this instance is stable and otherwise false.
  322. An instance is stable if it is in the 'null' state or if it is
  323. an 'owned' state not produced by substring operations, or
  324. after a call to 'stabilize()' or 'str()'. */
  325. bool is_stable() const;
  326. /** If 'is_stable()' does not return true, mutate so it does. */
  327. void stabilize();
  328. /** Get a pointer to a normal std::string if 'is_stable()' returns
  329. true and otherwise nullptr. The pointer is valid until this
  330. instance is mutated or destroyed. */
  331. std::string const* str_if_stable() const;
  332. /** Get a reference to a normal std::string. The reference
  333. is valid until this instance is mutated or destroyed. */
  334. std::string const& str();
  335. /** Get a pointer to a C-style null-terminated string
  336. containing the same value as this instance. The pointer
  337. is valid until this instance is mutated, destroyed,
  338. or str() is called. */
  339. char const* c_str();
  340. const_iterator begin() const noexcept { return this->view_.begin(); }
  341. const_iterator end() const noexcept { return this->view_.end(); }
  342. const_iterator cbegin() const noexcept { return this->begin(); }
  343. const_iterator cend() const noexcept { return this->end(); }
  344. const_reverse_iterator rbegin() const noexcept
  345. {
  346. return this->view_.rbegin();
  347. }
  348. const_reverse_iterator rend() const noexcept { return this->view_.rend(); }
  349. const_reverse_iterator crbegin() const noexcept { return this->rbegin(); }
  350. const_reverse_iterator crend() const noexcept { return this->rend(); }
  351. /** Append to the string using any type that implements the
  352. AsStringView trait. */
  353. template <typename T>
  354. typename std::enable_if<AsStringView<T>::value, String&>::type operator+=(
  355. T&& s)
  356. {
  357. string_view v = AsStringView<T>::view(std::forward<T>(s));
  358. std::string r;
  359. r.reserve(this->size() + v.size());
  360. r.assign(this->data(), this->size());
  361. r.append(v.data(), v.size());
  362. return *this = std::move(r);
  363. }
  364. template <typename T>
  365. typename std::enable_if<AsStringView<T>::value, String&>::type append(T&& s)
  366. {
  367. string_view v = AsStringView<T>::view(std::forward<T>(s));
  368. std::string r;
  369. r.reserve(this->size() + v.size());
  370. r.assign(this->data(), this->size());
  371. r.append(v.data(), v.size());
  372. return *this = std::move(r);
  373. }
  374. /** Assign to an empty string. */
  375. void clear() { *this = ""_s; }
  376. /** Insert 'count' copies of 'ch' at position 'index'. */
  377. String& insert(size_type index, size_type count, char ch);
  378. /** Insert into the string using any type that implements the
  379. AsStringView trait. */
  380. template <typename T>
  381. typename std::enable_if<AsStringView<T>::value, String&>::type insert(
  382. size_type index, T&& s)
  383. {
  384. string_view v = AsStringView<T>::view(std::forward<T>(s));
  385. std::string r;
  386. r.reserve(this->size() + v.size());
  387. r.assign(this->data(), this->size());
  388. r.insert(index, v.data(), v.size());
  389. return *this = std::move(r);
  390. }
  391. /** Erase 'count' characters starting at position 'index'. */
  392. String& erase(size_type index = 0, size_type count = npos);
  393. void push_back(char ch)
  394. {
  395. std::string s;
  396. s.reserve(this->size() + 1);
  397. s.assign(this->data(), this->size());
  398. s.push_back(ch);
  399. *this = std::move(s);
  400. }
  401. void pop_back() { *this = String(*this, 0, this->size() - 1); }
  402. template <typename T>
  403. typename std::enable_if<AsStringView<T>::value, String&>::type replace(
  404. size_type pos, size_type count, T&& s)
  405. {
  406. const_iterator first = this->begin() + pos;
  407. const_iterator last = first + count;
  408. return this->replace(first, last, std::forward<T>(s));
  409. }
  410. template <typename InputIterator>
  411. String& replace(const_iterator first, const_iterator last,
  412. InputIterator first2, InputIterator last2)
  413. {
  414. std::string out;
  415. out.append(this->view_.begin(), first);
  416. out.append(first2, last2);
  417. out.append(last, this->view_.end());
  418. return *this = std::move(out);
  419. }
  420. template <typename T>
  421. typename std::enable_if<AsStringView<T>::value, String&>::type replace(
  422. const_iterator first, const_iterator last, T&& s)
  423. {
  424. string_view v = AsStringView<T>::view(std::forward<T>(s));
  425. std::string out;
  426. out.reserve((first - this->view_.begin()) + v.size() +
  427. (this->view_.end() - last));
  428. out.append(this->view_.begin(), first);
  429. out.append(v.data(), v.size());
  430. out.append(last, this->view_.end());
  431. return *this = std::move(out);
  432. }
  433. template <typename T>
  434. typename std::enable_if<AsStringView<T>::value, String&>::type replace(
  435. size_type pos, size_type count, T&& s, size_type pos2,
  436. size_type count2 = npos)
  437. {
  438. string_view v = AsStringView<T>::view(std::forward<T>(s));
  439. v = v.substr(pos2, count2);
  440. return this->replace(pos, count, v);
  441. }
  442. String& replace(size_type pos, size_type count, size_type count2, char ch)
  443. {
  444. const_iterator first = this->begin() + pos;
  445. const_iterator last = first + count;
  446. return this->replace(first, last, count2, ch);
  447. }
  448. String& replace(const_iterator first, const_iterator last, size_type count2,
  449. char ch)
  450. {
  451. std::string out;
  452. out.reserve(static_cast<size_type>(first - this->view_.begin()) + count2 +
  453. static_cast<size_type>(this->view_.end() - last));
  454. out.append(this->view_.begin(), first);
  455. out.append(count2, ch);
  456. out.append(last, this->view_.end());
  457. return *this = std::move(out);
  458. }
  459. size_type copy(char* dest, size_type count, size_type pos = 0) const;
  460. void resize(size_type count) { this->resize(count, char()); }
  461. void resize(size_type count, char ch)
  462. {
  463. std::string s;
  464. s.reserve(count);
  465. if (count <= this->size()) {
  466. s.assign(this->data(), count);
  467. } else {
  468. s.assign(this->data(), this->size());
  469. s.resize(count, ch);
  470. }
  471. *this = std::move(s);
  472. }
  473. void swap(String& other) noexcept
  474. {
  475. std::swap(this->string_, other.string_);
  476. std::swap(this->view_, other.view_);
  477. }
  478. /** Return a substring starting at position 'pos' and
  479. consisting of at most 'count' characters. */
  480. String substr(size_type pos = 0, size_type count = npos) const;
  481. template <typename T>
  482. typename std::enable_if<AsStringView<T>::value, int>::type compare(
  483. T&& s) const
  484. {
  485. return this->view_.compare(AsStringView<T>::view(std::forward<T>(s)));
  486. }
  487. int compare(size_type pos1, size_type count1, string_view v) const
  488. {
  489. return this->view_.compare(pos1, count1, v);
  490. }
  491. int compare(size_type pos1, size_type count1, string_view v, size_type pos2,
  492. size_type count2) const
  493. {
  494. return this->view_.compare(pos1, count1, v, pos2, count2);
  495. }
  496. int compare(size_type pos1, size_type count1, char const* s) const
  497. {
  498. return this->view_.compare(pos1, count1, s);
  499. }
  500. int compare(size_type pos1, size_type count1, char const* s,
  501. size_type count2) const
  502. {
  503. return this->view_.compare(pos1, count1, s, count2);
  504. }
  505. template <typename T>
  506. typename std::enable_if<AsStringView<T>::value, size_type>::type find(
  507. T&& s, size_type pos = 0) const
  508. {
  509. string_view v = AsStringView<T>::view(std::forward<T>(s));
  510. return this->view_.find(v, pos);
  511. }
  512. size_type find(char const* s, size_type pos, size_type count) const
  513. {
  514. return this->view_.find(s, pos, count);
  515. }
  516. template <typename T>
  517. typename std::enable_if<AsStringView<T>::value, size_type>::type rfind(
  518. T&& s, size_type pos = npos) const
  519. {
  520. string_view v = AsStringView<T>::view(std::forward<T>(s));
  521. return this->view_.rfind(v, pos);
  522. }
  523. size_type rfind(char const* s, size_type pos, size_type count) const
  524. {
  525. return this->view_.rfind(s, pos, count);
  526. }
  527. template <typename T>
  528. typename std::enable_if<AsStringView<T>::value, size_type>::type
  529. find_first_of(T&& s, size_type pos = 0) const
  530. {
  531. string_view v = AsStringView<T>::view(std::forward<T>(s));
  532. return this->view_.find_first_of(v, pos);
  533. }
  534. size_type find_first_of(char const* s, size_type pos, size_type count) const
  535. {
  536. return this->view_.find_first_of(s, pos, count);
  537. }
  538. template <typename T>
  539. typename std::enable_if<AsStringView<T>::value, size_type>::type
  540. find_first_not_of(T&& s, size_type pos = 0) const
  541. {
  542. string_view v = AsStringView<T>::view(std::forward<T>(s));
  543. return this->view_.find_first_not_of(v, pos);
  544. }
  545. size_type find_first_not_of(char const* s, size_type pos,
  546. size_type count) const
  547. {
  548. return this->view_.find_first_not_of(s, pos, count);
  549. }
  550. template <typename T>
  551. typename std::enable_if<AsStringView<T>::value, size_type>::type
  552. find_last_of(T&& s, size_type pos = npos) const
  553. {
  554. string_view v = AsStringView<T>::view(std::forward<T>(s));
  555. return this->view_.find_last_of(v, pos);
  556. }
  557. size_type find_last_of(char const* s, size_type pos, size_type count) const
  558. {
  559. return this->view_.find_last_of(s, pos, count);
  560. }
  561. template <typename T>
  562. typename std::enable_if<AsStringView<T>::value, size_type>::type
  563. find_last_not_of(T&& s, size_type pos = npos) const
  564. {
  565. string_view v = AsStringView<T>::view(std::forward<T>(s));
  566. return this->view_.find_last_not_of(v, pos);
  567. }
  568. size_type find_last_not_of(char const* s, size_type pos,
  569. size_type count) const
  570. {
  571. return this->view_.find_last_not_of(s, pos, count);
  572. }
  573. private:
  574. // Internal constructor to move from existing String.
  575. String(String&& s, Private) noexcept
  576. : String(std::move(s))
  577. {
  578. }
  579. // Internal constructor for dynamically allocated string.
  580. String(std::string&& s, Private);
  581. // Internal constructor for view of statically allocated string.
  582. String(string_view v, Private)
  583. : view_(v)
  584. {
  585. }
  586. void internally_mutate_to_stable_string();
  587. std::shared_ptr<std::string const> string_;
  588. string_view view_;
  589. };
  590. /**
  591. * Trait for comparable types.
  592. */
  593. template <typename T>
  594. struct IsComparable : std::false_type
  595. {
  596. };
  597. template <typename T>
  598. struct IsComparable<T&> : IsComparable<T>
  599. {
  600. };
  601. template <typename T>
  602. struct IsComparable<T const> : IsComparable<T>
  603. {
  604. };
  605. template <typename T>
  606. struct IsComparable<T const*> : IsComparable<T*>
  607. {
  608. };
  609. template <typename T, std::string::size_type N>
  610. struct IsComparable<T const[N]> : IsComparable<T[N]>
  611. {
  612. };
  613. template <>
  614. struct IsComparable<char*> : std::true_type
  615. {
  616. };
  617. template <std::string::size_type N>
  618. struct IsComparable<char[N]> : std::true_type
  619. {
  620. };
  621. template <>
  622. struct IsComparable<std::string> : std::true_type
  623. {
  624. };
  625. template <>
  626. struct IsComparable<char> : std::true_type
  627. {
  628. };
  629. /** comparison operators */
  630. inline bool operator==(String const& l, String const& r)
  631. {
  632. return l.view() == r.view();
  633. }
  634. template <typename L>
  635. typename std::enable_if<IsComparable<L>::value, bool>::type operator==(
  636. L&& l, String const& r)
  637. {
  638. return AsStringView<L>::view(std::forward<L>(l)) == r.view();
  639. }
  640. template <typename R>
  641. typename std::enable_if<IsComparable<R>::value, bool>::type operator==(
  642. String const& l, R&& r)
  643. {
  644. return l.view() == AsStringView<R>::view(std::forward<R>(r));
  645. }
  646. inline bool operator!=(String const& l, String const& r)
  647. {
  648. return l.view() != r.view();
  649. }
  650. template <typename L>
  651. typename std::enable_if<IsComparable<L>::value, bool>::type operator!=(
  652. L&& l, String const& r)
  653. {
  654. return AsStringView<L>::view(std::forward<L>(l)) != r.view();
  655. }
  656. template <typename R>
  657. typename std::enable_if<IsComparable<R>::value, bool>::type operator!=(
  658. String const& l, R&& r)
  659. {
  660. return l.view() != AsStringView<R>::view(std::forward<R>(r));
  661. }
  662. inline bool operator<(String const& l, String const& r)
  663. {
  664. return l.view() < r.view();
  665. }
  666. template <typename L>
  667. typename std::enable_if<IsComparable<L>::value, bool>::type operator<(
  668. L&& l, String const& r)
  669. {
  670. return AsStringView<L>::view(std::forward<L>(l)) < r.view();
  671. }
  672. template <typename R>
  673. typename std::enable_if<IsComparable<R>::value, bool>::type operator<(
  674. String const& l, R&& r)
  675. {
  676. return l.view() < AsStringView<R>::view(std::forward<R>(r));
  677. }
  678. inline bool operator<=(String const& l, String const& r)
  679. {
  680. return l.view() <= r.view();
  681. }
  682. template <typename L>
  683. typename std::enable_if<IsComparable<L>::value, bool>::type operator<=(
  684. L&& l, String const& r)
  685. {
  686. return AsStringView<L>::view(std::forward<L>(l)) <= r.view();
  687. }
  688. template <typename R>
  689. typename std::enable_if<IsComparable<R>::value, bool>::type operator<=(
  690. String const& l, R&& r)
  691. {
  692. return l.view() <= AsStringView<R>::view(std::forward<R>(r));
  693. }
  694. inline bool operator>(String const& l, String const& r)
  695. {
  696. return l.view() > r.view();
  697. }
  698. template <typename L>
  699. typename std::enable_if<IsComparable<L>::value, bool>::type operator>(
  700. L&& l, String const& r)
  701. {
  702. return AsStringView<L>::view(std::forward<L>(l)) > r.view();
  703. }
  704. template <typename R>
  705. typename std::enable_if<IsComparable<R>::value, bool>::type operator>(
  706. String const& l, R&& r)
  707. {
  708. return l.view() > AsStringView<R>::view(std::forward<R>(r));
  709. }
  710. inline bool operator>=(String const& l, String const& r)
  711. {
  712. return l.view() >= r.view();
  713. }
  714. template <typename L>
  715. typename std::enable_if<IsComparable<L>::value, bool>::type operator>=(
  716. L&& l, String const& r)
  717. {
  718. return AsStringView<L>::view(std::forward<L>(l)) >= r.view();
  719. }
  720. template <typename R>
  721. typename std::enable_if<IsComparable<R>::value, bool>::type operator>=(
  722. String const& l, R&& r)
  723. {
  724. return l.view() >= AsStringView<R>::view(std::forward<R>(r));
  725. }
  726. std::ostream& operator<<(std::ostream& os, String const& s);
  727. std::string& operator+=(std::string& self, String const& s);
  728. template <typename L, typename R>
  729. struct StringOpPlus
  730. {
  731. L l;
  732. R r;
  733. #if defined(__SUNPRO_CC)
  734. StringOpPlus(L in_l, R in_r)
  735. : l(in_l)
  736. , r(in_r)
  737. {
  738. }
  739. #endif
  740. operator std::string() const;
  741. std::string::size_type size() const
  742. {
  743. return this->l.size() + this->r.size();
  744. }
  745. };
  746. template <typename T>
  747. struct StringAdd
  748. {
  749. static bool const value = AsStringView<T>::value;
  750. using temp_type = string_view;
  751. template <typename S>
  752. static temp_type temp(S&& s)
  753. {
  754. return AsStringView<T>::view(std::forward<S>(s));
  755. }
  756. };
  757. template <typename L, typename R>
  758. struct StringAdd<StringOpPlus<L, R>> : std::true_type
  759. {
  760. using temp_type = StringOpPlus<L, R> const&;
  761. static temp_type temp(temp_type s) { return s; }
  762. };
  763. template <typename L, typename R>
  764. StringOpPlus<L, R>::operator std::string() const
  765. {
  766. std::string s;
  767. s.reserve(this->size());
  768. s += *this;
  769. return s;
  770. }
  771. template <typename L, typename R>
  772. std::string& operator+=(std::string& s, StringOpPlus<L, R> const& a)
  773. {
  774. s.reserve(s.size() + a.size());
  775. s += a.l;
  776. s += a.r;
  777. return s;
  778. }
  779. template <typename L, typename R>
  780. String& operator+=(String& s, StringOpPlus<L, R> const& a)
  781. {
  782. std::string r;
  783. r.reserve(s.size() + a.size());
  784. r.assign(s.data(), s.size());
  785. r += a.l;
  786. r += a.r;
  787. s = std::move(r);
  788. return s;
  789. }
  790. template <typename L, typename R>
  791. std::ostream& operator<<(std::ostream& os, StringOpPlus<L, R> const& a)
  792. {
  793. return os << a.l << a.r;
  794. }
  795. template <typename L, typename R>
  796. struct IntoString<StringOpPlus<L, R>> : std::true_type
  797. {
  798. static std::string into_string(StringOpPlus<L, R> const& a) { return a; }
  799. };
  800. template <typename L, typename R>
  801. typename std::enable_if<StringAdd<L>::value && StringAdd<R>::value,
  802. StringOpPlus<typename StringAdd<L>::temp_type,
  803. typename StringAdd<R>::temp_type>>::type
  804. operator+(L&& l, R&& r)
  805. {
  806. return { StringAdd<L>::temp(std::forward<L>(l)),
  807. StringAdd<R>::temp(std::forward<R>(r)) };
  808. }
  809. template <typename LL, typename LR, typename R>
  810. typename std::enable_if<AsStringView<R>::value, bool>::type operator==(
  811. StringOpPlus<LL, LR> const& l, R&& r)
  812. {
  813. return std::string(l) == AsStringView<R>::view(std::forward<R>(r));
  814. }
  815. template <typename L, typename RL, typename RR>
  816. typename std::enable_if<AsStringView<L>::value, bool>::type operator==(
  817. L&& l, StringOpPlus<RL, RR> const& r)
  818. {
  819. return AsStringView<L>::view(std::forward<L>(l)) == std::string(r);
  820. }
  821. } // namespace cm
  822. namespace std {
  823. template <>
  824. struct hash<cm::String>
  825. {
  826. using argument_type = cm::String;
  827. using result_type = size_t;
  828. result_type operator()(argument_type const& s) const noexcept
  829. {
  830. result_type const h(std::hash<cm::string_view>{}(s.view()));
  831. return h;
  832. }
  833. };
  834. }