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
use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
#[derive(Debug, Default)]
pub struct UniqueNameGenerator {
names: HashMap<u64, usize>,
}
impl UniqueNameGenerator {
pub fn add_variable<'a>(&mut self, name: &'a str) -> Cow<'a, str> {
let mut stem = stem(name);
let hash = calculate_hash(&stem);
let n = self.names.entry(hash).or_insert(0);
*n += 1;
if *n >= 2 {
stem = Cow::Owned(stem.into_owned() + &n.to_string());
}
stem
}
}
fn stem(name: &str) -> Cow<str> {
if name.contains(|c: char| !c.is_ascii_alphabetic()) || name.is_empty() {
let mut owned = name.replace(|c: char| !c.is_ascii_alphabetic(), "");
if owned.is_empty() {
owned.push('v');
}
Cow::Owned(owned)
} else {
Cow::Borrowed(name)
}
}
fn calculate_hash(t: &str) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
pub(crate) fn buf_contains(haystack: &[u8], needle: &str) -> bool {
let needle = needle.as_bytes();
haystack
.windows(needle.len())
.any(|window| window == needle)
}