|
1 | 1 | mod utils;
|
2 | 2 |
|
3 | 3 | pub use utils::{conditional_select, DebugRandomEngine, lt_f};
|
| 4 | +use std::collections::bounded_vec::BoundedVec; |
4 | 5 |
|
5 | 6 | /**
|
6 | 7 | * @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>
|
204 | 205 | }
|
205 | 206 | }
|
206 | 207 |
|
| 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 | + |
207 | 214 | // ######################################################
|
208 | 215 | // S T R I N G B O D Y
|
209 | 216 | // ######################################################
|
@@ -410,6 +417,12 @@ impl<let MaxPaddedBytes: u32, let PaddedChunks: u32, let MaxBytes: u32> StringBo
|
410 | 417 | }
|
411 | 418 | }
|
412 | 419 |
|
| 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 | + |
413 | 426 | /// Given an input byte array, convert into 31-byte chunks
|
414 | 427 | ///
|
415 | 428 | /// Cost: ~0.5 gates per byte
|
@@ -546,6 +559,32 @@ unconstrained fn test_partial_match() {
|
546 | 559 | assert(position == 123);
|
547 | 560 | }
|
548 | 561 |
|
| 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 | + |
549 | 588 | #[test]
|
550 | 589 | fn regression_20() {
|
551 | 590 | let haystack: [u8; 128] = [
|
|
0 commit comments