|
8 | 8 | //! This file is automatically generated from the grammar registry |
9 | 9 | //! (`langs/group-*/*/def/arborium.kdl`). Do not edit manually. |
10 | 10 |
|
| 11 | +use std::borrow::Cow; |
11 | 12 | use std::collections::HashMap; |
12 | 13 | use std::sync::{Arc, RwLock}; |
13 | 14 |
|
@@ -61,37 +62,36 @@ impl GrammarStore { |
61 | 62 | // Fast path: check if already cached |
62 | 63 | { |
63 | 64 | let grammars = self.grammars.read().unwrap(); |
64 | | - if let Some(grammar) = grammars.get(normalized) { |
| 65 | + if let Some(grammar) = grammars.get(&*normalized) { |
65 | 66 | return Some(grammar.clone()); |
66 | 67 | } |
67 | 68 | } |
68 | 69 |
|
69 | 70 | // Slow path: compile and cache |
70 | | - let grammar = Self::compile_grammar(normalized)?; |
| 71 | + let grammar = Self::compile_grammar(&normalized)?; |
71 | 72 | let grammar = Arc::new(grammar); |
72 | 73 |
|
73 | 74 | { |
74 | 75 | let mut grammars = self.grammars.write().unwrap(); |
75 | 76 | // Double-check in case another thread compiled it |
76 | | - if let Some(existing) = grammars.get(normalized) { |
| 77 | + if let Some(existing) = grammars.get(&*normalized) { |
77 | 78 | return Some(existing.clone()); |
78 | 79 | } |
79 | | - grammars.insert(normalized.to_string(), grammar.clone()); |
| 80 | + grammars.insert(normalized.into_owned(), grammar.clone()); |
80 | 81 | } |
81 | 82 |
|
82 | 83 | Some(grammar) |
83 | 84 | } |
84 | 85 |
|
85 | 86 | /// Normalize a language name to its canonical form. |
86 | | - fn normalize_language(language: &str) -> &'static str { |
| 87 | + fn normalize_language(language: &str) -> Cow<'_, str> { |
87 | 88 | match language { |
88 | 89 | // Aliases (generated from arborium.kdl) |
89 | 90 | <% for (alias, canonical) in aliases { %> |
90 | | - "<%= alias %>" => "<%= canonical %>", |
| 91 | + "<%= alias %>" => Cow::Borrowed("<%= canonical %>"), |
91 | 92 | <% } %> |
92 | | - // Known canonical IDs pass through as-is |
93 | | - // (leak the string to get &'static str - this is fine since language names are finite) |
94 | | - _ => Box::leak(language.to_string().into_boxed_str()), |
| 93 | + // Unknown language names pass through as-is |
| 94 | + _ => Cow::Borrowed(language), |
95 | 95 | } |
96 | 96 | } |
97 | 97 |
|
|
0 commit comments