cmString.hxx 25 KB

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