string_ref.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. Copyright (c) Marshall Clow 2012-2015.
  3. Copyright (c) Glen Joseph Fernandes 2019 ([email protected])
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. For more information, see http://www.boost.org
  7. Based on the StringRef implementation in LLVM (http://llvm.org) and
  8. N3422 by Jeffrey Yasskin
  9. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
  10. */
  11. #ifndef BOOST_STRING_REF_HPP
  12. #define BOOST_STRING_REF_HPP
  13. #include <boost/config.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. #include <boost/utility/ostream_string.hpp>
  16. #include <boost/utility/string_ref_fwd.hpp>
  17. #include <boost/throw_exception.hpp>
  18. #include <cstddef>
  19. #include <stdexcept>
  20. #include <algorithm>
  21. #include <iterator>
  22. #include <string>
  23. #include <iosfwd>
  24. #if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || (defined(BOOST_GCC) && ((BOOST_GCC+0) / 100) <= 406)
  25. // GCC 4.6 cannot handle a defaulted function with noexcept specifier
  26. #define BOOST_STRING_REF_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
  27. #endif
  28. namespace boost {
  29. namespace detail {
  30. // A helper functor because sometimes we don't have lambdas
  31. template <typename charT, typename traits>
  32. class string_ref_traits_eq {
  33. public:
  34. string_ref_traits_eq ( charT ch ) : ch_(ch) {}
  35. bool operator () ( charT val ) const { return traits::eq ( ch_, val ); }
  36. charT ch_;
  37. };
  38. }
  39. template<typename charT, typename traits>
  40. class basic_string_ref {
  41. public:
  42. // types
  43. typedef charT value_type;
  44. typedef const charT* pointer;
  45. typedef const charT& reference;
  46. typedef const charT& const_reference;
  47. typedef pointer const_iterator; // impl-defined
  48. typedef const_iterator iterator;
  49. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  50. typedef const_reverse_iterator reverse_iterator;
  51. typedef std::size_t size_type;
  52. typedef std::ptrdiff_t difference_type;
  53. static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1);
  54. // construct/copy
  55. BOOST_CONSTEXPR basic_string_ref () BOOST_NOEXCEPT
  56. : ptr_(NULL), len_(0) {}
  57. // by defaulting these functions, basic_string_ref becomes
  58. // trivially copy/move constructible.
  59. BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) BOOST_NOEXCEPT
  60. #ifndef BOOST_STRING_REF_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
  61. = default;
  62. #else
  63. : ptr_(rhs.ptr_), len_(rhs.len_) {}
  64. #endif
  65. basic_string_ref& operator=(const basic_string_ref &rhs) BOOST_NOEXCEPT
  66. #ifndef BOOST_STRING_REF_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
  67. = default;
  68. #else
  69. {
  70. ptr_ = rhs.ptr_;
  71. len_ = rhs.len_;
  72. return *this;
  73. }
  74. #endif
  75. basic_string_ref(const charT* str) BOOST_NOEXCEPT
  76. : ptr_(str), len_(traits::length(str)) {}
  77. template<typename Allocator>
  78. basic_string_ref(const std::basic_string<charT, traits, Allocator>& str)
  79. : ptr_(str.data()), len_(str.length()) {}
  80. // #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  81. // // Constructing a string_ref from a temporary string is a bad idea
  82. // template<typename Allocator>
  83. // basic_string_ref( std::basic_string<charT, traits, Allocator>&&)
  84. // = delete;
  85. // #endif
  86. BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) BOOST_NOEXCEPT
  87. : ptr_(str), len_(len) {}
  88. #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  89. template<typename Allocator>
  90. explicit operator std::basic_string<charT, traits, Allocator>() const {
  91. return std::basic_string<charT, traits, Allocator> ( begin(), end());
  92. }
  93. #endif
  94. std::basic_string<charT, traits> to_string () const {
  95. return std::basic_string<charT, traits> ( begin(), end());
  96. }
  97. // iterators
  98. BOOST_CONSTEXPR const_iterator begin() const { return ptr_; }
  99. BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; }
  100. BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; }
  101. BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; }
  102. const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); }
  103. const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); }
  104. const_reverse_iterator rend() const { return const_reverse_iterator (begin()); }
  105. const_reverse_iterator crend() const { return const_reverse_iterator (begin()); }
  106. // capacity
  107. BOOST_CONSTEXPR size_type size() const { return len_; }
  108. BOOST_CONSTEXPR size_type length() const { return len_; }
  109. BOOST_CONSTEXPR size_type max_size() const { return len_; }
  110. BOOST_CONSTEXPR bool empty() const { return len_ == 0; }
  111. // element access
  112. BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; }
  113. const charT& at(size_t pos) const {
  114. if ( pos >= len_ )
  115. BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) );
  116. return ptr_[pos];
  117. }
  118. BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; }
  119. BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; }
  120. BOOST_CONSTEXPR const charT* data() const { return ptr_; }
  121. // modifiers
  122. void clear() { len_ = 0; }
  123. void remove_prefix(size_type n) {
  124. if ( n > len_ )
  125. n = len_;
  126. ptr_ += n;
  127. len_ -= n;
  128. }
  129. void remove_suffix(size_type n) {
  130. if ( n > len_ )
  131. n = len_;
  132. len_ -= n;
  133. }
  134. // basic_string_ref string operations
  135. basic_string_ref substr(size_type pos, size_type n=npos) const {
  136. if ( pos > size())
  137. BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
  138. return basic_string_ref(data() + pos, (std::min)(size() - pos, n));
  139. }
  140. int compare(basic_string_ref x) const {
  141. const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_));
  142. return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 );
  143. }
  144. bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); }
  145. bool starts_with(basic_string_ref x) const {
  146. return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0;
  147. }
  148. bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); }
  149. bool ends_with(basic_string_ref x) const {
  150. return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0;
  151. }
  152. size_type find(basic_string_ref s) const {
  153. if (s.empty()) return 0;
  154. const_iterator iter = std::search ( this->cbegin (), this->cend (),
  155. s.cbegin (), s.cend (), traits::eq );
  156. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  157. }
  158. size_type find(charT c) const {
  159. const_iterator iter = std::find_if ( this->cbegin (), this->cend (),
  160. detail::string_ref_traits_eq<charT, traits> ( c ));
  161. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  162. }
  163. size_type rfind(basic_string_ref s) const {
  164. if (s.empty()) return 0;
  165. const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
  166. s.crbegin (), s.crend (), traits::eq );
  167. return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size());
  168. }
  169. size_type rfind(charT c) const {
  170. const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (),
  171. detail::string_ref_traits_eq<charT, traits> ( c ));
  172. return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
  173. }
  174. size_type find_first_of(charT c) const { return find (c); }
  175. size_type find_last_of (charT c) const { return rfind (c); }
  176. size_type find_first_of(basic_string_ref s) const {
  177. const_iterator iter = std::find_first_of
  178. ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq );
  179. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  180. }
  181. size_type find_last_of(basic_string_ref s) const {
  182. const_reverse_iterator iter = std::find_first_of
  183. ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq );
  184. return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
  185. }
  186. size_type find_first_not_of(basic_string_ref s) const {
  187. const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s );
  188. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  189. }
  190. size_type find_first_not_of(charT c) const {
  191. for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter )
  192. if ( !traits::eq ( c, *iter ))
  193. return std::distance ( this->cbegin (), iter );
  194. return npos;
  195. }
  196. size_type find_last_not_of(basic_string_ref s) const {
  197. const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s );
  198. return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
  199. }
  200. size_type find_last_not_of(charT c) const {
  201. for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter )
  202. if ( !traits::eq ( c, *iter ))
  203. return this->size() - 1 - std::distance(this->crbegin(), iter);
  204. return npos;
  205. }
  206. private:
  207. template <typename Iterator>
  208. Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const {
  209. for ( ; first != last ; ++first )
  210. if ( 0 == traits::find ( s.ptr_, s.len_, *first ))
  211. return first;
  212. return last;
  213. }
  214. const charT *ptr_;
  215. std::size_t len_;
  216. };
  217. // Comparison operators
  218. // Equality
  219. template<typename charT, typename traits>
  220. inline bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  221. if ( x.size () != y.size ()) return false;
  222. return x.compare(y) == 0;
  223. }
  224. template<typename charT, typename traits, typename Allocator>
  225. inline bool operator==(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  226. return x == basic_string_ref<charT, traits>(y);
  227. }
  228. template<typename charT, typename traits, typename Allocator>
  229. inline bool operator==(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  230. return basic_string_ref<charT, traits>(x) == y;
  231. }
  232. template<typename charT, typename traits>
  233. inline bool operator==(basic_string_ref<charT, traits> x, const charT * y) {
  234. return x == basic_string_ref<charT, traits>(y);
  235. }
  236. template<typename charT, typename traits>
  237. inline bool operator==(const charT * x, basic_string_ref<charT, traits> y) {
  238. return basic_string_ref<charT, traits>(x) == y;
  239. }
  240. // Inequality
  241. template<typename charT, typename traits>
  242. inline bool operator!=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  243. if ( x.size () != y.size ()) return true;
  244. return x.compare(y) != 0;
  245. }
  246. template<typename charT, typename traits, typename Allocator>
  247. inline bool operator!=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  248. return x != basic_string_ref<charT, traits>(y);
  249. }
  250. template<typename charT, typename traits, typename Allocator>
  251. inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  252. return basic_string_ref<charT, traits>(x) != y;
  253. }
  254. template<typename charT, typename traits>
  255. inline bool operator!=(basic_string_ref<charT, traits> x, const charT * y) {
  256. return x != basic_string_ref<charT, traits>(y);
  257. }
  258. template<typename charT, typename traits>
  259. inline bool operator!=(const charT * x, basic_string_ref<charT, traits> y) {
  260. return basic_string_ref<charT, traits>(x) != y;
  261. }
  262. // Less than
  263. template<typename charT, typename traits>
  264. inline bool operator<(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  265. return x.compare(y) < 0;
  266. }
  267. template<typename charT, typename traits, typename Allocator>
  268. inline bool operator<(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  269. return x < basic_string_ref<charT, traits>(y);
  270. }
  271. template<typename charT, typename traits, typename Allocator>
  272. inline bool operator<(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  273. return basic_string_ref<charT, traits>(x) < y;
  274. }
  275. template<typename charT, typename traits>
  276. inline bool operator<(basic_string_ref<charT, traits> x, const charT * y) {
  277. return x < basic_string_ref<charT, traits>(y);
  278. }
  279. template<typename charT, typename traits>
  280. inline bool operator<(const charT * x, basic_string_ref<charT, traits> y) {
  281. return basic_string_ref<charT, traits>(x) < y;
  282. }
  283. // Greater than
  284. template<typename charT, typename traits>
  285. inline bool operator>(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  286. return x.compare(y) > 0;
  287. }
  288. template<typename charT, typename traits, typename Allocator>
  289. inline bool operator>(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  290. return x > basic_string_ref<charT, traits>(y);
  291. }
  292. template<typename charT, typename traits, typename Allocator>
  293. inline bool operator>(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  294. return basic_string_ref<charT, traits>(x) > y;
  295. }
  296. template<typename charT, typename traits>
  297. inline bool operator>(basic_string_ref<charT, traits> x, const charT * y) {
  298. return x > basic_string_ref<charT, traits>(y);
  299. }
  300. template<typename charT, typename traits>
  301. inline bool operator>(const charT * x, basic_string_ref<charT, traits> y) {
  302. return basic_string_ref<charT, traits>(x) > y;
  303. }
  304. // Less than or equal to
  305. template<typename charT, typename traits>
  306. inline bool operator<=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  307. return x.compare(y) <= 0;
  308. }
  309. template<typename charT, typename traits, typename Allocator>
  310. inline bool operator<=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  311. return x <= basic_string_ref<charT, traits>(y);
  312. }
  313. template<typename charT, typename traits, typename Allocator>
  314. inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  315. return basic_string_ref<charT, traits>(x) <= y;
  316. }
  317. template<typename charT, typename traits>
  318. inline bool operator<=(basic_string_ref<charT, traits> x, const charT * y) {
  319. return x <= basic_string_ref<charT, traits>(y);
  320. }
  321. template<typename charT, typename traits>
  322. inline bool operator<=(const charT * x, basic_string_ref<charT, traits> y) {
  323. return basic_string_ref<charT, traits>(x) <= y;
  324. }
  325. // Greater than or equal to
  326. template<typename charT, typename traits>
  327. inline bool operator>=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  328. return x.compare(y) >= 0;
  329. }
  330. template<typename charT, typename traits, typename Allocator>
  331. inline bool operator>=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  332. return x >= basic_string_ref<charT, traits>(y);
  333. }
  334. template<typename charT, typename traits, typename Allocator>
  335. inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  336. return basic_string_ref<charT, traits>(x) >= y;
  337. }
  338. template<typename charT, typename traits>
  339. inline bool operator>=(basic_string_ref<charT, traits> x, const charT * y) {
  340. return x >= basic_string_ref<charT, traits>(y);
  341. }
  342. template<typename charT, typename traits>
  343. inline bool operator>=(const charT * x, basic_string_ref<charT, traits> y) {
  344. return basic_string_ref<charT, traits>(x) >= y;
  345. }
  346. // Inserter
  347. template<class charT, class traits>
  348. inline std::basic_ostream<charT, traits>&
  349. operator<<(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
  350. return boost::ostream_string(os, str.data(), str.size());
  351. }
  352. #if 0
  353. // numeric conversions
  354. //
  355. // These are short-term implementations.
  356. // In a production environment, I would rather avoid the copying.
  357. //
  358. inline int stoi (string_ref str, size_t* idx=0, int base=10) {
  359. return std::stoi ( std::string(str), idx, base );
  360. }
  361. inline long stol (string_ref str, size_t* idx=0, int base=10) {
  362. return std::stol ( std::string(str), idx, base );
  363. }
  364. inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
  365. return std::stoul ( std::string(str), idx, base );
  366. }
  367. inline long long stoll (string_ref str, size_t* idx=0, int base=10) {
  368. return std::stoll ( std::string(str), idx, base );
  369. }
  370. inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) {
  371. return std::stoull ( std::string(str), idx, base );
  372. }
  373. inline float stof (string_ref str, size_t* idx=0) {
  374. return std::stof ( std::string(str), idx );
  375. }
  376. inline double stod (string_ref str, size_t* idx=0) {
  377. return std::stod ( std::string(str), idx );
  378. }
  379. inline long double stold (string_ref str, size_t* idx=0) {
  380. return std::stold ( std::string(str), idx );
  381. }
  382. inline int stoi (wstring_ref str, size_t* idx=0, int base=10) {
  383. return std::stoi ( std::wstring(str), idx, base );
  384. }
  385. inline long stol (wstring_ref str, size_t* idx=0, int base=10) {
  386. return std::stol ( std::wstring(str), idx, base );
  387. }
  388. inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
  389. return std::stoul ( std::wstring(str), idx, base );
  390. }
  391. inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
  392. return std::stoll ( std::wstring(str), idx, base );
  393. }
  394. inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) {
  395. return std::stoull ( std::wstring(str), idx, base );
  396. }
  397. inline float stof (wstring_ref str, size_t* idx=0) {
  398. return std::stof ( std::wstring(str), idx );
  399. }
  400. inline double stod (wstring_ref str, size_t* idx=0) {
  401. return std::stod ( std::wstring(str), idx );
  402. }
  403. inline long double stold (wstring_ref str, size_t* idx=0) {
  404. return std::stold ( std::wstring(str), idx );
  405. }
  406. #endif
  407. }
  408. #if 0
  409. namespace std {
  410. // Hashing
  411. template<> struct hash<boost::string_ref>;
  412. template<> struct hash<boost::u16string_ref>;
  413. template<> struct hash<boost::u32string_ref>;
  414. template<> struct hash<boost::wstring_ref>;
  415. }
  416. #endif
  417. #endif