Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:25

0001 //
0002 // Copyright (c) 2017 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_DETAIL_VARINT_HPP
0011 #define BOOST_BEAST_DETAIL_VARINT_HPP
0012 
0013 #include <boost/static_assert.hpp>
0014 #include <cstdlib>
0015 #include <iterator>
0016 #include <type_traits>
0017 
0018 namespace boost {
0019 namespace beast {
0020 namespace detail {
0021 
0022 // https://developers.google.com/protocol-buffers/docs/encoding#varints
0023 
0024 inline
0025 std::size_t
0026 varint_size(std::size_t value)
0027 {
0028     std::size_t n = 1;
0029     while(value > 127)
0030     {
0031         ++n;
0032         value /= 128;
0033     }
0034     return n;
0035 }
0036 
0037 template<class FwdIt>
0038 std::size_t
0039 varint_read(FwdIt& first)
0040 {
0041     using value_type = typename
0042         std::iterator_traits<FwdIt>::value_type;
0043     BOOST_STATIC_ASSERT(
0044         std::is_integral<value_type>::value &&
0045         sizeof(value_type) == 1);
0046     std::size_t value = 0;
0047     std::size_t factor = 1;
0048     while((*first & 0x80) != 0)
0049     {
0050         value += (*first++ & 0x7f) * factor;
0051         factor *= 128;
0052     }
0053     value += *first++ * factor;
0054     return value;
0055 }
0056 
0057 template<class FwdIt>
0058 void
0059 varint_write(FwdIt& first, std::size_t value)
0060 {
0061     using value_type = typename
0062         std::iterator_traits<FwdIt>::value_type;
0063     BOOST_STATIC_ASSERT(
0064         std::is_integral<value_type>::value &&
0065         sizeof(value_type) == 1);
0066     while(value > 127)
0067     {
0068         *first++ = static_cast<value_type>(
0069             0x80 | value);
0070         value /= 128;
0071     }
0072     *first++ = static_cast<value_type>(value);
0073 }
0074 
0075 } // detail
0076 } // beast
0077 } // boost
0078 
0079 #endif