authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-26 23:44:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:57-07:00
loge9a271cba3c79358cc8d24a3892fbbefc4e12d9d
treefe16372f3f789648a066bae301d045a5bf94f8ef
parent9860dd475ab754f85f2f8c96039255bda8721002

std.tz: update to new Reader API


1 files changed, 29 insertions(+), 27 deletions(-)

lib/std/tz.zig+29-27
......@@ -1,6 +1,9 @@
1const std = @import("std.zig");
21const builtin = @import("builtin");
32
3const std = @import("std.zig");
4const Reader = std.Io.Reader;
5const Allocator = std.mem.Allocator;
6
47pub const Transition = struct {
58 ts: i64,
69 timetype: *Timetype,
......@@ -34,7 +37,7 @@ pub const Leapsecond = struct {
3437};
3538
3639pub const Tz = struct {
37 allocator: std.mem.Allocator,
40 allocator: Allocator,
3841 transitions: []const Transition,
3942 timetypes: []const Timetype,
4043 leapseconds: []const Leapsecond,
......@@ -54,8 +57,8 @@ pub const Tz = struct {
5457 },
5558 };
5659
57 pub fn parse(allocator: std.mem.Allocator, reader: anytype) !Tz {
58 var legacy_header = try reader.readStruct(Header);
60 pub fn parse(allocator: Allocator, reader: *Reader) !Tz {
61 var legacy_header = try reader.takeStruct(Header, .little);
5962 if (!std.mem.eql(u8, &legacy_header.magic, "TZif")) return error.BadHeader;
6063 if (legacy_header.version != 0 and legacy_header.version != '2' and legacy_header.version != '3') return error.BadVersion;
6164
......@@ -68,9 +71,9 @@ pub const Tz = struct {
6871 } else {
6972 // If the format is modern, just skip over the legacy data
7073 const skipv = legacy_header.counts.timecnt * 5 + legacy_header.counts.typecnt * 6 + legacy_header.counts.charcnt + legacy_header.counts.leapcnt * 8 + legacy_header.counts.isstdcnt + legacy_header.counts.isutcnt;
71 try reader.skipBytes(skipv, .{});
74 try reader.discardAll(skipv);
7275
73 var header = try reader.readStruct(Header);
76 var header = try reader.takeStruct(Header, .little);
7477 if (!std.mem.eql(u8, &header.magic, "TZif")) return error.BadHeader;
7578 if (header.version != '2' and header.version != '3') return error.BadVersion;
7679 if (builtin.target.cpu.arch.endian() != std.builtin.Endian.big) {
......@@ -81,7 +84,7 @@ pub const Tz = struct {
8184 }
8285 }
8386
84 fn parseBlock(allocator: std.mem.Allocator, reader: anytype, header: Header, legacy: bool) !Tz {
87 fn parseBlock(allocator: Allocator, reader: *Reader, header: Header, legacy: bool) !Tz {
8588 if (header.counts.isstdcnt != 0 and header.counts.isstdcnt != header.counts.typecnt) return error.Malformed; // rfc8536: isstdcnt [...] MUST either be zero or equal to "typecnt"
8689 if (header.counts.isutcnt != 0 and header.counts.isutcnt != header.counts.typecnt) return error.Malformed; // rfc8536: isutcnt [...] MUST either be zero or equal to "typecnt"
8790 if (header.counts.typecnt == 0) return error.Malformed; // rfc8536: typecnt [...] MUST NOT be zero
......@@ -98,12 +101,12 @@ pub const Tz = struct {
98101 // Parse transition types
99102 var i: usize = 0;
100103 while (i < header.counts.timecnt) : (i += 1) {
101 transitions[i].ts = if (legacy) try reader.readInt(i32, .big) else try reader.readInt(i64, .big);
104 transitions[i].ts = if (legacy) try reader.takeInt(i32, .big) else try reader.takeInt(i64, .big);
102105 }
103106
104107 i = 0;
105108 while (i < header.counts.timecnt) : (i += 1) {
106 const tt = try reader.readByte();
109 const tt = try reader.takeByte();
107110 if (tt >= timetypes.len) return error.Malformed; // rfc8536: Each type index MUST be in the range [0, "typecnt" - 1]
108111 transitions[i].timetype = &timetypes[tt];
109112 }
......@@ -111,11 +114,11 @@ pub const Tz = struct {
111114 // Parse time types
112115 i = 0;
113116 while (i < header.counts.typecnt) : (i += 1) {
114 const offset = try reader.readInt(i32, .big);
117 const offset = try reader.takeInt(i32, .big);
115118 if (offset < -2147483648) return error.Malformed; // rfc8536: utoff [...] MUST NOT be -2**31
116 const dst = try reader.readByte();
119 const dst = try reader.takeByte();
117120 if (dst != 0 and dst != 1) return error.Malformed; // rfc8536: (is)dst [...] The value MUST be 0 or 1.
118 const idx = try reader.readByte();
121 const idx = try reader.takeByte();
119122 if (idx > header.counts.charcnt - 1) return error.Malformed; // rfc8536: (desig)idx [...] Each index MUST be in the range [0, "charcnt" - 1]
120123 timetypes[i] = .{
121124 .offset = offset,
......@@ -128,7 +131,7 @@ pub const Tz = struct {
128131 }
129132
130133 var designators_data: [256 + 6]u8 = undefined;
131 try reader.readNoEof(designators_data[0..header.counts.charcnt]);
134 try reader.readSliceAll(designators_data[0..header.counts.charcnt]);
132135 const designators = designators_data[0..header.counts.charcnt];
133136 if (designators[designators.len - 1] != 0) return error.Malformed; // rfc8536: charcnt [...] includes the trailing NUL (0x00) octet
134137
......@@ -144,12 +147,12 @@ pub const Tz = struct {
144147 // Parse leap seconds
145148 i = 0;
146149 while (i < header.counts.leapcnt) : (i += 1) {
147 const occur: i64 = if (legacy) try reader.readInt(i32, .big) else try reader.readInt(i64, .big);
150 const occur: i64 = if (legacy) try reader.takeInt(i32, .big) else try reader.takeInt(i64, .big);
148151 if (occur < 0) return error.Malformed; // rfc8536: occur [...] MUST be nonnegative
149152 if (i > 0 and leapseconds[i - 1].occurrence + 2419199 > occur) return error.Malformed; // rfc8536: occur [...] each later value MUST be at least 2419199 greater than the previous value
150153 if (occur > std.math.maxInt(i48)) return error.Malformed; // Unreasonably far into the future
151154
152 const corr = try reader.readInt(i32, .big);
155 const corr = try reader.takeInt(i32, .big);
153156 if (i == 0 and corr != -1 and corr != 1) return error.Malformed; // rfc8536: The correction value in the first leap-second record, if present, MUST be either one (1) or minus one (-1)
154157 if (i > 0 and leapseconds[i - 1].correction != corr + 1 and leapseconds[i - 1].correction != corr - 1) return error.Malformed; // rfc8536: The correction values in adjacent leap-second records MUST differ by exactly one (1)
155158 if (corr > std.math.maxInt(i16)) return error.Malformed; // Unreasonably large correction
......@@ -163,7 +166,7 @@ pub const Tz = struct {
163166 // Parse standard/wall indicators
164167 i = 0;
165168 while (i < header.counts.isstdcnt) : (i += 1) {
166 const stdtime = try reader.readByte();
169 const stdtime = try reader.takeByte();
167170 if (stdtime == 1) {
168171 timetypes[i].flags |= 0x02;
169172 }
......@@ -172,7 +175,7 @@ pub const Tz = struct {
172175 // Parse UT/local indicators
173176 i = 0;
174177 while (i < header.counts.isutcnt) : (i += 1) {
175 const ut = try reader.readByte();
178 const ut = try reader.takeByte();
176179 if (ut == 1) {
177180 timetypes[i].flags |= 0x04;
178181 if (!timetypes[i].standardTimeIndicator()) return error.Malformed; // rfc8536: standard/wall value MUST be one (1) if the UT/local value is one (1)
......@@ -182,9 +185,8 @@ pub const Tz = struct {
182185 // Footer
183186 var footer: ?[]u8 = null;
184187 if (!legacy) {
185 if ((try reader.readByte()) != '\n') return error.Malformed; // An rfc8536 footer must start with a newline
186 var footerdata_buf: [128]u8 = undefined;
187 const footer_mem = reader.readUntilDelimiter(&footerdata_buf, '\n') catch |err| switch (err) {
188 if ((try reader.takeByte()) != '\n') return error.Malformed; // An rfc8536 footer must start with a newline
189 const footer_mem = reader.takeSentinel('\n') catch |err| switch (err) {
188190 error.StreamTooLong => return error.OverlargeFooter, // Read more than 128 bytes, much larger than any reasonable POSIX TZ string
189191 else => return err,
190192 };
......@@ -194,7 +196,7 @@ pub const Tz = struct {
194196 }
195197 errdefer if (footer) |ft| allocator.free(ft);
196198
197 return Tz{
199 return .{
198200 .allocator = allocator,
199201 .transitions = transitions,
200202 .timetypes = timetypes,
......@@ -215,9 +217,9 @@ pub const Tz = struct {
215217
216218test "slim" {
217219 const data = @embedFile("tz/asia_tokyo.tzif");
218 var in_stream = std.io.fixedBufferStream(data);
220 var in_stream: Reader = .fixed(data);
219221
220 var tz = try std.Tz.parse(std.testing.allocator, in_stream.reader());
222 var tz = try std.Tz.parse(std.testing.allocator, &in_stream);
221223 defer tz.deinit();
222224
223225 try std.testing.expectEqual(tz.transitions.len, 9);
......@@ -228,9 +230,9 @@ test "slim" {
228230
229231test "fat" {
230232 const data = @embedFile("tz/antarctica_davis.tzif");
231 var in_stream = std.io.fixedBufferStream(data);
233 var in_stream: Reader = .fixed(data);
232234
233 var tz = try std.Tz.parse(std.testing.allocator, in_stream.reader());
235 var tz = try std.Tz.parse(std.testing.allocator, &in_stream);
234236 defer tz.deinit();
235237
236238 try std.testing.expectEqual(tz.transitions.len, 8);
......@@ -241,9 +243,9 @@ test "fat" {
241243test "legacy" {
242244 // Taken from Slackware 8.0, from 2001
243245 const data = @embedFile("tz/europe_vatican.tzif");
244 var in_stream = std.io.fixedBufferStream(data);
246 var in_stream: Reader = .fixed(data);
245247
246 var tz = try std.Tz.parse(std.testing.allocator, in_stream.reader());
248 var tz = try std.Tz.parse(std.testing.allocator, &in_stream);
247249 defer tz.deinit();
248250
249251 try std.testing.expectEqual(tz.transitions.len, 170);