Skip to content

Commit 2354acd

Browse files
authored
Renamed index_codepoints to slice_codepoints and updated to do it cleaner (#67)
* Renamed index_codepoints to slice_codepoints and updated to do it cleaner Signed-off-by: declark1 <daniel.clark@ibm.com> * Update apply_masks to use slice_codepoints Signed-off-by: declark1 <daniel.clark@ibm.com> --------- Signed-off-by: declark1 <daniel.clark@ibm.com>
1 parent 189a8e1 commit 2354acd

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/orchestrator.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ async fn handle_detection_task(
361361
.filter_map(|resp| {
362362
let mut result: TokenClassificationResult = resp.into();
363363
result.word =
364-
index_codepoints(&chunk.text, result.start as usize, result.end as usize);
364+
slice_codepoints(&chunk.text, result.start as usize, result.end as usize);
365365
result.start += chunk.offset as u32;
366366
result.end += chunk.offset as u32;
367367
(result.score >= threshold).then_some(result)
@@ -579,19 +579,18 @@ async fn generate(
579579
}
580580
}
581581

582-
/// Get codepoints of text between start and end indices
583-
fn index_codepoints(text: &str, start: usize, end: usize) -> String {
584-
let chars = text.chars().collect::<Vec<_>>();
585-
chars[start..end].iter().collect()
582+
/// Slices chars between start and end indices.
583+
fn slice_codepoints(text: &str, start: usize, end: usize) -> String {
584+
let len = end - start;
585+
text.chars().skip(start).take(len).collect()
586586
}
587587

588588
/// Applies masks to input text, returning (offset, masked_text) pairs.
589589
fn apply_masks(text: &str, masks: &[(usize, usize)]) -> Vec<(usize, String)> {
590-
let chars = text.chars().collect::<Vec<_>>();
591590
masks
592591
.iter()
593592
.map(|(start, end)| {
594-
let masked_text = chars[*start..*end].iter().cloned().collect();
593+
let masked_text = slice_codepoints(text, *start, *end);
595594
(*start, masked_text)
596595
})
597596
.collect()
@@ -704,10 +703,10 @@ mod tests {
704703
}
705704

706705
#[test]
707-
fn test_index_codepoints() {
706+
fn test_slice_codepoints() {
708707
let s = "Hello world";
709-
assert_eq!(index_codepoints(s, 0, 5), "Hello");
708+
assert_eq!(slice_codepoints(s, 0, 5), "Hello");
710709
let s = "哈囉世界";
711-
assert_eq!(index_codepoints(s, 3, 4), "界");
710+
assert_eq!(slice_codepoints(s, 3, 4), "界");
712711
}
713712
}

0 commit comments

Comments
 (0)