[Bugfix] [Rust Frontend] Fix stop string truncation with repeated matches (#46113)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Signed-off-by: reidliu41 <reid201711@gmail.com>
This commit is contained in:
Reid
2026-06-22 14:11:29 +08:00
committed by GitHub
co-authored by mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 9037498c22
commit 31124749d1
+8 -1
View File
@@ -309,7 +309,7 @@ fn matches_stop_string(stops: &[String], output: &str, new_bytes: usize) -> Opti
.find_map(|(ss_idx, (ss, len, start_off))| { .find_map(|(ss_idx, (ss, len, start_off))| {
output[start_off..] output[start_off..]
.windows(len) .windows(len)
.rposition(|w| w == ss) .position(|w| w == ss)
.map(|pos| (ss_idx, start_off + pos)) .map(|pos| (ss_idx, start_off + pos))
}) })
} }
@@ -562,6 +562,13 @@ mod tests {
assert_eq!(result, Some((0, 4))); assert_eq!(result, Some((0, 4)));
} }
#[test]
fn stop_string_matches_leftmost_with_multiple_new_bytes() {
let stops = vec!["\n".to_string()];
let result = matches_stop_string(&stops, "Answer\n\n", 2);
assert_eq!(result, Some((0, 6)));
}
#[test] #[test]
fn stop_string_matches_at_beginning() { fn stop_string_matches_at_beginning() {
let stops = vec!["say".to_string()]; let stops = vec!["say".to_string()];