-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
std.mem: introduce cut functions; rename "index of" to "find" #25351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
We have similar /// Splits the `haystack` around the first occurrence of `needle`, returning parts before and after.
///
/// This is a Zig version of Go's `string.Cut` / Rust's `str::split_once`. Cut turns out to be a
/// surprisingly versatile primitive for ad-hoc string processing. Often `std.mem.indexOf` and
/// `std.mem.split` can be replaced with a shorter and clearer code using `cut`.
pub fn cut(haystack: []const u8, needle: []const u8) ?struct { []const u8, []const u8 } {
const index = std.mem.indexOf(u8, haystack, needle) orelse return null;
return .{ haystack[0..index], haystack[index + needle.len ..] };
}
pub fn cut_prefix(haystack: []const u8, needle: []const u8) ?[]const u8 {
if (std.mem.startsWith(u8, haystack, needle)) {
return haystack[needle.len..];
}
return null;
}
pub fn cut_suffix(haystack: []const u8, needle: []const u8) ?[]const u8 {
if (std.mem.endsWith(u8, haystack, needle)) {
return haystack[haystack.len - needle.len ..];
}
return null;
}I have a strong opinion that Specific suggestions:
|
src/main.zig
Outdated
| var it = mem.splitScalar(u8, rest, '='); | ||
| const mod_name = it.first(); | ||
| const root_src_orig = if (it.peek() != null) it.rest() else null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const mod_name, const root_src_orig = mem.cut(u8, rest, "=") orelse .{ rest, null };would be one example of cut being useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would need to be cutScalar in order to match performance characteristics
fixes a bug in how -fstructured-cfg and -fno-structured-cfg are handled.
Moving towards our function naming convention of having one word per concept and constructing function names out of concatenated concepts. In `std.mem` the concepts are: * "find" - return index of substring * "pos" - starting index parameter * "last" - search from the end * "linear" - simple for loop rather than fancy algo * "scalar" - substring is a single element
|
tagging 1.0 will be a bittersweet moment, indeed |
|
What about the order of these "concepts" in the identifier? If I saw correctly, there's now
Since "any", "none", and "scalar" are all describing the same thing, namely the kind of needle the function takes, I would assume them all to be positioned the same relative to other "concepts". This would aid both discoverability as well as ease of memorizing and editing (i.e., if I notice I actually needed I can imagine both orders as sensible, namely:
|
|
@andrewrk Pinging since I believe my comment might have been overlooked. It'd just be a pity to have to breaking-change these names again if the inconsistency is only noticed after the next release. Feel free to ignore if I've simply misunderstood the naming. :-) |
Introduce cut functions:
cut,cutPrefix,cutSuffix,cutScalar,cutLast,cutLastScalarMoving towards our function naming convention of having one word per concept and constructing function names out of concatenated concepts.
In
std.memthe concepts are:* "find" - return index of substring
* "pos" - starting index parameter
* "last" - search from the end
* "linear" - simple for loop rather than fancy algo
* "scalar" - substring is a single element