toolchest 0.1.0

Essential utility collection for Rust - the missing complement to itertools
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Additional string utilities: slugify, pluralize, singularize, levenshtein

/// Create a URL-friendly slug from a string
pub fn slugify(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    let mut last_dash = false;
    for ch in input.chars() {
        if ch.is_ascii_alphanumeric() {
            out.push(ch.to_ascii_lowercase());
            last_dash = false;
        } else if (ch.is_whitespace() || ch == '-' || ch == '_') && !last_dash && !out.is_empty() {
            out.push('-');
            last_dash = true;
        }
    }
    if out.ends_with('-') {
        out.pop();
    }
    out
}

/// Simple template interpolation: replaces {{key}} using provider
pub fn template<F>(input: &str, mut provider: F) -> String
where
    F: FnMut(&str) -> Option<String>,
{
    let mut out = String::with_capacity(input.len());
    let bytes = input.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if i + 3 < bytes.len() && &bytes[i..i + 2] == b"{{" {
            if let Some(end) = input[i + 2..].find("}}").map(|e| i + 2 + e) {
                let key = &input[i + 2..end].trim();
                if let Some(val) = provider(key) {
                    out.push_str(&val);
                } else {
                    out.push_str(&input[i..end + 2]);
                }
                i = end + 2;
                continue;
            }
        }
        out.push(bytes[i] as char);
        i += 1;
    }
    out
}

/// Case-insensitive contains
pub fn contains_ci(haystack: &str, needle: &str) -> bool {
    haystack.to_lowercase().contains(&needle.to_lowercase())
}

/// Case-insensitive starts_with
pub fn starts_with_ci(haystack: &str, prefix: &str) -> bool {
    haystack.to_lowercase().starts_with(&prefix.to_lowercase())
}

/// Case-insensitive ends_with
pub fn ends_with_ci(haystack: &str, suffix: &str) -> bool {
    haystack.to_lowercase().ends_with(&suffix.to_lowercase())
}

/// Strip prefix if present
pub fn strip_prefix<'a>(s: &'a str, prefix: &str) -> &'a str {
    s.strip_prefix(prefix).unwrap_or(s)
}

/// Strip suffix if present
pub fn strip_suffix<'a>(s: &'a str, suffix: &str) -> &'a str {
    s.strip_suffix(suffix).unwrap_or(s)
}

/// Ensure string has the prefix, adding if missing
pub fn ensure_prefix(s: &str, prefix: &str) -> String {
    if s.starts_with(prefix) {
        s.to_string()
    } else {
        format!("{prefix}{s}")
    }
}

/// Ensure string has the suffix, adding if missing
pub fn ensure_suffix(s: &str, suffix: &str) -> String {
    if s.ends_with(suffix) {
        s.to_string()
    } else {
        format!("{s}{suffix}")
    }
}

/// Very simple pluralize for common English nouns
pub fn pluralize(word: &str) -> String {
    if word.ends_with("y") && !matches!(word.chars().nth_back(1), Some('a' | 'e' | 'i' | 'o' | 'u'))
    {
        let mut s = word.to_string();
        s.pop();
        s.push_str("ies");
        s
    } else if word.ends_with('s')
        || word.ends_with("x")
        || word.ends_with("ch")
        || word.ends_with("sh")
    {
        format!("{word}es")
    } else {
        format!("{word}s")
    }
}

/// Very simple singularize matching the above pluralize
pub fn singularize(word: &str) -> String {
    if let Some(base) = word.strip_suffix("ies") {
        format!("{base}y")
    } else if let Some(base) = word.strip_suffix("es") {
        base.to_string()
    } else if let Some(base) = word.strip_suffix('s') {
        base.to_string()
    } else {
        word.to_string()
    }
}

/// Levenshtein distance between two strings
pub fn levenshtein_distance(a: &str, b: &str) -> usize {
    let (a_len, b_len) = (a.chars().count(), b.chars().count());
    if a_len == 0 {
        return b_len;
    }
    if b_len == 0 {
        return a_len;
    }
    let mut prev: Vec<usize> = (0..=b_len).collect();
    let mut curr = vec![0usize; b_len + 1];
    let b_chars: Vec<char> = b.chars().collect();
    for (i, ca) in a.chars().enumerate() {
        curr[0] = i + 1;
        for (j, &cb) in b_chars.iter().enumerate() {
            let cost = if ca == cb { 0 } else { 1 };
            curr[j + 1] = (prev[j + 1] + 1).min(curr[j] + 1).min(prev[j] + cost);
        }
        std::mem::swap(&mut prev, &mut curr);
    }
    prev[b_len]
}

