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
#![allow(unknown_lints)]
use std::collections::HashSet;
use std::hash::Hash;
use std::hash::BuildHasher;
#[allow(needless_pass_by_value)]
pub fn insert_or_get<H, T>(set: &mut HashSet<T, H>, value: T) -> &T
where
T: Hash + Eq + Clone,
H: BuildHasher,
{
if set.contains(&value) {
set.get(&value).expect(
"insert_or_get: HashSet API is fubar, get after contains got us nothing...",
)
} else {
set.insert(value.clone());
set.get(&value).expect(
"insert_or_get: HashSet API is fubar, get after insert got us nothing...",
)
}
}
pub fn string_unescape(string: &str) -> String {
let mut result = String::with_capacity(string.len() - 2);
let mut copy = true;
for chunk in string[1..string.len() - 1].split('\\') {
if copy {
result.push_str(chunk);
copy = false;
} else if chunk.is_empty() {
result.push('\\');
copy = true;
} else {
match &chunk[0..1] {
"n" => result.push('\n'),
"r" => result.push('\r'),
"t" => result.push('\t'),
char => result.push_str(char),
}
result.push_str(&chunk[1..])
}
}
result
}
pub fn string_escape(s: &str) -> String {
let mut result = String::with_capacity(s.len()+2);
result.push('"');
for c in s.chars() {
match c {
'\t' => result.push_str(r"\t"),
'\r' => result.push_str(r"\r"),
'\n' => result.push_str(r"\n"),
'\\' => result.push_str(r"\\"),
'"' => result.push_str(r#"\""#),
'\x20' ... '\x7e' => result.push(c),
_ => {
for c in c.escape_unicode() {
result.push(c);
}
},
}
}
result.push('"');
result
}
pub unsafe fn extend_lifetime<'a, 'b: 'a, T: ?Sized>(t: &'a T) -> &'b T {
&*(t as *const T)
}
pub unsafe fn extend_lifetime_mut<'a, 'b: 'a, T: ?Sized>(t: &'a mut T) -> &'b mut T {
&mut *(t as *mut T)
}