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

Publish 0.2.0 with newlines treated as width 1 #68

Merged
merged 3 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.14"
version = "0.2.0"
authors = [
"kwantam <kwantam@gmail.com>",
"Manish Goregaokar <manishsmail@gmail.com>",
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ to your `Cargo.toml`:
[dependencies]
unicode-width = "0.1.11"
```


## Changelog


### 0.2.0

- Treat `\n` as width 1 (#60)
- Treat ambiguous `Modifier_Letter`s as narrow (#63)
- Support `Grapheme_Cluster_Break=Prepend` (#62)
- Support lots of ligatures (#53)

Note: If you are using `unicode-width` for linebreaking, the change treating `\n` as width 1 _may cause behavior changes_. It is recommended that in such cases you feed already-line segmented text to `unicode-width`. In other words, please apply higher level control character based line breaking protocols before feeding text to `unicode-width`. Relying on any character producing a stable width in this crate is likely the sign of a bug.
5 changes: 1 addition & 4 deletions scripts/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,10 +1281,7 @@ def lookup_fns(
s += """
if c <= '\\u{A0}' {
match c {
// 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),
'\\n' => (1, WidthInfo::LINE_FEED),
'\\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
_ => (1, WidthInfo::DEFAULT),
}
Expand Down
10 changes: 2 additions & 8 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ fn width_in_str(c: char, mut next_info: WidthInfo) -> (i8, WidthInfo) {
}
if c <= '\u{A0}' {
match c {
// 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),
'\n' => (1, WidthInfo::LINE_FEED),
'\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
_ => (1, WidthInfo::DEFAULT),
}
Expand Down Expand Up @@ -510,10 +507,7 @@ fn width_in_str_cjk(c: char, mut next_info: WidthInfo) -> (i8, WidthInfo) {
}
if c <= '\u{A0}' {
match c {
// 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),
'\n' => (1, WidthInfo::LINE_FEED),
'\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
_ => (1, WidthInfo::DEFAULT),
}
Expand Down
24 changes: 5 additions & 19 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,18 @@ fn test_control_line_break() {
assert_width!('\r', None, None);
assert_width!('\n', None, None);
assert_width!("\r", 1, 1);
// This is 0 due to #60
assert_width!("\n", 0, 0);
assert_width!("\r\n", 0, 0);
assert_width!("\n", 1, 1);
assert_width!("\r\n", 1, 1);
assert_width!("\0", 1, 1);
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);
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);
}

#[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 @@ -423,10 +418,6 @@ 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 @@ -597,11 +588,6 @@ 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