Add a very limited URI parser.

master
Philippe Pittoli 2023-01-11 14:16:01 +01:00
parent a0e9515600
commit b73550bdcf
1 changed files with 31 additions and 0 deletions

View File

@ -2,6 +2,37 @@ const std = @import("std");
// const hexdump = @import("./hexdump.zig");
const testing = std.testing;
/// A VERY LIGHT and INCOMPLETE way of decoding URI.
/// DO NOT USE IT UNLESS YOU KNOW WHAT TO EXPECT.
pub const URI = struct {
protocol: []const u8,
address: []const u8,
path: []const u8,
const Self = @This();
pub fn read(uri_to_decode: []const u8) Self {
var protocolit = std.mem.split(u8, uri_to_decode, "://");
var protocol = protocolit.first();
var addressit = std.mem.split(u8, protocolit.next().?, "/");
var address = addressit.first();
var path = addressit.rest();
return Self { .protocol = protocol
, .address = address
, .path = path };
}
};
test "URI simple decoding" {
var uri = URI.read("tcp://127.0.0.1:8000/some-path");
try testing.expectEqualSlices(u8, uri.protocol, "tcp");
try testing.expectEqualSlices(u8, uri.address, "127.0.0.1:8000");
try testing.expectEqualSlices(u8, uri.path, "some-path");
}
pub fn print_eq(expected: anytype, obj: anytype) !void {
var buffer: [4096]u8 = undefined;