diff options
author | Andreas Grois <andi@grois.info> | 2022-10-22 20:15:27 +0200 |
---|---|---|
committer | Andreas Grois <andi@grois.info> | 2022-10-22 20:15:27 +0200 |
commit | e17a38b1c44215911c1a84d94413e7f486edcaa3 (patch) | |
tree | 1bbc2b8e8edee55329b57da9c3de2f74bf71489a | |
parent | dcbb58222b4428a92be9e75fc1455dfdebb7ab45 (diff) |
Pre-Allocate resulting password.
It's not perfect, but a much better guess than previously.
-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 a96698d..390836a 100644 --- a/src/passwordmaker/mod.rs +++ b/src/passwordmaker/mod.rs @@ -186,13 +186,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 } enum GetGraphemesIteratorInner { |