Skip to content

Commit b5b483f

Browse files
authored
Merge pull request #68 from magic-akari/master
2 parents 244ecb6 + cfafdb8 commit b5b483f

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ categories = ["development-tools"]
1515
[dependencies]
1616
nom = "7.0.0"
1717
unicode_categories = "0.1.1"
18-
once_cell = "1"
19-
regex = "1.6"
2018

2119
[dev-dependencies]
2220
criterion = "0.4"

src/formatter.rs

+40-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use regex::Regex;
21
use std::borrow::Cow;
32

43
use crate::indentation::Indentation;
@@ -7,14 +6,47 @@ use crate::params::Params;
76
use crate::tokenizer::{Token, TokenKind};
87
use crate::{FormatOptions, QueryParams};
98

10-
use once_cell::sync::Lazy;
11-
12-
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)^(--|/\*)\s*fmt\s*:\s*(off|on)").unwrap());
13-
9+
// -- fmt: off
10+
// -- fmt: on
1411
pub(crate) fn check_fmt_off(s: &str) -> Option<bool> {
15-
RE.captures(s)?
16-
.get(2)
17-
.map(|matched| matched.as_str().eq_ignore_ascii_case("off"))
12+
let mut state = 0;
13+
14+
const ON: bool = false;
15+
const OFF: bool = true;
16+
17+
const NEXT: u8 = 1;
18+
const STAY: u8 = 0;
19+
20+
// SPACE SPACE SPACE n
21+
// ┌┐ ┌┐ ┌┐ ┌───────────► ON
22+
// - - ▼ f m t ▼ : ▼ o │ N
23+
// 0 ───► 1 ───► 2 ───► 3 ───► 4 ───► 5 ───► 6 ───► 7
24+
// F M T O │ f f
25+
// └────► 8 ───► OFF
26+
// F F
27+
for c in s.bytes() {
28+
state += match (state, c) {
29+
(0 | 1, b'-') => NEXT,
30+
(2, b' ') => STAY,
31+
(2, b'f' | b'F') => NEXT,
32+
(3, b'm' | b'M') => NEXT,
33+
(4, b't' | b'T') => NEXT,
34+
(5, b' ') => STAY,
35+
(5, b':') => NEXT,
36+
(6, b' ') => STAY,
37+
(6, b'o' | b'O') => NEXT,
38+
(7, b'n' | b'N') => {
39+
return Some(ON);
40+
}
41+
(7, b'f' | b'F') => NEXT,
42+
(8, b'f' | b'F') => {
43+
return Some(OFF);
44+
}
45+
_ => return None,
46+
};
47+
}
48+
49+
None
1850
}
1951

2052
pub(crate) fn format(

0 commit comments

Comments
 (0)