Sha256: 60a48f1d437919269c8f85a59359858f75862e6ca229cb570df612e1b6c3fd23
Contents?: true
Size: 1.26 KB
Versions: 34
Compression:
Stored size: 1.26 KB
Contents
// Copyright (c) 2017-2020 Dr. Colin Hirsch and Daniel Frey // Please see LICENSE for license or visit https://github.com/taocpp/json/ #ifndef TAO_JSON_INTERNAL_BASE64URL_HPP #define TAO_JSON_INTERNAL_BASE64URL_HPP #include <stdexcept> #include <string> namespace tao::json::internal { template< typename T > [[nodiscard]] std::string base64url( const T& v ) { static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; std::string s; s.reserve( ( v.size() + 2 ) / 3 * 4 ); unsigned cycle = 0; unsigned encode = 0; for( const auto c : v ) { encode <<= 8; encode += static_cast< unsigned char >( c ); s += table[ ( encode >> ( ++cycle * 2 ) ) & 0x3f ]; if( cycle == 3 ) { cycle = 0; s += table[ encode & 0x3f ]; } } switch( cycle ) { case 0: break; case 1: s += table[ ( encode << 4 ) & 0x3f ]; break; case 2: s += table[ ( encode << 2 ) & 0x3f ]; break; default: throw std::logic_error( "code should be unreachable" ); // LCOV_EXCL_LINE } return s; } } // namespace tao::json::internal #endif
Version data entries
34 entries across 34 versions & 1 rubygems