Sha256: 76ec7d9854c26f01a5cebf052fd1f268d78f79c9544dab167d42a30649f7f07f

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

// Copyright (c) 2017-2022 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/json/

#ifndef TAO_JSON_INTERNAL_BASE64_HPP
#define TAO_JSON_INTERNAL_BASE64_HPP

#include <stdexcept>
#include <string>

namespace tao::json::internal
{
   template< typename T >
   [[nodiscard]] std::string base64( 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 ];
            s += "==";
            break;

         case 2:
            s += table[ ( encode << 2 ) & 0x3f ];
            s += '=';
            break;

         default:
            throw std::logic_error( "code should be unreachable" );  // LCOV_EXCL_LINE
      }

      return s;
   }

}  // namespace tao::json::internal

#endif

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
couchbase-3.4.5 ext/couchbase/third_party/json/include/tao/json/internal/base64.hpp
couchbase-3.4.4 ext/couchbase/third_party/json/include/tao/json/internal/base64.hpp
couchbase-3.4.3 ext/couchbase/third_party/json/include/tao/json/internal/base64.hpp
couchbase-3.4.2 ext/couchbase/third_party/json/include/tao/json/internal/base64.hpp
couchbase-3.4.1 ext/couchbase/third_party/json/include/tao/json/internal/base64.hpp
couchbase-3.4.0 ext/couchbase/third_party/json/include/tao/json/internal/base64.hpp
couchbase-3.3.0 ext/couchbase/third_party/json/include/tao/json/internal/base64.hpp