cmString.hxx 22 KB

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