libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
writer_runtime.cpp
1#include "writer.hpp"
2
3namespace LibXR::Print
4{
5Writer::CodeReader::CodeReader(const uint8_t* codes) : pos_(codes), base_(codes) {}
6
7FormatOp Writer::CodeReader::ReadOp() { return static_cast<FormatOp>(*pos_++); }
8
10{
11 return static_cast<FormatType>(*pos_++);
12}
13
15{
16 return Spec{.flags = *pos_++,
17 .fill = static_cast<char>(*pos_++),
18 .width = *pos_++,
19 .precision = *pos_++};
20}
21
23{
24 auto text = reinterpret_cast<const char*>(pos_);
25 size_t size = std::strlen(text);
26 pos_ += size + 1;
27 return std::string_view(text, size);
28}
29
31{
32 auto offset = Read<uint16_t>();
33 auto size = Read<uint16_t>();
34 auto text = reinterpret_cast<const char*>(base_ + offset);
35 return std::string_view(text, size);
36}
37
38Writer::ArgumentReader::ArgumentReader(const uint8_t* data) : pos_(data) {}
39
40bool Writer::AppendBufferChar(char* buffer, size_t capacity, size_t& size, char ch)
41{
42 if (size >= capacity)
43 {
44 return false;
45 }
46 buffer[size++] = ch;
47 return true;
48}
49
50bool Writer::AppendBufferText(char* buffer, size_t capacity, size_t& size,
51 std::string_view text)
52{
53 if (size > capacity || text.size() > capacity - size)
54 {
55 return false;
56 }
57 std::memcpy(buffer + size, text.data(), text.size());
58 size += text.size();
59 return true;
60}
61
62bool Writer::AppendBufferU32ZeroPad(char* buffer, size_t capacity, size_t& size,
63 uint32_t value, uint8_t width)
64{
66 size_t digit_count = AppendUnsigned<10>(digits, value);
67 size_t zero_count =
68 (width > digit_count) ? static_cast<size_t>(width) - digit_count : 0;
69
70 for (size_t i = 0; i < zero_count; ++i)
71 {
72 if (!AppendBufferChar(buffer, capacity, size, '0'))
73 {
74 return false;
75 }
76 }
77
78 return AppendBufferText(buffer, capacity, size, std::string_view(digits, digit_count));
79}
80
81#if LIBXR_PRINT_ENABLE_FLOAT
82
83size_t Writer::TrimGeneralText(char* text, size_t size)
84{
85 size_t exponent_pos = size;
86 for (size_t i = 0; i < size; ++i)
87 {
88 if (text[i] == 'e' || text[i] == 'E')
89 {
90 exponent_pos = i;
91 break;
92 }
93 }
94
95 size_t mantissa_end = exponent_pos;
96 while (mantissa_end > 0 && text[mantissa_end - 1] == '0')
97 {
98 --mantissa_end;
99 }
100 if (mantissa_end > 0 && text[mantissa_end - 1] == '.')
101 {
102 --mantissa_end;
103 }
104
105 if (exponent_pos == size)
106 {
107 return mantissa_end;
108 }
109
110 std::memmove(text + mantissa_end, text + exponent_pos, size - exponent_pos);
111 return mantissa_end + (size - exponent_pos);
112}
113
114bool Writer::AppendExponentText(char* out, size_t& out_size, int exponent,
115 bool upper_case)
116{
117 if (!AppendBufferChar(out, float_buffer_capacity, out_size, upper_case ? 'E' : 'e'))
118 {
119 return false;
120 }
121 if (!AppendBufferChar(out, float_buffer_capacity, out_size, exponent < 0 ? '-' : '+'))
122 {
123 return false;
124 }
125
127 unsigned int magnitude = static_cast<unsigned int>(exponent < 0 ? -exponent : exponent);
128 size_t digit_count = AppendUnsigned<10>(digits, magnitude);
129 if (digit_count < 2 && !AppendBufferChar(out, float_buffer_capacity, out_size, '0'))
130 {
131 return false;
132 }
133
134 return AppendBufferText(out, float_buffer_capacity, out_size,
135 std::string_view(digits, digit_count));
136}
137
138#endif
139} // namespace LibXR::Print
ArgumentReader(const uint8_t *data)
从单个运行期参数打包字节块构造读取器 / Create a reader over one packed runtime argument blob
Spec ReadSpec()
读取紧跟在 GenericField 类型字节后的 4 字节字段载荷 / Read the 4-byte field payload that follows one GenericField type...
std::string_view ReadInlineText()
读取内嵌在记录流中的短文本 / Read a null-terminated short text payload embedded in the record stream
std::string_view ReadTextRef()
读取指向尾部文本池的偏移和长度 / Read an offset-size pair pointing into the trailing text pool
CodeReader(const uint8_t *codes)
从单个编译字节块起点构造读取器 / Create a reader over the beginning of one compiled byte blob
FormatOp ReadOp()
读取下一条运行期操作码 / Read the next runtime opcode
FormatType ReadFormatType()
读取 GenericField 载荷中的语义类型字节 / Read the semantic type byte carried by one GenericField payload
static consteval size_t UnsignedDigitCapacity()
返回某个无符号整数在指定进制下所需的最大数字个数 / Return the maximum digit count required for one unsigned integer under the...
Definition writer.hpp:10
static bool AppendBufferText(char *buffer, size_t capacity, size_t &size, std::string_view text)
向有界本地格式化缓冲区追加一段文本 / Append one text span to a bounded local formatting buffer
static bool AppendBufferU32ZeroPad(char *buffer, size_t capacity, size_t &size, uint32_t value, uint8_t width)
追加一个 uint32_t 十进制值,并在前面补零到目标宽度 / Append one uint32_t decimal value and pad leading zeros up to the ta...
static size_t AppendUnsigned(char(&out)[N], UInt value)
把一个无符号整数写进调用方提供的定长数字缓冲区 / Append one unsigned integer into a caller-provided fixed-size digit buffer
Definition writer.hpp:38
static bool AppendBufferChar(char *buffer, size_t capacity, size_t &size, char ch)
向有界本地格式化缓冲区追加一个字符 / Append one character to a bounded local formatting buffer
运行期对单个字段描述字节组的解码视图 / Runtime view of one decoded field specification byte group
Definition writer.hpp:188