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 @@
44const Uri = @This();
55const std = @import("std.zig");
66const testing = std.testing;
7const Allocator = std.mem.Allocator;
78
89scheme: []const u8,
910user: ?[]const u8 = null,
......@@ -15,15 +16,15 @@ query: ?[]const u8 = null,
1516fragment: ?[]const u8 = null,
1617
1718/// 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 {
1920 return escapeStringWithFn(allocator, input, isUnreserved);
2021}
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 {
2324 return escapeStringWithFn(allocator, input, isPathChar);
2425}
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 {
2728 return escapeStringWithFn(allocator, input, isQueryChar);
2829}
2930
......@@ -39,7 +40,7 @@ pub fn writeEscapedQuery(writer: anytype, input: []const u8) !void {
3940 return writeEscapedStringWithFn(writer, input, isQueryChar);
4041}
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 {
4344 var outsize: usize = 0;
4445 for (input) |c| {
4546 outsize += if (keepUnescaped(c)) @as(usize, 1) else 3;
......@@ -76,7 +77,7 @@ pub fn writeEscapedStringWithFn(writer: anytype, input: []const u8, comptime kee
7677
7778/// Parses a URI string and unescapes all %XX where XX is a valid hex number. Otherwise, verbatim copies
7879/// 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 {
8081 var outsize: usize = 0;
8182 var inptr: usize = 0;
8283 while (inptr < input.len) {
......@@ -361,7 +362,7 @@ pub fn parse(text: []const u8) ParseError!Uri {
361362/// Implementation of RFC 3986, Section 5.2.4. Removes dot segments from a URI path.
362363///
363364/// `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 {
365366 var result = std.ArrayList(u8).init(allocator);
366367 defer result.deinit();
367368
......@@ -399,7 +400,7 @@ fn removeDotSegments(allocator: std.mem.Allocator, paths: []const []const u8) st
399400/// Resolves a URI against a base URI, conforming to RFC 3986, Section 5.
400401///
401402/// 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 {
403404 var target: Uri = Uri{
404405 .scheme = "",
405406 .user = null,