Skip to content

Commit

Permalink
Treat newlines as width 0 in the 0.1 stream, publish 0.1.14 (#67)
Browse files Browse the repository at this point in the history
* Treat newlines as width 0

* Publish 0.1.14
  • Loading branch information
Manishearth authored Sep 19, 2024
1 parent 2517d68 commit 9eaafa5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "unicode-width"
version = "0.1.13"
version = "0.1.14"
authors = [
"kwantam <[email protected]>",
"Manish Goregaokar <[email protected]>",
Expand Down
5 changes: 4 additions & 1 deletion scripts/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,10 @@ def lookup_fns(
s += """
if c <= '\\u{A0}' {
match c {
'\\n' => (1, WidthInfo::LINE_FEED),
// According to the spec, LF should be width 1, which is how it is often rendered when it is forced to have a single-line rendering
// However, this makes it harder to use this crate to calculate line breaks, and breaks assumptions of downstream crates.
// https://github.com/unicode-rs/unicode-width/issues/60
'\\n' => (0, WidthInfo::LINE_FEED),
'\\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
_ => (1, WidthInfo::DEFAULT),
}
Expand Down
10 changes: 8 additions & 2 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ fn width_in_str(c: char, mut next_info: WidthInfo) -> (i8, WidthInfo) {
}
if c <= '\u{A0}' {
match c {
'\n' => (1, WidthInfo::LINE_FEED),
// According to the spec, LF should be width 1, which is how it is often rendered when it is forced to have a single-line rendering
// However, this makes it harder to use this crate to calculate line breaks, and breaks assumptions of downstream crates.
// https://github.com/unicode-rs/unicode-width/issues/60
'\n' => (0, WidthInfo::LINE_FEED),
'\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
_ => (1, WidthInfo::DEFAULT),
}
Expand Down Expand Up @@ -507,7 +510,10 @@ fn width_in_str_cjk(c: char, mut next_info: WidthInfo) -> (i8, WidthInfo) {
}
if c <= '\u{A0}' {
match c {
'\n' => (1, WidthInfo::LINE_FEED),
// According to the spec, LF should be width 1, which is how it is often rendered when it is forced to have a single-line rendering
// However, this makes it harder to use this crate to calculate line breaks, and breaks assumptions of downstream crates.
// https://github.com/unicode-rs/unicode-width/issues/60
'\n' => (0, WidthInfo::LINE_FEED),
'\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
_ => (1, WidthInfo::DEFAULT),
}
Expand Down
24 changes: 19 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,23 @@ fn test_control_line_break() {
assert_width!('\r', None, None);
assert_width!('\n', None, None);
assert_width!("\r", 1, 1);
assert_width!("\n", 1, 1);
assert_width!("\r\n", 1, 1);
// This is 0 due to #60
assert_width!("\n", 0, 0);
assert_width!("\r\n", 0, 0);
assert_width!("\0", 1, 1);
assert_width!("1\t2\r\n3\u{85}4", 7, 7);
assert_width!("\r\u{FE0F}\n", 2, 2);
assert_width!("\r\u{200D}\n", 2, 2);
assert_width!("1\t2\r\n3\u{85}4", 6, 6);
assert_width!("\r\u{FE0F}\n", 1, 1);
assert_width!("\r\u{200D}\n", 1, 1);
}

#[test]
fn char_str_consistent() {
let mut s = String::with_capacity(4);
for c in '\0'..=char::MAX {
// Newlines are special cased (#60)
if c == '\n' {
continue;
}
s.clear();
s.push(c);
assert_eq!(c.width().unwrap_or(1), s.width());
Expand Down Expand Up @@ -418,6 +423,10 @@ fn test_khmer_coeng() {
assert_width!(format!("\u{17D2}{c}"), 0, 0);
assert_width!(format!("\u{17D2}\u{200D}\u{200D}{c}"), 0, 0);
} else {
// Newlines are special cased (#60)
if c == '\n' {
continue;
}
assert_width!(
format!("\u{17D2}{c}"),
c.width().unwrap_or(1),
Expand Down Expand Up @@ -588,6 +597,11 @@ fn emoji_test_file() {
}
}

#[test]
fn test_newline_zero_issue_60() {
assert_width!("a\na", 2, 2);
}

// Test traits are unsealed

#[cfg(feature = "cjk")]
Expand Down

0 comments on commit 9eaafa5

Please sign in to comment.