PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/External/boost/boost/utility/string_ref.hpp

http://klayge.codeplex.com
C++ Header | 386 lines | 284 code | 71 blank | 31 comment | 44 complexity | 0bfaf16dfcff5b8b71773f3a5936bd44 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, Unlicense, BSD-3-Clause
  1. /*
  2. Copyright (c) Marshall Clow 2012-2012.
  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 <stdexcept>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <string>
  18. namespace boost {
  19. namespace detail {
  20. // A helper functor because sometimes we don't have lambdas
  21. template <typename charT, typename traits>
  22. class string_ref_traits_eq {
  23. public:
  24. string_ref_traits_eq ( charT ch ) : ch_(ch) {}
  25. bool operator () ( charT val ) const { return traits::eq ( ch_, val ); }
  26. charT ch_;
  27. };
  28. }
  29. template<typename charT, typename traits> class basic_string_ref;
  30. typedef basic_string_ref<char, std::char_traits<char> > string_ref;
  31. typedef basic_string_ref<wchar_t, std::char_traits<wchar_t> > wstring_ref;
  32. #ifndef BOOST_NO_CXX11_CHAR16_T
  33. typedef basic_string_ref<char16_t, std::char_traits<char16_t> > u16string_ref;
  34. #endif
  35. #ifndef BOOST_NO_CXX11_CHAR32_T
  36. typedef basic_string_ref<char32_t, std::char_traits<char32_t> > u32string_ref;
  37. #endif
  38. template<typename charT, typename traits>
  39. class basic_string_ref {
  40. public:
  41. // types
  42. typedef charT value_type;
  43. typedef const charT* pointer;
  44. typedef const charT& reference;
  45. typedef const charT& const_reference;
  46. typedef pointer const_iterator; // impl-defined
  47. typedef const_iterator iterator;
  48. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  49. typedef const_reverse_iterator reverse_iterator;
  50. typedef std::size_t size_type;
  51. typedef ptrdiff_t difference_type;
  52. static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1);
  53. // construct/copy
  54. BOOST_CONSTEXPR basic_string_ref ()
  55. : ptr_(NULL), len_(0) {}
  56. BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs)
  57. : ptr_(rhs.ptr_), len_(rhs.len_) {}
  58. basic_string_ref& operator=(const basic_string_ref &rhs) {
  59. ptr_ = rhs.ptr_;
  60. len_ = rhs.len_;
  61. return *this;
  62. }
  63. basic_string_ref(const charT* str)
  64. : ptr_(str), len_(traits::length(str)) {}
  65. template<typename Allocator>
  66. basic_string_ref(const std::basic_string<charT, traits, Allocator>& str)
  67. : ptr_(str.data()), len_(str.length()) {}
  68. BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len)
  69. : ptr_(str), len_(len) {}
  70. #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  71. template<typename Allocator>
  72. explicit operator std::basic_string<charT, traits, Allocator>() const {
  73. return std::basic_string<charT, traits, Allocator> ( ptr_, len_ );
  74. }
  75. #endif
  76. // iterators
  77. BOOST_CONSTEXPR const_iterator begin() const { return ptr_; }
  78. BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; }
  79. BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; }
  80. BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; }
  81. const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); }
  82. const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); }
  83. const_reverse_iterator rend() const { return const_reverse_iterator (begin()); }
  84. const_reverse_iterator crend() const { return const_reverse_iterator (begin()); }
  85. // capacity
  86. BOOST_CONSTEXPR size_type size() const { return len_; }
  87. BOOST_CONSTEXPR size_type length() const { return len_; }
  88. BOOST_CONSTEXPR size_type max_size() const { return len_; }
  89. BOOST_CONSTEXPR bool empty() const { return len_ == 0; }
  90. // element access
  91. BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; }
  92. const charT& at(size_t pos) const {
  93. if ( pos >= len_ )
  94. throw std::out_of_range ( "boost::string_ref::at" );
  95. return ptr_[pos];
  96. }
  97. BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; }
  98. BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; }
  99. BOOST_CONSTEXPR const charT* data() const { return ptr_; }
  100. // modifiers
  101. void clear() { len_ = 0; }
  102. void remove_prefix(size_type n) {
  103. if ( n > len_ )
  104. n = len_;
  105. ptr_ += n;
  106. len_ -= n;
  107. }
  108. void remove_suffix(size_type n) {
  109. if ( n > len_ )
  110. n = len_;
  111. len_ -= n;
  112. }
  113. // basic_string_ref string operations
  114. BOOST_CONSTEXPR
  115. basic_string_ref substr(size_type pos, size_type n=npos) const {
  116. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1600)
  117. // Looks like msvc 8 and 9 have a codegen bug when one branch of
  118. // a conditional operator is a throw expression. -EAN 2012/12/04
  119. if ( pos > size()) throw std::out_of_range ( "string_ref::substr" );
  120. if ( n == npos || pos + n > size()) n = size () - pos;
  121. return basic_string_ref ( data() + pos, n );
  122. #else
  123. return pos > size() ? throw std::out_of_range ( "string_ref::substr" ) :
  124. basic_string_ref ( data() + pos, n == npos || pos + n > size() ? size() - pos : n );
  125. #endif
  126. }
  127. int compare(basic_string_ref x) const {
  128. int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_));
  129. return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 );
  130. }
  131. bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); }
  132. bool starts_with(basic_string_ref x) const {
  133. return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0;
  134. }
  135. bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); }
  136. bool ends_with(basic_string_ref x) const {
  137. return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0;
  138. }
  139. size_type find(basic_string_ref s) const {
  140. const_iterator iter = std::search ( this->cbegin (), this->cend (),
  141. s.cbegin (), s.cend (), traits::eq );
  142. return iter = this->cend () ? npos : std::distance ( this->cbegin (), iter );
  143. }
  144. size_type find(charT c) const {
  145. const_iterator iter = std::find_if ( this->cbegin (), this->cend (),
  146. detail::string_ref_traits_eq<charT, traits> ( c ));
  147. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  148. }
  149. size_type rfind(basic_string_ref s) const {
  150. const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
  151. s.crbegin (), s.crend (), traits::eq );
  152. return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
  153. }
  154. size_type rfind(charT c) const {
  155. const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (),
  156. detail::string_ref_traits_eq<charT, traits> ( c ));
  157. return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
  158. }
  159. size_type find_first_of(charT c) const { return find (c); }
  160. size_type find_last_of (charT c) const { return rfind (c); }
  161. size_type find_first_of(basic_string_ref s) const {
  162. const_iterator iter = std::find_first_of
  163. ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq );
  164. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  165. }
  166. size_type find_last_of(basic_string_ref s) const {
  167. const_reverse_iterator iter = std::find_first_of
  168. ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq );
  169. return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter);
  170. }
  171. size_type find_first_not_of(basic_string_ref s) const {
  172. const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s );
  173. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  174. }
  175. size_type find_first_not_of(charT c) const {
  176. for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter )
  177. if ( !traits::eq ( c, *iter ))
  178. return std::distance ( this->cbegin (), iter );
  179. return npos;
  180. }
  181. size_type find_last_not_of(basic_string_ref s) const {
  182. const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s );
  183. return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
  184. }
  185. size_type find_last_not_of(charT c) const {
  186. for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter )
  187. if ( !traits::eq ( c, *iter ))
  188. return reverse_distance ( this->crbegin (), iter );
  189. return npos;
  190. }
  191. private:
  192. template <typename r_iter>
  193. size_type reverse_distance ( r_iter first, r_iter last ) const {
  194. return len_ - 1 - std::distance ( first, last );
  195. }
  196. template <typename Iterator>
  197. Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const {
  198. for ( ; first != last ; ++first )
  199. if ( 0 == traits::find ( s.ptr_, s.len_, *first ))
  200. return first;
  201. return last;
  202. }
  203. const charT *ptr_;
  204. std::size_t len_;
  205. };
  206. // Comparison operators
  207. template<typename charT, typename traits>
  208. bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  209. if ( x.size () != y.size ()) return false;
  210. return x.compare(y) == 0;
  211. }
  212. template<typename charT, typename traits>
  213. bool operator!=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  214. if ( x.size () != y.size ()) return true;
  215. return x.compare(y) != 0;
  216. }
  217. template<typename charT, typename traits>
  218. bool operator<(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  219. return x.compare(y) < 0;
  220. }
  221. template<typename charT, typename traits>
  222. bool operator>(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  223. return x.compare(y) > 0;
  224. }
  225. template<typename charT, typename traits>
  226. bool operator<=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  227. return x.compare(y) <= 0;
  228. }
  229. template<typename charT, typename traits>
  230. bool operator>=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
  231. return x.compare(y) >= 0;
  232. }
  233. // Inserter
  234. template<class charT, class traits>
  235. std::basic_ostream<charT, traits>&
  236. operator<<(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
  237. #ifdef BOOST_NO_CXX11_RANGE_BASED_FOR
  238. for ( typename basic_string_ref<charT, traits>::const_iterator iter = str.begin (); iter != str.end (); ++iter )
  239. os << *iter;
  240. #else
  241. for ( charT x : str )
  242. os << x;
  243. #endif
  244. return os;
  245. }
  246. #if 0
  247. // numeric conversions
  248. //
  249. // These are short-term implementations.
  250. // In a production environment, I would rather avoid the copying.
  251. //
  252. int stoi (string_ref str, size_t* idx=0, int base=10) {
  253. return std::stoi ( std::string(str), idx, base );
  254. }
  255. long stol (string_ref str, size_t* idx=0, int base=10) {
  256. return std::stol ( std::string(str), idx, base );
  257. }
  258. unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
  259. return std::stoul ( std::string(str), idx, base );
  260. }
  261. long long stoll (string_ref str, size_t* idx=0, int base=10) {
  262. return std::stoll ( std::string(str), idx, base );
  263. }
  264. unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) {
  265. return std::stoull ( std::string(str), idx, base );
  266. }
  267. float stof (string_ref str, size_t* idx=0) {
  268. return std::stof ( std::string(str), idx );
  269. }
  270. double stod (string_ref str, size_t* idx=0) {
  271. return std::stod ( std::string(str), idx );
  272. }
  273. long double stold (string_ref str, size_t* idx=0) {
  274. return std::stold ( std::string(str), idx );
  275. }
  276. int stoi (wstring_ref str, size_t* idx=0, int base=10) {
  277. return std::stoi ( std::wstring(str), idx, base );
  278. }
  279. long stol (wstring_ref str, size_t* idx=0, int base=10) {
  280. return std::stol ( std::wstring(str), idx, base );
  281. }
  282. unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
  283. return std::stoul ( std::wstring(str), idx, base );
  284. }
  285. long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
  286. return std::stoll ( std::wstring(str), idx, base );
  287. }
  288. unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) {
  289. return std::stoull ( std::wstring(str), idx, base );
  290. }
  291. float stof (wstring_ref str, size_t* idx=0) {
  292. return std::stof ( std::wstring(str), idx );
  293. }
  294. double stod (wstring_ref str, size_t* idx=0) {
  295. return std::stod ( std::wstring(str), idx );
  296. }
  297. long double stold (wstring_ref str, size_t* idx=0) {
  298. return std::stold ( std::wstring(str), idx );
  299. }
  300. #endif
  301. }
  302. #if 0
  303. namespace std {
  304. // Hashing
  305. template<> struct hash<boost::string_ref>;
  306. template<> struct hash<boost::u16string_ref>;
  307. template<> struct hash<boost::u32string_ref>;
  308. template<> struct hash<boost::wstring_ref>;
  309. }
  310. #endif
  311. #endif