Skip to content

Commit b476bd2

Browse files
committed
Make the width function of char return u32
The width of a Unicode codepoint is not related to some in-memory buffer.
1 parent cf51e55 commit b476bd2

File tree

8 files changed

+15
-15
lines changed

8 files changed

+15
-15
lines changed

src/etc/unicode.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,13 @@ def emit_charwidth_module(f, width_table):
457457
""")
458458

459459
f.write("""
460-
pub fn width(c: char, is_cjk: bool) -> Option<usize> {
461-
match c as usize {
460+
pub fn width(c: char, is_cjk: bool) -> Option<u32> {
461+
match c as u32 {
462462
_c @ 0 => Some(0), // null is zero width
463463
cu if cu < 0x20 => None, // control sequences have no width
464464
cu if cu < 0x7F => Some(1), // ASCII
465465
cu if cu < 0xA0 => None, // more control sequences
466-
_ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as usize)
466+
_ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as u32)
467467
}
468468
}
469469

src/libcollections/str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ impl str {
14561456
/// characters be treated as 1 column (i.e., `is_cjk = false`) if the locale is unknown.
14571457
#[unstable(feature = "unicode",
14581458
reason = "this functionality may only be provided by libunicode")]
1459-
pub fn width(&self, is_cjk: bool) -> usize {
1459+
pub fn width(&self, is_cjk: bool) -> u32 {
14601460
UnicodeStr::width(&self[..], is_cjk)
14611461
}
14621462

src/libcore/char.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ enum EscapeUnicodeState {
286286
Backslash,
287287
Type,
288288
LeftBrace,
289-
Value(usize),
289+
Value(u8),
290290
RightBrace,
291291
Done,
292292
}

src/librustc_driver/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Available lint options:
576576
.map(|&s| s.name.width(true))
577577
.max().unwrap_or(0);
578578
let padded = |x: &str| {
579-
let mut s = repeat(" ").take(max_name_len - x.chars().count())
579+
let mut s = repeat(" ").take((max_name_len - x.width(true)) as usize)
580580
.collect::<String>();
581581
s.push_str(x);
582582
s
@@ -603,7 +603,7 @@ Available lint options:
603603
.map(|&(s, _)| s.width(true))
604604
.max().unwrap_or(0);
605605
let padded = |x: &str| {
606-
let mut s = repeat(" ").take(max_name_len - x.chars().count())
606+
let mut s = repeat(" ").take((max_name_len - x.width(true)) as usize)
607607
.collect::<String>();
608608
s.push_str(x);
609609
s

src/libsyntax/diagnostic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ fn highlight_lines(err: &mut EmitterWriter,
545545
_ => lastc.width(false).unwrap_or(0),
546546
};
547547
col += count;
548-
s.extend(::std::iter::repeat('~').take(count));
548+
s.extend(::std::iter::repeat('~').take(count as usize));
549549

550550
let hi = cm.lookup_char_pos(sp.hi);
551551
if hi.col != lo.col {
@@ -556,7 +556,7 @@ fn highlight_lines(err: &mut EmitterWriter,
556556
_ => ch.width(false).unwrap_or(0),
557557
};
558558
col += count;
559-
s.extend(::std::iter::repeat('~').take(count));
559+
s.extend(::std::iter::repeat('~').take(count as usize));
560560
}
561561
}
562562

src/libunicode/char.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,5 +447,5 @@ impl char {
447447
/// `is_cjk` = `false`) if the context cannot be reliably determined.
448448
#[unstable(feature = "unicode",
449449
reason = "needs expert opinion. is_cjk flag stands out as ugly")]
450-
pub fn width(self, is_cjk: bool) -> Option<usize> { charwidth::width(self, is_cjk) }
450+
pub fn width(self, is_cjk: bool) -> Option<u32> { charwidth::width(self, is_cjk) }
451451
}

src/libunicode/tables.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7603,13 +7603,13 @@ pub mod charwidth {
76037603
}
76047604
}
76057605

7606-
pub fn width(c: char, is_cjk: bool) -> Option<usize> {
7607-
match c as usize {
7606+
pub fn width(c: char, is_cjk: bool) -> Option<u32> {
7607+
match c as u32 {
76087608
_c @ 0 => Some(0), // null is zero width
76097609
cu if cu < 0x20 => None, // control sequences have no width
76107610
cu if cu < 0x7F => Some(1), // ASCII
76117611
cu if cu < 0xA0 => None, // more control sequences
7612-
_ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as usize)
7612+
_ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as u32)
76137613
}
76147614
}
76157615

src/libunicode/u_str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub trait UnicodeStr {
4141
fn words<'a>(&'a self) -> Words<'a>;
4242
fn is_whitespace(&self) -> bool;
4343
fn is_alphanumeric(&self) -> bool;
44-
fn width(&self, is_cjk: bool) -> usize;
44+
fn width(&self, is_cjk: bool) -> u32;
4545
fn trim<'a>(&'a self) -> &'a str;
4646
fn trim_left<'a>(&'a self) -> &'a str;
4747
fn trim_right<'a>(&'a self) -> &'a str;
@@ -76,7 +76,7 @@ impl UnicodeStr for str {
7676
fn is_alphanumeric(&self) -> bool { self.chars().all(|c| c.is_alphanumeric()) }
7777

7878
#[inline]
79-
fn width(&self, is_cjk: bool) -> usize {
79+
fn width(&self, is_cjk: bool) -> u32 {
8080
self.chars().map(|c| c.width(is_cjk).unwrap_or(0)).sum()
8181
}
8282

0 commit comments

Comments
 (0)