| ... | @@ -5,7 +5,7 @@ const Uri = @This(); | ... | @@ -5,7 +5,7 @@ const Uri = @This(); |
| 5 | const std = @import("std.zig"); | 5 | const std = @import("std.zig"); |
| 6 | const testing = std.testing; | 6 | const testing = std.testing; |
| 7 | | 7 | |
| 8 | scheme: ?[]const u8, | 8 | scheme: []const u8, |
| 9 | user: ?[]const u8, | 9 | user: ?[]const u8, |
| 10 | password: ?[]const u8, | 10 | password: ?[]const u8, |
| 11 | host: ?[]const u8, | 11 | host: ?[]const u8, |
| ... | @@ -98,8 +98,9 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort }; | ... | @@ -98,8 +98,9 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort }; |
| 98 | /// The return value will contain unescaped strings pointing into the | 98 | /// The return value will contain unescaped strings pointing into the |
| 99 | /// original `text`. Each component that is provided, will be non-`null`. | 99 | /// original `text`. Each component that is provided, will be non-`null`. |
| 100 | pub fn parse(text: []const u8) ParseError!Uri { | 100 | pub fn parse(text: []const u8) ParseError!Uri { |
| | 101 | var reader = SliceReader{ .slice = text }; |
| 101 | var uri = Uri{ | 102 | var uri = Uri{ |
| 102 | .scheme = null, | 103 | .scheme = reader.readWhile(isSchemeChar), |
| 103 | .user = null, | 104 | .user = null, |
| 104 | .password = null, | 105 | .password = null, |
| 105 | .host = null, | 106 | .host = null, |
| ... | @@ -109,10 +110,6 @@ pub fn parse(text: []const u8) ParseError!Uri { | ... | @@ -109,10 +110,6 @@ pub fn parse(text: []const u8) ParseError!Uri { |
| 109 | .fragment = null, | 110 | .fragment = null, |
| 110 | }; | 111 | }; |
| 111 | | 112 | |
| 112 | var reader = SliceReader{ .slice = text }; | | |
| 113 | | | |
| 114 | uri.scheme = reader.readWhile(isSchemeChar); | | |
| 115 | | | |
| 116 | // after the scheme, a ':' must appear | 113 | // after the scheme, a ':' must appear |
| 117 | if (reader.get()) |c| { | 114 | if (reader.get()) |c| { |
| 118 | if (c != ':') | 115 | if (c != ':') |
| ... | @@ -296,7 +293,7 @@ fn isQuerySeparator(c: u8) bool { | ... | @@ -296,7 +293,7 @@ fn isQuerySeparator(c: u8) bool { |
| 296 | | 293 | |
| 297 | test "basic" { | 294 | test "basic" { |
| 298 | const parsed = try parse("https://ziglang.org/download"); | 295 | const parsed = try parse("https://ziglang.org/download"); |
| 299 | try testing.expectEqualStrings("https", parsed.scheme orelse return error.UnexpectedNull); | 296 | try testing.expectEqualStrings("https", parsed.scheme); |
| 300 | try testing.expectEqualStrings("ziglang.org", parsed.host orelse return error.UnexpectedNull); | 297 | try testing.expectEqualStrings("ziglang.org", parsed.host orelse return error.UnexpectedNull); |
| 301 | try testing.expectEqualStrings("/download", parsed.path); | 298 | try testing.expectEqualStrings("/download", parsed.path); |
| 302 | try testing.expectEqual(@as(?u16, null), parsed.port); | 299 | try testing.expectEqual(@as(?u16, null), parsed.port); |
| ... | @@ -304,7 +301,7 @@ test "basic" { | ... | @@ -304,7 +301,7 @@ test "basic" { |
| 304 | | 301 | |
| 305 | test "with port" { | 302 | test "with port" { |
| 306 | const parsed = try parse("http://example:1337/"); | 303 | const parsed = try parse("http://example:1337/"); |
| 307 | try testing.expectEqualStrings("http", parsed.scheme orelse return error.UnexpectedNull); | 304 | try testing.expectEqualStrings("http", parsed.scheme); |
| 308 | try testing.expectEqualStrings("example", parsed.host orelse return error.UnexpectedNull); | 305 | try testing.expectEqualStrings("example", parsed.host orelse return error.UnexpectedNull); |
| 309 | try testing.expectEqualStrings("/", parsed.path); | 306 | try testing.expectEqualStrings("/", parsed.path); |
| 310 | try testing.expectEqual(@as(?u16, 1337), parsed.port); | 307 | try testing.expectEqual(@as(?u16, 1337), parsed.port); |
| ... | @@ -315,12 +312,12 @@ test "should fail gracefully" { | ... | @@ -315,12 +312,12 @@ test "should fail gracefully" { |
| 315 | } | 312 | } |
| 316 | | 313 | |
| 317 | test "scheme" { | 314 | test "scheme" { |
| 318 | try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme.?); | 315 | try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme); |
| 319 | try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme.?); | 316 | try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme); |
| 320 | try std.testing.expectEqualSlices(u8, "a.b.c", (try parse("a.b.c:_")).scheme.?); | 317 | try std.testing.expectEqualSlices(u8, "a.b.c", (try parse("a.b.c:_")).scheme); |
| 321 | try std.testing.expectEqualSlices(u8, "ab+", (try parse("ab+:_")).scheme.?); | 318 | try std.testing.expectEqualSlices(u8, "ab+", (try parse("ab+:_")).scheme); |
| 322 | try std.testing.expectEqualSlices(u8, "X+++", (try parse("X+++:_")).scheme.?); | 319 | try std.testing.expectEqualSlices(u8, "X+++", (try parse("X+++:_")).scheme); |
| 323 | try std.testing.expectEqualSlices(u8, "Y+-.", (try parse("Y+-.:_")).scheme.?); | 320 | try std.testing.expectEqualSlices(u8, "Y+-.", (try parse("Y+-.:_")).scheme); |
| 324 | } | 321 | } |
| 325 | | 322 | |
| 326 | test "authority" { | 323 | test "authority" { |