25#ifndef JAU_BASE_CODEC_HPP_
26#define JAU_BASE_CODEC_HPP_
62 std::string_view symbols_;
68 : name_(std::move(_name)), base_(_base), symbols_(_symbols), padding64_(_padding64), cpf(_cpf) {}
71 constexpr const std::string&
name() const noexcept {
return name_; }
74 constexpr int base() const noexcept {
return base_; }
77 constexpr const std::string_view&
symbols() const noexcept {
return symbols_; }
80 constexpr char padding64() const noexcept {
return padding64_; }
83 constexpr int code_point(
const char c)
const noexcept {
return cpf(c); }
86 constexpr char operator[](
size_t cp )
const noexcept {
return symbols_[cp]; }
89 std::string res(
"alphabet[");
91 res.append(
", base <= "+std::to_string(
base())+
"]");
99 return lhs.base() != rhs.base() || lhs.name() != rhs.name() || lhs.symbols() != rhs.symbols();
103 return !( lhs != rhs );
126 static inline constexpr const std::string_view data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
128 static int s_code_point(
const char c)
noexcept {
129 if (
'A' <= c && c <=
'Z') {
131 }
else if (
'a' <= c && c <=
'z') {
133 }
else if (
'0' <= c && c <=
'9') {
135 }
else if (
'+' == c) {
137 }
else if (
'/' == c) {
146 :
alphabet(
"base64", 64, data,
'=', s_code_point) {}
171 static inline constexpr const std::string_view data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
173 static int s_code_point(
const char c)
noexcept {
174 if (
'A' <= c && c <=
'Z') {
176 }
else if (
'a' <= c && c <=
'z') {
178 }
else if (
'0' <= c && c <=
'9') {
180 }
else if (
'-' == c) {
182 }
else if (
'_' == c) {
191 :
alphabet(
"base64url", 64, data,
'=', s_code_point) {}
215 static inline constexpr const std::string_view data =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
217 static int s_code_point(
const char c)
noexcept {
218 if (
'0' <= c && c <=
'9') {
220 }
else if (
'a' <= c && c <=
'z') {
222 }
else if (
'A' <= c && c <=
'Z') {
224 }
else if (
'-' == c) {
226 }
else if (
'_' == c) {
235 :
alphabet(
"natural64", 64, data,
'=', s_code_point) {}
256 static inline constexpr const std::string_view data =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_!#%&()+,/:;<=>?@[]^{}~";
258 static int s_code_point(
const char c)
noexcept {
259 if (
'0' <= c && c <=
'9') {
261 }
else if (
'a' <= c && c <=
'z') {
263 }
else if (
'A' <= c && c <=
'Z') {
298 :
alphabet(
"natural86", 86, data, 0, s_code_point) {}
320 static inline constexpr const std::string_view data =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
322 static int s_code_point(
const char c)
noexcept {
323 if (
'0' <= c && c <=
'9') {
325 }
else if (
'A' <= c && c <=
'Z') {
327 }
else if (
'-' == c) {
329 }
else if (
'_' == c) {
338 :
alphabet(
"ascii38", 38, data,
'=', s_code_point) {}
360 static inline constexpr const std::string_view data =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
362 static int s_code_point(
const char c)
noexcept {
363 if (
'0' <= c && c <=
'9') {
365 }
else if (
'A' <= c && c <=
'Z') {
367 }
else if (
'a' <= c && c <=
'z') {
369 }
else if (
'-' == c) {
371 }
else if (
'_' == c) {
380 :
alphabet(
"ascii64", 64, data,
'=', s_code_point) {}
399 static inline constexpr const std::string_view data =
"!#%&()+,-/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{}~";
401 static int s_code_point(
const char c)
noexcept {
402 if (
'0' <= c && c <=
'9') {
404 }
else if (
'A' <= c && c <=
'Z') {
406 }
else if (
'a' <= c && c <=
'z') {
444 :
alphabet(
"ascii86", 86, data, 0, s_code_point) {}
465 std::string
encode(
int num,
const alphabet& aspec,
const unsigned int min_width=0) noexcept;
485 std::
string encode(int64_t num, const alphabet& aspec, const
unsigned int min_width=0) noexcept;
503 int64_t
decode(const
std::string_view& str, const alphabet& aspec) noexcept;
516 std::
string encode64(const
void* in_octets,
size_t in_len, const alphabet& aspec) noexcept;
528 std::vector<uint8_t>
decode64(const
std::string_view& str, const alphabet& aspec) noexcept;
537 size_t insert_lf(
std::
string& str, const
size_t period) noexcept;
560 std::string e =
encode64(in_octets, in_len, aspec);
578 std::string e =
encode64(in_octets, in_len, aspec);
Base Alphabet Specification providing the alphabet for encode() and decode().
constexpr int base() const noexcept
The fixed base used for this alphabet.
std::string to_string() const noexcept
constexpr int code_point(const char c) const noexcept
Returns the code-point of the given character or -1 if not element of this alphabet.
constexpr char operator[](size_t cp) const noexcept
Retrieve the character at given code-point of this alphabet.
constexpr const std::string_view & symbols() const noexcept
The string of symbols of this alphabet.
alphabet(std::string _name, int _base, std::string_view _symbols, char _padding64, code_point_func _cpf) noexcept
constexpr char padding64() const noexcept
Padding symbol for base <= 64 and block encoding only.
int(* code_point_func)(const char c) noexcept
constexpr const std::string & name() const noexcept
Human readable name for this alphabet instance.
ascii38_alphabet() noexcept
ascii64_alphabet() noexcept
ascii86_alphabet() noexcept
base64_alphabet() noexcept
base64url_alphabet() noexcept
natural64_alphabet() noexcept
natural86_alphabet() noexcept
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_mime(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::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...
std::string encode64_pem(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_lf(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...
bool operator!=(const alphabet &lhs, const alphabet &rhs) noexcept
std::string to_string(const alphabet &v) noexcept
bool operator==(const alphabet &lhs, const alphabet &rhs) noexcept