@@ -22,9 +22,9 @@ use std::panic;
2222use std:: path:: { Path , PathBuf } ;
2323
2424pub use gimli;
25- pub use glob;
2625pub use object;
2726pub use regex;
27+ pub use walkdir;
2828pub use wasmparser;
2929
3030pub use cc:: { cc, extra_c_flags, extra_cxx_flags, Cc } ;
@@ -245,21 +245,37 @@ pub fn uname() -> String {
245245 output. stdout_utf8 ( )
246246}
247247
248- /// Inside a glob pattern of files (paths), read their contents and count the
248+ /// Search for all files in the current working directory with the extension `ext`,
249+ /// read their contents and count the
249250/// number of regex matches with a given expression (re).
250251#[ track_caller]
251- pub fn count_regex_matches_in_file_glob ( re : & str , paths : & str ) -> usize {
252- let re = regex:: Regex :: new ( re) . expect ( format ! ( "Regex expression {re} is not valid." ) . as_str ( ) ) ;
253- let paths = glob:: glob ( paths) . expect ( format ! ( "Glob expression {paths} is not valid." ) . as_str ( ) ) ;
254- use io:: BufRead ;
255- paths
256- . filter_map ( |entry| entry. ok ( ) )
257- . filter ( |entry| entry. as_path ( ) . is_file ( ) )
258- . filter_map ( |path| fs:: File :: open ( & path) . ok ( ) )
259- . map ( |file| io:: BufReader :: new ( file) )
260- . flat_map ( |reader| reader. lines ( ) . filter_map ( |entry| entry. ok ( ) ) )
261- . filter ( |line| re. is_match ( line) )
262- . count ( )
252+ pub fn count_regex_matches_in_files_with_extension ( re : & regex:: Regex , ext : & str ) -> usize {
253+ use std:: io:: BufRead ;
254+ use walkdir:: { DirEntry , WalkDir } ;
255+
256+ let walker = WalkDir :: new ( cwd ( ) ) . into_iter ( ) ;
257+
258+ fn is_hidden ( entry : & DirEntry ) -> bool {
259+ entry. file_name ( ) . to_str ( ) . map ( |s| s. starts_with ( "." ) ) . unwrap_or ( false )
260+ }
261+
262+ let mut count = 0 ;
263+
264+ for entry in walker. filter_entry ( |e| !is_hidden ( e) ) {
265+ let entry = entry. expect ( "failed to get DirEntry" ) ;
266+ if !entry. path ( ) . is_file ( ) {
267+ continue ;
268+ }
269+
270+ if !entry. path ( ) . extension ( ) . is_some_and ( |e| e == ext) {
271+ continue ;
272+ }
273+
274+ let content = fs_wrapper:: read ( entry. path ( ) ) ;
275+ count += content. lines ( ) . filter ( |line| re. is_match ( & line. as_ref ( ) . unwrap ( ) ) ) . count ( ) ;
276+ }
277+
278+ count
263279}
264280
265281fn handle_failed_output ( cmd : & Command , output : CompletedProcess , caller_line_number : u32 ) -> ! {
0 commit comments