Skip to content
Open
Show file tree
Hide file tree
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
58 changes: 56 additions & 2 deletions src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)?;

Expand Down Expand Up @@ -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()]);

Expand Down Expand Up @@ -1092,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<usize> = (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")]
Expand Down
26 changes: 26 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading