| author | |
| committer | |
| log | db70f7e35558c0df34cce0497ec0eea8e984d30d |
| tree | 468bca13780ba3868d69fc0ed7edfdde853d4685 |
| parent | 096c970516efebab24d3424f39040efb53ffb183 |
| signature |
9 files changed, 0 insertions(+), 366 deletions(-)
src/BufIndexer.zig deleted-61| ... | ... | @@ -1,61 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | const indexBufferT = extras.indexBufferT; | |
| 5 | const rawIntBytes = extras.rawIntBytes; | |
| 6 | ||
| 7 | pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { | |
| 8 | return struct { | |
| 9 | bytes: [*]const u8, | |
| 10 | max_len: usize, | |
| 11 | ||
| 12 | const Self = @This(); | |
| 13 | ||
| 14 | pub fn init(bytes: [*]const u8, max_len: usize) Self { | |
| 15 | return .{ | |
| 16 | .bytes = bytes, | |
| 17 | .max_len = max_len, | |
| 18 | }; | |
| 19 | } | |
| 20 | ||
| 21 | /// asserts 'idx' to be in bounds | |
| 22 | pub fn at(self: *const Self, idx: usize) T { | |
| 23 | return indexBufferT(self.bytes, T, endian, idx, self.max_len); | |
| 24 | } | |
| 25 | }; | |
| 26 | } | |
| 27 | ||
| 28 | test { | |
| 29 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); | |
| 30 | const bindex = BufIndexer(u64, .big).init(&bytes, 1); | |
| 31 | try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132); | |
| 32 | } | |
| 33 | ||
| 34 | test { | |
| 35 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); | |
| 36 | const bindex = BufIndexer(u32, .big).init(&bytes, 2); | |
| 37 | try std.testing.expect(bindex.at(0) == 0x4e5a7da9); | |
| 38 | try std.testing.expect(bindex.at(1) == 0xf3f1d132); | |
| 39 | } | |
| 40 | ||
| 41 | test { | |
| 42 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); | |
| 43 | const bindex = BufIndexer(u16, .big).init(&bytes, 4); | |
| 44 | try std.testing.expect(bindex.at(0) == 0x4e5a); | |
| 45 | try std.testing.expect(bindex.at(1) == 0x7da9); | |
| 46 | try std.testing.expect(bindex.at(2) == 0xf3f1); | |
| 47 | try std.testing.expect(bindex.at(3) == 0xd132); | |
| 48 | } | |
| 49 | ||
| 50 | test { | |
| 51 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); | |
| 52 | const bindex = BufIndexer(u8, .big).init(&bytes, 8); | |
| 53 | try std.testing.expect(bindex.at(0) == 0x4e); | |
| 54 | try std.testing.expect(bindex.at(1) == 0x5a); | |
| 55 | try std.testing.expect(bindex.at(2) == 0x7d); | |
| 56 | try std.testing.expect(bindex.at(3) == 0xa9); | |
| 57 | try std.testing.expect(bindex.at(4) == 0xf3); | |
| 58 | try std.testing.expect(bindex.at(5) == 0xf1); | |
| 59 | try std.testing.expect(bindex.at(6) == 0xd1); | |
| 60 | try std.testing.expect(bindex.at(7) == 0x32); | |
| 61 | } |
src/FixedMaxBuffer.zig deleted-52| ... | ... | @@ -1,52 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | const assert = std.debug.assert; | |
| 5 | ||
| 6 | pub fn FixedMaxBuffer(comptime max_len: usize) type { | |
| 7 | return struct { | |
| 8 | buf: [max_len]u8, | |
| 9 | len: usize, | |
| 10 | pos: usize, | |
| 11 | ||
| 12 | const Self = @This(); | |
| 13 | pub const Reader = std.io.GenericReader(*Self, error{}, read); | |
| 14 | ||
| 15 | pub fn init(r: anytype, runtime_len: usize) !Self { | |
| 16 | var fmr = Self{ | |
| 17 | .buf = undefined, | |
| 18 | .len = runtime_len, | |
| 19 | .pos = 0, | |
| 20 | }; | |
| 21 | _ = try r.readAll(fmr.buf[0..runtime_len]); | |
| 22 | return fmr; | |
| 23 | } | |
| 24 | ||
| 25 | pub fn reader(self: *Self) Reader { | |
| 26 | return .{ .context = self }; | |
| 27 | } | |
| 28 | ||
| 29 | fn read(self: *Self, dest: []u8) error{}!usize { | |
| 30 | const buf = self.buf[0..self.len]; | |
| 31 | const size = @min(dest.len, buf.len - self.pos); | |
| 32 | const end = self.pos + size; | |
| 33 | std.mem.copy(u8, dest[0..size], buf[self.pos..end]); | |
| 34 | self.pos = end; | |
| 35 | return size; | |
| 36 | } | |
| 37 | ||
| 38 | pub fn readLen(self: *Self, len: usize) []const u8 { | |
| 39 | assert(self.pos + len <= self.len); | |
| 40 | defer self.pos += len; | |
| 41 | return self.buf[self.pos..][0..len]; | |
| 42 | } | |
| 43 | ||
| 44 | pub fn atEnd(self: *const Self) bool { | |
| 45 | return self.pos == self.len; | |
| 46 | } | |
| 47 | }; | |
| 48 | } | |
| 49 | ||
| 50 | test { | |
| 51 | std.testing.refAllDecls(FixedMaxBuffer(16)); | |
| 52 | } |
src/indexBufferT.zig deleted-24| ... | ... | @@ -1,24 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | const readType = extras.readType; | |
| 5 | const rawIntBytes = extras.rawIntBytes; | |
| 6 | ||
| 7 | pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T { | |
| 8 | std.debug.assert(idx < max_len); | |
| 9 | var fbs = std.io.fixedBufferStream((bytes + (idx * @sizeOf(T)))[0..@sizeOf(T)]); | |
| 10 | return readType(fbs.reader(), T, endian) catch |err| switch (err) { | |
| 11 | error.EndOfStream => unreachable, // assert above has been violated | |
| 12 | }; | |
| 13 | } | |
| 14 | ||
| 15 | test { | |
| 16 | const bytes = rawIntBytes(u32, 0x4e5a7da9); | |
| 17 | try std.testing.expect(indexBufferT(&bytes, u16, .big, 0, 2) == 0x4e5a); | |
| 18 | try std.testing.expect(indexBufferT(&bytes, u16, .big, 1, 2) == 0x7da9); | |
| 19 | } | |
| 20 | test { | |
| 21 | const bytes = rawIntBytes(u32, 0x4e5a7da9); | |
| 22 | try std.testing.expect(indexBufferT(&bytes, u16, .little, 0, 2) == 0x5a4e); | |
| 23 | try std.testing.expect(indexBufferT(&bytes, u16, .little, 1, 2) == 0xa97d); | |
| 24 | } |
src/lib.zig-16| ... | ... | @@ -6,7 +6,6 @@ const builtin = @import("builtin"); |
| 6 | 6 | |
| 7 | 7 | pub const reduceNumber = @import("./reduceNumber.zig").reduceNumber; |
| 8 | 8 | pub const fmtByteCountIEC = @import("./fmtByteCountIEC.zig").fmtByteCountIEC; |
| 9 | pub const randomSlice = @import("./randomSlice.zig").randomSlice; | |
| 10 | 9 | pub const trimPrefix = @import("./trimPrefix.zig").trimPrefix; |
| 11 | 10 | pub const trimPrefixEnsure = @import("./trimPrefixEnsure.zig").trimPrefixEnsure; |
| 12 | 11 | pub const trimSuffix = @import("./trimSuffix.zig").trimSuffix; |
| ... | ... | @@ -15,7 +14,6 @@ pub const base64EncodeAlloc = @import("./base64EncodeAlloc.zig").base64EncodeAll |
| 15 | 14 | pub const base64DecodeAlloc = @import("./base64DecodeAlloc.zig").base64DecodeAlloc; |
| 16 | 15 | pub const asciiUpper = @import("./asciiUpper.zig").asciiUpper; |
| 17 | 16 | pub const sliceToInt = @import("./sliceToInt.zig").sliceToInt; |
| 18 | pub const pipe = @import("./pipe.zig").pipe; | |
| 19 | 17 | pub const countScalar = @import("./countScalar.zig").countScalar; |
| 20 | 18 | pub const sortBy = @import("./sortBy.zig").sortBy; |
| 21 | 19 | pub const sortBySlice = @import("./sortBySlice.zig").sortBySlice; |
| ... | ... | @@ -23,13 +21,7 @@ pub const lessThanSlice = @import("./lessThanSlice.zig").lessThanSlice; |
| 23 | 21 | pub const containsString = @import("./containsString.zig").containsString; |
| 24 | 22 | pub const ensureFieldSubset = @import("./ensureFieldSubset.zig").ensureFieldSubset; |
| 25 | 23 | pub const fmtReplacer = @import("./fmtReplacer.zig").fmtReplacer; |
| 26 | pub const randomBytes = @import("./randomBytes.zig").randomBytes; | |
| 27 | pub const FixedMaxBuffer = @import("./FixedMaxBuffer.zig").FixedMaxBuffer; | |
| 28 | 24 | pub const hashBytes = @import("./hashBytes.zig").hashBytes; |
| 29 | pub const readType = @import("./readType.zig").readType; | |
| 30 | pub const indexBufferT = @import("./indexBufferT.zig").indexBufferT; | |
| 31 | pub const BufIndexer = @import("./BufIndexer.zig").BufIndexer; | |
| 32 | pub const skipToBoundary = @import("./skipToBoundary.zig").skipToBoundary; | |
| 33 | 25 | pub const is = @import("./is.zig").is; |
| 34 | 26 | pub const safeAdd = @import("./safeAdd.zig").safeAdd; |
| 35 | 27 | pub const safeAddWrap = @import("./safeAddWrap.zig").safeAddWrap; |
| ... | ... | @@ -98,7 +90,6 @@ pub const sumLen = @import("./sumLen.zig").sumLen; |
| 98 | 90 | test { |
| 99 | 91 | _ = @import("reduceNumber.zig"); |
| 100 | 92 | _ = @import("fmtByteCountIEC.zig"); |
| 101 | _ = @import("randomSlice.zig"); | |
| 102 | 93 | _ = @import("trimPrefix.zig"); |
| 103 | 94 | _ = @import("trimPrefixEnsure.zig"); |
| 104 | 95 | _ = @import("trimSuffix.zig"); |
| ... | ... | @@ -107,7 +98,6 @@ test { |
| 107 | 98 | _ = @import("base64DecodeAlloc.zig"); |
| 108 | 99 | _ = @import("asciiUpper.zig"); |
| 109 | 100 | _ = @import("sliceToInt.zig"); |
| 110 | _ = @import("pipe.zig"); | |
| 111 | 101 | _ = @import("countScalar.zig"); |
| 112 | 102 | _ = @import("sortBy.zig"); |
| 113 | 103 | _ = @import("sortBySlice.zig"); |
| ... | ... | @@ -115,13 +105,7 @@ test { |
| 115 | 105 | _ = @import("containsString.zig"); |
| 116 | 106 | _ = @import("ensureFieldSubset.zig"); |
| 117 | 107 | _ = @import("fmtReplacer.zig"); |
| 118 | _ = @import("randomBytes.zig"); | |
| 119 | _ = @import("FixedMaxBuffer.zig"); | |
| 120 | 108 | _ = @import("hashBytes.zig"); |
| 121 | _ = @import("readType.zig"); | |
| 122 | _ = @import("indexBufferT.zig"); | |
| 123 | _ = @import("BufIndexer.zig"); | |
| 124 | _ = @import("skipToBoundary.zig"); | |
| 125 | 109 | _ = @import("is.zig"); |
| 126 | 110 | _ = @import("safeAdd.zig"); |
| 127 | 111 | _ = @import("safeAddWrap.zig"); |
src/pipe.zig deleted-22| ... | ... | @@ -1,22 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | ||
| 5 | pub fn pipe(reader_from: anytype, writer_to: anytype) !void { | |
| 6 | var buf: [4096]u8 = undefined; | |
| 7 | while (true) { | |
| 8 | const n = try reader_from.read(&buf); | |
| 9 | if (n == 0) break; | |
| 10 | try writer_to.writeAll(buf[0..n]); | |
| 11 | } | |
| 12 | } | |
| 13 | ||
| 14 | test { | |
| 15 | const bytes = "abcdefghijklmnopqrstuvwxyz".*; | |
| 16 | var fba = std.io.fixedBufferStream(&bytes); | |
| 17 | const allocator = std.testing.allocator; | |
| 18 | var list = std.Io.Writer.Allocating.init(allocator); | |
| 19 | defer list.deinit(); | |
| 20 | try pipe(fba.reader(), &list.writer); | |
| 21 | try std.testing.expect(std.mem.eql(u8, &bytes, list.written())); | |
| 22 | } |
src/randomBytes.zig deleted-13| ... | ... | @@ -1,13 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | ||
| 5 | pub fn randomBytes(comptime len: usize) [len]u8 { | |
| 6 | var bytes: [len]u8 = undefined; | |
| 7 | std.crypto.random.bytes(&bytes); | |
| 8 | return bytes; | |
| 9 | } | |
| 10 | ||
| 11 | test { | |
| 12 | std.testing.refAllDecls(@This()); | |
| 13 | } |
src/randomSlice.zig deleted-22| ... | ... | @@ -1,22 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | ||
| 5 | const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"; | |
| 6 | ||
| 7 | pub fn randomSlice(alloc: std.mem.Allocator, rand: std.Random, comptime T: type, len: usize) ![]T { | |
| 8 | var buf = try alloc.alloc(T, len); | |
| 9 | var i: usize = 0; | |
| 10 | while (i < len) : (i += 1) { | |
| 11 | buf[i] = alphabet[rand.int(u8) % alphabet.len]; | |
| 12 | } | |
| 13 | return buf; | |
| 14 | } | |
| 15 | ||
| 16 | test { | |
| 17 | const allocator = std.testing.allocator; | |
| 18 | const rand = std.crypto.random; | |
| 19 | const slice = try randomSlice(allocator, rand, u8, 10); | |
| 20 | defer allocator.free(slice); | |
| 21 | try std.testing.expect(slice.len == 10); | |
| 22 | } |
src/readType.zig deleted-95| ... | ... | @@ -1,95 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | const builtin = @import("builtin"); | |
| 5 | const native_endian = builtin.target.cpu.arch.endian(); | |
| 6 | const rawIntBytes = extras.rawIntBytes; | |
| 7 | ||
| 8 | pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T { | |
| 9 | if (T == u8) return reader.readByte(); // single bytes dont have an endianness | |
| 10 | return switch (@typeInfo(T)) { | |
| 11 | .@"struct" => |t| { | |
| 12 | switch (t.layout) { | |
| 13 | .auto, .@"extern" => { | |
| 14 | var s: T = undefined; | |
| 15 | inline for (std.meta.fields(T)) |field| { | |
| 16 | @field(s, field.name) = try readType(reader, field.type, endian); | |
| 17 | } | |
| 18 | return s; | |
| 19 | }, | |
| 20 | .@"packed" => return @bitCast(try readType(reader, t.backing_integer.?, endian)), | |
| 21 | } | |
| 22 | }, | |
| 23 | .array => |t| { | |
| 24 | var s: T = undefined; | |
| 25 | for (0..t.len) |i| { | |
| 26 | s[i] = try readType(reader, t.child, endian); | |
| 27 | } | |
| 28 | return s; | |
| 29 | }, | |
| 30 | .int => try reader.readInt(T, endian), | |
| 31 | .@"enum" => |t| @enumFromInt(try readType(reader, t.tag_type, endian)), | |
| 32 | else => |e| @compileError(@tagName(e)), | |
| 33 | }; | |
| 34 | } | |
| 35 | ||
| 36 | test { | |
| 37 | const bytes = std.mem.toBytes(@as(u32, 0x4e5a7da9)); | |
| 38 | var fba = std.io.fixedBufferStream(&bytes); | |
| 39 | const I = u32; | |
| 40 | const i = try readType(fba.reader(), I, native_endian); | |
| 41 | try std.testing.expect(i == 1314553257); | |
| 42 | } | |
| 43 | ||
| 44 | test { | |
| 45 | const bytes = std.mem.toBytes(@as(u32, 0x4e5a7da9)); | |
| 46 | var fba = std.io.fixedBufferStream(&bytes); | |
| 47 | const E = enum(u32) { | |
| 48 | a, | |
| 49 | b, | |
| 50 | c, | |
| 51 | d = 0x4e5a7da9, | |
| 52 | e, | |
| 53 | }; | |
| 54 | const e = try readType(fba.reader(), E, native_endian); | |
| 55 | try std.testing.expect(e == .d); | |
| 56 | } | |
| 57 | ||
| 58 | test { | |
| 59 | const bytes = rawIntBytes(u32, 0x4e5a7da9); | |
| 60 | var fba = std.io.fixedBufferStream(&bytes); | |
| 61 | const S = struct { | |
| 62 | a: u16, | |
| 63 | b: u8, | |
| 64 | c: u8, | |
| 65 | }; | |
| 66 | const s = try readType(fba.reader(), S, .big); | |
| 67 | try std.testing.expect(s.a == 0x4e5a); | |
| 68 | try std.testing.expect(s.b == 0x7d); | |
| 69 | try std.testing.expect(s.c == 0xa9); | |
| 70 | } | |
| 71 | ||
| 72 | test { | |
| 73 | const bytes = rawIntBytes(u32, 0x4e5a7da9); | |
| 74 | var fba = std.io.fixedBufferStream(&bytes); | |
| 75 | const A = [2]u16; | |
| 76 | const a = try readType(fba.reader(), A, .big); | |
| 77 | try std.testing.expect(a[0] == 0x4e5a); | |
| 78 | try std.testing.expect(a[1] == 0x7da9); | |
| 79 | } | |
| 80 | ||
| 81 | test { | |
| 82 | const bytes = rawIntBytes(u32, 0x4e5a7da9); | |
| 83 | var fba = std.io.fixedBufferStream(&bytes); | |
| 84 | const S = packed struct { | |
| 85 | a: u16, | |
| 86 | b: u4, | |
| 87 | c: u4, | |
| 88 | d: u8, | |
| 89 | }; | |
| 90 | const s = try readType(fba.reader(), S, .big); | |
| 91 | try std.testing.expect(s.a == 0x7da9); | |
| 92 | try std.testing.expect(s.b == 0xa); | |
| 93 | try std.testing.expect(s.c == 0x5); | |
| 94 | try std.testing.expect(s.d == 0x4e); | |
| 95 | } |
src/skipToBoundary.zig deleted-61| ... | ... | @@ -1,61 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const string = []const u8; | |
| 3 | const extras = @import("./lib.zig"); | |
| 4 | ||
| 5 | pub fn skipToBoundary(pos: u64, boundary: u64, reader: anytype) !void { | |
| 6 | // const gdiff = counter.bytes_read % 4; | |
| 7 | // for (range(if (gdiff > 0) 4 - gdiff else 0)) |_| { | |
| 8 | const a = pos; | |
| 9 | const b = boundary; | |
| 10 | try reader.skipBytes(((a + (b - 1)) & ~(b - 1)) - a, .{}); | |
| 11 | } | |
| 12 | ||
| 13 | test { | |
| 14 | const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; | |
| 15 | var fba = std.io.fixedBufferStream(&array); | |
| 16 | var counter = std.io.countingReader(fba.reader()); | |
| 17 | const reader = counter.reader(); | |
| 18 | try reader.skipBytes(0, .{}); | |
| 19 | try skipToBoundary(counter.bytes_read, 4, reader); | |
| 20 | try std.testing.expect(counter.bytes_read == 0); | |
| 21 | } | |
| 22 | ||
| 23 | test { | |
| 24 | const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; | |
| 25 | var fba = std.io.fixedBufferStream(&array); | |
| 26 | var counter = std.io.countingReader(fba.reader()); | |
| 27 | const reader = counter.reader(); | |
| 28 | try reader.skipBytes(1, .{}); | |
| 29 | try skipToBoundary(counter.bytes_read, 4, reader); | |
| 30 | try std.testing.expect(counter.bytes_read == 4); | |
| 31 | } | |
| 32 | ||
| 33 | test { | |
| 34 | const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; | |
| 35 | var fba = std.io.fixedBufferStream(&array); | |
| 36 | var counter = std.io.countingReader(fba.reader()); | |
| 37 | const reader = counter.reader(); | |
| 38 | try reader.skipBytes(2, .{}); | |
| 39 | try skipToBoundary(counter.bytes_read, 4, reader); | |
| 40 | try std.testing.expect(counter.bytes_read == 4); | |
| 41 | } | |
| 42 | ||
| 43 | test { | |
| 44 | const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; | |
| 45 | var fba = std.io.fixedBufferStream(&array); | |
| 46 | var counter = std.io.countingReader(fba.reader()); | |
| 47 | const reader = counter.reader(); | |
| 48 | try reader.skipBytes(3, .{}); | |
| 49 | try skipToBoundary(counter.bytes_read, 4, reader); | |
| 50 | try std.testing.expect(counter.bytes_read == 4); | |
| 51 | } | |
| 52 | ||
| 53 | test { | |
| 54 | const array = [_]u8{ 5, 4, 3, 2, 1, 0 }; | |
| 55 | var fba = std.io.fixedBufferStream(&array); | |
| 56 | var counter = std.io.countingReader(fba.reader()); | |
| 57 | const reader = counter.reader(); | |
| 58 | try reader.skipBytes(4, .{}); | |
| 59 | try skipToBoundary(counter.bytes_read, 4, reader); | |
| 60 | try std.testing.expect(counter.bytes_read == 4); | |
| 61 | } |