aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-aegis / src / byte_scan.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Byte and pattern search over file contents (pure Rust). |
| 17 | |
| 18 | /// Index of the first `target` byte in `data`. |
| 19 | pub fn scan_byte(data: &[u8], target: u8) -> Option<usize> { |
| 20 | data.iter().position(|&b| b == target) |
| 21 | } |
| 22 | |
| 23 | /// Number of `target` bytes in `data`. |
| 24 | pub fn count_byte_matches(data: &[u8], target: u8) -> usize { |
| 25 | data.iter().filter(|&&b| b == target).count() |
| 26 | } |
| 27 | |
| 28 | /// Index of the first occurrence of `pattern` in `data`; an empty pattern matches at 0. |
| 29 | pub fn find_pattern(data: &[u8], pattern: &[u8]) -> Option<usize> { |
| 30 | if pattern.is_empty() { |
| 31 | return Some(0); |
| 32 | } |
| 33 | data.windows(pattern.len()) |
| 34 | .position(|window| window == pattern) |
| 35 | } |
| 36 | |
| 37 | #[cfg(test)] |
| 38 | mod tests { |
| 39 | use super::*; |
| 40 | |
| 41 | #[test] |
| 42 | fn test_byte_scan_and_pattern_search() { |
| 43 | let buffer = b"SOVEREIGN_SYSTEMS_SPARK_AEGIS_SCAN"; |
| 44 | let target = b'P'; |
| 45 | let found = scan_byte(buffer, target); |
| 46 | assert_eq!(found, Some(19)); |
| 47 | |
| 48 | let not_found = scan_byte(buffer, b'Z'); |
| 49 | assert_eq!(not_found, None); |
| 50 | |
| 51 | let pattern = b"SPARK_AEGIS"; |
| 52 | let pat_idx = find_pattern(buffer, pattern); |
| 53 | assert_eq!(pat_idx, Some(18)); |
| 54 | |
| 55 | let missing_pat = b"NONEXISTENT"; |
| 56 | assert_eq!(find_pattern(buffer, missing_pat), None); |
| 57 | |
| 58 | let count_s = count_byte_matches(buffer, b'S'); |
| 59 | assert_eq!(count_s, 7); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_pattern_longer_than_data() { |
| 64 | assert_eq!(find_pattern(b"AB", b"ABC"), None); |
| 65 | assert_eq!(find_pattern(b"AB", b""), Some(0)); |
| 66 | } |
| 67 | } |