about summary refs log tree commit diff
path: root/tvix/cli
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-07-29T22·38+0200
committerclbot <clbot@tvl.fyi>2023-07-31T21·50+0000
commit737a6ca01e3ee017834fd310cb942079b5d10446 (patch)
tree6e90ca43d734ac567b06c62ffdcd449632df3468 /tvix/cli
parentaa1982c0852de539eeddd742f7c41bd1a873a53c (diff)
refactor(tvix/cli/refscan): use wu-manber crate with &[u8] support r/6453
PR'ed at https://github.com/tvlfyi/wu-manber/pull/1, and now merged.

Change-Id: I8c71e359196396a1d42a3ea2ab7ac15b137b2db0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8992
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Diffstat (limited to 'tvix/cli')
-rw-r--r--tvix/cli/src/derivation.rs8
-rw-r--r--tvix/cli/src/refscan.rs26
2 files changed, 10 insertions, 24 deletions
diff --git a/tvix/cli/src/derivation.rs b/tvix/cli/src/derivation.rs
index f8afe1946110..6cde7a366c2b 100644
--- a/tvix/cli/src/derivation.rs
+++ b/tvix/cli/src/derivation.rs
@@ -311,9 +311,9 @@ mod derivation_builtins {
                 Default::default()
             } else {
                 let mut refscan = state.reference_scanner();
-                drv.arguments.iter().for_each(|s| refscan.scan_str(s));
-                drv.environment.values().for_each(|s| refscan.scan_bytes(s));
-                refscan.scan_str(&drv.builder);
+                drv.arguments.iter().for_each(|s| refscan.scan(s));
+                drv.environment.values().for_each(|s| refscan.scan(s));
+                refscan.scan(&drv.builder);
                 refscan.finalise()
             }
         };
@@ -400,7 +400,7 @@ mod derivation_builtins {
             .context("evaluating the `content` parameter of builtins.toFile")?;
 
         let mut refscan = state.borrow().reference_scanner();
-        refscan.scan_str(content.as_str());
+        refscan.scan(content.as_str());
         let refs = {
             let paths = state.borrow();
             refscan
diff --git a/tvix/cli/src/refscan.rs b/tvix/cli/src/refscan.rs
index 25ada4c86560..8d66d9e4e55b 100644
--- a/tvix/cli/src/refscan.rs
+++ b/tvix/cli/src/refscan.rs
@@ -37,29 +37,15 @@ impl<P: Clone + Ord + AsRef<[u8]>> ReferenceScanner<P> {
         }
     }
 
-    /// If the given &[u8] is also a valid UTF-8 string, scan for all non-
-    /// overlapping matches and collect them in the scanner.
-    /// TODO: ideally, wu-manber would just work with &[u8] directly.
-    pub fn scan_bytes(&mut self, haystack: &[u8]) {
-        if haystack.len() < STORE_PATH_LEN {
-            return;
-        }
-
-        match std::str::from_utf8(haystack) {
-            Ok(s) => self.scan_str(s),
-            Err(_) => {}
-        }
-    }
-
     /// Scan the given str for all non-overlapping matches and collect them
     /// in the scanner.
-    pub fn scan_str(&mut self, haystack: &str) {
-        if haystack.len() < STORE_PATH_LEN {
+    pub fn scan<S: AsRef<[u8]>>(&mut self, haystack: S) {
+        if haystack.as_ref().len() < STORE_PATH_LEN {
             return;
         }
 
         if let Some(searcher) = &self.searcher {
-            for m in searcher.find(&haystack) {
+            for m in searcher.find(haystack) {
                 self.matches.push(m.pat_idx);
             }
         }
@@ -85,7 +71,7 @@ mod tests {
     fn test_no_patterns() {
         let mut scanner: ReferenceScanner<String> = ReferenceScanner::new(vec![]);
 
-        scanner.scan_str(HELLO_DRV);
+        scanner.scan(HELLO_DRV);
 
         let result = scanner.finalise();
 
@@ -97,7 +83,7 @@ mod tests {
         let mut scanner = ReferenceScanner::new(vec![
             "/nix/store/4xw8n979xpivdc46a9ndcvyhwgif00hz-bash-5.1-p16".to_string(),
         ]);
-        scanner.scan_str(HELLO_DRV);
+        scanner.scan(HELLO_DRV);
 
         let result = scanner.finalise();
 
@@ -117,7 +103,7 @@ mod tests {
         ];
 
         let mut scanner = ReferenceScanner::new(candidates.clone());
-        scanner.scan_str(HELLO_DRV);
+        scanner.scan(HELLO_DRV);
 
         let result = scanner.finalise();
         assert_eq!(result.len(), 3);