Skip to content

Commit 9eaafa5

Browse files
authored
Treat newlines as width 0 in the 0.1 stream, publish 0.1.14 (#67)
* Treat newlines as width 0 * Publish 0.1.14
1 parent 2517d68 commit 9eaafa5

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "unicode-width"
4-
version = "0.1.13"
4+
version = "0.1.14"
55
authors = [
66
"kwantam <kwantam@gmail.com>",
77
"Manish Goregaokar <manishsmail@gmail.com>",

scripts/unicode.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,10 @@ def lookup_fns(
12811281
s += """
12821282
if c <= '\\u{A0}' {
12831283
match c {
1284-
'\\n' => (1, WidthInfo::LINE_FEED),
1284+
// 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
1285+
// However, this makes it harder to use this crate to calculate line breaks, and breaks assumptions of downstream crates.
1286+
// https://github.com/unicode-rs/unicode-width/issues/60
1287+
'\\n' => (0, WidthInfo::LINE_FEED),
12851288
'\\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
12861289
_ => (1, WidthInfo::DEFAULT),
12871290
}

src/tables.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ fn width_in_str(c: char, mut next_info: WidthInfo) -> (i8, WidthInfo) {
215215
}
216216
if c <= '\u{A0}' {
217217
match c {
218-
'\n' => (1, WidthInfo::LINE_FEED),
218+
// 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
219+
// However, this makes it harder to use this crate to calculate line breaks, and breaks assumptions of downstream crates.
220+
// https://github.com/unicode-rs/unicode-width/issues/60
221+
'\n' => (0, WidthInfo::LINE_FEED),
219222
'\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
220223
_ => (1, WidthInfo::DEFAULT),
221224
}
@@ -507,7 +510,10 @@ fn width_in_str_cjk(c: char, mut next_info: WidthInfo) -> (i8, WidthInfo) {
507510
}
508511
if c <= '\u{A0}' {
509512
match c {
510-
'\n' => (1, WidthInfo::LINE_FEED),
513+
// 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
514+
// However, this makes it harder to use this crate to calculate line breaks, and breaks assumptions of downstream crates.
515+
// https://github.com/unicode-rs/unicode-width/issues/60
516+
'\n' => (0, WidthInfo::LINE_FEED),
511517
'\r' if next_info == WidthInfo::LINE_FEED => (0, WidthInfo::DEFAULT),
512518
_ => (1, WidthInfo::DEFAULT),
513519
}

tests/tests.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,23 @@ fn test_control_line_break() {
214214
assert_width!('\r', None, None);
215215
assert_width!('\n', None, None);
216216
assert_width!("\r", 1, 1);
217-
assert_width!("\n", 1, 1);
218-
assert_width!("\r\n", 1, 1);
217+
// This is 0 due to #60
218+
assert_width!("\n", 0, 0);
219+
assert_width!("\r\n", 0, 0);
219220
assert_width!("\0", 1, 1);
220-
assert_width!("1\t2\r\n3\u{85}4", 7, 7);
221-
assert_width!("\r\u{FE0F}\n", 2, 2);
222-
assert_width!("\r\u{200D}\n", 2, 2);
221+
assert_width!("1\t2\r\n3\u{85}4", 6, 6);
222+
assert_width!("\r\u{FE0F}\n", 1, 1);
223+
assert_width!("\r\u{200D}\n", 1, 1);
223224
}
224225

225226
#[test]
226227
fn char_str_consistent() {
227228
let mut s = String::with_capacity(4);
228229
for c in '\0'..=char::MAX {
230+
// Newlines are special cased (#60)
231+
if c == '\n' {
232+
continue;
233+
}
229234
s.clear();
230235
s.push(c);
231236
assert_eq!(c.width().unwrap_or(1), s.width());
@@ -418,6 +423,10 @@ fn test_khmer_coeng() {
418423
assert_width!(format!("\u{17D2}{c}"), 0, 0);
419424
assert_width!(format!("\u{17D2}\u{200D}\u{200D}{c}"), 0, 0);
420425
} else {
426+
// Newlines are special cased (#60)
427+
if c == '\n' {
428+
continue;
429+
}
421430
assert_width!(
422431
format!("\u{17D2}{c}"),
423432
c.width().unwrap_or(1),
@@ -588,6 +597,11 @@ fn emoji_test_file() {
588597
}
589598
}
590599

600+
#[test]
601+
fn test_newline_zero_issue_60() {
602+
assert_width!("a\na", 2, 2);
603+
}
604+
591605
// Test traits are unsealed
592606

593607
#[cfg(feature = "cjk")]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy