Honeycomb  0.1
Component-Model Framework
Encode.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/String/Stream.h"
5 #include "Honey/Misc/MtMap.h"
6 
7 namespace honey
8 {
10 namespace encode
11 {
12 
14 namespace priv
15 {
16  mtkey(encode);
17  mtkey(decode);
19  MtMap< function<ostream& (ostream& os, const Bytes& val)>, encode,
20  function<istream& (istream& os, Bytes& val)>, decode>>),
21  exts,);
22 
23  struct Manip : honey::Manip<Manip> { Id encoding = "hex"_id; };
24 
25  extern const String hex_chars;
26  extern const int8 hex_chars_rev[55];
27 
28  extern const String base32_chars;
29  extern const int8 base32_chars_rev[73];
30 
31  extern const String base64_chars;
32  extern const int8 base64_chars_rev[80];
33 }
36 bool reg(const Id& id, const function<ostream& (ostream& os, const Bytes& val)>& encode,
38  const function<istream& (istream& os, Bytes& val)>& decode);
39 
41 inline bool isHex(Char c) { return c >= '0' && c < '0' + sizeof(priv::hex_chars_rev) && priv::hex_chars_rev[c - '0'] != -1; }
43 inline Char toHex(byte b) { return priv::hex_chars[b]; }
45 inline byte fromHex(Char c) { return priv::hex_chars_rev[c - '0']; }
49 Bytes hex_decode(const String& string);
51 inline ostream& hex(ostream& os) { priv::Manip::inst(os).encoding = "hex"_id; return os; }
53 inline istream& hex(istream& is) { priv::Manip::inst(is).encoding = "hex"_id; return is; }
54 
56 inline bool isDec(Char c) { return c >= '0' && c <= '9'; }
58 inline Char toDec(byte b) { return '0' + b; }
60 inline byte fromDec(Char c) { return c - '0'; }
64 Bytes dec_decode(const String& string);
66 inline ostream& dec(ostream& os) { priv::Manip::inst(os).encoding = "dec"_id; return os; }
68 inline istream& dec(istream& is) { priv::Manip::inst(is).encoding = "dec"_id; return is; }
69 
71 inline ostream& u8(ostream& os) { priv::Manip::inst(os).encoding = "u8"_id; return os; }
73 inline istream& u8(istream& is) { priv::Manip::inst(is).encoding = "u8"_id; return is; }
74 
76 inline bool isBase32(Char c) { return c >= '2' && c < '2' + sizeof(priv::base32_chars_rev) && priv::base32_chars_rev[c - '2'] != -1; }
78 inline Char toBase32(byte b) { return priv::base32_chars[b]; }
80 inline byte fromBase32(Char c) { return priv::base32_chars_rev[c - '2']; }
84 Bytes base32_decode(const String& string);
86 inline ostream& base32(ostream& os) { priv::Manip::inst(os).encoding = "base32"_id; return os; }
88 inline istream& base32(istream& is) { priv::Manip::inst(is).encoding = "base32"_id; return is; }
89 
91 inline bool isBase64(Char c) { return c >= '+' && c < '+' + sizeof(priv::base64_chars_rev) && priv::base64_chars_rev[c - '+'] != -1; }
93 inline Char toBase64(byte b) { return priv::base64_chars[b]; }
95 inline byte fromBase64(Char c) { return priv::base64_chars_rev[c - '+']; }
99 Bytes base64_decode(const String& string);
101 inline ostream& base64(ostream& os) { priv::Manip::inst(os).encoding = "base64"_id; return os; }
103 inline istream& base64(istream& is) { priv::Manip::inst(is).encoding = "base64"_id; return is; }
104 
105 } }
bool isDec(Char c)
Check if character is in decimal charset (numeric)
Definition: Encode.h:56
#define mt_global(Class, Func, Ctor)
Create a global object that will be initialized upon first access, so it can be accessed safely from ...
Definition: Meta.h:283
A contiguous region of referenced (not owned by object) memory.
Definition: Buffer.h:17
byte fromBase64(Char c)
Convert base64 character to byte.
Definition: Encode.h:95
#define mtkey(Name)
Construct a key type with name.
Definition: MtMap.h:63
Char toHex(byte b)
Convert byte to hexadecimal character.
Definition: Encode.h:43
uint8 byte
An unsigned 8-bit integer.
Definition: Bytes.h:12
String dec_encode(ByteBufConst bs)
Convert bytes to string using decimal encoding (big-endian integer)
Definition: Encode.cpp:131
Bytes base64_decode(const String &string)
Convert string to bytes using base64 decoding.
Definition: Encode.cpp:410
byte fromDec(Char c)
Convert decimal character to byte.
Definition: Encode.h:60
String base64_encode(ByteBufConst bs)
Convert bytes to string using base64 encoding.
Definition: Encode.cpp:373
byte fromHex(Char c)
Convert hexadecimal character to byte.
Definition: Encode.h:45
Char toBase32(byte b)
Convert byte to base32 character.
Definition: Encode.h:78
Bytes base32_decode(const String &string)
Convert string to bytes using base32 decoding.
Definition: Encode.cpp:294
ostream & dec(ostream &os)
Use decimal encoding (big-endian integer) when writing bytes to a string stream.
Definition: Encode.h:66
std::unordered_map< Key, Value, std::hash< Key >, std::equal_to< Key >, Alloc< pair< const Key, Value >>> unordered_map
std::unordered_map with custom allocator
Definition: StdUtil.h:83
String base32_encode(ByteBufConst bs)
Convert bytes to string using base32 encoding.
Definition: Encode.cpp:238
char int8
Definition: Core.h:11
Char toDec(byte b)
Convert byte to decimal character.
Definition: Encode.h:58
Bytes hex_decode(const String &string)
Convert string to bytes using hexadecimal decoding (high-nibble-first)
Definition: Encode.cpp:100
bool isBase32(Char c)
Check if character is in base32 charset (case-insensitive [a-z], numeric [2-7], and padding '=') ...
Definition: Encode.h:76
char16_t Char
Represents a single code unit (not code point) for class String.
Definition: String.h:13
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
ostream & hex(ostream &os)
Use hexadecimal encoding (high-nibble-first) when writing bytes to a string stream.
Definition: Encode.h:51
String of bytes.
Definition: Bytes.h:26
ostream & u8(ostream &os)
Use UTF-8 encoding when writing bytes to a string stream.
Definition: Encode.h:71
Bytes dec_decode(const String &string)
Convert string to bytes using decimal decoding (big-endian integer)
Definition: Encode.cpp:166
ostream & base64(ostream &os)
Use base64 encoding when writing bytes to a string stream.
Definition: Encode.h:101
bool isBase64(Char c)
Check if character is in base64 charset (alphanumeric, symbols [+/], and padding '=') ...
Definition: Encode.h:91
Char toBase64(byte b)
Convert byte to base64 character.
Definition: Encode.h:93
Base class to hold iostream manipulator state. Inherit from this class and call Subclass::inst(ios) t...
Definition: StdUtil.h:206
ostream & base32(ostream &os)
Use base32 encoding when writing bytes to a string stream.
Definition: Encode.h:86
String hex_encode(ByteBufConst bs)
Convert bytes to string using hexadecimal encoding (high-nibble-first)
Definition: Encode.cpp:87
bool isHex(Char c)
Check if character is in hexadecimal charset (numeric and case-insensitive [a-f]) ...
Definition: Encode.h:41
byte fromBase32(Char c)
Convert base32 character to byte.
Definition: Encode.h:80
Global Honeycomb namespace.
typename priv::MtMap_< Pairs... >::type MtMap
Declare a map type with MtMap
Definition: MtMap.h:46
bool reg(const Id &id, const function< ostream &(ostream &os, const Bytes &val)> &encode, const function< istream &(istream &os, Bytes &val)> &decode)
Register an encoding.
Definition: Encode.cpp:61