cmString.hxx 22 KB

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