diff options
| author | LmanTW <82919705+LmanTW@users.noreply.github.com> | 2025-02-15 10:40:55 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-15 03:40:55 +0100 |
| commit | 13ad984b1f403c240ee677c32c2b43980d098be3 (patch) | |
| tree | 3639c0ebb8073cfde079426a614abd64f831d418 /lib/std | |
| parent | 8a3aebaee0a68d037a6f311bc5c1b426e8e1884c (diff) | |
| download | zig-13ad984b1f403c240ee677c32c2b43980d098be3.tar.gz zig-13ad984b1f403c240ee677c32c2b43980d098be3.zip | |
std: add containsAtLeastScalar to mem (#22826)
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/mem.zig | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 78cbe77c65..15739869bf 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1611,6 +1611,8 @@ test count { /// Returns true if the haystack contains expected_count or more needles /// needle.len must be > 0 /// does not count overlapping needles +// +/// See also: `containsAtLeastScalar` pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: usize, needle: []const T) bool { assert(needle.len > 0); if (expected_count == 0) return true; @@ -1642,6 +1644,34 @@ test containsAtLeast { try testing.expect(!containsAtLeast(u8, " radar radar ", 3, "radar")); } +/// Returns true if the haystack contains expected_count or more needles +// +/// See also: `containsAtLeast` +pub fn containsAtLeastScalar(comptime T: type, haystack: []const T, expected_count: usize, needle: T) bool { + if (expected_count == 0) return true; + + var found: usize = 0; + + for (haystack) |item| { + if (item == needle) { + found += 1; + if (found == expected_count) return true; + } + } + + return false; +} + +test containsAtLeastScalar { + try testing.expect(containsAtLeastScalar(u8, "aa", 0, 'a')); + try testing.expect(containsAtLeastScalar(u8, "aa", 1, 'a')); + try testing.expect(containsAtLeastScalar(u8, "aa", 2, 'a')); + try testing.expect(!containsAtLeastScalar(u8, "aa", 3, 'a')); + + try testing.expect(containsAtLeastScalar(u8, "adadda", 3, 'd')); + try testing.expect(!containsAtLeastScalar(u8, "adadda", 4, 'd')); +} + /// Reads an integer from memory with size equal to bytes.len. /// T specifies the return type, which must be large enough to store /// the result. |
