|
29 | 29 | ) |
30 | 30 | from capa.features.extractors.ts.tools import LANGUAGE_TOOLKITS, BaseNamespace, CSharpNamespace, LanguageToolkit |
31 | 31 |
|
| 32 | +_RE_CSHARP_PAGE = re.compile(r'@ .*Page Language\s*=\s*"C#".*'.encode(), re.IGNORECASE) |
| 33 | +_RE_ASPX_IMPORT_DIRECTIVE = re.compile(r"@\s*Import Namespace=".encode(), re.IGNORECASE) |
| 34 | +_RE_ASPX_NAMESPACE = re.compile(r'@\s*Import namespace="(.*?)"'.encode(), re.IGNORECASE) |
| 35 | +_RE_RUNAT_SERVER = re.compile(r'runat\s*=\s*"server"'.encode()) |
| 36 | + |
32 | 37 |
|
33 | 38 | class TreeSitterBaseEngine: |
34 | 39 | buf: bytes |
@@ -227,29 +232,13 @@ def get_namespaces(self) -> Iterator[BaseNamespace]: |
227 | 232 | yield from self.get_imported_namespaces() |
228 | 233 |
|
229 | 234 | def is_c_sharp(self, node: Node) -> bool: |
230 | | - return bool( |
231 | | - re.match( |
232 | | - r'@ .*Page Language\s*=\s*"C#".*'.encode(), |
233 | | - self.get_byte_range(node), |
234 | | - re.IGNORECASE, |
235 | | - ) |
236 | | - ) |
| 235 | + return bool(_RE_CSHARP_PAGE.match(self.get_byte_range(node))) |
237 | 236 |
|
238 | 237 | def is_aspx_import_directive(self, node: Node) -> bool: |
239 | | - return bool( |
240 | | - re.match( |
241 | | - r"@\s*Import Namespace=".encode(), |
242 | | - self.get_byte_range(node), |
243 | | - re.IGNORECASE, |
244 | | - ) |
245 | | - ) |
| 238 | + return bool(_RE_ASPX_IMPORT_DIRECTIVE.match(self.get_byte_range(node))) |
246 | 239 |
|
247 | 240 | def get_aspx_namespace(self, node: Node) -> Optional[BaseNamespace]: |
248 | | - match = re.search( |
249 | | - r'@\s*Import namespace="(.*?)"'.encode(), |
250 | | - self.get_byte_range(node), |
251 | | - re.IGNORECASE, |
252 | | - ) |
| 241 | + match = _RE_ASPX_NAMESPACE.search(self.get_byte_range(node)) |
253 | 242 | return CSharpNamespace(match.group(1).decode("utf-8"), node) if match is not None else None |
254 | 243 |
|
255 | 244 |
|
@@ -295,4 +284,4 @@ def identify_language(self, node: Node) -> str: |
295 | 284 | return LANG_JS |
296 | 285 |
|
297 | 286 | def is_server_side_c_sharp(self, node: Node) -> bool: |
298 | | - return bool(re.findall(r'runat\s*=\s*"server"'.encode(), self.get_byte_range(node))) |
| 287 | + return bool(_RE_RUNAT_SERVER.search(self.get_byte_range(node))) |
0 commit comments