string_ref.hpp 21 KB

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