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
macro_rules! first_opt
{
($O: expr , $P: pat => $B: expr ) =>
(
for o in $O.iter()
{
match *o
{
$P =>
{
$B
break;
},
_ => ()
};
}
)
}
macro_rules! first_opt_default
{
($O: expr , $P: pat => $B: expr , _ => $E: expr ) =>
(
{
let mut found = false;
for o in $O.iter()
{
match *o
{
$P =>
{
found = true;
$B
break;
},
_ => ()
};
}
if !found
{
$E
}
}
)
}
pub(crate) trait OneWayOwned
{
type Output;
fn to_one_way_owned(&self) -> Self::Output;
}
impl<'l, T: OneWayOwned> OneWayOwned for &'l [T]
{
type Output = Vec<<T as OneWayOwned>::Output>;
fn to_one_way_owned(&self) -> Self::Output
{
self.iter().map(|v| v.to_one_way_owned()).collect()
}
}
pub(crate) fn escape(s: &str) -> String
{
let mut res = String::with_capacity(s.len());
for c in s.chars()
{
match c
{
'\\' => res.push_str(r"\\"),
'\n' => res.push_str(r"\n"),
'\t' => res.push_str(r"\t"),
'"' => res.push_str(r#"\""#),
c @ _ => res.push(c),
}
}
res
}
#[test]
fn escape_test()
{
assert_eq!(r"\\", escape(r"\"));
assert_eq!(r"\\\\", escape(r"\\"));
assert_eq!(r#"\\\""#, escape(r#"\""#));
assert_eq!(r#"\"\""#, escape(r#""""#));
assert_eq!(r"\n", escape("\n"));
}