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
use crate::parse::{parse_decimal, parse_id};
use crate::{Captures, CompileError, Error, ParseError, Regex};
use std::borrow::Cow;
use std::io;
use std::mem;
#[derive(Debug)]
pub struct Expander {
sub_char: char,
open: &'static str,
close: &'static str,
allow_undelimited_name: bool,
}
impl Default for Expander {
fn default() -> Self {
Expander {
sub_char: '$',
open: "{",
close: "}",
allow_undelimited_name: true,
}
}
}
impl Expander {
pub fn python() -> Expander {
Expander {
sub_char: '\\',
open: "g<",
close: ">",
allow_undelimited_name: false,
}
}
pub fn check(&self, template: &str, regex: &Regex) -> crate::Result<()> {
let on_group_num = |num| {
if num == 0 {
Ok(())
} else if !regex.named_groups.is_empty() {
Err(Error::CompileError(CompileError::NamedBackrefOnly))
} else if num < regex.captures_len() {
Ok(())
} else {
Err(Error::CompileError(CompileError::InvalidBackref))
}
};
self.exec(template, |step| match step {
Step::Char(_) => Ok(()),
Step::GroupName(name) => {
if regex.named_groups.contains_key(name) {
Ok(())
} else if let Ok(num) = name.parse() {
on_group_num(num)
} else {
Err(Error::CompileError(CompileError::InvalidBackref))
}
}
Step::GroupNum(num) => on_group_num(num),
Step::Error => Err(Error::ParseError(
0,
ParseError::GeneralParseError(
"parse error in template while expanding".to_string(),
),
)),
})
}
pub fn escape<'a>(&self, text: &'a str) -> Cow<'a, str> {
if text.contains(self.sub_char) {
let mut quoted = String::with_capacity(self.sub_char.len_utf8() * 2);
quoted.push(self.sub_char);
quoted.push(self.sub_char);
Cow::Owned(text.replace(self.sub_char, "ed))
} else {
Cow::Borrowed(text)
}
}
#[doc(hidden)]
#[deprecated(since = "0.4.0", note = "Use `escape` instead.")]
pub fn quote<'a>(&self, text: &'a str) -> Cow<'a, str> {
self.escape(text)
}
pub fn expansion(&self, template: &str, captures: &Captures<'_>) -> String {
let mut cursor = io::Cursor::new(Vec::with_capacity(template.len()));
self.write_expansion(&mut cursor, template, captures)
.expect("expansion succeeded");
String::from_utf8(cursor.into_inner()).expect("expansion is UTF-8")
}
pub fn append_expansion(&self, dst: &mut String, template: &str, captures: &Captures<'_>) {
let pos = dst.len();
let mut cursor = io::Cursor::new(mem::replace(dst, String::new()).into_bytes());
cursor.set_position(pos as u64);
self.write_expansion(&mut cursor, template, captures)
.expect("expansion succeeded");
*dst = String::from_utf8(cursor.into_inner()).expect("expansion is UTF-8");
}
pub fn write_expansion(
&self,
mut dst: impl io::Write,
template: &str,
captures: &Captures<'_>,
) -> io::Result<()> {
self.exec(template, |step| match step {
Step::Char(c) => write!(dst, "{}", c),
Step::GroupName(name) => {
if let Some(m) = captures.name(name) {
write!(dst, "{}", m.as_str())
} else if let Some(m) = name.parse().ok().and_then(|num| captures.get(num)) {
write!(dst, "{}", m.as_str())
} else {
Ok(())
}
}
Step::GroupNum(num) => {
if let Some(m) = captures.get(num) {
write!(dst, "{}", m.as_str())
} else {
Ok(())
}
}
Step::Error => Ok(()),
})
}
fn exec<'t, E>(
&self,
template: &'t str,
mut f: impl FnMut(Step<'t>) -> Result<(), E>,
) -> Result<(), E> {
debug_assert!(!self.open.is_empty());
debug_assert!(!self.close.is_empty());
let mut iter = template.chars();
while let Some(c) = iter.next() {
if c == self.sub_char {
let tail = iter.as_str();
let skip = if tail.starts_with(self.sub_char) {
f(Step::Char(self.sub_char))?;
1
} else if let Some((id, skip)) =
parse_id(tail, self.open, self.close).or_else(|| {
if self.allow_undelimited_name {
parse_id(tail, "", "")
} else {
None
}
})
{
f(Step::GroupName(id))?;
skip
} else if let Some((skip, num)) = parse_decimal(tail, 0) {
f(Step::GroupNum(num))?;
skip
} else {
f(Step::Error)?;
f(Step::Char(self.sub_char))?;
0
};
iter = iter.as_str()[skip..].chars();
} else {
f(Step::Char(c))?;
}
}
Ok(())
}
}
enum Step<'a> {
Char(char),
GroupName(&'a str),
GroupNum(usize),
Error,
}