Skip to content

Commit 6cda9b8

Browse files
authored
chore: simplify slice_string implementation (#66)
1 parent c1e9bdf commit 6cda9b8

File tree

2 files changed

+24
-81
lines changed

2 files changed

+24
-81
lines changed

src/_string_tools/string_chopper.nr

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
use crate::_string_tools::slice_packed_field::slice_fields;
22

3-
pub(crate) struct StringChopper<let NeedlePackedFields: u32> {}
3+
pub(crate) fn slice_string<let StringBytes: u32, let HaystackPackedFields: u32>(
4+
haystack: [Field; HaystackPackedFields],
5+
start_bytes: Field,
6+
num_bytes: Field,
7+
) -> [u8; StringBytes] {
8+
// We want to round up the quotient of StringBytes to the next multiple of 31
9+
let sliced: [Field; (StringBytes + 30) / 31] = slice_fields(haystack, start_bytes, num_bytes);
410

5-
impl<let NeedlePackedFields: u32> StringChopper<NeedlePackedFields> {
6-
pub(crate) fn slice_string<let StringBytes: u32, let HaystackPackedFields: u32>(
7-
_: Self,
8-
haystack: [Field; HaystackPackedFields],
9-
start_bytes: Field,
10-
num_bytes: Field,
11-
) -> [u8; StringBytes] {
12-
let mut parsed_string: [u8; StringBytes] = [0; StringBytes];
11+
let num_slices = StringBytes / 31;
12+
let overflow = StringBytes % 31;
1313

14-
let sliced: [Field; NeedlePackedFields] = slice_fields(haystack, start_bytes, num_bytes);
15-
16-
let sliced_bytes = sliced.map(|x: Field| {
17-
let r: [u8; 31] = x.to_be_bytes();
18-
r
19-
});
20-
21-
let num_slices = StringBytes / 31;
22-
let overflow = StringBytes % 31;
23-
for i in 0..num_slices {
24-
for j in 0..31 {
25-
parsed_string[i * 31 + j] = sliced_bytes[i][j];
26-
}
27-
}
28-
for j in 0..overflow {
29-
parsed_string[num_slices * 31 + j] = sliced_bytes[num_slices][j];
14+
let mut parsed_string: [u8; StringBytes] = [0; StringBytes];
15+
for i in 0..num_slices {
16+
let sliced_bytes: [u8; 31] = sliced[i].to_be_bytes();
17+
for j in 0..31 {
18+
parsed_string[i * 31 + j] = sliced_bytes[j];
3019
}
31-
parsed_string
3220
}
21+
22+
let sliced_bytes: [u8; 31] = sliced[num_slices].to_be_bytes();
23+
for j in 0..overflow {
24+
parsed_string[num_slices * 31 + j] = sliced_bytes[j];
25+
}
26+
parsed_string
3327
}

src/getters.nr

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::_comparison_tools::lt::{assert_gt_240_bit, assert_lt_240_bit, lt_field_16_bit};
2-
use crate::_string_tools::string_chopper::StringChopper;
2+
use crate::_string_tools::string_chopper::slice_string;
33
use crate::enums::Layer::ARRAY_LAYER;
44
use crate::json::JSON;
55
use crate::json_entry::JSONEntry;
@@ -179,60 +179,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
179179
"get_string, string size is larger than StringBytes",
180180
);
181181

182-
let mut result: [u8; StringBytes] = [0; StringBytes];
183-
if (StringBytes <= 31) {
184-
let s: StringChopper<1> = StringChopper {};
185-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
186-
} else if (StringBytes <= 62) {
187-
let s: StringChopper<2> = StringChopper {};
188-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
189-
} else if (StringBytes <= 93) {
190-
let s: StringChopper<3> = StringChopper {};
191-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
192-
} else if (StringBytes <= 124) {
193-
let s: StringChopper<4> = StringChopper {};
194-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
195-
} else if (StringBytes <= 155) {
196-
let s: StringChopper<5> = StringChopper {};
197-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
198-
} else if (StringBytes <= 186) {
199-
let s: StringChopper<6> = StringChopper {};
200-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
201-
} else if (StringBytes <= 217) {
202-
let s: StringChopper<7> = StringChopper {};
203-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
204-
} else if (StringBytes <= 248) // 8
205-
{
206-
let s: StringChopper<8> = StringChopper {};
207-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
208-
} else if (StringBytes <= 496) // 16
209-
{
210-
let s: StringChopper<16> = StringChopper {};
211-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
212-
} else if (StringBytes <= 992) // 32
213-
{
214-
let s: StringChopper<32> = StringChopper {};
215-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
216-
} else if (StringBytes <= 1984) {
217-
let s: StringChopper<64> = StringChopper {};
218-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
219-
} else if (StringBytes <= 3968) {
220-
let s: StringChopper<128> = StringChopper {};
221-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
222-
} else if (StringBytes <= 7936) {
223-
let s: StringChopper<256> = StringChopper {};
224-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
225-
} else if (StringBytes <= 15872) {
226-
let s: StringChopper<512> = StringChopper {};
227-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
228-
} else if (StringBytes <= 31774) {
229-
let s: StringChopper<1024> = StringChopper {};
230-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
231-
} else {
232-
// max 16 bits = 65535 = 2115 31-byte slices
233-
let s: StringChopper<2115> = StringChopper {};
234-
result = s.slice_string(self.json_packed, entry.json_pointer, entry.json_length);
235-
}
182+
let result: [u8; StringBytes] =
183+
slice_string(self.json_packed, entry.json_pointer, entry.json_length);
184+
236185
result
237186
}
238187

0 commit comments

Comments
 (0)