From 242311585b486d192bdf2bcab639b49c1dc6fba8 Mon Sep 17 00:00:00 2001 From: Dhruvan Bhalara <53393418+dhruvanbhalara@users.noreply.github.com> Date: Thu, 4 Jun 2026 10:12:13 +0530 Subject: [PATCH 1/2] 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. --- lib/src/utils/password_string_extensions.dart | 5 +++-- .../password_string_extensions_test.dart | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/src/utils/password_string_extensions.dart b/lib/src/utils/password_string_extensions.dart index bd95608..e3075be 100644 --- a/lib/src/utils/password_string_extensions.dart +++ b/lib/src/utils/password_string_extensions.dart @@ -29,8 +29,9 @@ extension PasswordStringX on String { /// Uses a [Set] for O(1) lookup per character instead of O(m) string search. bool containsAnyOf(String charSet) { if (charSet.isEmpty) return false; - for (var i = 0; i < length; i++) { - if (charSet.contains(this[i])) return true; + final set = charSet.runes.toSet(); + for (final rune in runes) { + if (set.contains(rune)) return true; } return false; } diff --git a/test/utils/password_string_extensions_test.dart b/test/utils/password_string_extensions_test.dart index b50858a..d1f3ace 100644 --- a/test/utils/password_string_extensions_test.dart +++ b/test/utils/password_string_extensions_test.dart @@ -45,5 +45,24 @@ void main() { expect(''.hasUnicode, isFalse); expect(('a' * 1000 + '\u03C0').hasUnicode, isTrue); }); + + group('containsAnyOf', () { + test('returns false on empty charset pool', () { + expect('abc'.containsAnyOf(''), isFalse); + }); + + test('returns false when no characters match', () { + expect('abc'.containsAnyOf('xyz'), isFalse); + }); + + test('returns true when character matches', () { + expect('abc'.containsAnyOf('cxz'), isTrue); + }); + + test('handles unicode characters correctly', () { + expect('abc\u{1F60A}'.containsAnyOf('\u{1F60A}'), isTrue); + expect('abc\u{1F60A}'.containsAnyOf('\u{1F60B}'), isFalse); + }); + }); }); } From 104e3aa54215b42c17fba69b74c750ca4e6a3fb0 Mon Sep 17 00:00:00 2001 From: Dhruvan Bhalara <53393418+dhruvanbhalara@users.noreply.github.com> Date: Thu, 4 Jun 2026 10:17:21 +0530 Subject: [PATCH 2/2] 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. --- CHANGELOG.md | 6 ++++++ pubspec.yaml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47fef93..32d5104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.0.1 + +- **Fix**: Make character set lookup faster in `containsAnyOf`. + - Used a Set of runes instead of checking characters linearly. + - Added unit tests for empty inputs, mismatches, and Unicode characters. + ## 1.0.0 - Initial release of `password_engine`. diff --git a/pubspec.yaml b/pubspec.yaml index 7815ac4..32467ca 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: password_engine description: "A comprehensive and extensible password generation library for Dart and Flutter" -version: 1.0.0 +version: 1.0.1 homepage: https://github.com/dhruvanbhalara/password_engine repository: https://github.com/dhruvanbhalara/password_engine issue_tracker: https://github.com/dhruvanbhalara/password_engine/issues