Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat newlines as width 0 in the 0.1 stream, publish 0.1.14 #67

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
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 <kwantam@gmail.com>",
"Manish Goregaokar <manishsmail@gmail.com>",
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