1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! Provides functions for human readable formatting of words and sentences.
//!
//! ```
//! let s = "(Even),Olsson&Rogstadkjærnet?";
//! assert_eq!(reword::name(s), "Even Olsson Rogstadkjærnet");
//! assert_eq!(reword::name_with_limit(s, 4), "EOR");
//! ```

#![no_std]

extern crate alloc;

use alloc::{string::String, vec::Vec};
use unicode_segmentation::UnicodeSegmentation;

/// Formats the input string as a name.
///
/// # Examples
/// ```
/// let s = "(Even),Olsson&Rogstadkjærnet?";
/// assert_eq!(reword::name(s), "Even Olsson Rogstadkjærnet");
/// ```
pub fn name<T: AsRef<str>>(s: T) -> String {
    let s = s.as_ref();
    let mut name = String::with_capacity(s.len());
    let mut it = s.unicode_words();
    if let Some(w) = it.next() {
        name.push_str(w);
        for w in it {
            name.push(' ');
            name.push_str(w)
        }
    }
    name
}

/// Formats the input string as a name and limits the length of the name.
///
/// # Examples
/// ```
/// let s = "(Even),Olsson&Rogstadkjærnet?";
/// assert_eq!(reword::name_with_limit(s, 4), "EOR");
/// assert_eq!(reword::name_with_limit(s, 25), "Even O Rogstadkjærnet");
/// ```
pub fn name_with_limit<T: AsRef<str>>(s: T, limit: usize) -> String {
    let s = s.as_ref();
    let mut name: Vec<&str> = s.unicode_words().collect();
    if name.is_empty() {
        return String::new();
    }

    let len = name.len();
    let mut n = Vec::with_capacity(len);
    let mut sum = 0;
    for &w in &name {
        let c = w.graphemes(true).count();
        sum += c;
        n.push(c);
    }

    let spaces = len - 1;
    let mut count = sum + spaces;
    let mut zip = name.iter_mut().zip(n);

    // Pop off the first name, so we can check that last.
    let head = zip.next();

    // Checks if the words needs to be shortened, starting with the first middle name.
    for (w, c) in zip {
        if count <= limit {
            break;
        }
        count -= c - 1;
        *w = w.graphemes(true).next().unwrap();
    }

    // Checks if the first name also needs to be shortened.
    if let Some((w, c)) = head {
        if count > limit {
            count -= c - 1;
            *w = w.graphemes(true).next().unwrap();
        }
    }

    if count <= limit {
        name.join(" ")
    } else if (count - spaces) <= limit {
        name.concat()
    } else {
        name[..limit].concat()
    }
}

/// Creates a username from the provided string.
///
/// # Examples
/// ```
/// assert_eq!(reword::username("Even O. R."), "evenor");
/// ```
pub fn username<T: AsRef<str>>(s: T) -> String {
    let s = s.as_ref();
    let mut username = String::with_capacity(s.len());
    for w in s.unicode_words().map(str::to_lowercase) {
        username.push_str(&w)
    }
    username
}

/// Creates a username from the provided string and limit.
///
/// # Examples
/// ```
/// assert_eq!(reword::username_with_limit("Even Olsson Rogstadkjærnet", 12), "evenor");
/// ```
pub fn username_with_limit<T: AsRef<str>>(s: T, limit: usize) -> String {
    let name = name_with_limit(s, limit);
    let mut username = String::with_capacity(name.len());
    for w in name.to_lowercase().split(' ') {
        username.push_str(&w)
    }
    username
}

/// Join the list with an 'or' before the last element of the list.
///
/// # Examples
/// ```
/// assert_eq!(reword::or_join(&["a", "b"]), "a or b");
/// assert_eq!(reword::or_join(&["a", "b", "c"]), "a, b, or c");
/// ```
pub fn or_join<T: AsRef<str>>(v: &[T]) -> String {
    if v.len() < 3 {
        join(v, " or ")
    } else {
        join(v, ", or ")
    }
}

/// Join the list with an 'and' before the last element of the list.
///
/// # Examples
/// ```
/// assert_eq!(reword::and_join(&["a", "b"]), "a and b");
/// assert_eq!(reword::and_join(&["a", "b", "c"]), "a, b, and c");
/// ```
pub fn and_join<T: AsRef<str>>(v: &[T]) -> String {
    if v.len() < 3 {
        join(v, " and ")
    } else {
        join(v, ", and ")
    }
}

fn join<T: AsRef<str>>(v: &[T], sep: &str) -> String {
    let mut s = String::with_capacity(v.len() * 16);
    if let Some((first, tail)) = v.split_first() {
        s.push_str(first.as_ref());
        if let Some((last, tail)) = tail.split_last() {
            for field in tail {
                s.push_str(", ");
                s.push_str(field.as_ref());
            }
            s.push_str(sep);
            s.push_str(last.as_ref());
        }
    }
    s
}

#[cfg(test)]
mod tests {
    #[test]
    fn name() {
        let s = "(Even), Olsson&Rogstadkjærnet?";
        assert_eq!(crate::name(s), "Even Olsson Rogstadkjærnet");
        assert_eq!(crate::name_with_limit(s, 25), "Even O Rogstadkjærnet");
        assert_eq!(crate::name_with_limit(s, 12), "Even O R");
        assert_eq!(crate::name_with_limit(s, 7), "E O R");
        assert_eq!(crate::name_with_limit(s, 4), "EOR");
        assert_eq!(crate::name_with_limit(s, 2), "EO");
        assert_eq!(crate::name_with_limit(s, 1), "E");
        assert_eq!(crate::name_with_limit(s, 0), "");
    }

    #[test]
    fn username() {
        let s = "(Even), Olsson&Rogstadkjærnet?";
        assert_eq!(crate::username(s), "evenolssonrogstadkjærnet");
        assert_eq!(crate::username_with_limit(s, 25), "evenorogstadkjærnet");
        assert_eq!(crate::username_with_limit(s, 12), "evenor");
        assert_eq!(crate::username_with_limit(s, 7), "eor");
        assert_eq!(crate::username_with_limit(s, 4), "eor");
        assert_eq!(crate::username_with_limit(s, 2), "eo");
        assert_eq!(crate::username_with_limit(s, 1), "e");
        assert_eq!(crate::username_with_limit(s, 0), "");
    }

    #[test]
    fn join() {
        assert_eq!(crate::or_join::<&str>(&[]), "");
        assert_eq!(crate::or_join::<&str>(&["a"]), "a");
        assert_eq!(crate::or_join::<&str>(&["a", "b"]), "a or b");
        assert_eq!(crate::or_join::<&str>(&["a", "b", "c"]), "a, b, or c");
        assert_eq!(
            crate::or_join::<&str>(&["a", "b", "c", "d", "e"]),
            "a, b, c, d, or e"
        );
        assert_eq!(crate::and_join::<&str>(&[]), "");
        assert_eq!(crate::and_join::<&str>(&["a"]), "a");
        assert_eq!(crate::and_join::<&str>(&["a", "b"]), "a and b");
        assert_eq!(crate::and_join::<&str>(&["a", "b", "c"]), "a, b, and c");
        assert_eq!(
            crate::and_join::<&str>(&["a", "b", "c", "d", "e"]),
            "a, b, c, d, and e"
        );
    }
}