From 87942e01d2a650dd94a55f0acbe0f8478376710d Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:32:41 +0500 Subject: [PATCH 1/2] sed: don't count empty matches inside a UTF-8 character Closes #644 Co-Authored-By: Claude Opus 5.5 --- src/sed/processor.rs | 35 +++++++++++++++++++++++++++++++++-- tests/by-util/test_sed.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/sed/processor.rs b/src/sed/processor.rs index b6831169..26d6f8b5 100644 --- a/src/sed/processor.rs +++ b/src/sed/processor.rs @@ -318,6 +318,31 @@ fn execute_pattern_as_shell_command( Ok(()) } +/// Whether byte offset `pos` of `text` falls inside a valid multi-byte UTF-8 +/// character, rather than on a character boundary. +fn is_inside_utf8_char(text: &[u8], pos: usize) -> bool { + let is_continuation = |b: u8| (0x80..=0xbf).contains(&b); + if pos >= text.len() || !is_continuation(text[pos]) { + return false; + } + let Some(start) = (1..=3.min(pos)) + .map(|back| pos - back) + .find(|&i| !is_continuation(text[i])) + else { + return false; + }; + let len = match text[start] { + 0xc2..=0xdf => 2, + 0xe0..=0xef => 3, + 0xf0..=0xf4 => 4, + _ => return false, + }; + pos < start + len + && text + .get(start..start + len) + .is_some_and(|c| std::str::from_utf8(c).is_ok()) +} + /// Perform the specified RE replacement in the provided pattern space. fn substitute( pattern: &mut IOChunk, @@ -332,6 +357,7 @@ fn substitute( let mut result = Vec::new(); let mut replaced = false; let text = pattern.as_bytes(); + let utf8 = context.character_mode == CharacterMode::Utf8; let regex = re_or_saved_re(sub.regex.as_ref(), context, &command.location)?; @@ -383,10 +409,15 @@ fn substitute( Ok(caps) => caps, Err(e) => break 'captures Err(e), }; - count += 1; - let m = caps.get(0)?.unwrap(); + // The byte engine also reports empty matches between the + // bytes of a character; in a UTF-8 locale they don't count. + if utf8 && m.start() == m.end() && is_inside_utf8_char(text, m.start()) { + continue; + } + count += 1; + // Always write the unmatched text before this match. result.extend_from_slice(&text[last_end..m.start()]); diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index e7ad2ef0..3aa4c9b0 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -4403,3 +4403,29 @@ fn test_lone_lead_byte_delimiter() { .succeeds() .stdout_only("y\n"); } + +#[test] +fn test_empty_match_respects_utf8_boundaries() { + // `é` is two bytes; the empty matches of `[0-9]*` are before and after + // it, never between its bytes. + for (script, expected) in [ + ("s/[0-9]*/N/g", "N\u{e9}N\n"), + ("s/[0-9]*/N/2", "\u{e9}N\n"), + ("s/[0-9]*/N/3", "\u{e9}\n"), + ] { + new_ucmd!() + .env("LC_ALL", "C.UTF-8") + .arg(script) + .pipe_in("\u{e9}\n") + .succeeds() + .stdout_is(expected); + } + + // In the C locale each byte is a character, so both gaps count. + new_ucmd!() + .env("LC_ALL", "C") + .arg("s/[0-9]*/N/g") + .pipe_in("\u{e9}\n") + .succeeds() + .stdout_is_bytes(b"N\xc3N\xa9N\n"); +} From a91b7296de26221265992a762260bfe28ea21ca7 Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:39:55 +0500 Subject: [PATCH 2/2] sed: unit-test is_inside_utf8_char Co-Authored-By: Claude Opus 5.5 --- src/sed/processor.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/sed/processor.rs b/src/sed/processor.rs index 26d6f8b5..7df9a615 100644 --- a/src/sed/processor.rs +++ b/src/sed/processor.rs @@ -1123,6 +1123,29 @@ mod tests { use std::io::{Read, Seek, SeekFrom}; use tempfile::tempfile; + #[test] + fn test_is_inside_utf8_char() { + // `é` (2 bytes), `€` (3 bytes), `😀` (4 bytes) + let text = "a\u{e9}\u{20ac}\u{1f600}".as_bytes(); + let inside: Vec = (0..=text.len()) + .filter(|&pos| is_inside_utf8_char(text, pos)) + .collect(); + assert_eq!(inside, [2, 4, 5, 7, 8, 9]); + + // A continuation byte with no lead byte is a character of its own. + assert!(!is_inside_utf8_char(b"a\xa9", 1)); + // So is one after a byte that never starts a sequence. + assert!(!is_inside_utf8_char(b"\xc0\xa9", 1)); + assert!(!is_inside_utf8_char(b"\xff\xa9", 1)); + // A lead byte whose sequence is cut short or malformed is not a character. + assert!(!is_inside_utf8_char(b"\xe2\x82", 1)); + assert!(!is_inside_utf8_char(b"\xe0\x80\x80", 1)); + // Past the lead byte's sequence the offset is a boundary again. + assert!(!is_inside_utf8_char(b"\xc3\xa9\xa9", 2)); + // More continuation bytes than any sequence has. + assert!(!is_inside_utf8_char(b"a\x80\x80\x80\x80", 4)); + } + #[rstest] #[case::named_escape_newline(b'\n', r"\n")] #[case::named_escape_tab(b'\t', r"\t")]