diff --git a/src/lib.nr b/src/lib.nr index 332ba5b..8f2da50 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -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" @@ -204,6 +206,15 @@ impl } } +impl From> for SubString { + fn from(input: BoundedVec) -> Self { + Self::new( + input.storage(), + input.len() as u32 + ) + } +} + // ###################################################### // S T R I N G B O D Y // ###################################################### @@ -409,6 +420,15 @@ impl StringBo } } +impl From> for StringBody { + fn from(input: BoundedVec) -> 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 @@ -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); +}