Skip to content

Commit f5a0cda

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 e7bd19d commit f5a0cda

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
@@ -2824,8 +2824,7 @@ impl OwnedDFA {
28242824
}
28252825
assert!(
28262826
!matches.contains_key(&start_id),
2827-
"{:?} is both a start and a match state, which is not allowed",
2828-
start_id,
2827+
"{start_id:?} is both a start and a match state, which is not allowed",
28292828
);
28302829
is_start.insert(start_id);
28312830
}
@@ -3085,7 +3084,7 @@ impl<T: AsRef<[u32]>> fmt::Debug for DFA<T> {
30853084
} else {
30863085
self.to_index(state.id())
30873086
};
3088-
write!(f, "{:06?}: ", id)?;
3087+
write!(f, "{id:06?}: ")?;
30893088
state.fmt(f)?;
30903089
write!(f, "\n")?;
30913090
}
@@ -3101,11 +3100,11 @@ impl<T: AsRef<[u32]>> fmt::Debug for DFA<T> {
31013100
Anchored::No => writeln!(f, "START-GROUP(unanchored)")?,
31023101
Anchored::Yes => writeln!(f, "START-GROUP(anchored)")?,
31033102
Anchored::Pattern(pid) => {
3104-
writeln!(f, "START_GROUP(pattern: {:?})", pid)?
3103+
writeln!(f, "START_GROUP(pattern: {pid:?})")?
31053104
}
31063105
}
31073106
}
3108-
writeln!(f, " {:?} => {:06?}", sty, id)?;
3107+
writeln!(f, " {sty:?} => {id:06?}")?;
31093108
}
31103109
if self.pattern_len() > 1 {
31113110
writeln!(f, "")?;
@@ -3116,13 +3115,13 @@ impl<T: AsRef<[u32]>> fmt::Debug for DFA<T> {
31163115
} else {
31173116
self.to_index(id)
31183117
};
3119-
write!(f, "MATCH({:06?}): ", id)?;
3118+
write!(f, "MATCH({id:06?}): ")?;
31203119
for (i, &pid) in self.ms.pattern_id_slice(i).iter().enumerate()
31213120
{
31223121
if i > 0 {
31233122
write!(f, ", ")?;
31243123
}
3125-
write!(f, "{:?}", pid)?;
3124+
write!(f, "{pid:?}")?;
31263125
}
31273126
writeln!(f, "")?;
31283127
}
@@ -3512,8 +3511,8 @@ impl TransitionTable<Vec<u32>> {
35123511
///
35133512
/// Both id1 and id2 must point to valid states, otherwise this panics.
35143513
fn swap(&mut self, id1: StateID, id2: StateID) {
3515-
assert!(self.is_valid(id1), "invalid 'id1' state: {:?}", id1);
3516-
assert!(self.is_valid(id2), "invalid 'id2' state: {:?}", id2);
3514+
assert!(self.is_valid(id1), "invalid 'id1' state: {id1:?}");
3515+
assert!(self.is_valid(id2), "invalid 'id2' state: {id2:?}");
35173516
// We only need to swap the parts of the state that are used. So if the
35183517
// stride is 64, but the alphabet length is only 33, then we save a lot
35193518
// of work.
@@ -4258,7 +4257,7 @@ impl<T: AsMut<[u32]>> StartTable<T> {
42584257
let len = self
42594258
.pattern_len
42604259
.expect("start states for each pattern enabled");
4261-
assert!(pid < len, "invalid pattern ID {:?}", pid);
4260+
assert!(pid < len, "invalid pattern ID {pid:?}");
42624261
self.stride
42634262
.checked_mul(pid)
42644263
.unwrap()
@@ -4849,9 +4848,9 @@ impl<'a> fmt::Debug for State<'a> {
48494848
write!(f, ", ")?;
48504849
}
48514850
if start == end {
4852-
write!(f, "{:?} => {:?}", start, id)?;
4851+
write!(f, "{start:?} => {id:?}")?;
48534852
} else {
4854-
write!(f, "{:?}-{:?} => {:?}", start, end, id)?;
4853+
write!(f, "{start:?}-{end:?} => {id:?}")?;
48554854
}
48564855
}
48574856
Ok(())
@@ -5048,7 +5047,7 @@ impl core::fmt::Display for BuildError {
50485047
match self.kind() {
50495048
BuildErrorKind::NFA(_) => write!(f, "error building NFA"),
50505049
BuildErrorKind::Unsupported(ref msg) => {
5051-
write!(f, "unsupported regex feature for DFAs: {}", msg)
5050+
write!(f, "unsupported regex feature for DFAs: {msg}")
50525051
}
50535052
BuildErrorKind::TooManyStates => write!(
50545053
f,
@@ -5080,11 +5079,10 @@ impl core::fmt::Display for BuildError {
50805079
),
50815080
BuildErrorKind::DFAExceededSizeLimit { limit } => write!(
50825081
f,
5083-
"DFA exceeded size limit of {:?} during determinization",
5084-
limit,
5082+
"DFA exceeded size limit of {limit:?} during determinization",
50855083
),
50865084
BuildErrorKind::DeterminizeExceededSizeLimit { limit } => {
5087-
write!(f, "determinization exceeded size limit of {:?}", limit)
5085+
write!(f, "determinization exceeded size limit of {limit:?}")
50885086
}
50895087
}
50905088
}

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
@@ -303,8 +303,7 @@ impl DFA<Vec<u8>> {
303303
);
304304
assert!(
305305
transition_len <= 257,
306-
"expected transition length {} to be <= 257",
307-
transition_len,
306+
"expected transition length {transition_len} to be <= 257",
308307
);
309308

310309
// Fill in the transition length.
@@ -2149,7 +2148,7 @@ impl<T: AsMut<[u8]>> StartTable<T> {
21492148
let len = self
21502149
.pattern_len
21512150
.expect("start states for each pattern enabled");
2152-
assert!(pid < len, "invalid pattern ID {:?}", pid);
2151+
assert!(pid < len, "invalid pattern ID {pid:?}");
21532152
self.stride
21542153
.checked_mul(pid)
21552154
.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)