string_ref.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. Copyright (c) Marshall Clow 2012-2015.
  3. Copyright (c) Glen Joseph Fernandes 2019 (glenjofe@gmail.com)
  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/io/ostream_put.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 ~static_cast<size_type>(0) / (sizeof(value_type) * 2u); }
  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_type 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() const {
  136. return basic_string_ref(data(), size());
  137. }
  138. basic_string_ref substr(size_type pos, size_type n=npos) const {
  139. if ( pos > size())
  140. BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
  141. return basic_string_ref(data() + pos, (std::min)(size() - pos, n));
  142. }
  143. int compare(basic_string_ref x) const {
  144. const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_));
  145. return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 );
  146. }
  147. bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); }
  148. bool starts_with(basic_string_ref x) const {
  149. return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0;
  150. }
  151. bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); }
  152. bool ends_with(basic_string_ref x) const {
  153. return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0;
  154. }
  155. size_type find(basic_string_ref s) const {
  156. if (s.empty()) return 0;
  157. const_iterator iter = std::search ( this->cbegin (), this->cend (),
  158. s.cbegin (), s.cend (), traits::eq );
  159. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  160. }
  161. size_type find(charT c) const {
  162. const_iterator iter = std::find_if ( this->cbegin (), this->cend (),
  163. detail::string_ref_traits_eq<charT, traits> ( c ));
  164. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  165. }
  166. size_type rfind(basic_string_ref s) const {
  167. if (s.empty()) return 0;
  168. const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
  169. s.crbegin (), s.crend (), traits::eq );
  170. return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size());
  171. }
  172. size_type rfind(charT c) const {
  173. const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (),
  174. detail::string_ref_traits_eq<charT, traits> ( c ));
  175. return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
  176. }
  177. size_type find_first_of(charT c) const { return find (c); }
  178. size_type find_last_of (charT c) const { return rfind (c); }
  179. size_type find_first_of(basic_string_ref s) const {
  180. const_iterator iter = std::find_first_of
  181. ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq );
  182. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  183. }
  184. size_type find_last_of(basic_string_ref s) const {
  185. const_reverse_iterator iter = std::find_first_of
  186. ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq );
  187. return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
  188. }
  189. size_type find_first_not_of(basic_string_ref s) const {
  190. const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s );
  191. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  192. }
  193. size_type find_first_not_of(charT c) const {
  194. for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter )
  195. if ( !traits::eq ( c, *iter ))
  196. return std::distance ( this->cbegin (), iter );
  197. return npos;
  198. }
  199. size_type find_last_not_of(basic_string_ref s) const {
  200. const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s );
  201. return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
  202. }
  203. size_type find_last_not_of(charT c) const {
  204. for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter )
  205. if ( !traits::eq ( c, *iter ))
  206. return this->size() - 1 - std::distance(this->crbegin(), iter);
  207. return npos;
  208. }
  209. private:
  210. template <typename Iterator>
  211. Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const {
  212. for ( ; first != last ; ++first )
  213. if ( 0 == traits::find ( s.ptr_, s.len_, *first ))
  214. return first;
  215. return last;
  216. }
  217. const charT *ptr_;
  218. std::size_t len_;
  219. };
  220. // Comparison operators
  221. // Equality
  222. template<typename charT, typename traits>
  223. inline bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  224. if ( x.size () != y.size ()) return false;
  225. return x.compare(y) == 0;
  226. }
  227. template<typename charT, typename traits, typename Allocator>
  228. inline bool operator==(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  229. return x == basic_string_ref<charT, traits>(y);
  230. }
  231. template<typename charT, typename traits, typename Allocator>
  232. inline bool operator==(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  233. return basic_string_ref<charT, traits>(x) == y;
  234. }
  235. template<typename charT, typename traits>
  236. inline bool operator==(basic_string_ref<charT, traits> x, const charT * y) {
  237. return x == basic_string_ref<charT, traits>(y);
  238. }
  239. template<typename charT, typename traits>
  240. inline bool operator==(const charT * x, basic_string_ref<charT, traits> y) {
  241. return basic_string_ref<charT, traits>(x) == y;
  242. }
  243. // Inequality
  244. template<typename charT, typename traits>
  245. inline bool operator!=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  246. if ( x.size () != y.size ()) return true;
  247. return x.compare(y) != 0;
  248. }
  249. template<typename charT, typename traits, typename Allocator>
  250. inline bool operator!=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  251. return x != basic_string_ref<charT, traits>(y);
  252. }
  253. template<typename charT, typename traits, typename Allocator>
  254. inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  255. return basic_string_ref<charT, traits>(x) != y;
  256. }
  257. template<typename charT, typename traits>
  258. inline bool operator!=(basic_string_ref<charT, traits> x, const charT * y) {
  259. return x != basic_string_ref<charT, traits>(y);
  260. }
  261. template<typename charT, typename traits>
  262. inline bool operator!=(const charT * x, basic_string_ref<charT, traits> y) {
  263. return basic_string_ref<charT, traits>(x) != y;
  264. }
  265. // Less than
  266. template<typename charT, typename traits>
  267. inline bool operator<(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  268. return x.compare(y) < 0;
  269. }
  270. template<typename charT, typename traits, typename Allocator>
  271. inline bool operator<(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  272. return x < basic_string_ref<charT, traits>(y);
  273. }
  274. template<typename charT, typename traits, typename Allocator>
  275. inline bool operator<(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  276. return basic_string_ref<charT, traits>(x) < y;
  277. }
  278. template<typename charT, typename traits>
  279. inline bool operator<(basic_string_ref<charT, traits> x, const charT * y) {
  280. return x < basic_string_ref<charT, traits>(y);
  281. }
  282. template<typename charT, typename traits>
  283. inline bool operator<(const charT * x, basic_string_ref<charT, traits> y) {
  284. return basic_string_ref<charT, traits>(x) < y;
  285. }
  286. // Greater than
  287. template<typename charT, typename traits>
  288. inline bool operator>(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  289. return x.compare(y) > 0;
  290. }
  291. template<typename charT, typename traits, typename Allocator>
  292. inline bool operator>(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  293. return x > basic_string_ref<charT, traits>(y);
  294. }
  295. template<typename charT, typename traits, typename Allocator>
  296. inline bool operator>(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  297. return basic_string_ref<charT, traits>(x) > y;
  298. }
  299. template<typename charT, typename traits>
  300. inline bool operator>(basic_string_ref<charT, traits> x, const charT * y) {
  301. return x > basic_string_ref<charT, traits>(y);
  302. }
  303. template<typename charT, typename traits>
  304. inline bool operator>(const charT * x, basic_string_ref<charT, traits> y) {
  305. return basic_string_ref<charT, traits>(x) > y;
  306. }
  307. // Less than or equal to
  308. template<typename charT, typename traits>
  309. inline bool operator<=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  310. return x.compare(y) <= 0;
  311. }
  312. template<typename charT, typename traits, typename Allocator>
  313. inline bool operator<=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  314. return x <= basic_string_ref<charT, traits>(y);
  315. }
  316. template<typename charT, typename traits, typename Allocator>
  317. inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  318. return basic_string_ref<charT, traits>(x) <= y;
  319. }
  320. template<typename charT, typename traits>
  321. inline bool operator<=(basic_string_ref<charT, traits> x, const charT * y) {
  322. return x <= basic_string_ref<charT, traits>(y);
  323. }
  324. template<typename charT, typename traits>
  325. inline bool operator<=(const charT * x, basic_string_ref<charT, traits> y) {
  326. return basic_string_ref<charT, traits>(x) <= y;
  327. }
  328. // Greater than or equal to
  329. template<typename charT, typename traits>
  330. inline bool operator>=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  331. return x.compare(y) >= 0;
  332. }
  333. template<typename charT, typename traits, typename Allocator>
  334. inline bool operator>=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
  335. return x >= basic_string_ref<charT, traits>(y);
  336. }
  337. template<typename charT, typename traits, typename Allocator>
  338. inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
  339. return basic_string_ref<charT, traits>(x) >= y;
  340. }
  341. template<typename charT, typename traits>
  342. inline bool operator>=(basic_string_ref<charT, traits> x, const charT * y) {
  343. return x >= basic_string_ref<charT, traits>(y);
  344. }
  345. template<typename charT, typename traits>
  346. inline bool operator>=(const charT * x, basic_string_ref<charT, traits> y) {
  347. return basic_string_ref<charT, traits>(x) >= y;
  348. }
  349. // Inserter
  350. template<class charT, class traits>
  351. inline std::basic_ostream<charT, traits>&
  352. operator<<(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
  353. return boost::io::ostream_put(os, str.data(), str.size());
  354. }
  355. #if 0
  356. // numeric conversions
  357. //
  358. // These are short-term implementations.
  359. // In a production environment, I would rather avoid the copying.
  360. //
  361. inline int stoi (string_ref str, size_t* idx=0, int base=10) {
  362. return std::stoi ( std::string(str), idx, base );
  363. }
  364. inline long stol (string_ref str, size_t* idx=0, int base=10) {
  365. return std::stol ( std::string(str), idx, base );
  366. }
  367. inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
  368. return std::stoul ( std::string(str), idx, base );
  369. }
  370. inline long long stoll (string_ref str, size_t* idx=0, int base=10) {
  371. return std::stoll ( std::string(str), idx, base );
  372. }
  373. inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) {
  374. return std::stoull ( std::string(str), idx, base );
  375. }
  376. inline float stof (string_ref str, size_t* idx=0) {
  377. return std::stof ( std::string(str), idx );
  378. }
  379. inline double stod (string_ref str, size_t* idx=0) {
  380. return std::stod ( std::string(str), idx );
  381. }
  382. inline long double stold (string_ref str, size_t* idx=0) {
  383. return std::stold ( std::string(str), idx );
  384. }
  385. inline int stoi (wstring_ref str, size_t* idx=0, int base=10) {
  386. return std::stoi ( std::wstring(str), idx, base );
  387. }
  388. inline long stol (wstring_ref str, size_t* idx=0, int base=10) {
  389. return std::stol ( std::wstring(str), idx, base );
  390. }
  391. inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
  392. return std::stoul ( std::wstring(str), idx, base );
  393. }
  394. inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
  395. return std::stoll ( std::wstring(str), idx, base );
  396. }
  397. inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) {
  398. return std::stoull ( std::wstring(str), idx, base );
  399. }
  400. inline float stof (wstring_ref str, size_t* idx=0) {
  401. return std::stof ( std::wstring(str), idx );
  402. }
  403. inline double stod (wstring_ref str, size_t* idx=0) {
  404. return std::stod ( std::wstring(str), idx );
  405. }
  406. inline long double stold (wstring_ref str, size_t* idx=0) {
  407. return std::stold ( std::wstring(str), idx );
  408. }
  409. #endif
  410. }
  411. #if 0
  412. namespace std {
  413. // Hashing
  414. template<> struct hash<boost::string_ref>;
  415. template<> struct hash<boost::u16string_ref>;
  416. template<> struct hash<boost::u32string_ref>;
  417. template<> struct hash<boost::wstring_ref>;
  418. }
  419. #endif
  420. #endif