Skip to content

Commit bc3c50c

Browse files
authored
Merge pull request #23700 from sorairolake/rename-trims
chore(std.mem): Rename `trimLeft` and `trimRight` to `trimStart` and `trimEnd`
2 parents 8285de6 + 195471a commit bc3c50c

File tree

22 files changed

+59
-47
lines changed

22 files changed

+59
-47
lines changed

lib/compiler/resinator/compile.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3274,7 +3274,7 @@ pub const StringTable = struct {
32743274
// Note: This is only the case for STRINGTABLE strings
32753275
const trimmed = trimToDoubleNUL(u16, utf16_string);
32763276
// We also want to trim any trailing NUL characters
3277-
break :trim std.mem.trimRight(u16, trimmed, &[_]u16{0});
3277+
break :trim std.mem.trimEnd(u16, trimmed, &[_]u16{0});
32783278
};
32793279

32803280
// String literals are limited to maxInt(u15) codepoints, so these UTF-16 encoded

lib/docs/wasm/markdown/Parser.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const Block = struct {
140140
/// (e.g. for a blockquote, this would be everything except the leading
141141
/// `>`). If unsuccessful, returns null.
142142
fn match(b: Block, line: []const u8) ?[]const u8 {
143-
const unindented = mem.trimLeft(u8, line, " \t");
143+
const unindented = mem.trimStart(u8, line, " \t");
144144
const indent = line.len - unindented.len;
145145
return switch (b.tag) {
146146
.list => line,
@@ -156,7 +156,7 @@ const Block = struct {
156156
.table_row => null,
157157
.heading => null,
158158
.code_block => code_block: {
159-
const trimmed = mem.trimRight(u8, unindented, " \t");
159+
const trimmed = mem.trimEnd(u8, unindented, " \t");
160160
if (mem.indexOfNone(u8, trimmed, "`") != null or trimmed.len != b.data.code_block.fence_len) {
161161
const effective_indent = @min(indent, b.data.code_block.indent);
162162
break :code_block line[effective_indent..];
@@ -225,7 +225,7 @@ pub fn feedLine(p: *Parser, line: []const u8) Allocator.Error!void {
225225
p.pending_blocks.items.len > 0 and
226226
p.pending_blocks.getLast().tag == .paragraph)
227227
{
228-
try p.addScratchStringLine(mem.trimLeft(u8, rest_line, " \t"));
228+
try p.addScratchStringLine(mem.trimStart(u8, rest_line, " \t"));
229229
return;
230230
}
231231

@@ -261,7 +261,7 @@ pub fn feedLine(p: *Parser, line: []const u8) Allocator.Error!void {
261261
last_pending_block.canAccept()
262262
else
263263
.blocks;
264-
const rest_line_trimmed = mem.trimLeft(u8, rest_line, " \t");
264+
const rest_line_trimmed = mem.trimStart(u8, rest_line, " \t");
265265
switch (can_accept) {
266266
.blocks => {
267267
// If we're inside a list item and the rest of the line is blank, it
@@ -500,7 +500,7 @@ fn appendBlockStart(p: *Parser, block_start: BlockStart) !void {
500500
}
501501

502502
fn startBlock(p: *Parser, line: []const u8) !?BlockStart {
503-
const unindented = mem.trimLeft(u8, line, " \t");
503+
const unindented = mem.trimStart(u8, line, " \t");
504504
const indent = line.len - unindented.len;
505505
if (isThematicBreak(line)) {
506506
// Thematic breaks take precedence over list items.

lib/std/crypto/tls/Client.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
364364
};
365365
P.AEAD.decrypt(cleartext, ciphertext, auth_tag, record_header, nonce, pv.server_handshake_key) catch
366366
return error.TlsBadRecordMac;
367-
cleartext_fragment_end += std.mem.trimRight(u8, cleartext, "\x00").len;
367+
cleartext_fragment_end += std.mem.trimEnd(u8, cleartext, "\x00").len;
368368
},
369369
}
370370
read_seq += 1;
@@ -1353,7 +1353,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.posix.iove
13531353
const cleartext = cleartext_buf[0..ciphertext.len];
13541354
P.AEAD.decrypt(cleartext, ciphertext, auth_tag, ad, nonce, pv.server_key) catch
13551355
return error.TlsBadRecordMac;
1356-
const msg = mem.trimRight(u8, cleartext, "\x00");
1356+
const msg = mem.trimEnd(u8, cleartext, "\x00");
13571357
break :cleartext .{ msg[0 .. msg.len - 1], @enumFromInt(msg[msg.len - 1]) };
13581358
},
13591359
.tls_1_2 => {

lib/std/elf.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,12 +2330,12 @@ pub const ar_hdr = extern struct {
23302330
ar_fmag: [2]u8,
23312331

23322332
pub fn date(self: ar_hdr) std.fmt.ParseIntError!u64 {
2333-
const value = mem.trimRight(u8, &self.ar_date, &[_]u8{0x20});
2333+
const value = mem.trimEnd(u8, &self.ar_date, &[_]u8{0x20});
23342334
return std.fmt.parseInt(u64, value, 10);
23352335
}
23362336

23372337
pub fn size(self: ar_hdr) std.fmt.ParseIntError!u32 {
2338-
const value = mem.trimRight(u8, &self.ar_size, &[_]u8{0x20});
2338+
const value = mem.trimEnd(u8, &self.ar_size, &[_]u8{0x20});
23392339
return std.fmt.parseInt(u32, value, 10);
23402340
}
23412341

@@ -2369,7 +2369,7 @@ pub const ar_hdr = extern struct {
23692369
pub fn nameOffset(self: ar_hdr) std.fmt.ParseIntError!?u32 {
23702370
const value = &self.ar_name;
23712371
if (value[0] != '/') return null;
2372-
const trimmed = mem.trimRight(u8, value, &[_]u8{0x20});
2372+
const trimmed = mem.trimEnd(u8, value, &[_]u8{0x20});
23732373
return try std.fmt.parseInt(u32, trimmed[1..], 10);
23742374
}
23752375
};

lib/std/fmt.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ pub fn formatFloatHexadecimal(
11621162

11631163
try writer.writeAll("0x");
11641164
try writer.writeByte(buf[0]);
1165-
const trimmed = mem.trimRight(u8, buf[1..], "0");
1165+
const trimmed = mem.trimEnd(u8, buf[1..], "0");
11661166
if (options.precision) |precision| {
11671167
if (precision > 0) try writer.writeAll(".");
11681168
} else if (trimmed.len > 0) {

lib/std/http/Client.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ pub const Response = struct {
473473
};
474474
if (first_line[8] != ' ') return error.HttpHeadersInvalid;
475475
const status: http.Status = @enumFromInt(parseInt3(first_line[9..12]));
476-
const reason = mem.trimLeft(u8, first_line[12..], " ");
476+
const reason = mem.trimStart(u8, first_line[12..], " ");
477477

478478
res.version = version;
479479
res.status = status;

lib/std/mem.zig

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,19 +1200,33 @@ pub fn allEqual(comptime T: type, slice: []const T, scalar: T) bool {
12001200
}
12011201

12021202
/// Remove a set of values from the beginning of a slice.
1203-
pub fn trimLeft(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
1203+
pub fn trimStart(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
12041204
var begin: usize = 0;
12051205
while (begin < slice.len and indexOfScalar(T, values_to_strip, slice[begin]) != null) : (begin += 1) {}
12061206
return slice[begin..];
12071207
}
12081208

1209+
test trimStart {
1210+
try testing.expectEqualSlices(u8, "foo\n ", trimStart(u8, " foo\n ", " \n"));
1211+
}
1212+
1213+
/// Deprecated: use `trimStart` instead.
1214+
pub const trimLeft = trimStart;
1215+
12091216
/// Remove a set of values from the end of a slice.
1210-
pub fn trimRight(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
1217+
pub fn trimEnd(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
12111218
var end: usize = slice.len;
12121219
while (end > 0 and indexOfScalar(T, values_to_strip, slice[end - 1]) != null) : (end -= 1) {}
12131220
return slice[0..end];
12141221
}
12151222

1223+
test trimEnd {
1224+
try testing.expectEqualSlices(u8, " foo", trimEnd(u8, " foo\n ", " \n"));
1225+
}
1226+
1227+
/// Deprecated: use `trimEnd` instead.
1228+
pub const trimRight = trimEnd;
1229+
12161230
/// Remove a set of values from the beginning and end of a slice.
12171231
pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
12181232
var begin: usize = 0;
@@ -1223,8 +1237,6 @@ pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []co
12231237
}
12241238

12251239
test trim {
1226-
try testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n"));
1227-
try testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n"));
12281240
try testing.expectEqualSlices(u8, "foo", trim(u8, " foo\n ", " \n"));
12291241
try testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n"));
12301242
}

lib/std/tar.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ const Header = struct {
250250
const raw = header.bytes[start..][0..len];
251251
// Zero-filled octal number in ASCII. Each numeric field of width w
252252
// contains w minus 1 digits, and a null
253-
const ltrimmed = std.mem.trimLeft(u8, raw, "0 ");
254-
const rtrimmed = std.mem.trimRight(u8, ltrimmed, " \x00");
253+
const ltrimmed = std.mem.trimStart(u8, raw, "0 ");
254+
const rtrimmed = std.mem.trimEnd(u8, ltrimmed, " \x00");
255255
if (rtrimmed.len == 0) return 0;
256256
return std.fmt.parseInt(u64, rtrimmed, 8) catch return error.TarHeader;
257257
}

lib/std/zig/LibCInstallation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ fn findNativeIncludeDirPosix(self: *LibCInstallation, args: FindNativeOptions) F
317317
while (path_i < search_paths.items.len) : (path_i += 1) {
318318
// search in reverse order
319319
const search_path_untrimmed = search_paths.items[search_paths.items.len - path_i - 1];
320-
const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " ");
320+
const search_path = std.mem.trimStart(u8, search_path_untrimmed, " ");
321321
var search_dir = fs.cwd().openDir(search_path, .{}) catch |err| switch (err) {
322322
error.FileNotFound,
323323
error.NotDir,

lib/std/zig/Parse.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ fn parseTypeExpr(p: *Parse) Error!?Node.Index {
18491849
var sentinel: ?Node.Index = null;
18501850
if (p.eatToken(.identifier)) |ident| {
18511851
const ident_slice = p.source[p.tokenStart(ident)..p.tokenStart(ident + 1)];
1852-
if (!std.mem.eql(u8, std.mem.trimRight(u8, ident_slice, &std.ascii.whitespace), "c")) {
1852+
if (!std.mem.eql(u8, std.mem.trimEnd(u8, ident_slice, &std.ascii.whitespace), "c")) {
18531853
p.tok_i -= 1;
18541854
}
18551855
} else if (p.eatToken(.colon)) |_| {

lib/std/zig/render.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,7 +2955,7 @@ fn renderComments(r: *Render, start: usize, end: usize) Error!bool {
29552955
const newline = if (newline_index) |i| comment_start + i else null;
29562956

29572957
const untrimmed_comment = tree.source[comment_start .. newline orelse tree.source.len];
2958-
const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.whitespace);
2958+
const trimmed_comment = mem.trimEnd(u8, untrimmed_comment, &std.ascii.whitespace);
29592959

29602960
// Don't leave any whitespace at the start of the file
29612961
if (index != 0) {
@@ -2976,7 +2976,7 @@ fn renderComments(r: *Render, start: usize, end: usize) Error!bool {
29762976

29772977
index = 1 + (newline orelse end - 1);
29782978

2979-
const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.whitespace);
2979+
const comment_content = mem.trimStart(u8, trimmed_comment["//".len..], &std.ascii.whitespace);
29802980
if (ais.disabled_offset != null and mem.eql(u8, comment_content, "zig fmt: on")) {
29812981
// Write the source for which formatting was disabled directly
29822982
// to the underlying writer, fixing up invalid whitespace.
@@ -3103,7 +3103,7 @@ fn tokenSliceForRender(tree: Ast, token_index: Ast.TokenIndex) []const u8 {
31033103
var ret = tree.tokenSlice(token_index);
31043104
switch (tree.tokenTag(token_index)) {
31053105
.container_doc_comment, .doc_comment => {
3106-
ret = mem.trimRight(u8, ret, &std.ascii.whitespace);
3106+
ret = mem.trimEnd(u8, ret, &std.ascii.whitespace);
31073107
},
31083108
else => {},
31093109
}

lib/std/zig/system.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,13 +1182,13 @@ fn detectAbiAndDynamicLinker(
11821182
// We detected shebang, now parse entire line.
11831183

11841184
// Trim leading "#!", spaces and tabs.
1185-
const trimmed_line = mem.trimLeft(u8, content[2..], &.{ ' ', '\t' });
1185+
const trimmed_line = mem.trimStart(u8, content[2..], &.{ ' ', '\t' });
11861186

11871187
// This line can have:
11881188
// * Interpreter path only,
11891189
// * Interpreter path and arguments, all separated by space, tab or NUL character.
11901190
// And optionally newline at the end.
1191-
const path_maybe_args = mem.trimRight(u8, trimmed_line, "\n");
1191+
const path_maybe_args = mem.trimEnd(u8, trimmed_line, "\n");
11921192

11931193
// Separate path and args.
11941194
const path_end = mem.indexOfAny(u8, path_maybe_args, &.{ ' ', '\t', 0 }) orelse path_maybe_args.len;

lib/std/zig/system/darwin.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn getSdk(allocator: Allocator, target: Target) ?[]const u8 {
5959
.Exited => |code| if (code != 0) return null,
6060
else => return null,
6161
}
62-
return allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")) catch null;
62+
return allocator.dupe(u8, mem.trimEnd(u8, result.stdout, "\r\n")) catch null;
6363
}
6464

6565
test {

lib/std/zig/system/linux.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ fn CpuinfoParser(comptime impl: anytype) type {
362362
while (true) {
363363
const line = (try reader.readUntilDelimiterOrEof(&line_buf, '\n')) orelse break;
364364
const colon_pos = mem.indexOfScalar(u8, line, ':') orelse continue;
365-
const key = mem.trimRight(u8, line[0..colon_pos], " \t");
366-
const value = mem.trimLeft(u8, line[colon_pos + 1 ..], " \t");
365+
const key = mem.trimEnd(u8, line[0..colon_pos], " \t");
366+
const value = mem.trimStart(u8, line[colon_pos + 1 ..], " \t");
367367

368368
if (!try obj.line_hook(key, value))
369369
break;

src/link.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub const Diags = struct {
192192
current_err.?.* = .{ .msg = duped_msg };
193193
} else if (current_err != null) {
194194
const context_prefix = ">>> ";
195-
var trimmed = mem.trimRight(u8, line, &std.ascii.whitespace);
195+
var trimmed = mem.trimEnd(u8, line, &std.ascii.whitespace);
196196
if (mem.startsWith(u8, trimmed, context_prefix)) {
197197
trimmed = trimmed[context_prefix.len..];
198198
}

src/link/MachO/Archive.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ pub const ar_hdr = extern struct {
159159
ar_fmag: [2]u8,
160160

161161
fn date(self: ar_hdr) !u64 {
162-
const value = mem.trimRight(u8, &self.ar_date, &[_]u8{@as(u8, 0x20)});
162+
const value = mem.trimEnd(u8, &self.ar_date, &[_]u8{@as(u8, 0x20)});
163163
return std.fmt.parseInt(u64, value, 10);
164164
}
165165

166166
fn size(self: ar_hdr) !u32 {
167-
const value = mem.trimRight(u8, &self.ar_size, &[_]u8{@as(u8, 0x20)});
167+
const value = mem.trimEnd(u8, &self.ar_size, &[_]u8{@as(u8, 0x20)});
168168
return std.fmt.parseInt(u32, value, 10);
169169
}
170170

@@ -178,7 +178,7 @@ pub const ar_hdr = extern struct {
178178
fn nameLength(self: ar_hdr) !?u32 {
179179
const value = &self.ar_name;
180180
if (!mem.startsWith(u8, value, "#1/")) return null;
181-
const trimmed = mem.trimRight(u8, self.ar_name["#1/".len..], &[_]u8{0x20});
181+
const trimmed = mem.trimEnd(u8, self.ar_name["#1/".len..], &[_]u8{0x20});
182182
return try std.fmt.parseInt(u32, trimmed, 10);
183183
}
184184
};

src/link/Wasm/Archive.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const Header = extern struct {
6464
}
6565

6666
fn getValue(raw: []const u8) []const u8 {
67-
return mem.trimRight(u8, raw, &[_]u8{@as(u8, 0x20)});
67+
return mem.trimEnd(u8, raw, &[_]u8{@as(u8, 0x20)});
6868
}
6969
};
7070

@@ -162,7 +162,7 @@ pub fn parseObject(
162162
.index => |index| n: {
163163
const long_file_names = file_contents[archive.long_file_names.off..][0..archive.long_file_names.len];
164164
const name = mem.sliceTo(long_file_names[index..], 0x0a);
165-
break :n mem.trimRight(u8, name, "/");
165+
break :n mem.trimEnd(u8, name, "/");
166166
},
167167
};
168168

test/src/Cases.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ const TestManifest = struct {
856856

857857
fn next(self: *TrailingIterator) ?[]const u8 {
858858
const next_inner = self.inner.next() orelse return null;
859-
return if (next_inner.len == 2) "" else std.mem.trimRight(u8, next_inner[3..], " \t");
859+
return if (next_inner.len == 2) "" else std.mem.trimEnd(u8, next_inner[3..], " \t");
860860
}
861861
};
862862

test/standalone/simple/guess_number/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn main() !void {
2020
try stdout.print("Input too long.\n", .{});
2121
continue;
2222
}
23-
const line = std.mem.trimRight(u8, line_buf[0..amt], "\r\n");
23+
const line = std.mem.trimEnd(u8, line_buf[0..amt], "\r\n");
2424

2525
const guess = fmt.parseUnsigned(u8, line, 10) catch {
2626
try stdout.print("Invalid number.\n", .{});

tools/docgen.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,10 @@ fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void {
943943
var cmd_cont: bool = false;
944944
var iter = std.mem.splitScalar(u8, trimmed_shell_content, '\n');
945945
while (iter.next()) |orig_line| {
946-
const line = mem.trimRight(u8, orig_line, " \r");
946+
const line = mem.trimEnd(u8, orig_line, " \r");
947947
if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] != '\\') {
948948
try out.writeAll("$ <kbd>");
949-
const s = std.mem.trimLeft(u8, line[1..], " ");
949+
const s = std.mem.trimStart(u8, line[1..], " ");
950950
if (escape) {
951951
try writeEscaped(out, s);
952952
} else {
@@ -955,7 +955,7 @@ fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void {
955955
try out.writeAll("</kbd>" ++ "\n");
956956
} else if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] == '\\') {
957957
try out.writeAll("$ <kbd>");
958-
const s = std.mem.trimLeft(u8, line[1..], " ");
958+
const s = std.mem.trimStart(u8, line[1..], " ");
959959
if (escape) {
960960
try writeEscaped(out, s);
961961
} else {

tools/doctest.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,10 +1109,10 @@ fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void {
11091109
var cmd_cont: bool = false;
11101110
var iter = std.mem.splitScalar(u8, trimmed_shell_content, '\n');
11111111
while (iter.next()) |orig_line| {
1112-
const line = mem.trimRight(u8, orig_line, " \r");
1112+
const line = mem.trimEnd(u8, orig_line, " \r");
11131113
if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] != '\\') {
11141114
try out.writeAll("$ <kbd>");
1115-
const s = std.mem.trimLeft(u8, line[1..], " ");
1115+
const s = std.mem.trimStart(u8, line[1..], " ");
11161116
if (escape) {
11171117
try writeEscaped(out, s);
11181118
} else {
@@ -1121,7 +1121,7 @@ fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void {
11211121
try out.writeAll("</kbd>" ++ "\n");
11221122
} else if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] == '\\') {
11231123
try out.writeAll("$ <kbd>");
1124-
const s = std.mem.trimLeft(u8, line[1..], " ");
1124+
const s = std.mem.trimStart(u8, line[1..], " ");
11251125
if (escape) {
11261126
try writeEscaped(out, s);
11271127
} else {

0 commit comments

Comments
 (0)