Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
minimal_runtime_io.cpp
Go to the documentation of this file.
1#include "joedb/ui/type_io.h"
3
4namespace joedb
5{
6 /////////////////////////////////////////////////////////////////////////////
7 char get_hex_char_from_digit(uint8_t n)
8 /////////////////////////////////////////////////////////////////////////////
9 {
10 n &= 0x0f;
11
12 if (n < 10)
13 return char('0' + n);
14 else
15 return char('a' + n - 10);
16 }
17
18 /////////////////////////////////////////////////////////////////////////////
19 void write_octal_character(std::ostream &out, uint8_t c)
20 /////////////////////////////////////////////////////////////////////////////
21 {
22 out.put('\\');
23 out.put(char('0' + ((c >> 6) & 7)));
24 out.put(char('0' + ((c >> 3) & 7)));
25 out.put(char('0' + ((c >> 0) & 7)));
26 }
27
28 ////////////////////////////////////////////////////////////////////////////
29 static void write_octal_character(std::ostream &out, uint8_t c, bool json)
30 ////////////////////////////////////////////////////////////////////////////
31 {
32 if (json)
33 throw Exception("json can't handle non-utf8 strings");
34 else
36 }
37
38 /////////////////////////////////////////////////////////////////////////////
39 void write_string(std::ostream &out, const std::string &s, bool json)
40 /////////////////////////////////////////////////////////////////////////////
41 {
42 out.put('"');
43
44 for (size_t i = 0; i < s.size(); i++)
45 {
46 const uint8_t c = uint8_t(s[i]);
47
48 #if 0
50 #else
51 if (c < 0x20 || c == 0x7f)
52 {
53 if (json)
54 {
55 out.put('\\');
56 out.put('u');
57 out.put('0');
58 out.put('0');
59 out.put(get_hex_char_from_digit(uint8_t(c >> 4)));
60 out.put(get_hex_char_from_digit(uint8_t(c & 0x0f)));
61 }
62 else
64 }
65 else if (c == '"')
66 out.put('\\').put('"');
67 else if (c == '\\')
68 out.put('\\').put('\\');
69 else if ((c & 0xe0) == 0xc0 &&
70 i + 1 < s.size() &&
71 (s[i + 1] & 0xc0) == 0x80)
72 {
73 out.put(c);
74 out.put(s[++i]);
75 }
76 else if // UTF-16 surrogate halves
77 (
78 c == 0xed &&
79 i + 1 < s.size() &&
80 (uint8_t(s[i + 1]) & 0xa0) == 0xa0
81 )
82 {
83 write_octal_character(out, c, json);
84 }
85 else if ((c & 0xf0) == 0xe0 &&
86 i + 2 < s.size() &&
87 (s[i + 1] & 0xc0) == 0x80 &&
88 (s[i + 2] & 0xc0) == 0x80)
89 {
90 out.put(c);
91 out.put(s[++i]);
92 out.put(s[++i]);
93 }
94 else if ((c & 0xf8) == 0xf0 &&
95 i + 3 < s.size() &&
96 (s[i + 1] & 0xc0) == 0x80 &&
97 (s[i + 2] & 0xc0) == 0x80 &&
98 (s[i + 3] & 0xc0) == 0x80)
99 {
100 out.put(char(c));
101 out.put(s[++i]);
102 out.put(s[++i]);
103 out.put(s[++i]);
104 }
105 else if (c & 0x80)
106 {
107 write_octal_character(out, c, json);
108 }
109 else
110 {
111 out.put(char(c));
112 }
113 #endif
114 }
115
116 out.put('"');
117 }
118
119 /////////////////////////////////////////////////////////////////////////////
120 void write_int8(std::ostream &out, int8_t value)
121 /////////////////////////////////////////////////////////////////////////////
122 {
123 out << int(value);
124 }
125}
void write_octal_character(std::ostream &out, uint8_t c)
void write_string(std::ostream &out, const std::string &s, bool json)
void write_int8(std::ostream &out, int8_t value)
char get_hex_char_from_digit(uint8_t n)
Definition Blob.h:7