-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathToString.zig
217 lines (193 loc) · 6.03 KB
/
ToString.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// TODO: Remove this? Replace with std writers/formatters?
const std = @import("std");
const utils = @import("utils.zig");
pub const Error = utils.Error;
const ToString = @This();
buffer: ?[]u8 = null,
got: usize = 0,
ext_func: ?fn(self: *ToString, str: []const u8) void = null,
truncate: ?[]const u8 = null,
fn left(self: *const ToString) usize {
if (self.buffer) |buf| {
return buf.len - self.got;
}
return std.math.maxInt(usize);
}
pub fn need(self: *const ToString, space: usize) Error!void {
if (self.truncate == null and space > self.left()) {
return Error.NotEnoughDestination;
}
}
pub fn get(self: *ToString) []u8 {
if (self.buffer) |buf| {
return buf[0..@minimum(buf.len, self.got)];
}
@panic("This ToString doesn't have a buffer set");
}
pub fn upgrade_buffer(self: *ToString, new: []u8) Error!?[]u8 {
if (self.buffer) |buf| {
_ = try utils.memory_copy_error(new, buf);
self.buffer = new;
return buf;
}
self.buffer = new;
self.got = 0;
return null;
}
fn _str(self: *ToString, s: []const u8) void {
if (self.buffer) |buf| {
if (self.left() == 0) {
return;
}
var from = s;
var i: usize = 0;
var in_truncated = false;
while (i < from.len) {
if (self.got >= buf.len) {
break;
} else if (self.truncate) |truncate_end| {
if (!in_truncated and self.left() == truncate_end.len) {
in_truncated = true;
from = truncate_end[0..];
continue;
}
}
buf[self.got] = from[i];
self.got += 1;
i += 1;
}
}
if (self.ext_func) |ef| {
ef(self, s);
}
}
fn _char(self: *ToString, c: u8) void {
self._str(([_]u8{c})[0..]);
}
pub fn char(self: *ToString, c: u8) Error!void {
try self.need(1);
self._char(c);
}
pub fn string(self: *ToString, src: []const u8) Error!void {
try self.need(src.len);
self._str(src);
}
pub fn string_truncate(self: *ToString, src: []const u8) void {
self._str(src[0..@minimum(src.len, self.left())]);
}
pub fn cstring(self: *ToString, cstr: [*:0]const u8) Error!void {
try self.string(utils.cstring_to_string(cstr));
}
fn hex_recurse(self: *ToString, value: usize) void {
const next = value / 0x10;
if (next > 0) {
self.hex_recurse(next);
}
self._char(utils.nibble_char(@intCast(u4, value % 0x10)));
}
pub fn hex(self: *ToString, value: usize) Error!void {
try self.need(2 + utils.hex_char_len(usize, value));
self._char('0');
self._char('x');
if (value == 0) {
self._char('0');
return;
}
self.hex_recurse(value);
}
fn _int_recurse(self: *ToString, sign: ?u8, needs: usize, value: anytype) Error!void {
const next = value / 10;
if (next > 0) {
try self._int_recurse(sign, needs + 1, next);
} else {
const add: usize = if (sign != null) 1 else 0;
try self.need(needs + add);
if (sign) |s| self._char(s);
}
self._char('0' + @intCast(u8, value % 10));
}
pub fn _int(self: *ToString, value: anytype) Error!void {
if (value == 0) {
try self.char('0');
return;
}
const signed = std.meta.trait.isSignedInt(@TypeOf(value));
try self._int_recurse(if (signed and value < 0) '-' else null, 1,
std.math.absCast(value));
}
pub fn int(self: *ToString, value: anytype) Error!void {
const Type = @TypeOf(value);
comptime std.debug.assert(@typeInfo(Type) == .Int);
try self._int(value);
}
pub fn uint(self: *ToString, value: usize) Error!void {
try self._int(value);
}
pub fn std_write(self: *ToString, bytes: []const u8) Error!usize {
try self.string(bytes);
return bytes.len;
}
pub const StdWriter = std.io.Writer(*ToString, Error, std_write);
pub fn std_writer(self: *ToString) StdWriter {
return StdWriter{.context = self};
}
test "ToString" {
{
var buffer: [128]u8 = undefined;
var ts = ToString{.buffer = buffer[0..]};
try ts.string("Hello ");
try ts.string("");
try ts.cstring("goodbye!");
try ts.cstring("");
try std.testing.expectEqualStrings("Hello goodbye!", ts.get());
}
{
var buffer: [1]u8 = undefined;
var ts = ToString{.buffer = buffer[0..]};
try std.testing.expectError(Error.NotEnoughDestination, ts.string("Hello"));
try std.testing.expectEqualStrings("", ts.get());
}
{
var buffer: [128]u8 = undefined;
var ts = ToString{.buffer = buffer[0..]};
try ts.hex(0x0);
try ts.hex(0x1);
try ts.hex(0xf);
try ts.hex(0x10);
try ts.hex(0xff);
try ts.hex(0x100);
try std.testing.expectEqualStrings("0x00x10xf0x100xff0x100", ts.get());
}
{
var buffer: [1]u8 = undefined;
var ts = ToString{.buffer = buffer[0..]};
try std.testing.expectError(Error.NotEnoughDestination, ts.hex(0xff));
try std.testing.expectEqualStrings("", ts.get());
}
{
var buffer: [10]u8 = undefined;
var ts = ToString{.buffer = buffer[0..]};
try ts.uint(10); // 2
try ts.uint(1234); // 6
try std.testing.expectError(Error.NotEnoughDestination, ts.uint(56789)); // 11
try ts.uint(5678); // 10
try std.testing.expectEqualStrings("1012345678", ts.get());
}
{
var buffer: [10]u8 = undefined;
var ts = ToString{.buffer = buffer[0..], .truncate = ".."};
try ts.uint(10);
try ts.uint(1234);
try std.testing.expectEqualStrings("101234", ts.get());
try ts.uint(56789);
try std.testing.expectEqualStrings("10123456..", ts.get());
try ts.string("another thing");
try std.testing.expectEqualStrings("10123456..", ts.get());
}
{
var buffer: [10]u8 = undefined;
var ts = ToString{.buffer = buffer[0..]};
try ts.std_writer().print("0o{o}", .{0o123});
try std.testing.expectEqualStrings("0o123", ts.get());
}
}