From e12c0d23ad72ffa9389d90311453db535f57e450 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Mon, 4 Mar 2024 16:24:45 +0000 Subject: Prepare to move to zine static site generator instead of hugo https://zine-ssg.io/documentation/ --- server/src/tmp.zig | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 server/src/tmp.zig (limited to 'server/src') diff --git a/server/src/tmp.zig b/server/src/tmp.zig new file mode 100644 index 0000000..71a9865 --- /dev/null +++ b/server/src/tmp.zig @@ -0,0 +1,44 @@ +const std = @import("std"); + +test "slices" { + // option 1 slices + const a = std.testing.allocator; + var words_al = std.ArrayList([]const u8).init(a); + defer words_al.deinit(); + var toks = std.mem.tokenizeAny(u8, wordlist, "\n "); + while (toks.next()) |tok| { + try words_al.append(tok); + } + const words: []const[]const u8 = try words_al.toOwnedSlice(); // a slice is a pointer + length (so 2 x usize x length) + defer a.free(words); + + try std.testing.expectEqualStrings("bar", words[1]); // words accessed by index +} + +test "offsets" { + // option 2 offsets + const a = std.testing.allocator; + var offsets_al = std.ArrayList(u16).init(a); + defer offsets_al.deinit(); + try offsets_al.append(0); + for (wordlist, 0..) |ch,ix| { + if (ch == '\n' and ix < wordlist.len) { + try offsets_al.append(@intCast(ix+1)); // we know we have less than 2^16 words + } + } + const offsets = try offsets_al.toOwnedSlice(); // offsets are just u16 x length, so ~4x less memory on 64 bit systems. + defer a.free(offsets); + // words accessed by slicing wordlist using offsets + // be careful of edge cases i.e. accessing the last word in the list, not handled here. + const word = wordlist[offsets[1]..(offsets[2]-1)]; + try std.testing.expectEqualStrings("bar", word); +} + +const wordlist: []const u8 = +\\foo +\\bar +\\baz +\\I +\\like +\\cheese +; -- cgit v1.2.3-ZIG