Skip to content

Commit 8d772eb

Browse files
committed
Inline format-args automata
This makes the code a bit easier to read and smaller. Some of it was done with this command, and later fixed by hand: ``` cargo clippy --workspace --allow-dirty --fix --benches --tests --bins -- -A clippy::all -W clippy::uninlined_format_args ```
1 parent 2695e29 commit 8d772eb

File tree

31 files changed

+139
-186
lines changed

31 files changed

+139
-186
lines changed

regex-automata/src/dfa/accel.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub(crate) fn find_fwd(
102102
2 => memchr::memchr2(bs[0], bs[1], &haystack[at..])?,
103103
3 => memchr::memchr3(bs[0], bs[1], bs[2], &haystack[at..])?,
104104
0 => panic!("cannot find with empty needles"),
105-
n => panic!("invalid needles length: {}", n),
105+
n => panic!("invalid needles length: {n}"),
106106
};
107107
Some(at + i)
108108
}
@@ -122,7 +122,7 @@ pub(crate) fn find_rev(
122122
2 => memchr::memrchr2(bs[0], bs[1], &haystack[..at]),
123123
3 => memchr::memrchr3(bs[0], bs[1], bs[2], &haystack[..at]),
124124
0 => panic!("cannot find with empty needles"),
125-
n => panic!("invalid needles length: {}", n),
125+
n => panic!("invalid needles length: {n}"),
126126
}
127127
}
128128

