| ... | @@ -1611,6 +1611,8 @@ test count { | ... | @@ -1611,6 +1611,8 @@ test count { |
| 1611 | /// Returns true if the haystack contains expected_count or more needles | 1611 | /// Returns true if the haystack contains expected_count or more needles |
| 1612 | /// needle.len must be > 0 | 1612 | /// needle.len must be > 0 |
| 1613 | /// does not count overlapping needles | 1613 | /// does not count overlapping needles |
| | 1614 | // |
| | 1615 | /// See also: `containsAtLeastScalar` |
| 1614 | pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: usize, needle: []const T) bool { | 1616 | pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: usize, needle: []const T) bool { |
| 1615 | assert(needle.len > 0); | 1617 | assert(needle.len > 0); |
| 1616 | if (expected_count == 0) return true; | 1618 | if (expected_count == 0) return true; |
| ... | @@ -1642,6 +1644,34 @@ test containsAtLeast { | ... | @@ -1642,6 +1644,34 @@ test containsAtLeast { |
| 1642 | try testing.expect(!containsAtLeast(u8, " radar radar ", 3, "radar")); | 1644 | try testing.expect(!containsAtLeast(u8, " radar radar ", 3, "radar")); |
| 1643 | } | 1645 | } |
| 1644 | | 1646 | |
| | 1647 | /// Returns true if the haystack contains expected_count or more needles |
| | 1648 | // |
| | 1649 | /// See also: `containsAtLeast` |
| | 1650 | pub fn containsAtLeastScalar(comptime T: type, haystack: []const T, expected_count: usize, needle: T) bool { |
| | 1651 | if (expected_count == 0) return true; |
| | 1652 | |
| | 1653 | var found: usize = 0; |
| | 1654 | |
| | 1655 | for (haystack) |item| { |
| | 1656 | if (item == needle) { |
| | 1657 | found += 1; |
| | 1658 | if (found == expected_count) return true; |
| | 1659 | } |
| | 1660 | } |
| | 1661 | |
| | 1662 | return false; |
| | 1663 | } |
| | 1664 | |
| | 1665 | test containsAtLeastScalar { |
| | 1666 | try testing.expect(containsAtLeastScalar(u8, "aa", 0, 'a')); |
| | 1667 | try testing.expect(containsAtLeastScalar(u8, "aa", 1, 'a')); |
| | 1668 | try testing.expect(containsAtLeastScalar(u8, "aa", 2, 'a')); |
| | 1669 | try testing.expect(!containsAtLeastScalar(u8, "aa", 3, 'a')); |
| | 1670 | |
| | 1671 | try testing.expect(containsAtLeastScalar(u8, "adadda", 3, 'd')); |
| | 1672 | try testing.expect(!containsAtLeastScalar(u8, "adadda", 4, 'd')); |
| | 1673 | } |
| | 1674 | |
| 1645 | /// Reads an integer from memory with size equal to bytes.len. | 1675 | /// Reads an integer from memory with size equal to bytes.len. |
| 1646 | /// T specifies the return type, which must be large enough to store | 1676 | /// T specifies the return type, which must be large enough to store |
| 1647 | /// the result. | 1677 | /// the result. |