#![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)
}