Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions crates/graphicle-memory/src/standing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,13 @@ pub fn openings(text: &str) -> Vec<(usize, usize)> {
}
let mut b = b;
if b - a > 300 {
b = a + text[a..a + 300].rfind(' ').unwrap_or(300);
while !text.is_char_boundary(b) {
b -= 1;
// The first 300 bytes, back to a character boundary: byte 300
// can fall inside a multi-byte character.
let mut cut = a + 300;
while !text.is_char_boundary(cut) {
cut -= 1;
}
b = text[a..cut].rfind(' ').map_or(cut, |space| a + space);
}
found.push((a, b));
}
Expand Down Expand Up @@ -387,6 +390,23 @@ mod tests {
);
}

#[test]
fn a_long_opening_ends_on_a_character_boundary() {
// Byte 300 of the opening falls inside an "é" (two bytes).
let text = format!("User: I need help with {}\n", "\u{e9}".repeat(200));
let spans = openings(&text);
assert_eq!(spans.len(), 1, "{spans:?}");
let (a, b) = spans[0];
assert_eq!(&text[a..b], "I need help with");
// Without a space in the first 300 bytes the cut is at the last
// whole character.
let text = format!("User: x{} b c d\n", "\u{4f60}".repeat(120));
let spans = openings(&text);
assert_eq!(spans.len(), 1, "{spans:?}");
let (a, b) = spans[0];
assert_eq!(text[a..b], format!("x{}", "\u{4f60}".repeat(99)));
}

#[test]
fn conflicts_pair_a_denial_with_the_users_own_statements_on_its_subject() {
let early = "Conversation date: 2024/03/10 (Sun) 00:00\n\n\
Expand Down