Joedb 10.5.0
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, std::string_view 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 (c < 0x20 || c == 0x7f)
49 {
50 if (json)
51 {
52 out.put('\\');
53 out.put('u');
54 out.put('0');
55 out.put('0');
56 out.put(get_hex_char_from_digit(uint8_t(c >> 4)));
57 out.put(get_hex_char_from_digit(uint8_t(c & 0x0f)));
58 }
59 else
61 }
62 else if (c == '?' && i > 0 && s[i - 1] == '?' && !json) // C++ trigraphs
63 out.put('\\').put('?');
64 else if (c == '"')
65 out.put('\\').put('"');
66 else if (c == '\\')
67 out.put('\\').put('\\');
68 else if (c >= 0xc2 && c <= 0xdf &&
69 i + 1 < s.size() &&
70 (s[i + 1] & 0xc0) == 0x80)
71 {
72 out.put(c);
73 out.put(s[++i]);
74 }
75 else if ((c & 0xf0) == 0xe0 &&
76 i + 2 < s.size() &&
77 (c != 0xe0 || uint8_t(s[i + 1]) >= 0xa0) &&
78 (c != 0xed || uint8_t(s[i + 1]) < 0xa0) &&
79 (s[i + 1] & 0xc0) == 0x80 &&
80 (s[i + 2] & 0xc0) == 0x80)
81 {
82 out.put(c);
83 out.put(s[++i]);
84 out.put(s[++i]);
85 }
86 else if (c >= 0xf0 && c <= 0xf4 &&
87 i + 3 < s.size() &&
88 (c != 0xf0 || uint8_t(s[i + 1]) >= 0x90) &&
89 (c != 0xf4 || uint8_t(s[i + 1]) < 0x90) &&
90 (s[i + 1] & 0xc0) == 0x80 &&
91 (s[i + 2] & 0xc0) == 0x80 &&
92 (s[i + 3] & 0xc0) == 0x80)
93 {
94 out.put(char(c));
95 out.put(s[++i]);
96 out.put(s[++i]);
97 out.put(s[++i]);
98 }
99 else if (c & 0x80)
100 {
101 write_octal_character(out, c, json);
102 }
103 else
104 {
105 out.put(char(c));
106 }
107 }
108
109 out.put('"');
110 }
111
112 /////////////////////////////////////////////////////////////////////////////
113 void write_int8(std::ostream &out, int8_t value)
114 /////////////////////////////////////////////////////////////////////////////
115 {
116 out << int(value);
117 }
118}
void write_octal_character(std::ostream &out, uint8_t c)
void write_int8(std::ostream &out, int8_t value)
void write_string(std::ostream &out, std::string_view s, bool json)
char get_hex_char_from_digit(uint8_t n)