Skip to content

Commit

Permalink
add "from" trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jtriley-eth committed Nov 25, 2024
1 parent 2fbf54f commit a2d48a2
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod utils;

pub use utils::{conditional_select, lt_f, DebugRandomEngine};

use std::collections::bounded_vec::BoundedVec;

/**
* @brief represents a byte-array of up to MaxBytes, that is used as a "haystack" array,
* where we want to validate a substring "needle" is present in the "haystack"
Expand Down Expand Up @@ -204,6 +206,15 @@ impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32>
}
}

impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32> From<BoundedVec<u8, MaxBytes>> for SubString<MaxPaddedBytes, PaddedChunksMinusOne, MaxBytes> {
fn from(input: BoundedVec<u8, MaxBytes>) -> Self {
Self::new(
input.storage(),
input.len() as u32
)
}
}

// ######################################################
// S T R I N G B O D Y
// ######################################################
Expand Down Expand Up @@ -409,6 +420,15 @@ impl<let MaxPaddedBytes: u32, let PaddedChunks: u32, let MaxBytes: u32> StringBo
}
}

impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32> From<BoundedVec<u8, MaxBytes>> for StringBody<MaxPaddedBytes, PaddedChunksMinusOne, MaxBytes> {
fn from(input: BoundedVec<u8, MaxBytes>) -> Self {
Self::new(
input.storage(),
input.len() as u32
)
}
}

/// Given an input byte array, convert into 31-byte chunks
///
/// Cost: ~0.5 gates per byte
Expand Down Expand Up @@ -544,3 +564,29 @@ unconstrained fn test_partial_match() {

assert(position == 123);
}

#[test]
fn test_substring_from_bounded_vec() {
let haystack_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
.as_bytes();
let needle_text = " dolor in reprehenderit in voluptate velit esse".as_bytes();

let mut haystack: StringBody512 = BoundedVec::from(haystack_text).into();
let mut needle: SubString64 = BoundedVec::from(needle_text).into();

let result = haystack.substring_match(needle);
assert(result.0 == true);
}

#[test]
fn test_string_body_from_bounded_vec() {
let haystack_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
.as_bytes();

let mut haystack: StringBody512 = BoundedVec::from(haystack_text).into();
let needle_text = " dolor in reprehenderit in voluptate velit esse".as_bytes();
let mut needle: SubString64 = BoundedVec::from(needle_text).into();

let result = haystack.substring_match(needle);
assert(result.0 == true);
}

0 comments on commit a2d48a2

Please sign in to comment.