diff options
author | Andreas Grois <andi@grois.info> | 2022-10-22 20:19:16 +0200 |
---|---|---|
committer | Andreas Grois <andi@grois.info> | 2022-10-22 20:19:16 +0200 |
commit | f9938b7e5afedf5bd09095210c4a640b10c72687 (patch) | |
tree | f742359f85919265efd88b9524594526d8e7e792 | |
parent | ea0f7ae2b1650aeeaf0c64a70b256cd57fec214e (diff) | |
parent | e17a38b1c44215911c1a84d94413e7f486edcaa3 (diff) |
Merge branch 'main' into feature/heap-allocation-free-base-conversion
-rw-r--r-- | src/passwordmaker/mod.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/passwordmaker/mod.rs b/src/passwordmaker/mod.rs index 5e51ee9..61a0a95 100644 --- a/src/passwordmaker/mod.rs +++ b/src/passwordmaker/mod.rs @@ -187,13 +187,18 @@ impl<'a> PasswordAssemblyParameters<'a> { } fn combine_prefix_password_suffix<'a, T : Iterator<Item=Grapheme<'a>>>(password: T, assembly_settings : &PasswordAssemblyParameters<'a>) -> String { - Grapheme::iter_from_str(assembly_settings.prefix) + //Rust's collect only uses the lower hint for pre-allocation. UnicodeSegmentation is giving correct hints, + //meaning that the lower bound is 1 (or 0 for empty strings). + //We know however, that assembly_settings.password_length is a much better lower bound. Still too low for + //passwords that contain characters that take more than 1 byte though. Still, this value should reduce the number of needed re-allocations drastically. + let mut result = String::with_capacity(assembly_settings.password_length); + result.extend(Grapheme::iter_from_str(assembly_settings.prefix) .chain(password) .take(assembly_settings.password_length.saturating_sub(assembly_settings.suffix_length)) .chain(Grapheme::iter_from_str(assembly_settings.suffix)) .take(assembly_settings.password_length)//cut end if suffix_length is larger than password_length... - .map(|g| g.get()) - .collect() + .map(|g| g.get())); + result } fn is_zero(i : &usize) -> bool { |