Skip to content

Commit

Permalink
Update all std.mem.split calls to their appropriate function
Browse files Browse the repository at this point in the history
Everywhere that can now use `splitScalar` should get a nice little performance boost.
  • Loading branch information
squeek502 committed May 6, 2023
1 parent 69f0a08 commit 982f5af
Show file tree
Hide file tree
Showing 27 changed files with 58 additions and 55 deletions.
6 changes: 3 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub fn build(b: *std.Build) !void {
},
2 => {
// Untagged development build (e.g. 0.10.0-dev.2025+ecf0050a9).
var it = mem.split(u8, git_describe, "-");
var it = mem.splitScalar(u8, git_describe, '-');
const tagged_ancestor = it.first();
const commit_height = it.next().?;
const commit_id = it.next().?;
Expand Down Expand Up @@ -859,14 +859,14 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {
while (lines_it.next()) |line| {
inline for (mappings) |mapping| {
if (mem.startsWith(u8, line, mapping.prefix)) {
var it = mem.split(u8, line, "\"");
var it = mem.splitScalar(u8, line, '"');
_ = it.first(); // skip the stuff before the quote
const quoted = it.next().?; // the stuff inside the quote
@field(ctx, mapping.field) = toNativePathSep(b, quoted);
}
}
if (mem.startsWith(u8, line, "#define ZIG_LLVM_LINK_MODE ")) {
var it = mem.split(u8, line, "\"");
var it = mem.splitScalar(u8, line, '"');
_ = it.next().?; // skip the stuff before the quote
const quoted = it.next().?; // the stuff inside the quote
ctx.llvm_linkage = if (mem.eql(u8, quoted, "shared")) .dynamic else .static;
Expand Down
2 changes: 1 addition & 1 deletion doc/docgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void {
const trimmed_shell_content = mem.trim(u8, shell_content, " \n");
try out.writeAll("<figure><figcaption class=\"shell-cap\">Shell</figcaption><pre><samp>");
var cmd_cont: bool = false;
var iter = std.mem.split(u8, trimmed_shell_content, "\n");
var iter = std.mem.splitScalar(u8, trimmed_shell_content, '\n');
while (iter.next()) |orig_line| {
const line = mem.trimRight(u8, orig_line, " ");
if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] != '\\') {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ fn checkCompileErrors(self: *Compile) !void {
// Render the expected lines into a string that we can compare verbatim.
var expected_generated = std.ArrayList(u8).init(arena);

var actual_line_it = mem.split(u8, actual_stderr, "\n");
var actual_line_it = mem.splitScalar(u8, actual_stderr, '\n');
for (self.expect_errors) |expect_line| {
const actual_line = actual_line_it.next() orelse {
try expected_generated.appendSlice(expect_line);
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Build/Step/ConfigHeader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fn render_autoconf(

var any_errors = false;
var line_index: u32 = 0;
var line_it = std.mem.split(u8, contents, "\n");
var line_it = std.mem.splitScalar(u8, contents, '\n');
while (line_it.next()) |line| : (line_index += 1) {
if (!std.mem.startsWith(u8, line, "#")) {
try output.appendSlice(line);
Expand Down Expand Up @@ -297,7 +297,7 @@ fn render_cmake(

var any_errors = false;
var line_index: u32 = 0;
var line_it = std.mem.split(u8, contents, "\n");
var line_it = std.mem.splitScalar(u8, contents, '\n');
while (line_it.next()) |line| : (line_index += 1) {
if (!std.mem.startsWith(u8, line, "#")) {
try output.appendSlice(line);
Expand Down
10 changes: 5 additions & 5 deletions lib/std/SemanticVersion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub fn order(lhs: Version, rhs: Version) std.math.Order {
if (lhs.pre == null and rhs.pre != null) return .gt;

// Iterate over pre-release identifiers until a difference is found.
var lhs_pre_it = std.mem.split(u8, lhs.pre.?, ".");
var rhs_pre_it = std.mem.split(u8, rhs.pre.?, ".");
var lhs_pre_it = std.mem.splitScalar(u8, lhs.pre.?, '.');
var rhs_pre_it = std.mem.splitScalar(u8, rhs.pre.?, '.');
while (true) {
const next_lid = lhs_pre_it.next();
const next_rid = rhs_pre_it.next();
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn parse(text: []const u8) !Version {
// Parse the required major, minor, and patch numbers.
const extra_index = std.mem.indexOfAny(u8, text, "-+");
const required = text[0..(extra_index orelse text.len)];
var it = std.mem.split(u8, required, ".");
var it = std.mem.splitScalar(u8, required, '.');
var ver = Version{
.major = try parseNum(it.first()),
.minor = try parseNum(it.next() orelse return error.InvalidVersion),
Expand All @@ -108,7 +108,7 @@ pub fn parse(text: []const u8) !Version {
// Check validity of optional pre-release identifiers.
// See: https://semver.org/#spec-item-9
if (ver.pre) |pre| {
it = std.mem.split(u8, pre, ".");
it = std.mem.splitScalar(u8, pre, '.');
while (it.next()) |id| {
// Identifiers MUST NOT be empty.
if (id.len == 0) return error.InvalidVersion;
Expand All @@ -127,7 +127,7 @@ pub fn parse(text: []const u8) !Version {
// Check validity of optional build metadata identifiers.
// See: https://semver.org/#spec-item-10
if (ver.build) |build| {
it = std.mem.split(u8, build, ".");
it = std.mem.splitScalar(u8, build, '.');
while (it.next()) |id| {
// Identifiers MUST NOT be empty.
if (id.len == 0) return error.InvalidVersion;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ pub const Version = struct {
// found no digits or '.' before unexpected character
if (end == 0) return error.InvalidVersion;

var it = std.mem.split(u8, text[0..end], ".");
var it = std.mem.splitScalar(u8, text[0..end], '.');
// substring is not empty, first call will succeed
const major = it.first();
if (major.len == 0) return error.InvalidVersion;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/crypto/Certificate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ pub const Parsed = struct {
return true; // exact match
}

var it_host = std.mem.split(u8, host_name, ".");
var it_dns = std.mem.split(u8, dns_name, ".");
var it_host = std.mem.splitScalar(u8, host_name, '.');
var it_dns = std.mem.splitScalar(u8, dns_name, '.');

const len_match = while (true) {
const host = it_host.next();
Expand Down
9 changes: 6 additions & 3 deletions lib/std/crypto/phc_encoding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ const mem = std.mem;
const meta = std.meta;

const fields_delimiter = "$";
const fields_delimiter_scalar = '$';
const version_param_name = "v";
const params_delimiter = ",";
const params_delimiter_scalar = ',';
const kv_delimiter = "=";
const kv_delimiter_scalar = '=';

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

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

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

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

// Split a `key=value` string into `key` and `value`
fn kvSplit(str: []const u8) !struct { key: []const u8, value: []const u8 } {
var it = mem.split(u8, str, kv_delimiter);
var it = mem.splitScalar(u8, str, kv_delimiter_scalar);
const key = it.first();
const value = it.next() orelse return Error.InvalidEncoding;
const ret = .{ .key = key, .value = value };
Expand Down
2 changes: 1 addition & 1 deletion lib/std/crypto/scrypt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ const crypt_format = struct {
out.r = try Codec.intDecode(u30, str[4..9]);
out.p = try Codec.intDecode(u30, str[9..14]);

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

const salt = it.first();
if (@hasField(T, "salt")) out.salt = salt;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/http/Client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub const Response = struct {
} else if (std.ascii.eqlIgnoreCase(header_name, "transfer-encoding")) {
// Transfer-Encoding: second, first
// Transfer-Encoding: deflate, chunked
var iter = mem.splitBackwards(u8, header_value, ",");
var iter = mem.splitBackwardsScalar(u8, header_value, ',');

if (iter.next()) |first| {
const trimmed = mem.trim(u8, first, " ");
Expand Down
2 changes: 1 addition & 1 deletion lib/std/http/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub const Request = struct {
} else if (std.ascii.eqlIgnoreCase(header_name, "transfer-encoding")) {
// Transfer-Encoding: second, first
// Transfer-Encoding: deflate, chunked
var iter = mem.splitBackwards(u8, header_value, ",");
var iter = mem.splitBackwardsScalar(u8, header_value, ',');

if (iter.next()) |first| {
const trimmed = mem.trim(u8, first, " ");
Expand Down
6 changes: 3 additions & 3 deletions lib/std/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ fn linuxLookupNameFromHosts(
},
else => |e| return e,
}) |line| {
var split_it = mem.split(u8, line, "#");
var split_it = mem.splitScalar(u8, line, '#');
const no_comment_line = split_it.first();

var line_it = mem.tokenizeAny(u8, no_comment_line, " \t");
Expand Down Expand Up @@ -1465,15 +1465,15 @@ fn getResolvConf(allocator: mem.Allocator, rc: *ResolvConf) !void {
else => |e| return e,
}) |line| {
const no_comment_line = no_comment_line: {
var split = mem.split(u8, line, "#");
var split = mem.splitScalar(u8, line, '#');
break :no_comment_line split.first();
};
var line_it = mem.tokenizeAny(u8, no_comment_line, " \t");

const token = line_it.next() orelse continue;
if (mem.eql(u8, token, "options")) {
while (line_it.next()) |sub_tok| {
var colon_it = mem.split(u8, sub_tok, ":");
var colon_it = mem.splitScalar(u8, sub_tok, ':');
const name = colon_it.first();
const value_txt = colon_it.next() orelse continue;
const value = std.fmt.parseInt(u8, value_txt, 10) catch |err| switch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub fn getEnvMap(allocator: Allocator) !EnvMap {

for (environ) |env| {
const pair = mem.sliceTo(env, 0);
var parts = mem.split(u8, pair, "=");
var parts = mem.splitScalar(u8, pair, '=');
const key = parts.first();
const value = parts.rest();
try result.put(key, value);
Expand Down
12 changes: 6 additions & 6 deletions lib/std/zig/CrossTarget.zig
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub fn parse(args: ParseOptions) !CrossTarget {
.dynamic_linker = DynamicLinker.init(args.dynamic_linker),
};

var it = mem.split(u8, args.arch_os_abi, "-");
var it = mem.splitScalar(u8, args.arch_os_abi, '-');
const arch_name = it.first();
const arch_is_native = mem.eql(u8, arch_name, "native");
if (!arch_is_native) {
Expand All @@ -257,7 +257,7 @@ pub fn parse(args: ParseOptions) !CrossTarget {

const opt_abi_text = it.next();
if (opt_abi_text) |abi_text| {
var abi_it = mem.split(u8, abi_text, ".");
var abi_it = mem.splitScalar(u8, abi_text, '.');
const abi = std.meta.stringToEnum(Target.Abi, abi_it.first()) orelse
return error.UnknownApplicationBinaryInterface;
result.abi = abi;
Expand Down Expand Up @@ -343,7 +343,7 @@ pub fn parse(args: ParseOptions) !CrossTarget {
/// This is intended to be used if the API user of CrossTarget needs to learn the
/// target CPU architecture in order to fully populate `ParseOptions`.
pub fn parseCpuArch(args: ParseOptions) ?Target.Cpu.Arch {
var it = mem.split(u8, args.arch_os_abi, "-");
var it = mem.splitScalar(u8, args.arch_os_abi, '-');
const arch_name = it.first();
const arch_is_native = mem.eql(u8, arch_name, "native");
if (arch_is_native) {
Expand Down Expand Up @@ -645,7 +645,7 @@ pub fn updateCpuFeatures(self: CrossTarget, set: *Target.Cpu.Feature.Set) void {
}

fn parseOs(result: *CrossTarget, diags: *ParseOptions.Diagnostics, text: []const u8) !void {
var it = mem.split(u8, text, ".");
var it = mem.splitScalar(u8, text, '.');
const os_name = it.first();
diags.os_name = os_name;
const os_is_native = mem.eql(u8, os_name, "native");
Expand Down Expand Up @@ -706,7 +706,7 @@ fn parseOs(result: *CrossTarget, diags: *ParseOptions.Diagnostics, text: []const
.linux,
.dragonfly,
=> {
var range_it = mem.split(u8, version_text, "...");
var range_it = mem.splitFull(u8, version_text, "...");

const min_text = range_it.next().?;
const min_ver = SemVer.parse(min_text) catch |err| switch (err) {
Expand All @@ -726,7 +726,7 @@ fn parseOs(result: *CrossTarget, diags: *ParseOptions.Diagnostics, text: []const
},

.windows => {
var range_it = mem.split(u8, version_text, "...");
var range_it = mem.splitFull(u8, version_text, "...");

const min_text = range_it.first();
const min_ver = std.meta.stringToEnum(Target.Os.WindowsVersion, min_text) orelse
Expand Down
2 changes: 1 addition & 1 deletion lib/std/zig/ErrorBundle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ fn renderErrorMessageToWriter(
///
/// This is used to split the message in `@compileError("hello\nworld")` for example.
fn writeMsg(eb: ErrorBundle, err_msg: ErrorMessage, stderr: anytype, indent: usize) !void {
var lines = std.mem.split(u8, eb.nullTerminatedString(err_msg.msg), "\n");
var lines = std.mem.splitScalar(u8, eb.nullTerminatedString(err_msg.msg), '\n');
while (lines.next()) |line| {
try stderr.writeAll(line);
if (lines.index == null) break;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1990,7 +1990,7 @@ fn renderArrayInit(
if (!expr_newlines[i]) {
try ais.writer().writeAll(expr_text);
} else {
var by_line = std.mem.split(u8, expr_text, "\n");
var by_line = std.mem.splitScalar(u8, expr_text, '\n');
var last_line_was_empty = false;
try ais.writer().writeAll(by_line.first());
while (by_line.next()) |line| {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/zig/system/NativeTargetInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {
const dynstr_size = @intCast(usize, dynstr.size);
const dynstr_bytes = buf[0..dynstr_size];
_ = try preadMin(file, dynstr_bytes, dynstr.offset, dynstr_bytes.len);
var it = mem.split(u8, dynstr_bytes, &.{0});
var it = mem.splitScalar(u8, dynstr_bytes, 0);
var max_ver: std.builtin.Version = .{ .major = 2, .minor = 2, .patch = 5 };
while (it.next()) |s| {
if (mem.startsWith(u8, s, "GLIBC_2.")) {
Expand Down
2 changes: 1 addition & 1 deletion src/Autodoc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4837,7 +4837,7 @@ fn findGuidePaths(self: *Autodoc, file: *File, str: []const u8) ![]const u8 {

// TODO: this algo is kinda inefficient

var it = std.mem.split(u8, str, "\n");
var it = std.mem.splitScalar(u8, str, '\n');
while (it.next()) |line| {
const trimmed_line = std.mem.trim(u8, line, " ");
if (std.mem.startsWith(u8, trimmed_line, guide_prefix)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4636,7 +4636,7 @@ pub fn hasSharedLibraryExt(filename: []const u8) bool {
return true;
}
// Look for .so.X, .so.X.Y, .so.X.Y.Z
var it = mem.split(u8, filename, ".");
var it = mem.splitScalar(u8, filename, '.');
_ = it.first();
var so_txt = it.next() orelse return false;
while (!mem.eql(u8, so_txt, "so")) {
Expand Down Expand Up @@ -5016,14 +5016,14 @@ fn parseLldStderr(comp: *Compilation, comptime prefix: []const u8, stderr: []con
defer context_lines.deinit();

var current_err: ?*LldError = null;
var lines = mem.split(u8, stderr, std.cstr.line_sep);
var lines = mem.splitFull(u8, stderr, std.cstr.line_sep);
while (lines.next()) |line| {
if (mem.startsWith(u8, line, prefix ++ ":")) {
if (current_err) |err| {
err.context_lines = try context_lines.toOwnedSlice();
}

var split = std.mem.split(u8, line, "error: ");
var split = std.mem.splitFull(u8, line, "error: ");
_ = split.first();

const duped_msg = try std.fmt.allocPrint(comp.gpa, "{s}: {s}", .{ prefix, split.rest() });
Expand Down
2 changes: 1 addition & 1 deletion src/libc_installation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub const LibCInstallation = struct {
var it = std.mem.tokenizeScalar(u8, contents, '\n');
while (it.next()) |line| {
if (line.len == 0 or line[0] == '#') continue;
var line_it = std.mem.split(u8, line, "=");
var line_it = std.mem.splitScalar(u8, line, '=');
const name = line_it.first();
const value = line_it.rest();
inline for (fields, 0..) |field, i| {
Expand Down
2 changes: 1 addition & 1 deletion src/link/MachO/Dylib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub const Id = struct {
var out: u32 = 0;
var values: [3][]const u8 = undefined;

var split = mem.split(u8, string, ".");
var split = mem.splitScalar(u8, string, '.');
var count: u4 = 0;
while (split.next()) |value| {
if (count > 2) {
Expand Down
Loading

0 comments on commit 982f5af

Please sign in to comment.