authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-16 21:16:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
log107992d50e6a07d98b325f8768882f17e454b4a4
treeae6048a8449a124244f9ef1cf392041b2a348ba4
parent743a0c966de933e3d0a271f942c2db525df5dbe8

std.Uri: refactor std.mem.Allocator -> Allocator


1 files changed, 8 insertions(+), 7 deletions(-)

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