1 module tagion.hibon.HiBONtoText;
2 @safe:
3 /**
4  HiBON Base64 with  ':' added in the front of the string as an indetifyer
5  is base64 base on the flowing ASCII characters
6  "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 +/"
7 
8             1111111111222222 22223333333334444444444455 5555555566 66
9   01234567890123456789012345 67890123456789012345678901 2345678901 23
10 
11   and = as padding
12 
13 
14 */
15 
16 import std.format;
17 import tagion.hibon.HiBONException;
18 import misc = tagion.utils.Miscellaneous;
19 import std.base64;
20 import std.typecons : TypedefType;
21 public import tagion.basic.Types;
22 import tagion.basic.Types : encodeBase64;
23 import tagion.hibon.Document;
24 import tagion.hibon.HiBONRecord;
25 
26 //alias toHex = misc.toHexString;
27 
28 enum {
29     hex_prefix = "0x",
30     HEX_PREFIX = "0X"
31 }
32 
33 string encodeBase64(const(Document) doc) pure {
34     return encodeBase64(doc.data);
35 }
36 
37 string encodeBase64(T)(const(T) t) pure
38 if (isHiBONRecord!T) {
39     return encodeBase64(t.serialize);
40 }
41 
42 @nogc bool isHexPrefix(const(char[]) str) pure nothrow {
43     if (str.length >= hex_prefix.length) {
44         return (str[0 .. hex_prefix.length] == hex_prefix)
45             || (str[0 .. HEX_PREFIX.length] == HEX_PREFIX);
46     }
47     return false;
48 }
49 
50 @nogc bool isBase64Prefix(const(char[]) str) pure nothrow {
51     return (str.length > 0) && (str[0] is BASE64Indetifyer);
52 }
53 
54 immutable(ubyte[]) decode(const(char[]) str) pure {
55     if (isBase64Prefix(str)) {
56         return Base64URL.decode(str[1 .. $]).idup;
57     }
58     else if (isHexPrefix(str)) {
59         return misc.decode(str[hex_prefix.length .. $]);
60     }
61     return misc.decode(str);
62 }
63 
64 Document decodeBase64(const(char[]) str) pure {
65     return Document(decode(str));
66 }