Skip to content

Instantly share code, notes, and snippets.

@nektro
Last active Mar 7, 2022
Embed
What would you like to do?
const UrlValues = @This();
const string = []const u8;
const std = @import("std");
const uri = @import("uri");
inner: std.StringArrayHashMap(string),
pub fn init(alloc: std.mem.Allocator) UrlValues {
return .{
.inner = std.StringArrayHashMap(string).init(alloc),
};
}
pub fn add(self: *UrlValues, key: string, value: string) !void {
try self.inner.putNoClobber(key, value);
}
pub fn encode(self: UrlValues) !string {
const alloc = self.inner.allocator;
var list = std.ArrayList(u8).init(alloc);
var iter = self.inner.iterator();
var i: usize = 0;
while (iter.next()) |entry| : (i += 1) {
if (i > 0) try list.writer().writeAll("&");
try list.writer().print("{s}={s}", .{ entry.key_ptr.*, uri.escapeString(alloc, entry.value_ptr.*) });
}
return list.toOwnedSlice();
}
id: dh4cemvjgaa6tbt8xu2qajm46mf5at3qc7zkcxfj
name: UrlValues
main: UrlValues.zig
license: MIT
dependencies:
- src: git https://github.com/MasterQ32/zig-uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment