Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
abbiecnt committed Aug 27, 2024
1 parent 924810a commit 263c448
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ impl Base {
pub fn as_ptr(&self) -> *const u8 {
self.ptr.as_ptr()
}

#[inline]
pub unsafe fn add(&self, count: usize) -> NonNull<u8> {
unsafe { self.ptr.add(count) }
}
}

impl Debug for Base {
Expand Down
9 changes: 5 additions & 4 deletions src/program/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{Base, Section};
use core::ops::Index;
use core::ptr::NonNull;
use core::slice::{from_raw_parts, SliceIndex};
use rayon::iter::IndexedParallelIterator;
use rayon::slice::ParallelSlice;
Expand Down Expand Up @@ -50,18 +51,18 @@ impl Program {
self.sections.iter().find(|section| section.name() == name)
}

pub fn find(&self, pattern: &[u8]) -> Option<*const u8> {
pub fn find(&self, pattern: &[u8]) -> Option<NonNull<u8>> {
self.as_slice()
.par_windows(pattern.len())
.position_first(|window| window == pattern)
.map(|offset| unsafe { self.as_ptr().add(offset) })
.map(|offset| unsafe { self.base.add(offset) })
}

pub fn rfind(&self, pattern: &[u8]) -> Option<*const u8> {
pub fn rfind(&self, pattern: &[u8]) -> Option<NonNull<u8>> {
self.as_slice()
.par_windows(pattern.len())
.position_last(|window| window == pattern)
.map(|offset| unsafe { self.as_ptr().add(offset) })
.map(|offset| unsafe { self.base.add(offset) })
}

fn init() -> Self {
Expand Down
11 changes: 6 additions & 5 deletions src/section.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::Base;
use core::ptr::NonNull;
use core::slice::from_raw_parts;
use rayon::iter::IndexedParallelIterator;
use rayon::slice::ParallelSlice;
Expand Down Expand Up @@ -32,21 +33,21 @@ impl Section {
unsafe { from_raw_parts(self.base.as_ptr(), self.len) }
}

pub fn find(&self, pattern: &[u8]) -> Option<*const u8> {
pub fn find(&self, pattern: &[u8]) -> Option<NonNull<u8>> {
self.as_slice()
.par_windows(pattern.len())
.position_first(|window| window == pattern)
.map(|offset| unsafe { self.as_ptr().add(offset) })
.map(|offset| unsafe { self.base.add(offset) })
}

pub fn rfind(&self, pattern: &[u8]) -> Option<*const u8> {
pub fn rfind(&self, pattern: &[u8]) -> Option<NonNull<u8>> {
self.as_slice()
.par_windows(pattern.len())
.position_last(|window| window == pattern)
.map(|offset| unsafe { self.as_ptr().add(offset) })
.map(|offset| unsafe { self.base.add(offset) })
}

pub(crate) fn new(name: &'static str, base: Base, len: usize) -> Self {
pub(crate) fn _new(name: &'static str, base: Base, len: usize) -> Self {
Self { name, base, len }
}
}

0 comments on commit 263c448

Please sign in to comment.