From 1740801d4503305ce80879994d2487e9eb66321a Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 10 Dec 2024 21:17:20 +0800 Subject: [PATCH 1/3] refactor: Remove regex dependency by using state machine --- Cargo.toml | 2 -- src/formatter.rs | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 33bb285..8109a4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,6 @@ categories = ["development-tools"] [dependencies] nom = "7.0.0" unicode_categories = "0.1.1" -once_cell = "1" -regex = "1.6" [dev-dependencies] criterion = "0.4" diff --git a/src/formatter.rs b/src/formatter.rs index 83bccc5..3a5ca6d 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -1,4 +1,3 @@ -use regex::Regex; use std::borrow::Cow; use crate::indentation::Indentation; @@ -7,14 +6,37 @@ use crate::params::Params; use crate::tokenizer::{Token, TokenKind}; use crate::{FormatOptions, QueryParams}; -use once_cell::sync::Lazy; - -static RE: Lazy = Lazy::new(|| Regex::new(r"(?i)^(--|/\*)\s*fmt\s*:\s*(off|on)").unwrap()); - +// -- fmt: off +// -- fmt: on pub(crate) fn check_fmt_off(s: &str) -> Option { - RE.captures(s)? - .get(2) - .map(|matched| matched.as_str().eq_ignore_ascii_case("off")) + let mut state = 0; + let mut fmt_state = None; + + for c in s.bytes() { + state = match (state, c) { + (0 | 1, b'-') => state + 1, + (2, b' ') => state, + (2, b'f' | b'F') => state + 1, + (3, b'm' | b'M') => state + 1, + (4, b't' | b'T') => state + 1, + (5, b' ') => state, + (5, b':') => state + 1, + (6, b' ') => state, + (6, b'o' | b'O') => state + 1, + (7, b'n' | b'N') => { + fmt_state = Some(false); + break; + } + (7, b'f' | b'F') => state + 1, + (8, b'f' | b'F') => { + fmt_state = Some(true); + break; + } + _ => return None, + }; + } + + fmt_state } pub(crate) fn format( From 8842dc4b3af0b63fde8970a692f13dc6d40bdd6b Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 10 Dec 2024 22:26:32 +0800 Subject: [PATCH 2/3] chore: clean code --- src/formatter.rs | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 3a5ca6d..77ffd52 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -10,33 +10,43 @@ use crate::{FormatOptions, QueryParams}; // -- fmt: on pub(crate) fn check_fmt_off(s: &str) -> Option { let mut state = 0; - let mut fmt_state = None; + const ON: bool = false; + const OFF: bool = true; + + const NEXT: u32 = 1; + const STAY: u32 = 0; + + // SPACE SPACE SPACE n + // ┌┐ ┌┐ ┌┐ ┌───────────► ON + // - - ▼ f m t ▼ : ▼ o │ N + // 0 ───► 1 ───► 2 ───► 3 ───► 4 ───► 5 ───► 6 ───► 7 + // F M T O │ f f + // └────► 8 ───► OFF + // F F for c in s.bytes() { - state = match (state, c) { - (0 | 1, b'-') => state + 1, - (2, b' ') => state, - (2, b'f' | b'F') => state + 1, - (3, b'm' | b'M') => state + 1, - (4, b't' | b'T') => state + 1, - (5, b' ') => state, - (5, b':') => state + 1, - (6, b' ') => state, - (6, b'o' | b'O') => state + 1, + state += match (state, c) { + (0 | 1, b'-') => NEXT, + (2, b' ') => STAY, + (2, b'f' | b'F') => NEXT, + (3, b'm' | b'M') => NEXT, + (4, b't' | b'T') => NEXT, + (5, b' ') => STAY, + (5, b':') => NEXT, + (6, b' ') => STAY, + (6, b'o' | b'O') => NEXT, (7, b'n' | b'N') => { - fmt_state = Some(false); - break; + return Some(ON); } - (7, b'f' | b'F') => state + 1, + (7, b'f' | b'F') => NEXT, (8, b'f' | b'F') => { - fmt_state = Some(true); - break; + return Some(OFF); } _ => return None, }; } - fmt_state + None } pub(crate) fn format( From cfafdb8bea284a5861643c8337fd8dd927fdce25 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 10 Dec 2024 22:37:27 +0800 Subject: [PATCH 3/3] chore: clean code --- src/formatter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 77ffd52..53269f7 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -14,8 +14,8 @@ pub(crate) fn check_fmt_off(s: &str) -> Option { const ON: bool = false; const OFF: bool = true; - const NEXT: u32 = 1; - const STAY: u32 = 0; + const NEXT: u8 = 1; + const STAY: u8 = 0; // SPACE SPACE SPACE n // ┌┐ ┌┐ ┌┐ ┌───────────► ON