25#ifndef JAU_BASE_CODEC_HPP_
26#define JAU_BASE_CODEC_HPP_
63 std::string_view symbols_;
69 : name_(std::move(_name)), base_(_base), symbols_(_symbols), padding64_(_padding64), cpf(_cpf) {}
72 constexpr const std::string&
name() const noexcept {
return name_; }
75 constexpr int base() const noexcept {
return base_; }
78 constexpr const std::string_view&
symbols() const noexcept {
return symbols_; }
81 constexpr char padding64() const noexcept {
return padding64_; }
84 constexpr int code_point(
const char c)
const noexcept {
return cpf(c); }
87 constexpr char operator[](
size_t cp )
const noexcept {
return symbols_[cp]; }
90 std::string res(
"alphabet[");
100 return lhs.base() != rhs.base() || lhs.name() != rhs.name() || lhs.symbols() != rhs.symbols();
104 return !( lhs != rhs );
127 static inline constexpr const std::string_view data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
129 static int s_code_point(
const char c)
noexcept {
130 if (
'A' <= c && c <=
'Z') {
132 }
else if (
'a' <= c && c <=
'z') {
134 }
else if (
'0' <= c && c <=
'9') {
136 }
else if (
'+' == c) {
138 }
else if (
'/' == c) {
147 :
alphabet("base64", 64, data, '=', s_code_point) {}
172 static inline constexpr const std::string_view data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
174 static int s_code_point(
const char c)
noexcept {
175 if (
'A' <= c && c <=
'Z') {
177 }
else if (
'a' <= c && c <=
'z') {
179 }
else if (
'0' <= c && c <=
'9') {
181 }
else if (
'-' == c) {
183 }
else if (
'_' == c) {
192 :
alphabet("base64url", 64, data, '=', s_code_point) {}
216 static inline constexpr const std::string_view data =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
218 static int s_code_point(
const char c)
noexcept {
219 if (
'0' <= c && c <=
'9') {
221 }
else if (
'a' <= c && c <=
'z') {
223 }
else if (
'A' <= c && c <=
'Z') {
225 }
else if (
'-' == c) {
227 }
else if (
'_' == c) {
236 :
alphabet("natural64", 64, data, '=', s_code_point) {}
257 static inline constexpr const std::string_view data =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_!#%&()+,/:;<=>?@[]^{}~";
259 static int s_code_point(
const char c)
noexcept {
260 if (
'0' <= c && c <=
'9') {
262 }
else if (
'a' <= c && c <=
'z') {
264 }
else if (
'A' <= c && c <=
'Z') {
299 :
alphabet("natural86", 86, data, 0, s_code_point) {}
321 static inline constexpr const std::string_view data =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
323 static int s_code_point(
const char c)
noexcept {
324 if (
'0' <= c && c <=
'9') {
326 }
else if (
'A' <= c && c <=
'Z') {
328 }
else if (
'-' == c) {
330 }
else if (
'_' == c) {
339 :
alphabet("ascii38", 38, data, '=', s_code_point) {}
361 static inline constexpr const std::string_view data =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
363 static int s_code_point(
const char c)
noexcept {
364 if (
'0' <= c && c <=
'9') {
366 }
else if (
'A' <= c && c <=
'Z') {
368 }
else if (
'a' <= c && c <=
'z') {
370 }
else if (
'-' == c) {
372 }
else if (
'_' == c) {
381 :
alphabet("ascii64", 64, data, '=', s_code_point) {}
400 static inline constexpr const std::string_view data =
"!#%&()+,-/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{}~";
402 static int s_code_point(
const char c)
noexcept {
403 if (
'0' <= c && c <=
'9') {
405 }
else if (
'A' <= c && c <=
'Z') {
407 }
else if (
'a' <= c && c <=
'z') {
445 :
alphabet("ascii86", 86, data, 0, s_code_point) {}
466 std::string
encode(
int num,
const alphabet& aspec,
const unsigned int min_width=0) noexcept;
486 std::
string encode(int64_t num, const alphabet& aspec, const
unsigned int min_width=0) noexcept;
504 int64_t
decode(const
std::string_view& str, const alphabet& aspec) noexcept;
517 std::
string encode64(const
void* in_octets,
size_t in_len, const alphabet& aspec) noexcept;
529 std::vector<uint8_t>
decode64(const
std::string_view& str, const alphabet& aspec) noexcept;
538 size_t insert_lf(
std::
string& str, const
size_t period) noexcept;
561 std::string e =
encode64(in_octets, in_len, aspec);
579 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.
Safe base 38 alphabet with ASCII code-point sorting order.
ascii38_alphabet() noexcept
Safe base 64 alphabet with ASCII code-point sorting order.
ascii64_alphabet() noexcept
Base 86 alphabet with ASCII code-point sorting order.
ascii86_alphabet() noexcept
Safe canonical base64 alphabet, without ASCII code-point sorting order.
base64_alphabet() noexcept
Safe canonical base64url alphabet, without ASCII code-point sorting order.
base64url_alphabet() noexcept
Safe natural base 64 alphabet, both without ASCII code-point sorting order.
natural64_alphabet() noexcept
Natural base 86 alphabet, without ASCII code-point sorting order.
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