as_literal.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen 2006. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_AS_LITERAL_HPP
  11. #define BOOST_RANGE_AS_LITERAL_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/range/iterator_range.hpp>
  16. #include <boost/range/detail/str_types.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <cstring>
  19. #if !defined(BOOST_NO_CXX11_CHAR16_T) || !defined(BOOST_NO_CXX11_CHAR32_T)
  20. #include <string> // for std::char_traits
  21. #endif
  22. #ifndef BOOST_NO_CWCHAR
  23. #include <cwchar>
  24. #endif
  25. namespace boost
  26. {
  27. namespace range_detail
  28. {
  29. inline std::size_t length( const char* s )
  30. {
  31. return strlen( s );
  32. }
  33. #ifndef BOOST_NO_CXX11_CHAR16_T
  34. inline std::size_t length( const char16_t* s )
  35. {
  36. return std::char_traits<char16_t>::length( s );
  37. }
  38. #endif
  39. #ifndef BOOST_NO_CXX11_CHAR32_T
  40. inline std::size_t length( const char32_t* s )
  41. {
  42. return std::char_traits<char32_t>::length( s );
  43. }
  44. #endif
  45. #ifndef BOOST_NO_CWCHAR
  46. inline std::size_t length( const wchar_t* s )
  47. {
  48. return wcslen( s );
  49. }
  50. #endif
  51. //
  52. // Remark: the compiler cannot choose between T* and T[sz]
  53. // overloads, so we must put the T* internal to the
  54. // unconstrained version.
  55. //
  56. inline bool is_char_ptr( char* )
  57. {
  58. return true;
  59. }
  60. inline bool is_char_ptr( const char* )
  61. {
  62. return true;
  63. }
  64. #ifndef BOOST_NO_CXX11_CHAR16_T
  65. inline bool is_char_ptr( char16_t* )
  66. {
  67. return true;
  68. }
  69. inline bool is_char_ptr( const char16_t* )
  70. {
  71. return true;
  72. }
  73. #endif
  74. #ifndef BOOST_NO_CXX11_CHAR32_T
  75. inline bool is_char_ptr( char32_t* )
  76. {
  77. return true;
  78. }
  79. inline bool is_char_ptr( const char32_t* )
  80. {
  81. return true;
  82. }
  83. #endif
  84. #ifndef BOOST_NO_CWCHAR
  85. inline bool is_char_ptr( wchar_t* )
  86. {
  87. return true;
  88. }
  89. inline bool is_char_ptr( const wchar_t* )
  90. {
  91. return true;
  92. }
  93. #endif
  94. template< class T >
  95. inline long is_char_ptr( const T& /* r */ )
  96. {
  97. return 0L;
  98. }
  99. template< class T >
  100. inline iterator_range<T*>
  101. make_range( T* const r, bool )
  102. {
  103. return iterator_range<T*>( r, r + length(r) );
  104. }
  105. template< class T >
  106. inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<T>::type>
  107. make_range( T& r, long )
  108. {
  109. return boost::make_iterator_range( r );
  110. }
  111. }
  112. template< class Range >
  113. inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<Range>::type>
  114. as_literal( Range& r )
  115. {
  116. return range_detail::make_range( r, range_detail::is_char_ptr(r) );
  117. }
  118. template< class Range >
  119. inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<const Range>::type>
  120. as_literal( const Range& r )
  121. {
  122. return range_detail::make_range( r, range_detail::is_char_ptr(r) );
  123. }
  124. template< class Char, std::size_t sz >
  125. inline iterator_range<Char*> as_literal( Char (&arr)[sz] )
  126. {
  127. return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
  128. }
  129. template< class Char, std::size_t sz >
  130. inline iterator_range<const Char*> as_literal( const Char (&arr)[sz] )
  131. {
  132. return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
  133. }
  134. }
  135. #endif