| ... | ... | @@ -4,6 +4,7 @@ |
| 4 | 4 | const Uri = @This(); |
| 5 | 5 | const std = @import("std.zig"); |
| 6 | 6 | const testing = std.testing; |
| 7 | const Allocator = std.mem.Allocator; |
| 7 | 8 | |
| 8 | 9 | scheme: []const u8, |
| 9 | 10 | user: ?[]const u8 = null, |
| ... | ... | @@ -15,15 +16,15 @@ query: ?[]const u8 = null, |
| 15 | 16 | fragment: ?[]const u8 = null, |
| 16 | 17 | |
| 17 | 18 | /// Applies URI encoding and replaces all reserved characters with their respective %XX code. |
| 18 | | pub fn escapeString(allocator: std.mem.Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 19 | pub fn escapeString(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 19 | 20 | return escapeStringWithFn(allocator, input, isUnreserved); |
| 20 | 21 | } |
| 21 | 22 | |
| 22 | | pub fn escapePath(allocator: std.mem.Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 23 | pub fn escapePath(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 23 | 24 | return escapeStringWithFn(allocator, input, isPathChar); |
| 24 | 25 | } |
| 25 | 26 | |
| 26 | | pub fn escapeQuery(allocator: std.mem.Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 27 | pub fn escapeQuery(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 27 | 28 | return escapeStringWithFn(allocator, input, isQueryChar); |
| 28 | 29 | } |
| 29 | 30 | |
| ... | ... | @@ -39,7 +40,7 @@ pub fn writeEscapedQuery(writer: anytype, input: []const u8) !void { |
| 39 | 40 | return writeEscapedStringWithFn(writer, input, isQueryChar); |
| 40 | 41 | } |
| 41 | 42 | |
| 42 | | pub fn escapeStringWithFn(allocator: std.mem.Allocator, input: []const u8, comptime keepUnescaped: fn (c: u8) bool) std.mem.Allocator.Error![]u8 { |
| 43 | pub fn escapeStringWithFn(allocator: Allocator, input: []const u8, comptime keepUnescaped: fn (c: u8) bool) Allocator.Error![]u8 { |
| 43 | 44 | var outsize: usize = 0; |
| 44 | 45 | for (input) |c| { |
| 45 | 46 | outsize += if (keepUnescaped(c)) @as(usize, 1) else 3; |
| ... | ... | @@ -76,7 +77,7 @@ pub fn writeEscapedStringWithFn(writer: anytype, input: []const u8, comptime kee |
| 76 | 77 | |
| 77 | 78 | /// Parses a URI string and unescapes all %XX where XX is a valid hex number. Otherwise, verbatim copies |
| 78 | 79 | /// them to the output. |
| 79 | | pub fn unescapeString(allocator: std.mem.Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 80 | pub fn unescapeString(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 80 | 81 | var outsize: usize = 0; |
| 81 | 82 | var inptr: usize = 0; |
| 82 | 83 | while (inptr < input.len) { |
| ... | ... | @@ -361,7 +362,7 @@ pub fn parse(text: []const u8) ParseError!Uri { |
| 361 | 362 | /// Implementation of RFC 3986, Section 5.2.4. Removes dot segments from a URI path. |
| 362 | 363 | /// |
| 363 | 364 | /// `std.fs.path.resolvePosix` is not sufficient here because it may return relative paths and does not preserve trailing slashes. |
| 364 | | fn removeDotSegments(allocator: std.mem.Allocator, paths: []const []const u8) std.mem.Allocator.Error![]const u8 { |
| 365 | fn removeDotSegments(allocator: Allocator, paths: []const []const u8) Allocator.Error![]const u8 { |
| 365 | 366 | var result = std.ArrayList(u8).init(allocator); |
| 366 | 367 | defer result.deinit(); |
| 367 | 368 | |
| ... | ... | @@ -399,7 +400,7 @@ fn removeDotSegments(allocator: std.mem.Allocator, paths: []const []const u8) st |
| 399 | 400 | /// Resolves a URI against a base URI, conforming to RFC 3986, Section 5. |
| 400 | 401 | /// |
| 401 | 402 | /// Assumes `arena` owns all memory in `base` and `ref`. `arena` will own all memory in the returned URI. |
| 402 | | pub fn resolve(base: Uri, ref: Uri, strict: bool, arena: std.mem.Allocator) std.mem.Allocator.Error!Uri { |
| 403 | pub fn resolve(base: Uri, ref: Uri, strict: bool, arena: Allocator) Allocator.Error!Uri { |
| 403 | 404 | var target: Uri = Uri{ |
| 404 | 405 | .scheme = "", |
| 405 | 406 | .user = null, |