/// Damerau-Levenshtein distance (allows transposition)
pub fn damerau_levenshtein(a: &str, b: &str) -> usize {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    let (m, n) = (a_chars.len(), b_chars.len());
    if m == 0 {
        return n;
    }
    if n == 0 {
        return m;
    }
    let mut d = vec![vec![0usize; n + 1]; m + 1];
    for (i, row) in d.iter_mut().enumerate().take(m + 1) {
        row[0] = i;
    }
    if let Some(first_row) = d.get_mut(0) {
        for (j, cell) in first_row.iter_mut().enumerate().take(n + 1) {
            *cell = j;
        }
    }
    for i in 1..=m {
        for j in 1..=n {
            let cost = if a_chars[i - 1] == b_chars[j - 1] {
                0
            } else {
                1
            };
            d[i][j] = (d[i - 1][j] + 1)
                .min(d[i][j - 1] + 1)
                .min(d[i - 1][j - 1] + cost);
            if i > 1
                && j > 1
                && a_chars[i - 1] == b_chars[j - 2]
                && a_chars[i - 2] == b_chars[j - 1]
            {
                d[i][j] = d[i][j].min(d[i - 2][j - 2] + 1);
            }
        }
    }
    d[m][n]
}

/// Reverse characters of a string
pub fn reverse(s: &str) -> String {
    s.chars().rev().collect()
}

/// Check if string is a palindrome (ignores case and non-alphanumerics)
pub fn is_palindrome(s: &str) -> bool {
    let filtered: String = s
        .chars()
        .filter(|c| c.is_alphanumeric())
        .map(|c| c.to_ascii_lowercase())
        .collect();
    filtered.chars().eq(filtered.chars().rev())
}

/// Count non-overlapping occurrences of a substring
pub fn count_occurrences(haystack: &str, needle: &str) -> usize {
    if needle.is_empty() {
        return 0;
    }
    haystack.match_indices(needle).count()
}

/// Find all start indices of a substring
pub fn find_all_indices(haystack: &str, needle: &str) -> Vec<usize> {
    if needle.is_empty() {
        return vec![];
    }
    haystack.match_indices(needle).map(|(i, _)| i).collect()
}

/// Word wrap text at width, breaking on whitespace
pub fn wrap(text: &str, width: usize) -> String {
    if width == 0 {
        return text.to_string();
    }
    let mut out = String::new();
    let mut line_len = 0usize;
    for word in text.split_whitespace() {
        let wlen = word.len();
        if line_len == 0 {
            out.push_str(word);
            line_len = wlen;
        } else if line_len + 1 + wlen <= width {
            out.push(' ');
            out.push_str(word);
            line_len += 1 + wlen;
        } else {
            out.push('\n');
            out.push_str(word);
            line_len = wlen;
        }
    }
    out
}

