mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
std.mem: Add countScalar
This commit is contained in:
parent
83f8441c4f
commit
98dd8856ef
@ -260,7 +260,7 @@ pub fn build(b: *std.Build) !void {
|
||||
};
|
||||
const git_describe = mem.trim(u8, git_describe_untrimmed, " \n\r");
|
||||
|
||||
switch (mem.count(u8, git_describe, "-")) {
|
||||
switch (mem.countScalar(u8, git_describe, '-')) {
|
||||
0 => {
|
||||
// Tagged release version (e.g. 0.10.0).
|
||||
if (!mem.eql(u8, git_describe, version_string)) {
|
||||
|
||||
@ -1704,6 +1704,26 @@ test count {
|
||||
try testing.expect(count(u8, "owowowu", "owowu") == 1);
|
||||
}
|
||||
|
||||
/// Returns the number of needles inside the haystack
|
||||
pub fn countScalar(comptime T: type, haystack: []const T, needle: T) usize {
|
||||
var i: usize = 0;
|
||||
var found: usize = 0;
|
||||
|
||||
while (findScalarPos(T, haystack, i, needle)) |idx| {
|
||||
i = idx + 1;
|
||||
found += 1;
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
test countScalar {
|
||||
try testing.expectEqual(0, countScalar(u8, "", 'h'));
|
||||
try testing.expectEqual(1, countScalar(u8, "h", 'h'));
|
||||
try testing.expectEqual(2, countScalar(u8, "hh", 'h'));
|
||||
try testing.expectEqual(3, countScalar(u8, " abcabc abc", 'b'));
|
||||
}
|
||||
|
||||
/// Returns true if the haystack contains expected_count or more needles
|
||||
/// needle.len must be > 0
|
||||
/// does not count overlapping needles
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user