@@ -267,7 +267,7 @@ impl<A: AsRef<[AccelTy]>> Accels<A> {
267267
#[cfg_attr(feature = "perf-inline", inline(always))]
268268
pub fn needles(&self, i: usize) -> &[u8] {
269269
if i >= self.len() {
270-
panic!("invalid accelerator index {}", i);
270+
panic!("invalid accelerator index {i}");
271271
}
272272
let bytes = self.as_bytes();
273273
let offset = ACCEL_TY_SIZE + i * ACCEL_CAP;
@@ -313,8 +313,7 @@ impl<A: AsRef<[AccelTy]>> Accels<A> {
313313
assert_eq!(
314314
nwrite % ACCEL_TY_SIZE,
315315
0,
316-
"expected accelerator bytes written to be a multiple of {}",
317-
ACCEL_TY_SIZE,
316+
"expected accelerator bytes written to be a multiple of {ACCEL_TY_SIZE}",
318317
);
319318
if dst.len() < nwrite {
320319
return Err(SerializeError::buffer_too_small("accelerators"));

regex-automata/src/dfa/dense.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,8 +2837,7 @@ impl OwnedDFA {
28372837
}
28382838
assert!(
28392839
!matches.contains_key(&start_id),
2840-
"{:?} is both a start and a match state, which is not allowed",
2841-
start_id,
2840+
"{start_id:?} is both a start and a match state, which is not allowed",
28422841
);
28432842
is_start.insert(start_id);
28442843
}
@@ -3098,7 +3097,7 @@ impl<T: AsRef<[u32]>> fmt::Debug for DFA<T> {
30983097
} else {
30993098
self.to_index(state.id())
31003099
};
3101-
write!(f, "{:06?}: ", id)?;
3100+
write!(f, "{id:06?}: ")?;
31023101
state.fmt(f)?;
31033102
write!(f, "\n")?;
31043103
}
@@ -3114,11 +3113,11 @@ impl<T: AsRef<[u32]>> fmt::Debug for DFA<T> {
31143113
Anchored::No => writeln!(f, "START-GROUP(unanchored)")?,
31153114
Anchored::Yes => writeln!(f, "START-GROUP(anchored)")?,
31163115
Anchored::Pattern(pid) => {
3117-
writeln!(f, "START_GROUP(pattern: {:?})", pid)?
3116+
writeln!(f, "START_GROUP(pattern: {pid:?})")?
31183117
}
31193118
}
31203119
}
3121-
writeln!(f, " {:?} => {:06?}", sty, id)?;
3120+
writeln!(f, " {sty:?} => {id:06?}")?;
31223121
}
31233122
if self.pattern_len() > 1 {
31243123
writeln!(f, "")?;
@@ -3129,13 +3128,13 @@ impl<T: AsRef<[u32]>> fmt::Debug for DFA<T> {
31293128
} else {
31303129
self.to_index(id)
31313130
};
3132-
write!(f, "MATCH({:06?}): ", id)?;
3131+
write!(f, "MATCH({id:06?}): ")?;
31333132
for (i, &pid) in self.ms.pattern_id_slice(i).iter().enumerate()
31343133
{
31353134
if i > 0 {
31363135
write!(f, ", ")?;
31373136
}
3138-
write!(f, "{:?}", pid)?;
3137+
write!(f, "{pid:?}")?;
31393138
}
31403139
writeln!(f, "")?;
31413140
}
@@ -3525,8 +3524,8 @@ impl TransitionTable<Vec<u32>> {
35253524
///
35263525
/// Both id1 and id2 must point to valid states, otherwise this panics.
35273526
fn swap(&mut self, id1: StateID, id2: StateID) {
3528-
assert!(self.is_valid(id1), "invalid 'id1' state: {:?}", id1);
3529-
assert!(self.is_valid(id2), "invalid 'id2' state: {:?}", id2);
3527+
assert!(self.is_valid(id1), "invalid 'id1' state: {id1:?}");
3528+
assert!(self.is_valid(id2), "invalid 'id2' state: {id2:?}");
35303529
// We only need to swap the parts of the state that are used. So if the
35313530
// stride is 64, but the alphabet length is only 33, then we save a lot
35323531
// of work.
@@ -4277,7 +4276,7 @@ impl<T: AsMut<[u32]>> StartTable<T> {
42774276
let len = self
42784277
.pattern_len
42794278
.expect("start states for each pattern enabled");
4280-
assert!(pid < len, "invalid pattern ID {:?}", pid);
4279+
assert!(pid < len, "invalid pattern ID {pid:?}");
42814280
self.stride
42824281
.checked_mul(pid)
42834282
.unwrap()
@@ -4868,9 +4867,9 @@ impl<'a> fmt::Debug for State<'a> {
48684867
write!(f, ", ")?;
48694868
}
48704869
if start == end {
4871-
write!(f, "{:?} => {:?}", start, id)?;
4870+
write!(f, "{start:?} => {id:?}")?;
48724871
} else {
4873-
write!(f, "{:?}-{:?} => {:?}", start, end, id)?;
4872+
write!(f, "{start:?}-{end:?} => {id:?}")?;
48744873
}
48754874
}
48764875
Ok(())
@@ -5135,7 +5134,7 @@ impl core::fmt::Display for BuildError {
51355134
match self.kind() {
51365135
BuildErrorKind::NFA(_) => write!(f, "error building NFA"),
51375136
BuildErrorKind::Unsupported(ref msg) => {
5138-
write!(f, "unsupported regex feature for DFAs: {}", msg)
5137+
write!(f, "unsupported regex feature for DFAs: {msg}")
51395138
}
51405139
BuildErrorKind::TooManyStates => write!(
51415140
f,
@@ -5167,11 +5166,10 @@ impl core::fmt::Display for BuildError {
51675166
),
51685167
BuildErrorKind::DFAExceededSizeLimit { limit } => write!(
51695168
f,
5170-
"DFA exceeded size limit of {:?} during determinization",
5171-
limit,
5169+
"DFA exceeded size limit of {limit:?} during determinization",
51725170
),
51735171
BuildErrorKind::DeterminizeExceededSizeLimit { limit } => {
5174-
write!(f, "determinization exceeded size limit of {:?}", limit)
5172+
write!(f, "determinization exceeded size limit of {limit:?}")
51755173
}
51765174
}
51775175
}

regex-automata/src/dfa/determinize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'a> Runner<'a> {
280280
let per_elem = size_of::<StateID>() + size_of::<Vec<PatternID>>();
281281
let pats = total_pat_len * size_of::<PatternID>();
282282
let mem = (matches.len() * per_elem) + pats;
283-
log::debug!("matches map built, memory usage: {}", mem);
283+
log::debug!("matches map built, memory usage: {mem}");
284284
}
285285
// At this point, we shuffle the "special" states in the final DFA.
286286
// This permits a DFA's match loop to detect a match condition (among

regex-automata/src/dfa/onepass.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,7 @@ impl core::fmt::Debug for DFA {
24082408
}
24092409
write!(f, "{:06?}", sid.as_usize())?;
24102410
if !pateps.is_empty() {
2411-
write!(f, " ({:?})", pateps)?;
2411+
write!(f, " ({pateps:?})")?;
24122412
}
24132413
write!(f, ": ")?;
24142414
debug_state_transitions(f, self, sid)?;
@@ -2939,7 +2939,7 @@ impl core::fmt::Debug for Slots {
29392939
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
29402940
write!(f, "S")?;
29412941
for slot in self.iter() {
2942-
write!(f, "-{:?}", slot)?;
2942+
write!(f, "-{slot:?}")?;
29432943
}
29442944
Ok(())
29452945
}
@@ -3050,23 +3050,19 @@ impl core::fmt::Display for BuildError {
30503050
Word(_) => write!(f, "NFA contains Unicode word boundary"),
30513051
TooManyStates { limit } => write!(
30523052
f,
3053-
"one-pass DFA exceeded a limit of {:?} for number of states",
3054-
limit,
3053+
"one-pass DFA exceeded a limit of {limit:?} for number of states",
30553054
),
30563055
TooManyPatterns { limit } => write!(
30573056
f,
3058-
"one-pass DFA exceeded a limit of {:?} for number of patterns",
3059-
limit,
3057+
"one-pass DFA exceeded a limit of {limit:?} for number of patterns",
30603058
),
30613059
UnsupportedLook { look } => write!(
30623060
f,
3063-
"one-pass DFA does not support the {:?} assertion",
3064-
look,
3061+
"one-pass DFA does not support the {look:?} assertion",
30653062
),
30663063
ExceededSizeLimit { limit } => write!(
30673064
f,
3068-
"one-pass DFA exceeded size limit of {:?} during building",
3069-
limit,
3065+
"one-pass DFA exceeded size limit of {limit:?} during building",
30703066
),
30713067
NotOnePass { msg } => write!(
30723068
f,
@@ -3089,7 +3085,7 @@ mod tests {
30893085
let predicate = |err: &str| err.contains("conflicting transition");
30903086

30913087
let err = DFA::new(r"a*[ab]").unwrap_err().to_string();
3092-
assert!(predicate(&err), "{}", err);
3088+
assert!(predicate(&err), "{err}");
30933089
}
30943090

30953091
#[test]
@@ -3099,7 +3095,7 @@ mod tests {
30993095
};
31003096

31013097
let err = DFA::new(r"(^|$)a").unwrap_err().to_string();
3102-
assert!(predicate(&err), "{}", err);
3098+
assert!(predicate(&err), "{err}");
31033099
}
31043100

31053101
#[test]
@@ -3109,7 +3105,7 @@ mod tests {
31093105
};
31103106

31113107
let err = DFA::new_many(&[r"^", r"$"]).unwrap_err().to_string();
3112-
assert!(predicate(&err), "{}", err);
3108+
assert!(predicate(&err), "{err}");
31133109
}
31143110

31153111
// This test is meant to build a one-pass regex with the maximum number of

regex-automata/src/dfa/sparse.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ impl DFA<Vec<u8>> {
299299
);
300300
assert!(
301301
transition_len <= 257,
302-
"expected transition length {} to be <= 257",
303-
transition_len,
302+
"expected transition length {transition_len} to be <= 257",
304303
);
305304

306305
// Fill in the transition length.
@@ -2158,7 +2157,7 @@ impl<T: AsMut<[u8]>> StartTable<T> {
21582157
let len = self
21592158
.pattern_len
21602159
.expect("start states for each pattern enabled");
2161-
assert!(pid < len, "invalid pattern ID {:?}", pid);
2160+
assert!(pid < len, "invalid pattern ID {pid:?}");
21622161
self.stride
21632162
.checked_mul(pid)
21642163
.unwrap()

regex-automata/src/hybrid/dfa.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,8 +2598,8 @@ impl<'i, 'c> Lazy<'i, 'c> {
25982598
unit: alphabet::Unit,
25992599
to: LazyStateID,
26002600
) {
2601-
assert!(self.as_ref().is_valid(from), "invalid 'from' id: {:?}", from);
2602-
assert!(self.as_ref().is_valid(to), "invalid 'to' id: {:?}", to);
2601+
assert!(self.as_ref().is_valid(from), "invalid 'from' id: {from:?}");
2602+
assert!(self.as_ref().is_valid(to), "invalid 'to' id: {to:?}");
26032603
let offset =
26042604
from.as_usize_untagged() + self.dfa.classes.get_by_unit(unit);
26052605
self.cache.trans[offset] = to;
@@ -4080,10 +4080,9 @@ impl Builder {
40804080
// and mush on.
40814081
if self.config.get_skip_cache_capacity_check() {
40824082
debug!(
4083-
"given capacity ({}) is too small, \
4083+
"given capacity ({cache_capacity}) is too small, \
40844084
since skip_cache_capacity_check is enabled, \
4085-
setting cache capacity to minimum ({})",
4086-
cache_capacity, min_cache,
4085+
setting cache capacity to minimum ({min_cache})",
40874086
);
40884087
cache_capacity = min_cache;
40894088
} else {

regex-automata/src/hybrid/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,15 @@ impl core::fmt::Display for BuildError {
8080
BuildErrorKind::InsufficientCacheCapacity { minimum, given } => {
8181
write!(
8282
f,
83-
"given cache capacity ({}) is smaller than \
84-
minimum required ({})",
85-
given, minimum,
83+
"given cache capacity ({given}) is smaller than \
84+
minimum required ({minimum})",
8685
)
8786
}
8887
BuildErrorKind::InsufficientStateIDCapacity { ref err } => {
8988
err.fmt(f)
9089
}
9190
BuildErrorKind::Unsupported(ref msg) => {
92-
write!(f, "unsupported regex feature for DFAs: {}", msg)
91+
write!(f, "unsupported regex feature for DFAs: {msg}")
9392
}
9493
}
9594
}

regex-automata/src/meta/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl From<MatchError> for RetryFailError {
234234
// backtracker's wrapper will never hand out a backtracker engine
235235
// when the haystack would be too long.
236236
HaystackTooLong { .. } | UnsupportedAnchored { .. } => {
237-
unreachable!("found impossible error in meta engine: {}", merr)
237+
unreachable!("found impossible error in meta engine: {merr}")
238238
}
239239
}
240240
}

regex-automata/src/meta/limited.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ pub(crate) fn dfa_try_search_half_rev(
7878
at -= 1;
7979
if at < min_start {
8080
trace!(
81-
"reached position {} which is before the previous literal \
81+
"reached position {at} which is before the previous literal \
8282
match, quitting to avoid quadratic behavior",
83-
at,
8483
);
8584
return Err(RetryError::Quadratic(RetryQuadraticError::new()));
8685
}
@@ -114,9 +113,8 @@ pub(crate) fn dfa_try_search_half_rev(
114113
&& !was_dead
115114
{
116115
trace!(
117-
"reached beginning of search at offset {} without hitting \
116+
"reached beginning of search at offset {at} without hitting \
118117
a dead state, quitting to avoid potential false positive match",
119-
at,
120118
);
121119
return Err(RetryError::Quadratic(RetryQuadraticError::new()));
122120
}
@@ -161,9 +159,8 @@ pub(crate) fn hybrid_try_search_half_rev(
161159
at -= 1;
162160
if at < min_start {
163161
trace!(
164-
"reached position {} which is before the previous literal \
162+
"reached position {at} which is before the previous literal \
165163
match, quitting to avoid quadratic behavior",
166-
at,
167164
);
168165
return Err(RetryError::Quadratic(RetryQuadraticError::new()));
169166
}
@@ -176,9 +173,8 @@ pub(crate) fn hybrid_try_search_half_rev(
176173
&& !was_dead
177174
{
178175
trace!(
179-
"reached beginning of search at offset {} without hitting \
176+
"reached beginning of search at offset {at} without hitting \
180177
a dead state, quitting to avoid potential false positive match",
181-
at,
182178
);
183179
return Err(RetryError::Quadratic(RetryQuadraticError::new()));
184180
}

regex-automata/src/meta/literal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ pub(crate) fn alternation_literals(
5353
HirKind::Literal(Literal(ref bytes)) => {
5454
lit.extend_from_slice(bytes);
5555
}
56-
_ => unreachable!("expected literal, got {:?}", e),
56+
_ => unreachable!("expected literal, got {e:?}"),
5757
}
5858
}
5959
}
60-
_ => unreachable!("expected literal or concat, got {:?}", alt),
60+
_ => unreachable!("expected literal or concat, got {alt:?}"),
6161
}
6262
lits.push(lit);
6363
}

0 commit comments

Comments
 (0)