diff options
author | Martin Ashby <martin@ashbysoft.com> | 2024-03-04 16:24:45 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2024-03-10 22:03:45 +0000 |
commit | e12c0d23ad72ffa9389d90311453db535f57e450 (patch) | |
tree | 00e8caf78140dee2c9af4568d7d2fb083b80b906 /server | |
parent | 5ed483825a50cadb1d3d2dd55f9e4ebc52716660 (diff) | |
download | mfashby.net-e12c0d23ad72ffa9389d90311453db535f57e450.tar.gz mfashby.net-e12c0d23ad72ffa9389d90311453db535f57e450.tar.bz2 mfashby.net-e12c0d23ad72ffa9389d90311453db535f57e450.tar.xz mfashby.net-e12c0d23ad72ffa9389d90311453db535f57e450.zip |
Prepare to move to zine static site generator instead of hugo
https://zine-ssg.io/documentation/
Diffstat (limited to 'server')
-rw-r--r-- | server/src/tmp.zig | 44 |
1 files changed, 44 insertions, 0 deletions
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 +; |