35 const int base = aspec.base();
36 if( 0 > num || 1 >= base ) {
41 std::div_t quotient = std::div(num, base);
42 res.insert( res.begin(), aspec[ quotient.rem ] );
46 const char s0 = aspec[0];
47 for(
unsigned int i=res.length(); i<min_width; ++i) {
48 res.insert(res.begin(), s0);
54 const int base = aspec.base();
55 if( 0 > num || 1 >= base ) {
60 std::lldiv_t quotient = std::lldiv(num, (int64_t)base);
61 res.insert( res.begin(), aspec[ quotient.rem ] );
65 const char s0 = aspec[0];
66 for(
unsigned int i=res.length(); i<min_width; ++i) {
67 res.insert(res.begin(), s0);
74 const int base = aspec.base();
78 std::string::size_type str_len = str.length();
80 for (std::string::size_type pos = 0; pos < str_len; ++pos) {
81 const int cp = aspec.code_point( str[pos] );
85 res = res * base +
static_cast<int64_t
>(cp);
91 if( 64 != aspec.base() ) {
94 const char padding = aspec.padding64();
95 const uint8_t* in_bytes = (
const uint8_t*)in_octets;
97 size_t out_len = ( in_len + 2 ) / 3 * 4;
101 while( 0 < in_len && 0 < out_len ) {
105 res.push_back( aspec[ ( in_bytes[0] >> 2 ) & 0x3f ] );
106 if( 0 == --in_len ) {
109 res.push_back( aspec[ ( in_bytes[0] << 4 ) & 0x3f ] );
111 res.push_back(padding);
112 res.push_back(padding);
118 res.push_back( aspec[ ( ( in_bytes[0] << 4 ) + ( in_bytes[1] >> 4) ) & 0x3f ] );
120 if( 0 == --in_len ) {
123 res.push_back( aspec[ ( in_bytes[1] << 2 ) & 0x3f ] );
125 res.push_back(padding);
131 res.push_back( aspec[ ( ( in_bytes[1] << 2 ) + ( in_bytes[2] >> 6) ) & 0x3f ] );
133 res.push_back( aspec[ in_bytes[2] & 0x3f ] );
142 if( 64 != aspec.base() ) {
143 return std::vector<uint8_t>();
145 size_t in_len = in_code.length();
147 return std::vector<uint8_t>();
149 const char padding = aspec.padding64();
150 const char* in_chars = in_code.data();
152 const size_t out_len = 3 * ( in_len / 4 ) + 2;
153 std::vector<uint8_t> res;
154 res.reserve(out_len);
156 while( in_len >= 2 ) {
157 const int cp0 = aspec.code_point( in_chars[0] );
158 const int cp1 = aspec.code_point( in_chars[1] );
159 if( 0 > cp0 || 0 > cp1 ) {
162 res.push_back( cp0 << 2 | cp1 >> 4 );
169 if( padding == in_chars[2] ) {
173 if( padding != in_chars[3] ) {
177 const int cp2 = aspec.code_point( in_chars[2] );
181 res.push_back( ( ( cp1 << 4 ) & 0xf0 ) | ( cp2 >> 2 ) );
188 if( padding == in_chars[3] ) {
193 const int cp3 = aspec.code_point( in_chars[3] );
197 res.push_back( ( ( cp2 << 6 ) & 0xc0 ) | cp3 );
205 DBG_PRINT(
"in_len %zu/%zu at '%s', out_len %zu/%zu\n", (in_code.length()-in_len), in_code.length(), std::string(in_code).c_str(), res.size(), out_len);
206 return std::vector<uint8_t>();
214 for(
size_t i = period; i < str.length(); i += period + 1) {
216 PRAGMA_DISABLE_WARNING_RESTRICT
226 auto it =
jau::remove_if( str.begin(), str.end(), [&count](
char c){
234 str.erase(it, str.end());
Base Alphabet Specification providing the alphabet for encode() and decode().
#define DBG_PRINT(...)
Use for environment-variable environment::DEBUG conditional debug messages, prefix '[elapsed_time] De...
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
Identical to C++20 std::remove_if() of algorithm
size_t insert_lf(std::string &str, const size_t period) noexcept
Inserts a line feed (LF) character \n (ASCII 0x0a) after every period of characters.
std::string encode(int num, const alphabet &aspec, const unsigned int min_width=0) noexcept
Encodes a given positive decimal number to a symbolic string representing a given alphabet and its ba...
std::string encode64(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
std::vector< uint8_t > decode64(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
size_t remove_lf(std::string &str) noexcept
Removes line feed character from str.
int64_t decode(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing a given alphabet and its base to a positive decimal numb...
#define PRAGMA_DISABLE_WARNING_PUSH
#define PRAGMA_DISABLE_WARNING_POP
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.