Refactor regex compilation and use is_some_and
This commit introduces several improvements: - Pre-compiles regular expressions in `ast/tester.rs` to avoid redundant compilation, improving performance. - Replaces `.extension().map_or(false, |ext| ext == "myc")` with the more concise and readable `.extension().is_some_and(|ext| ext == "myc")`. - Simplifies upvalue capture logic by using `.or_default()` instead of `.or_insert_with(HashSet::new)`. - Adds a `Default` implementation for `TracingObserver`. - Optimizes string literal parsing in `highlight_myc` to correctly handle escape sequences. - Uses `saturating_sub` for bracket depth to prevent underflow.
This commit is contained in:
+5
-5
@@ -46,7 +46,7 @@ impl Default for CompilerApp {
|
||||
.map(|entries| {
|
||||
entries
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.path().extension().map_or(false, |ext| ext == "myc"))
|
||||
.filter(|e| e.path().extension().is_some_and(|ext| ext == "myc"))
|
||||
.filter_map(|e| {
|
||||
let path = e.path();
|
||||
let name = e.file_name().into_string().ok()?;
|
||||
@@ -184,7 +184,7 @@ impl CompilerApp {
|
||||
if let Ok(entries) = std::fs::read_dir("examples") {
|
||||
self.examples = entries
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.path().extension().map_or(false, |ext| ext == "myc"))
|
||||
.filter(|e| e.path().extension().is_some_and(|ext| ext == "myc"))
|
||||
.filter_map(|e| {
|
||||
let path = e.path();
|
||||
let name = e.file_name().into_string().ok()?;
|
||||
@@ -445,8 +445,8 @@ fn highlight_myc(ctx: &egui::Context, code: &str) -> egui::text::LayoutJob {
|
||||
while let Some(next) = chars.next() {
|
||||
text.push(next);
|
||||
if next == '"' { break; }
|
||||
if next == '\\' {
|
||||
if let Some(escaped) = chars.next() { text.push(escaped); }
|
||||
if next == '\\' && let Some(escaped) = chars.next() {
|
||||
text.push(escaped);
|
||||
}
|
||||
}
|
||||
color = color_str;
|
||||
@@ -463,7 +463,7 @@ fn highlight_myc(ctx: &egui::Context, code: &str) -> egui::text::LayoutJob {
|
||||
bracket_depth += 1;
|
||||
}
|
||||
')' | ']' | '}' => {
|
||||
if bracket_depth > 0 { bracket_depth -= 1; }
|
||||
bracket_depth = bracket_depth.saturating_sub(1);
|
||||
color = color_bracket[bracket_depth % 3];
|
||||
}
|
||||
'0'..='9' => {
|
||||
|
||||
Reference in New Issue
Block a user