| 1 | use crate::fold; |
| 2 | use alloc::string::String; |
| 3 | use alloc::vec::Vec; |
| 4 | use unicode_segmentation::UnicodeSegmentation; |
| 5 | |
| 6 | /// Formats the input string as a name. |
| 7 | /// |
| 8 | /// # Examples |
| 9 | /// ``` |
| 10 | /// let t = "(Even),Olsson&Rogstadkjærnet?"; |
| 11 | /// assert_eq!(reword::name(t), "Even Olsson Rogstadkjærnet"); |
| 12 | /// ``` |
| 13 | pub fn name<T: AsRef<str>>(t: T) -> String { |
| 14 | t.as_ref() |
| 15 | .unicode_words() |
| 16 | .fold(String::new(), |acc, w| fold(acc, w, ' ')) |
| 17 | } |
| 18 | |
| 19 | /// Formats the input string as a name and limits the length of the name. |
| 20 | /// |
| 21 | /// # Examples |
| 22 | /// ``` |
| 23 | /// let t = "(Even),Olsson&Rogstadkjærnet?"; |
| 24 | /// assert_eq!(reword::name_with_limit(t, 4), "EOR"); |
| 25 | /// assert_eq!(reword::name_with_limit(t, 25), "Even O Rogstadkjærnet"); |
| 26 | /// ``` |
| 27 | pub fn name_with_limit<T: AsRef<str>>(t: T, limit: usize) -> String { |
| 28 | let mut name: Vec<&str> = t.as_ref().unicode_words().collect(); |
| 29 | if name.is_empty() { |
| 30 | return String::new(); |
| 31 | } |
| 32 | |
| 33 | let len = name.len(); |
| 34 | let mut n = Vec::with_capacity(len); |
| 35 | let mut sum = 0; |
| 36 | for &w in &name { |
| 37 | let c = w.graphemes(true).count(); |
| 38 | sum += c; |
| 39 | n.push(c); |
| 40 | } |
| 41 | |
| 42 | let spaces = len - 1; |
| 43 | let mut count = sum + spaces; |
| 44 | let mut zip = name.iter_mut().zip(n); |
| 45 | |
| 46 | // Pop off the first name, so we can check that last. |
| 47 | let head = zip.next(); |
| 48 | |
| 49 | // Checks if the words needs to be shortened, starting with the first middle name. |
| 50 | for (w, c) in zip { |
| 51 | if count <= limit { |
| 52 | break; |
| 53 | } |
| 54 | count -= c - 1; |
| 55 | *w = w.graphemes(true).next().unwrap(); |
| 56 | } |
| 57 | |
| 58 | // Checks if the first name also needs to be shortened. |
| 59 | if let Some((w, c)) = head { |
| 60 | if count > limit { |
| 61 | count -= c - 1; |
| 62 | *w = w.graphemes(true).next().unwrap(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if count <= limit { |
| 67 | name.join(" ") |
| 68 | } else if (count - spaces) <= limit { |
| 69 | name.concat() |
| 70 | } else { |
| 71 | name[..limit].concat() |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// Creates a username from the provided string. |
| 76 | /// |
| 77 | /// A username can only consist of alphanumeric characters. |
| 78 | /// |
| 79 | /// # Examples |
| 80 | /// ``` |
| 81 | /// assert_eq!(reword::username("Even Olsson Rogstadkjærnet"), "evenolssonrogstadkjærnet"); |
| 82 | /// ``` |
| 83 | pub fn username<T: AsRef<str>>(t: T) -> String { |
| 84 | name(t) |
| 85 | .chars() |
| 86 | .filter(|c| c.is_alphanumeric()) |
| 87 | .flat_map(|c| c.to_lowercase()) |
| 88 | .collect() |
| 89 | } |
| 90 | |
| 91 | /// Creates a username from the provided string and limit. |
| 92 | /// |
| 93 | /// A username can only consist of alphanumeric characters. |
| 94 | /// |
| 95 | /// # Examples |
| 96 | /// ``` |
| 97 | /// assert_eq!(reword::username_with_limit("Even Olsson Rogstadkjærnet", 25), "evenorogstadkjærnet"); |
| 98 | /// ``` |
| 99 | pub fn username_with_limit<T: AsRef<str>>(t: T, limit: usize) -> String { |
| 100 | name_with_limit(t, limit) |
| 101 | .chars() |
| 102 | .filter(|c| c.is_alphanumeric()) |
| 103 | .flat_map(|c| c.to_lowercase()) |
| 104 | .collect() |
| 105 | } |