/// Add indentation with given prefix to each non-empty line
pub fn indent(text: &str, prefix: &str) -> String {
    text.lines()
        .map(|l| {
            if l.is_empty() {
                "".to_string()
            } else {
                format!("{prefix}{l}")
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Remove up to n leading spaces or a given prefix from each line
pub fn dedent(text: &str, n: usize) -> String {
    text.lines()
        .map(|l| {
            let mut count = 0usize;
            let mut idx = 0usize;
            for ch in l.chars() {
                if ch == ' ' && count < n {
                    count += 1;
                    idx += 1;
                } else {
                    break;
                }
            }
            &l[idx..]
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Check if string looks like an email
pub fn is_email(s: &str) -> bool {
    s.contains('@')
        && s.split('@').count() == 2
        && s.split('@').nth(1).is_some_and(|d| d.contains('.'))
}

/// Check if string looks like a URL (very basic)
pub fn is_url(s: &str) -> bool {
    s.starts_with("http://") || s.starts_with("https://")
}

/// Check if string is UUID v4 format
pub fn is_uuid(s: &str) -> bool {
    let parts: Vec<&str> = s.split('-').collect();
    if parts.len() != 5 {
        return false;
    }
    let lens = [8, 4, 4, 4, 12];
    if !parts
        .iter()
        .zip(lens.iter())
        .all(|(p, &len)| p.len() == len && p.chars().all(|c| c.is_ascii_hexdigit()))
    {
        return false;
    }
    parts[2].starts_with('4')
}

/// Check if string is numeric (digits only)
pub fn is_numeric(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_digit())
}

/// Repeat string n times
pub fn repeat(s: &str, n: usize) -> String {
    s.repeat(n)
}

/// Replace all occurrences with callback providing replacement per match
pub fn replace_all<F>(s: &str, needle: &str, mut f: F) -> String
where
    F: FnMut(&str) -> String,
{
    if needle.is_empty() {
        return s.to_string();
    }
    let mut out = String::with_capacity(s.len());
    let mut last = 0usize;
    for (idx, m) in s.match_indices(needle) {
        out.push_str(&s[last..idx]);
        out.push_str(&f(m));
        last = idx + needle.len();
    }
    out.push_str(&s[last..]);
    out
}

/// Smart word splitting: splits by whitespace, underscores, hyphens, and camelCase boundaries
pub fn split_words(s: &str) -> Vec<String> {
    let mut words = Vec::new();
    let mut current = String::new();
    for ch in s.chars() {
        if ch.is_whitespace() || ch == '_' || ch == '-' {
            if !current.is_empty() {
                words.push(current.clone());
                current.clear();
            }
            continue;
        }
        if ch.is_ascii_uppercase()
            && !current.is_empty()
            && current
                .chars()
                .last()
                .is_some_and(|c| c.is_ascii_lowercase())
        {
            words.push(current.clone());
            current.clear();
        }
        current.push(ch);
    }
    if !current.is_empty() {
        words.push(current);
    }
    words
}

/// Longest common prefix of two strings
pub fn longest_common_prefix(a: &str, b: &str) -> String {
    let mut out = String::new();
    for (ca, cb) in a.chars().zip(b.chars()) {
        if ca == cb {
            out.push(ca);
        } else {
            break;
        }
    }
    out
}

/// Longest common suffix of two strings
pub fn longest_common_suffix(a: &str, b: &str) -> String {
    let mut out = String::new();
    for (ca, cb) in a.chars().rev().zip(b.chars().rev()) {
        if ca == cb {
            out.insert(0, ca);
        } else {
            break;
        }
    }
    out
}

/// Shell-escape a string by wrapping in single quotes and escaping inner single quotes
pub fn escape_shell(s: &str) -> String {
    format!("'{}'", s.replace("'", "'\\''"))
}

/// Random ASCII string from given charset
pub fn random_string(len: usize) -> String {
    let charset = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    let mut state = std::time::Instant::now().elapsed().as_nanos();
    let mut next_u64 = || {
        state = state.wrapping_mul(1664525).wrapping_add(1013904223);
        (state >> 32) as u64
    };
    let mut out = String::with_capacity(len);
    for _ in 0..len {
        let idx = (next_u64() as usize) % charset.len();
        out.push(charset[idx] as char);
    }
    out
}

/// Mask part of a string, leaving prefix and suffix visible
pub fn mask(s: &str, prefix: usize, suffix: usize, mask_char: char) -> String {
    if s.len() <= prefix + suffix {
        return s.to_string();
    }
    let mut out = String::new();
    out.push_str(&s[..prefix]);
    out.push_str(&mask_char.to_string().repeat(s.len() - prefix - suffix));
    out.push_str(&s[s.len() - suffix..]);
    out
}

/// Truncate the middle with ellipsis if longer than max_len
pub fn ellipsis_middle(s: &str, max_len: usize) -> String {
    if s.len() <= max_len || max_len < 3 {
        return s.to_string();
    }
    let side = (max_len - 3) / 2;
    format!("{}...{}", &s[..side], &s[s.len() - side..])
}

/// Collapse consecutive whitespace to single spaces and trim ends
pub fn normalize_whitespace(s: &str) -> String {
    s.split_whitespace().collect::<Vec<_>>().join(" ")
}