buffer_view.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright Antony Polukhin, 2011-2024.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_LEXICAL_CAST_DETAIL_BUFFER_VIEW_HPP
  7. #define BOOST_LEXICAL_CAST_DETAIL_BUFFER_VIEW_HPP
  8. #include <boost/config.hpp>
  9. #ifdef BOOST_HAS_PRAGMA_ONCE
  10. # pragma once
  11. #endif
  12. #include <iosfwd>
  13. namespace boost { namespace conversion { namespace detail {
  14. template < typename CharT >
  15. struct buffer_view {
  16. const CharT* begin;
  17. const CharT* end;
  18. };
  19. template < typename CharT >
  20. buffer_view<CharT> make_buffer_view(const CharT* begin, const CharT* end) {
  21. return buffer_view<CharT>{begin, end};
  22. }
  23. inline buffer_view<char> make_buffer_view(const signed char* begin, const signed char* end) {
  24. return buffer_view<char>{
  25. reinterpret_cast<const char*>(begin),
  26. reinterpret_cast<const char*>(end)
  27. };
  28. }
  29. inline buffer_view<char> make_buffer_view(const unsigned char* begin, const unsigned char* end) {
  30. return buffer_view<char>{
  31. reinterpret_cast<const char*>(begin),
  32. reinterpret_cast<const char*>(end)
  33. };
  34. }
  35. template< typename CharT, typename Elem, typename Traits >
  36. std::basic_ostream<Elem,Traits>& operator<<(
  37. std::basic_ostream<Elem, Traits>& os,
  38. buffer_view<CharT> r)
  39. {
  40. while (r.begin != r.end) {
  41. os << r.begin[0];
  42. ++r.begin;
  43. }
  44. return os;
  45. }
  46. }}} // namespace boost::conversion::detail
  47. #endif // BOOST_LEXICAL_CAST_DETAIL_BUFFER_VIEW_HPP