summaryrefslogtreecommitdiff
path: root/src/bin/util.zig
blob: 9fb2ce431149e82b9faf12e97282d4686977d817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const std = @import("std");
pub fn validateUser(user: []const u8) !void {
    if (user.len == 0 or user.len > 64) return error.InvalidUser;
    if (user[0] == '.') return error.InvalidUser;
    for (user) |c| switch (c) {
        'a'...'z', '0'...'9', '.', '_', '-' => {},
        else => return error.InvalidUser,
    };
}

pub fn normalizeUser(user: []const u8, buf: *[64]u8) ![]const u8 {
    const out = std.ascii.lowerString(buf, user);
    try validateUser(out);
    return out;
}

pub fn literal(w: *std.Io.Writer, host: []const u8) !void {
    const bare = std.mem.trim(u8, host, "[]");
    if (std.mem.indexOfScalar(u8, bare, ':') != null)
        try w.print("[IPv6:{s}]", .{bare})
    else
        try w.print("[{s}]", .{bare});
}