libdelimit: refactored find_subslice()
This commit is contained in:
parent
28bc2cd0d0
commit
c1df6e6322
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Emma Tebibyte <emma@tebibyte.media>
|
||||
* Copyright (c) 2025 silty silt <silt@tebibyte.media>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
@ -18,7 +19,7 @@
|
||||
|
||||
use std::{
|
||||
io::{ Read, Result },
|
||||
mem::self,
|
||||
mem,
|
||||
};
|
||||
|
||||
const BUFFER_SIZE: usize = 4096;
|
||||
@ -72,21 +73,11 @@ impl<T> Iterator for Delimited<T> where T: Read {
|
||||
}
|
||||
|
||||
fn find_subslice(stack: &[u8], key: &[u8]) -> Option<usize> {
|
||||
if key.len() == 1 {
|
||||
return stack.iter().position(|&b| b == key[0]);
|
||||
match key.len() {
|
||||
/* TODO: is this optimization necessary? */
|
||||
1 => stack.iter().position(|&b| b == key[0]),
|
||||
_ => stack.windows(key.len()).position(|w| w == key),
|
||||
}
|
||||
|
||||
if key.len() > stack.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
for i in 0..=stack.len() - key.len() {
|
||||
if &stack[i..i + key.len()] == key {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user