Skip to content

Commit 0758014

Browse files
jtriley2pkashbrti
andauthored
feat: Added BoundedVec type conversion (#14)
Co-authored-by: kashbrti <[email protected]>
1 parent fba4ab0 commit 0758014

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/lib.nr

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod utils;
22

33
pub use utils::{conditional_select, DebugRandomEngine, lt_f};
4+
use std::collections::bounded_vec::BoundedVec;
45

56
/**
67
* @brief represents a byte-array of up to MaxBytes, that is used as a "haystack" array,
@@ -204,6 +205,12 @@ impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32>
204205
}
205206
}
206207

208+
impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32> From<BoundedVec<u8, MaxBytes>> for SubString<MaxPaddedBytes, PaddedChunksMinusOne, MaxBytes> {
209+
fn from(input: BoundedVec<u8, MaxBytes>) -> Self {
210+
Self::new(input.storage(), input.len() as u32)
211+
}
212+
}
213+
207214
// ######################################################
208215
// S T R I N G B O D Y
209216
// ######################################################
@@ -410,6 +417,12 @@ impl<let MaxPaddedBytes: u32, let PaddedChunks: u32, let MaxBytes: u32> StringBo
410417
}
411418
}
412419

420+
impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32> From<BoundedVec<u8, MaxBytes>> for StringBody<MaxPaddedBytes, PaddedChunksMinusOne, MaxBytes> {
421+
fn from(input: BoundedVec<u8, MaxBytes>) -> Self {
422+
Self::new(input.storage(), input.len() as u32)
423+
}
424+
}
425+
413426
/// Given an input byte array, convert into 31-byte chunks
414427
///
415428
/// Cost: ~0.5 gates per byte
@@ -546,6 +559,32 @@ unconstrained fn test_partial_match() {
546559
assert(position == 123);
547560
}
548561

562+
#[test]
563+
fn test_substring_from_bounded_vec() {
564+
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."
565+
.as_bytes();
566+
let needle_text = " dolor in reprehenderit in voluptate velit esse".as_bytes();
567+
568+
let mut haystack: StringBody512 = BoundedVec::from(haystack_text).into();
569+
let mut needle: SubString64 = BoundedVec::from(needle_text).into();
570+
571+
let result = haystack.substring_match(needle);
572+
assert(result.0 == true);
573+
}
574+
575+
#[test]
576+
fn test_string_body_from_bounded_vec() {
577+
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."
578+
.as_bytes();
579+
580+
let mut haystack: StringBody512 = BoundedVec::from(haystack_text).into();
581+
let needle_text = " dolor in reprehenderit in voluptate velit esse".as_bytes();
582+
let mut needle: SubString64 = BoundedVec::from(needle_text).into();
583+
584+
let result = haystack.substring_match(needle);
585+
assert(result.0 == true);
586+
}
587+
549588
#[test]
550589
fn regression_20() {
551590
let haystack: [u8; 128] = [

0 commit comments

Comments
 (0)