Skip to content

Commit 5a6f17a

Browse files
fix(utils): use Set for containsAnyOf character check (#20)
* fix: use Set for containsAnyOf character check Change character search in containsAnyOf to use a Set of runes. This matches the documentation's claim of O(1) character lookup and improves lookup speed. * chore: bump version to 1.0.1 and update changelog Bump library version to 1.0.1 in pubspec.yaml and document the containsAnyOf performance fix in CHANGELOG.md.
1 parent 1ece4ee commit 5a6f17a

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 1.0.1
4+
5+
- **Fix**: Make character set lookup faster in `containsAnyOf`.
6+
- Used a Set of runes instead of checking characters linearly.
7+
- Added unit tests for empty inputs, mismatches, and Unicode characters.
8+
39
## 1.0.0
410

511
- Initial release of `password_engine`.

lib/src/utils/password_string_extensions.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ extension PasswordStringX on String {
2929
/// Uses a [Set] for O(1) lookup per character instead of O(m) string search.
3030
bool containsAnyOf(String charSet) {
3131
if (charSet.isEmpty) return false;
32-
for (var i = 0; i < length; i++) {
33-
if (charSet.contains(this[i])) return true;
32+
final set = charSet.runes.toSet();
33+
for (final rune in runes) {
34+
if (set.contains(rune)) return true;
3435
}
3536
return false;
3637
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: password_engine
22
description: "A comprehensive and extensible password generation library for Dart and Flutter"
3-
version: 1.0.0
3+
version: 1.0.1
44
homepage: https://github.com/dhruvanbhalara/password_engine
55
repository: https://github.com/dhruvanbhalara/password_engine
66
issue_tracker: https://github.com/dhruvanbhalara/password_engine/issues

test/utils/password_string_extensions_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,24 @@ void main() {
4545
expect(''.hasUnicode, isFalse);
4646
expect(('a' * 1000 + '\u03C0').hasUnicode, isTrue);
4747
});
48+
49+
group('containsAnyOf', () {
50+
test('returns false on empty charset pool', () {
51+
expect('abc'.containsAnyOf(''), isFalse);
52+
});
53+
54+
test('returns false when no characters match', () {
55+
expect('abc'.containsAnyOf('xyz'), isFalse);
56+
});
57+
58+
test('returns true when character matches', () {
59+
expect('abc'.containsAnyOf('cxz'), isTrue);
60+
});
61+
62+
test('handles unicode characters correctly', () {
63+
expect('abc\u{1F60A}'.containsAnyOf('\u{1F60A}'), isTrue);
64+
expect('abc\u{1F60A}'.containsAnyOf('\u{1F60B}'), isFalse);
65+
});
66+
});
4867
});
4968
}

0 commit comments

Comments
 (0)