Skip to content

Commit 629f0d2

Browse files
authored
Merge pull request #15579 from squeek502/mem-delimiters
Split `std.mem.split` and `tokenize` into `sequence`, `any`, and `scalar` versions
2 parents 3add9d8 + 104f405 commit 629f0d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+587
-235
lines changed

build.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn build(b: *std.Build) !void {
235235
},
236236
2 => {
237237
// Untagged development build (e.g. 0.10.0-dev.2025+ecf0050a9).
238-
var it = mem.split(u8, git_describe, "-");
238+
var it = mem.splitScalar(u8, git_describe, '-');
239239
const tagged_ancestor = it.first();
240240
const commit_height = it.next().?;
241241
const commit_id = it.next().?;
@@ -280,7 +280,7 @@ pub fn build(b: *std.Build) !void {
280280
// That means we also have to rely on stage1 compiled c++ files. We parse config.h to find
281281
// the information passed on to us from cmake.
282282
if (cfg.cmake_prefix_path.len > 0) {
283-
var it = mem.tokenize(u8, cfg.cmake_prefix_path, ";");
283+
var it = mem.tokenizeScalar(u8, cfg.cmake_prefix_path, ';');
284284
while (it.next()) |path| {
285285
b.addSearchPrefix(path);
286286
}
@@ -682,7 +682,7 @@ fn addCxxKnownPath(
682682
if (!std.process.can_spawn)
683683
return error.RequiredLibraryNotFound;
684684
const path_padded = b.exec(&.{ ctx.cxx_compiler, b.fmt("-print-file-name={s}", .{objname}) });
685-
var tokenizer = mem.tokenize(u8, path_padded, "\r\n");
685+
var tokenizer = mem.tokenizeAny(u8, path_padded, "\r\n");
686686
const path_unpadded = tokenizer.next().?;
687687
if (mem.eql(u8, path_unpadded, objname)) {
688688
if (errtxt) |msg| {
@@ -705,7 +705,7 @@ fn addCxxKnownPath(
705705
}
706706

707707
fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void {
708-
var it = mem.tokenize(u8, list, ";");
708+
var it = mem.tokenizeScalar(u8, list, ';');
709709
while (it.next()) |lib| {
710710
if (mem.startsWith(u8, lib, "-l")) {
711711
exe.linkSystemLibrary(lib["-l".len..]);
@@ -850,18 +850,18 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {
850850
// .prefix = ZIG_LLVM_LINK_MODE parsed manually below
851851
};
852852

853-
var lines_it = mem.tokenize(u8, config_h_text, "\r\n");
853+
var lines_it = mem.tokenizeAny(u8, config_h_text, "\r\n");
854854
while (lines_it.next()) |line| {
855855
inline for (mappings) |mapping| {
856856
if (mem.startsWith(u8, line, mapping.prefix)) {
857-
var it = mem.split(u8, line, "\"");
857+
var it = mem.splitScalar(u8, line, '"');
858858
_ = it.first(); // skip the stuff before the quote
859859
const quoted = it.next().?; // the stuff inside the quote
860860
@field(ctx, mapping.field) = toNativePathSep(b, quoted);
861861
}
862862
}
863863
if (mem.startsWith(u8, line, "#define ZIG_LLVM_LINK_MODE ")) {
864-
var it = mem.split(u8, line, "\"");
864+
var it = mem.splitScalar(u8, line, '"');
865865
_ = it.next().?; // skip the stuff before the quote
866866
const quoted = it.next().?; // the stuff inside the quote
867867
ctx.llvm_linkage = if (mem.eql(u8, quoted, "shared")) .dynamic else .static;

doc/docgen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void {
12231223
const trimmed_shell_content = mem.trim(u8, shell_content, " \n");
12241224
try out.writeAll("<figure><figcaption class=\"shell-cap\">Shell</figcaption><pre><samp>");
12251225
var cmd_cont: bool = false;
1226-
var iter = std.mem.split(u8, trimmed_shell_content, "\n");
1226+
var iter = std.mem.splitScalar(u8, trimmed_shell_content, '\n');
12271227
while (iter.next()) |orig_line| {
12281228
const line = mem.trimRight(u8, orig_line, " ");
12291229
if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] != '\\') {

lib/std/Build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ pub fn findProgram(self: *Build, names: []const []const u8, paths: []const []con
13881388
if (fs.path.isAbsolute(name)) {
13891389
return name;
13901390
}
1391-
var it = mem.tokenize(u8, PATH, &[_]u8{fs.path.delimiter});
1391+
var it = mem.tokenizeScalar(u8, PATH, fs.path.delimiter);
13921392
while (it.next()) |path| {
13931393
const full_path = self.pathJoin(&.{
13941394
path,

lib/std/Build/Cache.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ pub const Manifest = struct {
438438

439439
const input_file_count = self.files.items.len;
440440
var any_file_changed = false;
441-
var line_iter = mem.tokenize(u8, file_contents, "\n");
441+
var line_iter = mem.tokenizeScalar(u8, file_contents, '\n');
442442
var idx: usize = 0;
443443
if (if (line_iter.next()) |line| !std.mem.eql(u8, line, manifest_header) else true) {
444444
if (try self.upgradeToExclusiveLock()) continue;
@@ -467,7 +467,7 @@ pub const Manifest = struct {
467467
break :blk new;
468468
};
469469

470-
var iter = mem.tokenize(u8, line, " ");
470+
var iter = mem.tokenizeScalar(u8, line, ' ');
471471
const size = iter.next() orelse return error.InvalidFormat;
472472
const inode = iter.next() orelse return error.InvalidFormat;
473473
const mtime_nsec_str = iter.next() orelse return error.InvalidFormat;

lib/std/Build/Step/CheckObject.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ const Action = struct {
103103
assert(act.tag == .match or act.tag == .not_present);
104104
const phrase = act.phrase.resolve(b, step);
105105
var candidate_var: ?struct { name: []const u8, value: u64 } = null;
106-
var hay_it = mem.tokenize(u8, mem.trim(u8, haystack, " "), " ");
107-
var needle_it = mem.tokenize(u8, mem.trim(u8, phrase, " "), " ");
106+
var hay_it = mem.tokenizeScalar(u8, mem.trim(u8, haystack, " "), ' ');
107+
var needle_it = mem.tokenizeScalar(u8, mem.trim(u8, phrase, " "), ' ');
108108

109109
while (needle_it.next()) |needle_tok| {
110110
const hay_tok = hay_it.next() orelse return false;
@@ -155,7 +155,7 @@ const Action = struct {
155155
var op_stack = std.ArrayList(enum { add, sub, mod, mul }).init(gpa);
156156
var values = std.ArrayList(u64).init(gpa);
157157

158-
var it = mem.tokenize(u8, phrase, " ");
158+
var it = mem.tokenizeScalar(u8, phrase, ' ');
159159
while (it.next()) |next| {
160160
if (mem.eql(u8, next, "+")) {
161161
try op_stack.append(.add);
@@ -365,7 +365,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
365365
var vars = std.StringHashMap(u64).init(gpa);
366366

367367
for (self.checks.items) |chk| {
368-
var it = mem.tokenize(u8, output, "\r\n");
368+
var it = mem.tokenizeAny(u8, output, "\r\n");
369369
for (chk.actions.items) |act| {
370370
switch (act.tag) {
371371
.match => {

lib/std/Build/Step/Compile.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ fn runPkgConfig(self: *Compile, lib_name: []const u8) ![]const []const u8 {
853853
var zig_args = ArrayList([]const u8).init(b.allocator);
854854
defer zig_args.deinit();
855855

856-
var it = mem.tokenize(u8, stdout, " \r\n\t");
856+
var it = mem.tokenizeAny(u8, stdout, " \r\n\t");
857857
while (it.next()) |tok| {
858858
if (mem.eql(u8, tok, "-I")) {
859859
const dir = it.next() orelse return error.PkgConfigInvalidOutput;
@@ -2101,10 +2101,10 @@ fn execPkgConfigList(self: *std.Build, out_code: *u8) (PkgConfigError || ExecErr
21012101
const stdout = try self.execAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
21022102
var list = ArrayList(PkgConfigPkg).init(self.allocator);
21032103
errdefer list.deinit();
2104-
var line_it = mem.tokenize(u8, stdout, "\r\n");
2104+
var line_it = mem.tokenizeAny(u8, stdout, "\r\n");
21052105
while (line_it.next()) |line| {
21062106
if (mem.trim(u8, line, " \t").len == 0) continue;
2107-
var tok_it = mem.tokenize(u8, line, " \t");
2107+
var tok_it = mem.tokenizeAny(u8, line, " \t");
21082108
try list.append(PkgConfigPkg{
21092109
.name = tok_it.next() orelse return error.PkgConfigInvalidOutput,
21102110
.desc = tok_it.rest(),
@@ -2224,7 +2224,7 @@ fn checkCompileErrors(self: *Compile) !void {
22242224
// Render the expected lines into a string that we can compare verbatim.
22252225
var expected_generated = std.ArrayList(u8).init(arena);
22262226

2227-
var actual_line_it = mem.split(u8, actual_stderr, "\n");
2227+
var actual_line_it = mem.splitScalar(u8, actual_stderr, '\n');
22282228
for (self.expect_errors) |expect_line| {
22292229
const actual_line = actual_line_it.next() orelse {
22302230
try expected_generated.appendSlice(expect_line);

lib/std/Build/Step/ConfigHeader.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ fn render_autoconf(
250250

251251
var any_errors = false;
252252
var line_index: u32 = 0;
253-
var line_it = std.mem.split(u8, contents, "\n");
253+
var line_it = std.mem.splitScalar(u8, contents, '\n');
254254
while (line_it.next()) |line| : (line_index += 1) {
255255
if (!std.mem.startsWith(u8, line, "#")) {
256256
try output.appendSlice(line);
257257
try output.appendSlice("\n");
258258
continue;
259259
}
260-
var it = std.mem.tokenize(u8, line[1..], " \t\r");
260+
var it = std.mem.tokenizeAny(u8, line[1..], " \t\r");
261261
const undef = it.next().?;
262262
if (!std.mem.eql(u8, undef, "undef")) {
263263
try output.appendSlice(line);
@@ -297,14 +297,14 @@ fn render_cmake(
297297

298298
var any_errors = false;
299299
var line_index: u32 = 0;
300-
var line_it = std.mem.split(u8, contents, "\n");
300+
var line_it = std.mem.splitScalar(u8, contents, '\n');
301301
while (line_it.next()) |line| : (line_index += 1) {
302302
if (!std.mem.startsWith(u8, line, "#")) {
303303
try output.appendSlice(line);
304304
try output.appendSlice("\n");
305305
continue;
306306
}
307-
var it = std.mem.tokenize(u8, line[1..], " \t\r");
307+
var it = std.mem.tokenizeAny(u8, line[1..], " \t\r");
308308
const cmakedefine = it.next().?;
309309
if (!std.mem.eql(u8, cmakedefine, "cmakedefine") and
310310
!std.mem.eql(u8, cmakedefine, "cmakedefine01"))

lib/std/SemanticVersion.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ pub fn order(lhs: Version, rhs: Version) std.math.Order {
4242
if (lhs.pre == null and rhs.pre != null) return .gt;
4343

4444
// Iterate over pre-release identifiers until a difference is found.
45-
var lhs_pre_it = std.mem.split(u8, lhs.pre.?, ".");
46-
var rhs_pre_it = std.mem.split(u8, rhs.pre.?, ".");
45+
var lhs_pre_it = std.mem.splitScalar(u8, lhs.pre.?, '.');
46+
var rhs_pre_it = std.mem.splitScalar(u8, rhs.pre.?, '.');
4747
while (true) {
4848
const next_lid = lhs_pre_it.next();
4949
const next_rid = rhs_pre_it.next();
@@ -86,7 +86,7 @@ pub fn parse(text: []const u8) !Version {
8686
// Parse the required major, minor, and patch numbers.
8787
const extra_index = std.mem.indexOfAny(u8, text, "-+");
8888
const required = text[0..(extra_index orelse text.len)];
89-
var it = std.mem.split(u8, required, ".");
89+
var it = std.mem.splitScalar(u8, required, '.');
9090
var ver = Version{
9191
.major = try parseNum(it.first()),
9292
.minor = try parseNum(it.next() orelse return error.InvalidVersion),
@@ -108,7 +108,7 @@ pub fn parse(text: []const u8) !Version {
108108
// Check validity of optional pre-release identifiers.
109109
// See: https://semver.org/#spec-item-9
110110
if (ver.pre) |pre| {
111-
it = std.mem.split(u8, pre, ".");
111+
it = std.mem.splitScalar(u8, pre, '.');
112112
while (it.next()) |id| {
113113
// Identifiers MUST NOT be empty.
114114
if (id.len == 0) return error.InvalidVersion;
@@ -127,7 +127,7 @@ pub fn parse(text: []const u8) !Version {
127127
// Check validity of optional build metadata identifiers.
128128
// See: https://semver.org/#spec-item-10
129129
if (ver.build) |build| {
130-
it = std.mem.split(u8, build, ".");
130+
it = std.mem.splitScalar(u8, build, '.');
131131
while (it.next()) |id| {
132132
// Identifiers MUST NOT be empty.
133133
if (id.len == 0) return error.InvalidVersion;

lib/std/builtin.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ pub const Version = struct {
531531
// found no digits or '.' before unexpected character
532532
if (end == 0) return error.InvalidVersion;
533533

534-
var it = std.mem.split(u8, text[0..end], ".");
534+
var it = std.mem.splitScalar(u8, text[0..end], '.');
535535
// substring is not empty, first call will succeed
536536
const major = it.first();
537537
if (major.len == 0) return error.InvalidVersion;

lib/std/child_process.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ pub const ChildProcess = struct {
850850
return original_err;
851851
}
852852

853-
var it = mem.tokenize(u16, PATH, &[_]u16{';'});
853+
var it = mem.tokenizeScalar(u16, PATH, ';');
854854
while (it.next()) |search_path| {
855855
dir_buf.clearRetainingCapacity();
856856
try dir_buf.appendSlice(self.allocator, search_path);
@@ -1064,7 +1064,7 @@ fn windowsCreateProcessPathExt(
10641064
// Now we know that at least *a* file matching the wildcard exists, we can loop
10651065
// through PATHEXT in order and exec any that exist
10661066

1067-
var ext_it = mem.tokenize(u16, pathext, &[_]u16{';'});
1067+
var ext_it = mem.tokenizeScalar(u16, pathext, ';');
10681068
while (ext_it.next()) |ext| {
10691069
if (!windowsCreateProcessSupportsExtension(ext)) continue;
10701070

lib/std/crypto/Certificate.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ pub const Parsed = struct {
337337
return true; // exact match
338338
}
339339

340-
var it_host = std.mem.split(u8, host_name, ".");
341-
var it_dns = std.mem.split(u8, dns_name, ".");
340+
var it_host = std.mem.splitScalar(u8, host_name, '.');
341+
var it_dns = std.mem.splitScalar(u8, dns_name, '.');
342342

343343
const len_match = while (true) {
344344
const host = it_host.next();

lib/std/crypto/phc_encoding.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const mem = std.mem;
77
const meta = std.meta;
88

99
const fields_delimiter = "$";
10+
const fields_delimiter_scalar = '$';
1011
const version_param_name = "v";
1112
const params_delimiter = ",";
13+
const params_delimiter_scalar = ',';
1214
const kv_delimiter = "=";
15+
const kv_delimiter_scalar = '=';
1316

1417
pub const Error = std.crypto.errors.EncodingError || error{NoSpaceLeft};
1518

@@ -73,7 +76,7 @@ pub fn BinValue(comptime max_len: usize) type {
7376
/// Other fields will also be deserialized from the function parameters section.
7477
pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult {
7578
var out = mem.zeroes(HashResult);
76-
var it = mem.split(u8, str, fields_delimiter);
79+
var it = mem.splitScalar(u8, str, fields_delimiter_scalar);
7780
var set_fields: usize = 0;
7881

7982
while (true) {
@@ -104,7 +107,7 @@ pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult
104107

105108
// Read optional parameters
106109
var has_params = false;
107-
var it_params = mem.split(u8, field, params_delimiter);
110+
var it_params = mem.splitScalar(u8, field, params_delimiter_scalar);
108111
while (it_params.next()) |params| {
109112
const param = kvSplit(params) catch break;
110113
var found = false;
@@ -252,7 +255,7 @@ fn serializeTo(params: anytype, out: anytype) !void {
252255

253256
// Split a `key=value` string into `key` and `value`
254257
fn kvSplit(str: []const u8) !struct { key: []const u8, value: []const u8 } {
255-
var it = mem.split(u8, str, kv_delimiter);
258+
var it = mem.splitScalar(u8, str, kv_delimiter_scalar);
256259
const key = it.first();
257260
const value = it.next() orelse return Error.InvalidEncoding;
258261
const ret = .{ .key = key, .value = value };

lib/std/crypto/scrypt.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ const crypt_format = struct {
287287
out.r = try Codec.intDecode(u30, str[4..9]);
288288
out.p = try Codec.intDecode(u30, str[9..14]);
289289

290-
var it = mem.split(u8, str[14..], "$");
290+
var it = mem.splitScalar(u8, str[14..], '$');
291291

292292
const salt = it.first();
293293
if (@hasField(T, "salt")) out.salt = salt;

lib/std/fs.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3021,7 +3021,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
30213021
} else if (argv0.len != 0) {
30223022
// argv[0] is not empty (and not a path): search it inside PATH
30233023
const PATH = std.os.getenvZ("PATH") orelse return error.FileNotFound;
3024-
var path_it = mem.tokenize(u8, PATH, &[_]u8{path.delimiter});
3024+
var path_it = mem.tokenizeScalar(u8, PATH, path.delimiter);
30253025
while (path_it.next()) |a_path| {
30263026
var resolved_path_buf: [MAX_PATH_BYTES - 1:0]u8 = undefined;
30273027
const resolved_path = std.fmt.bufPrintZ(&resolved_path_buf, "{s}/{s}", .{

0 commit comments

Comments
 (0)