Skip to content

Commit 976291f

Browse files
committed
chore(std.mem): Rename trimLeft and trimRight
Rename `trimLeft` to `trimStart`, and `trimRight` to `trimEnd`. `trimLeft` and `trimRight` functions remain as deprecated aliases for these new names.
1 parent 3783b1b commit 976291f

File tree

22 files changed

+65
-47
lines changed

22 files changed

+65
-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: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,19 +1198,37 @@ pub fn allEqual(comptime T: type, slice: []const T, scalar: T) bool {
11981198
}
11991199

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

1207+
test trimStart {
1208+
try testing.expectEqualSlices(u8, "foo\n ", trimStart(u8, " foo\n ", " \n"));
1209+
}
1210+
1211+
/// Deprecated: use `trimStart` instead.
1212+
pub fn trimLeft(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
1213+
return trimStart(T, slice, values_to_strip);
1214+
}
1215+
12071216
/// Remove a set of values from the end of a slice.
1208-
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 {
12091218
var end: usize = slice.len;
12101219
while (end > 0 and indexOfScalar(T, values_to_strip, slice[end - 1]) != null) : (end -= 1) {}
12111220
return slice[0..end];
12121221
}
12131222

1223+
test trimEnd {
1224+
try testing.expectEqualSlices(u8, " foo", trimEnd(u8, " foo\n ", " \n"));
1225+
}
1226+
1227+
/// Deprecated: use `trimEnd` instead.
1228+
pub fn trimRight(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
1229+
return trimEnd(T, slice, values_to_strip);
1230+
}
1231+
12141232
/// Remove a set of values from the beginning and end of a slice.
12151233
pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []const T {
12161234
var begin: usize = 0;
@@ -1221,8 +1239,8 @@ pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []co
12211239
}
12221240

12231241
test trim {
1224-
try testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n"));
1225-
try testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n"));
1242+
try testing.expectEqualSlices(u8, "foo\n ", trimStart(u8, " foo\n ", " \n"));
1243+
try testing.expectEqualSlices(u8, " foo", trimEnd(u8, " foo\n ", " \n"));
12261244
try testing.expectEqualSlices(u8, "foo", trim(u8, " foo\n ", " \n"));
12271245
try testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n"));
12281246
}

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)) |_| {

0 commit comments

Comments
 (0)