@@ -5,7 +5,7 @@ const Uri = @This();
55const std = @import ("std.zig" );
66const testing = std .testing ;
77
8- scheme : ? []const u8 ,
8+ scheme : []const u8 ,
99user : ? []const u8 ,
1010password : ? []const u8 ,
1111host : ? []const u8 ,
@@ -98,8 +98,9 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort };
9898/// The return value will contain unescaped strings pointing into the
9999/// original `text`. Each component that is provided, will be non-`null`.
100100pub fn parse (text : []const u8 ) ParseError ! Uri {
101+ var reader = SliceReader { .slice = text };
101102 var uri = Uri {
102- .scheme = null ,
103+ .scheme = reader . readWhile ( isSchemeChar ) ,
103104 .user = null ,
104105 .password = null ,
105106 .host = null ,
@@ -109,10 +110,6 @@ pub fn parse(text: []const u8) ParseError!Uri {
109110 .fragment = null ,
110111 };
111112
112- var reader = SliceReader { .slice = text };
113-
114- uri .scheme = reader .readWhile (isSchemeChar );
115-
116113 // after the scheme, a ':' must appear
117114 if (reader .get ()) | c | {
118115 if (c != ':' )
@@ -296,15 +293,15 @@ fn isQuerySeparator(c: u8) bool {
296293
297294test "basic" {
298295 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 );
300297 try testing .expectEqualStrings ("ziglang.org" , parsed .host orelse return error .UnexpectedNull );
301298 try testing .expectEqualStrings ("/download" , parsed .path );
302299 try testing .expectEqual (@as (? u16 , null ), parsed .port );
303300}
304301
305302test "with port" {
306303 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 );
308305 try testing .expectEqualStrings ("example" , parsed .host orelse return error .UnexpectedNull );
309306 try testing .expectEqualStrings ("/" , parsed .path );
310307 try testing .expectEqual (@as (? u16 , 1337 ), parsed .port );
@@ -315,12 +312,12 @@ test "should fail gracefully" {
315312}
316313
317314test "scheme" {
318- try std .testing .expectEqualSlices (u8 , "http" , (try parse ("http:_" )).scheme .? );
319- 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 .? );
321- try std .testing .expectEqualSlices (u8 , "ab+" , (try parse ("ab+:_" )).scheme .? );
322- try std .testing .expectEqualSlices (u8 , "X+++" , (try parse ("X+++:_" )).scheme .? );
323- try std .testing .expectEqualSlices (u8 , "Y+-." , (try parse ("Y+-.:_" )).scheme .? );
315+ try std .testing .expectEqualSlices (u8 , "http" , (try parse ("http:_" )).scheme );
316+ try std .testing .expectEqualSlices (u8 , "scheme-mee" , (try parse ("scheme-mee:_" )).scheme );
317+ try std .testing .expectEqualSlices (u8 , "a.b.c" , (try parse ("a.b.c:_" )).scheme );
318+ try std .testing .expectEqualSlices (u8 , "ab+" , (try parse ("ab+:_" )).scheme );
319+ try std .testing .expectEqualSlices (u8 , "X+++" , (try parse ("X+++:_" )).scheme );
320+ try std .testing .expectEqualSlices (u8 , "Y+-." , (try parse ("Y+-.:_" )).scheme );
324321}
325322
326323test "authority" {
0 commit comments