diff options
Diffstat (limited to 'crates/shirabe-class-map-generator')
3 files changed, 59 insertions, 33 deletions
diff --git a/crates/shirabe-class-map-generator/src/class_map_generator.rs b/crates/shirabe-class-map-generator/src/class_map_generator.rs index 1d2084d..608dccb 100644 --- a/crates/shirabe-class-map-generator/src/class_map_generator.rs +++ b/crates/shirabe-class-map-generator/src/class_map_generator.rs @@ -3,7 +3,8 @@ use crate::class_map::ClassMap; use crate::file_list::FileList; use crate::php_file_parser::PhpFileParser; -use shirabe_external_packages::composer::pcre::preg::Preg; +use indexmap::indexmap; +use shirabe_external_packages::composer::pcre::preg::{CaptureKey, Preg}; use shirabe_external_packages::symfony::component::finder::finder::Finder; use shirabe_external_packages::symfony::component::finder::spl_file_info::SplFileInfo; use shirabe_php_shim::{ @@ -149,7 +150,7 @@ impl ClassMapGenerator { for file in files { let mut file_path = file.get_pathname(); - let ext = pathinfo(&PhpMixed::String(file_path.clone()), PATHINFO_EXTENSION); + let ext = pathinfo(PhpMixed::String(file_path.clone()), PATHINFO_EXTENSION); if !in_array( ext, &PhpMixed::List( @@ -327,13 +328,13 @@ impl ClassMapGenerator { ".", &Self::normalize_path(file_path), ) - .unwrap_or_else(|| Self::normalize_path(file_path)); + .unwrap_or_else(|_| Self::normalize_path(file_path)); let short_base_path = Preg::replace( &format!("{{^{}}}", preg_quote(&cwd, None)), ".", &Self::normalize_path(base_path), ) - .unwrap_or_else(|| Self::normalize_path(base_path)); + .unwrap_or_else(|_| Self::normalize_path(base_path)); for class in rejected_classes { self.class_map.add_psr_violation( @@ -374,11 +375,18 @@ impl ClassMapGenerator { } // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive: - if let Some(m) = Preg::is_match_strict_groups( + let mut r#match: indexmap::IndexMap<_, _> = indexmap![]; + if Preg::is_match_strict_groups3( r"{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix", &path, - ) { - prefix = m.get(1).cloned().unwrap_or_default(); + Some(&mut r#match), + ) + .unwrap_or(false) + { + prefix = r#match + .get(&CaptureKey::ByIndex(1)) + .cloned() + .unwrap_or_default(); path = substr(&path, strlen(&prefix) as i64, None); } @@ -401,7 +409,12 @@ impl ClassMapGenerator { // ensure c: is normalized to C: let prefix = Preg::replace_callback( r"{(?:^|://)[a-z]:$}i", - |m| m.get("0").cloned().unwrap_or_default().to_uppercase(), + |m| { + m.get(&CaptureKey::ByIndex(0)) + .cloned() + .unwrap_or_default() + .to_uppercase() + }, &prefix, ) .unwrap_or(prefix); diff --git a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs index 1a33533..ec59414 100644 --- a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs +++ b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs @@ -1,7 +1,7 @@ //! ref: composer/vendor/composer/class-map-generator/src/PhpFileCleaner.php use indexmap::IndexMap; -use shirabe_external_packages::composer::pcre::preg::Preg; +use shirabe_external_packages::composer::pcre::preg::{CaptureKey, Preg}; use shirabe_php_shim::preg_quote; use std::sync::Mutex; @@ -88,13 +88,13 @@ impl PhpFileCleaner { } if char == '<' && self.peek('<') { - let mut r#match: Vec<String> = vec![]; + let mut r#match: IndexMap<CaptureKey, String> = IndexMap::new(); if self.r#match( r#"{<<<[ \t]*+(['\"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*+)\1(?:\r\n|\n|\r)}A"#, Some(&mut r#match), ) { - self.index += r#match[0].len(); - let delimiter = r#match[2].clone(); + self.index += r#match.get(&CaptureKey::ByIndex(0)).map(|s| s.len()).unwrap_or(0); + let delimiter = r#match.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); self.skip_heredoc(&delimiter); clean.push_str("null"); continue; @@ -122,15 +122,21 @@ impl PhpFileCleaner { let end = self.index + entry.length; if end <= self.len && &self.contents[self.index..end] == entry.name { let offset = if self.index > 0 { self.index - 1 } else { 0 }; - let mut r#match: Vec<String> = vec![]; - if Preg::is_match_at( + let mut r#match: IndexMap<CaptureKey, String> = IndexMap::new(); + if Preg::is_match5( &entry.pattern, &self.contents, - &mut r#match, + Some(&mut r#match), 0, offset, - ) { - return clean + &r#match[0]; + ) + .unwrap_or(false) + { + return clean + + r#match + .get(&CaptureKey::ByIndex(0)) + .map(|s| s.as_str()) + .unwrap_or(""); } } } @@ -139,11 +145,15 @@ impl PhpFileCleaner { self.index += 1; let rest_pattern = REST_PATTERN.lock().unwrap().clone(); if let Some(rest_pattern) = rest_pattern { - let mut r#match: Vec<String> = vec![]; + let mut r#match: IndexMap<CaptureKey, String> = IndexMap::new(); if self.r#match(&rest_pattern, Some(&mut r#match)) { + let m0 = r#match + .get(&CaptureKey::ByIndex(0)) + .cloned() + .unwrap_or_default(); clean.push(char); - clean.push_str(&r#match[0]); - self.index += r#match[0].len(); + clean.push_str(&m0); + self.index += m0.len(); } else { clean.push(char); } @@ -257,7 +267,8 @@ impl PhpFileCleaner { self.index + 1 < self.len && self.contents.as_bytes()[self.index + 1] as char == char } - fn r#match(&self, regex: &str, r#match: Option<&mut Vec<String>>) -> bool { - Preg::is_match_strict_groups_at(regex, &self.contents, r#match, 0, self.index) + fn r#match(&self, regex: &str, r#match: Option<&mut IndexMap<CaptureKey, String>>) -> bool { + Preg::is_match_strict_groups5(regex, &self.contents, r#match, 0, self.index) + .unwrap_or(false) } } diff --git a/crates/shirabe-class-map-generator/src/php_file_parser.rs b/crates/shirabe-class-map-generator/src/php_file_parser.rs index 9bc7c5c..d87db82 100644 --- a/crates/shirabe-class-map-generator/src/php_file_parser.rs +++ b/crates/shirabe-class-map-generator/src/php_file_parser.rs @@ -3,7 +3,7 @@ use crate::php_file_cleaner::PhpFileCleaner; use anyhow::anyhow; use indexmap::IndexMap; -use shirabe_external_packages::composer::pcre::preg::Preg; +use shirabe_external_packages::composer::pcre::preg::{CaptureKey, Preg}; use shirabe_php_shim::{ HHVM_VERSION, PHP_EOL, PHP_VERSION_ID, RuntimeException, error_get_last, file_exists, file_get_contents, function_exists, is_file, is_readable, ltrim, php_strip_whitespace, sprintf, @@ -63,11 +63,10 @@ impl PhpFileParser { // return early if there is no chance of matching anything in this file let pattern = format!("{{\\b(?:class|interface|trait{})\\s}}i", extra_types); - let matches_0 = Preg::is_match_all_strict_groups(&pattern, &contents); - if matches_0.as_ref().map(|m| m[0].is_empty()).unwrap_or(true) { + let max_matches = Preg::match_all_strict_groups(&pattern, &contents)?; + if max_matches == 0 { return Ok(vec![]); } - let max_matches = matches_0.as_ref().unwrap()[0].len(); let mut p = PhpFileCleaner::new(contents, max_matches); let contents = p.clean(); @@ -81,22 +80,25 @@ impl PhpFileParser { )", et = extra_types ); - let mut matches: IndexMap<String, Vec<String>> = IndexMap::new(); - Preg::match_all(&pattern2, &contents, &mut matches); + let mut matches: IndexMap<_, _> = IndexMap::new(); + Preg::match_all3(&pattern2, &contents, Some(&mut matches))?; let mut classes = vec![]; let mut namespace = String::new(); - let len = matches.get("type").map(|v| v.len()).unwrap_or(0); + let len = matches + .get(&CaptureKey::ByName("type".to_owned())) + .map(|v| v.len()) + .unwrap_or(0); for i in 0..len { let ns = matches - .get("ns") + .get(&CaptureKey::ByName("ns".to_owned())) .and_then(|v| v.get(i)) .map(|s| s.as_str()) .unwrap_or(""); if !ns.is_empty() { let nsname = matches - .get("nsname") + .get(&CaptureKey::ByName("nsname".to_owned())) .and_then(|v| v.get(i)) .map(|s| s.as_str()) .unwrap_or(""); @@ -112,7 +114,7 @@ impl PhpFileParser { ) + "\\"; } else { let name = matches - .get("name") + .get(&CaptureKey::ByName("name".to_owned())) .and_then(|v| v.get(i)) .map(|s| s.as_str()) .unwrap_or(""); @@ -133,7 +135,7 @@ impl PhpFileParser { &name[1..], ) } else if matches - .get("type") + .get(&CaptureKey::ByName("type".to_owned())) .and_then(|v| v.get(i)) .map(|s| s.to_lowercase()) .as_deref() |
