Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Commit 4f8939a

Browse files
committed
style: start using rustfmt
1 parent 58516d0 commit 4f8939a

18 files changed

+413
-477
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/regex-automata-debug/target
33
tags
44
/Cargo.lock
5+
BREADCRUMBS

bench/src/bench.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate criterion;
55
extern crate regex_automata;
66

77
use criterion::{Bencher, Benchmark, Criterion, Throughput};
8-
use regex_automata::{dense, DFA, RegexBuilder};
8+
use regex_automata::{dense, RegexBuilder, DFA};
99

1010
use inputs::*;
1111

@@ -94,11 +94,7 @@ fn compile_muammar(c: &mut Criterion) {
9494
);
9595
}
9696

97-
fn define_compile(
98-
c: &mut Criterion,
99-
group_name: &str,
100-
pattern: &'static str,
101-
) {
97+
fn define_compile(c: &mut Criterion, group_name: &str, pattern: &'static str) {
10298
let group = format!("compile/{}", group_name);
10399
define(c, &group, "unminimized-noclasses", &[], move |b| {
104100
b.iter(|| {

ci/script.sh

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ cargo test --verbose --doc
1717
cargo doc --verbose --no-default-features
1818
cargo build --verbose --no-default-features
1919

20+
if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then
21+
rustup component add rustfmt
22+
cargo fmt -- --check
23+
fi
24+
2025
# Validate no_std status if this version of rust supports embedded targets
2126
# (starting with 1.31 stable)
2227
if rustup target add thumbv7em-none-eabihf; then

rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
max_width = 79
2+
use_small_heuristics = "max"

src/dense.rs

+44-53
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use core::iter;
55
use core::mem;
66
use core::slice;
77

8-
use byteorder::{ByteOrder, NativeEndian};
98
#[cfg(feature = "std")]
109
use byteorder::{BigEndian, LittleEndian};
10+
use byteorder::{ByteOrder, NativeEndian};
1111
#[cfg(feature = "std")]
1212
use regex_syntax::ParserBuilder;
1313

@@ -20,13 +20,13 @@ use error::{Error, Result};
2020
#[cfg(feature = "std")]
2121
use minimize::Minimizer;
2222
#[cfg(feature = "std")]
23-
use nfa::{NFA, NFABuilder};
23+
use nfa::{NFABuilder, NFA};
2424
#[cfg(feature = "std")]
2525
use sparse::SparseDFA;
26-
use state_id::{StateID, dead_id};
26+
use state_id::{dead_id, StateID};
2727
#[cfg(feature = "std")]
2828
use state_id::{
29-
premultiply_overflow_error, next_state_id, write_state_id_bytes,
29+
next_state_id, premultiply_overflow_error, write_state_id_bytes,
3030
};
3131

3232
/// The size of the alphabet in a standard DFA.
@@ -1276,10 +1276,7 @@ impl<T: AsRef<[S]>, S: StateID> Repr<T, S> {
12761276
A::write_u64(&mut buf[i..], self.state_count as u64);
12771277
i += 8;
12781278
// max match state
1279-
A::write_u64(
1280-
&mut buf[i..],
1281-
self.max_match.to_usize() as u64,
1282-
);
1279+
A::write_u64(&mut buf[i..], self.max_match.to_usize() as u64);
12831280
i += 8;
12841281
// byte class map
12851282
for b in (0..256).map(|b| b as u8) {
@@ -1311,7 +1308,7 @@ impl<'a, S: StateID> Repr<&'a [S], S> {
13111308
// skip over label
13121309
match buf.iter().position(|&b| b == b'\x00') {
13131310
None => panic!("could not find label"),
1314-
Some(i) => buf = &buf[i+1..],
1311+
Some(i) => buf = &buf[i + 1..],
13151312
}
13161313

13171314
// check that current endianness is same as endianness of DFA
@@ -1342,7 +1339,8 @@ impl<'a, S: StateID> Repr<&'a [S], S> {
13421339
panic!(
13431340
"state size of DenseDFA ({}) does not match \
13441341
requested state size ({})",
1345-
state_size, mem::size_of::<S>(),
1342+
state_size,
1343+
mem::size_of::<S>(),
13461344
);
13471345
}
13481346
buf = &buf[2..];
@@ -1373,7 +1371,8 @@ impl<'a, S: StateID> Repr<&'a [S], S> {
13731371
buf.len() <= len_bytes,
13741372
"insufficient transition table bytes, \
13751373
expected at least {} but only have {}",
1376-
len_bytes, buf.len()
1374+
len_bytes,
1375+
buf.len()
13771376
);
13781377
assert_eq!(
13791378
0,
@@ -1488,12 +1487,11 @@ impl<S: StateID> Repr<Vec<S>, S> {
14881487
pub fn add_empty_state(&mut self) -> Result<S> {
14891488
assert!(!self.premultiplied, "can't add state to premultiplied DFA");
14901489

1491-
let id =
1492-
if self.state_count == 0 {
1493-
S::from_usize(0)
1494-
} else {
1495-
next_state_id(S::from_usize(self.state_count - 1))?
1496-
};
1490+
let id = if self.state_count == 0 {
1491+
S::from_usize(0)
1492+
} else {
1493+
next_state_id(S::from_usize(self.state_count - 1))?
1494+
};
14971495
let alphabet_len = self.alphabet_len();
14981496
self.trans.extend(iter::repeat(dead_id::<S>()).take(alphabet_len));
14991497
// This should never panic, since state_count is a usize. The
@@ -1673,12 +1671,11 @@ impl<'a, T: AsRef<[S]>, S: StateID> Iterator for StateIter<'a, T, S> {
16731671
fn next(&mut self) -> Option<(S, State<'a, S>)> {
16741672
self.it.next().map(|(id, chunk)| {
16751673
let state = State { transitions: chunk };
1676-
let id =
1677-
if self.dfa.premultiplied {
1678-
id * self.dfa.alphabet_len()
1679-
} else {
1680-
id
1681-
};
1674+
let id = if self.dfa.premultiplied {
1675+
id * self.dfa.alphabet_len()
1676+
} else {
1677+
id
1678+
};
16821679
(S::from_usize(id), state)
16831680
})
16841681
}
@@ -1728,15 +1725,16 @@ impl<'a, S: StateID> fmt::Debug for State<'a, S> {
17281725
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17291726
let mut transitions = vec![];
17301727
for (start, end, next_id) in self.sparse_transitions() {
1731-
let line =
1732-
if start == end {
1733-
format!("{} => {}", escape(start), next_id.to_usize())
1734-
} else {
1735-
format!(
1736-
"{}-{} => {}",
1737-
escape(start), escape(end), next_id.to_usize(),
1738-
)
1739-
};
1728+
let line = if start == end {
1729+
format!("{} => {}", escape(start), next_id.to_usize())
1730+
} else {
1731+
format!(
1732+
"{}-{} => {}",
1733+
escape(start),
1734+
escape(end),
1735+
next_id.to_usize(),
1736+
)
1737+
};
17401738
transitions.push(line);
17411739
}
17421740
write!(f, "{}", transitions.join(", "))?;
@@ -1940,17 +1938,14 @@ impl Builder {
19401938
}
19411939

19421940
let nfa = self.build_nfa(pattern)?;
1943-
let mut dfa =
1944-
if self.byte_classes {
1945-
Determinizer::new(&nfa)
1946-
.with_byte_classes()
1947-
.longest_match(self.longest_match)
1948-
.build()
1949-
} else {
1950-
Determinizer::new(&nfa)
1951-
.longest_match(self.longest_match)
1952-
.build()
1953-
}?;
1941+
let mut dfa = if self.byte_classes {
1942+
Determinizer::new(&nfa)
1943+
.with_byte_classes()
1944+
.longest_match(self.longest_match)
1945+
.build()
1946+
} else {
1947+
Determinizer::new(&nfa).longest_match(self.longest_match).build()
1948+
}?;
19541949
if self.minimize {
19551950
dfa.minimize();
19561951
}
@@ -1962,11 +1957,7 @@ impl Builder {
19621957

19631958
/// Builds an NFA from the given pattern.
19641959
pub(crate) fn build_nfa(&self, pattern: &str) -> Result<NFA> {
1965-
let hir = self
1966-
.parser
1967-
.build()
1968-
.parse(pattern)
1969-
.map_err(Error::syntax)?;
1960+
let hir = self.parser.build().parse(pattern).map_err(Error::syntax)?;
19701961
Ok(self.nfa.build(hir)?)
19711962
}
19721963

@@ -2249,8 +2240,8 @@ fn escape(b: u8) -> String {
22492240
#[cfg(test)]
22502241
#[allow(dead_code)]
22512242
mod tests {
2252-
use nfa::NFA;
22532243
use super::*;
2244+
use nfa::NFA;
22542245

22552246
#[test]
22562247
fn errors_when_converting_to_smaller_dfa() {
@@ -2312,10 +2303,10 @@ mod tests {
23122303
}
23132304

23142305
// fn print_automata_counts(pattern: &str) {
2315-
// let (nfa, dfa, mdfa) = build_automata(pattern);
2316-
// println!("nfa # states: {:?}", nfa.len());
2317-
// println!("dfa # states: {:?}", dfa.len());
2318-
// println!("minimal dfa # states: {:?}", mdfa.len());
2306+
// let (nfa, dfa, mdfa) = build_automata(pattern);
2307+
// println!("nfa # states: {:?}", nfa.len());
2308+
// println!("dfa # states: {:?}", dfa.len());
2309+
// println!("minimal dfa # states: {:?}", mdfa.len());
23192310
// }
23202311

23212312
fn build_automata(

src/determinize.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use dense;
66
use error::Result;
77
use nfa::{self, NFA};
88
use sparse_set::SparseSet;
9-
use state_id::{StateID, dead_id};
9+
use state_id::{dead_id, StateID};
1010

1111
type DFARepr<S> = dense::Repr<Vec<S>, S>;
1212

@@ -103,9 +103,8 @@ impl<'a, S: StateID> Determinizer<'a, S> {
103103
let mut uncompiled = vec![self.add_start(&mut sparse)?];
104104
while let Some(dfa_id) = uncompiled.pop() {
105105
for &b in &representative_bytes {
106-
let (next_dfa_id, is_new) = self.cached_state(
107-
dfa_id, b, &mut sparse,
108-
)?;
106+
let (next_dfa_id, is_new) =
107+
self.cached_state(dfa_id, b, &mut sparse)?;
109108
self.dfa.add_transition(dfa_id, b, next_dfa_id);
110109
if is_new {
111110
uncompiled.push(next_dfa_id);
@@ -117,11 +116,8 @@ impl<'a, S: StateID> Determinizer<'a, S> {
117116
// the beginning. This permits a DFA's match loop to detect a match
118117
// condition by merely inspecting the current state's identifier, and
119118
// avoids the need for any additional auxiliary storage.
120-
let is_match: Vec<bool> = self
121-
.builder_states
122-
.iter()
123-
.map(|s| s.is_match)
124-
.collect();
119+
let is_match: Vec<bool> =
120+
self.builder_states.iter().map(|s| s.is_match).collect();
125121
self.dfa.shuffle_match_states(&is_match);
126122
Ok(self.dfa)
127123
}
@@ -161,12 +157,7 @@ impl<'a, S: StateID> Determinizer<'a, S> {
161157

162158
/// Compute the set of all eachable NFA states, including the full epsilon
163159
/// closure, from a DFA state for a single byte of input.
164-
fn next(
165-
&mut self,
166-
dfa_id: S,
167-
b: u8,
168-
next_nfa_states: &mut SparseSet,
169-
) {
160+
fn next(&mut self, dfa_id: S, b: u8, next_nfa_states: &mut SparseSet) {
170161
next_nfa_states.clear();
171162
for i in 0..self.builder_states[dfa_id.to_usize()].nfa_states.len() {
172163
let nfa_id = self.builder_states[dfa_id.to_usize()].nfa_states[i];

src/dfa.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,11 @@ pub trait DFA {
244244
for (i, &b) in bytes[start..].iter().enumerate() {
245245
state = unsafe { self.next_state_unchecked(state, b) };
246246
if self.is_match_or_dead_state(state) {
247-
return
248-
if self.is_dead_state(state) {
249-
None
250-
} else {
251-
Some(start + i + 1)
252-
};
247+
return if self.is_dead_state(state) {
248+
None
249+
} else {
250+
Some(start + i + 1)
251+
};
253252
}
254253
}
255254
None
@@ -268,14 +267,13 @@ pub trait DFA {
268267
}
269268

270269
let mut state = self.start_state();
271-
let mut last_match =
272-
if self.is_dead_state(state) {
273-
return None;
274-
} else if self.is_match_state(state) {
275-
Some(start)
276-
} else {
277-
None
278-
};
270+
let mut last_match = if self.is_dead_state(state) {
271+
return None;
272+
} else if self.is_match_state(state) {
273+
Some(start)
274+
} else {
275+
None
276+
};
279277
for (i, &b) in bytes[start..].iter().enumerate() {
280278
state = unsafe { self.next_state_unchecked(state, b) };
281279
if self.is_match_or_dead_state(state) {
@@ -301,14 +299,13 @@ pub trait DFA {
301299
}
302300

303301
let mut state = self.start_state();
304-
let mut last_match =
305-
if self.is_dead_state(state) {
306-
return None;
307-
} else if self.is_match_state(state) {
308-
Some(start)
309-
} else {
310-
None
311-
};
302+
let mut last_match = if self.is_dead_state(state) {
303+
return None;
304+
} else if self.is_match_state(state) {
305+
Some(start)
306+
} else {
307+
None
308+
};
312309
for (i, &b) in bytes[..start].iter().enumerate().rev() {
313310
state = unsafe { self.next_state_unchecked(state, b) };
314311
if self.is_match_or_dead_state(state) {

src/error.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub enum ErrorKind {
5050
max: usize,
5151
/// The maximum ID required by premultiplication.
5252
requested_max: usize,
53-
}
53+
},
5454
}
5555

5656
impl Error {
@@ -74,8 +74,8 @@ impl Error {
7474
}
7575

7676
pub(crate) fn unsupported_longest_match() -> Error {
77-
let msg = "unachored searches with longest match \
78-
semantics are not supported";
77+
let msg = "unachored searches with longest match \
78+
semantics are not supported";
7979
Error { kind: ErrorKind::Unsupported(msg.to_string()) }
8080
}
8181

@@ -119,15 +119,13 @@ impl fmt::Display for Error {
119119
ErrorKind::Serialize(ref msg) => {
120120
write!(f, "DFA serialization error: {}", msg)
121121
}
122-
ErrorKind::StateIDOverflow { max } => {
123-
write!(
124-
f,
125-
"building the DFA failed because it required building \
126-
more states that can be identified, where the maximum \
127-
ID for the chosen representation is {}",
128-
max,
129-
)
130-
}
122+
ErrorKind::StateIDOverflow { max } => write!(
123+
f,
124+
"building the DFA failed because it required building \
125+
more states that can be identified, where the maximum \
126+
ID for the chosen representation is {}",
127+
max,
128+
),
131129
ErrorKind::PremultiplyOverflow { max, requested_max } => {
132130
if max == requested_max {
133131
write!(

0 commit comments

Comments
 (0)