Skip to content

Commit 177bcc3

Browse files
committed
add MAX_LEN_UTF8 and MAX_LEN_UTF16 constants
1 parent bf3c6c5 commit 177bcc3

File tree

14 files changed

+48
-17
lines changed

14 files changed

+48
-17
lines changed

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
#![feature(ascii_char)]
108108
#![feature(assert_matches)]
109109
#![feature(async_iterator)]
110+
#![feature(char_max_len)]
110111
#![feature(coerce_unsized)]
111112
#![feature(const_align_of_val)]
112113
#![feature(const_box)]

library/alloc/src/string.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
4343
#![stable(feature = "rust1", since = "1.0.0")]
4444

45+
use core::char::MAX_LEN_UTF8;
4546
use core::error::Error;
4647
use core::fmt;
4748
use core::hash;
@@ -1342,9 +1343,10 @@ impl String {
13421343
#[inline]
13431344
#[stable(feature = "rust1", since = "1.0.0")]
13441345
pub fn push(&mut self, ch: char) {
1346+
13451347
match ch.len_utf8() {
13461348
1 => self.vec.push(ch as u8),
1347-
_ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()),
1349+
_ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; MAX_LEN_UTF8]).as_bytes()),
13481350
}
13491351
}
13501352

@@ -1641,7 +1643,7 @@ impl String {
16411643
#[stable(feature = "rust1", since = "1.0.0")]
16421644
pub fn insert(&mut self, idx: usize, ch: char) {
16431645
assert!(self.is_char_boundary(idx));
1644-
let mut bits = [0; 4];
1646+
let mut bits = [0; MAX_LEN_UTF8];
16451647
let bits = ch.encode_utf8(&mut bits).as_bytes();
16461648

16471649
unsafe {
@@ -2629,7 +2631,7 @@ impl ToString for core::ascii::Char {
26292631
impl ToString for char {
26302632
#[inline]
26312633
fn to_string(&self) -> String {
2632-
String::from(self.encode_utf8(&mut [0; 4]))
2634+
String::from(self.encode_utf8(&mut [0; MAX_LEN_UTF8]))
26332635
}
26342636
}
26352637

library/alloc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(iter_array_chunks)]
44
#![feature(assert_matches)]
55
#![feature(btree_extract_if)]
6+
#![feature(char_max_len)]
67
#![feature(cow_is_borrowed)]
78
#![feature(const_cow_is_borrowed)]
89
#![feature(const_heap)]

library/alloc/tests/str.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::assert_matches::assert_matches;
44
use std::borrow::Cow;
5+
use std::char::MAX_LEN_UTF8;
56
use std::cmp::Ordering::{Equal, Greater, Less};
67
use std::str::{from_utf8, from_utf8_unchecked};
78

@@ -1230,7 +1231,7 @@ fn test_to_uppercase_rev_iterator() {
12301231
#[test]
12311232
#[cfg_attr(miri, ignore)] // Miri is too slow
12321233
fn test_chars_decoding() {
1233-
let mut bytes = [0; 4];
1234+
let mut bytes = [0; MAX_LEN_UTF8];
12341235
for c in (0..0x110000).filter_map(std::char::from_u32) {
12351236
let s = c.encode_utf8(&mut bytes);
12361237
if Some(c) != s.chars().next() {
@@ -1242,7 +1243,7 @@ fn test_chars_decoding() {
12421243
#[test]
12431244
#[cfg_attr(miri, ignore)] // Miri is too slow
12441245
fn test_chars_rev_decoding() {
1245-
let mut bytes = [0; 4];
1246+
let mut bytes = [0; MAX_LEN_UTF8];
12461247
for c in (0..0x110000).filter_map(std::char::from_u32) {
12471248
let s = c.encode_utf8(&mut bytes);
12481249
if Some(c) != s.chars().rev().next() {

library/core/src/char/methods.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ impl char {
7474
#[stable(feature = "assoc_char_consts", since = "1.52.0")]
7575
pub const MAX: char = '\u{10ffff}';
7676

77+
/// The maximum number of bytes required to [encode](char::encode_utf8) a `char` to
78+
/// UTF-8 encoding.
79+
#[unstable(feature = "char_max_len", issue = "none")]
80+
pub const MAX_LEN_UTF8: usize = 4;
81+
82+
/// The maximum number of two-byte units required to [encode](char::encode_utf16) a `char`
83+
/// to UTF-16 encoding.
84+
#[unstable(feature = "char_max_len", issue = "none")]
85+
pub const MAX_LEN_UTF16: usize = 2;
86+
7787
/// `U+FFFD REPLACEMENT CHARACTER` (�) is used in Unicode to represent a
7888
/// decoding error.
7989
///

library/core/src/char/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ const MAX_THREE_B: u32 = 0x10000;
9393
#[stable(feature = "rust1", since = "1.0.0")]
9494
pub const MAX: char = char::MAX;
9595

96+
/// The maximum number of bytes required to [encode](char::encode_utf8) a `char` to
97+
/// UTF-8 encoding.
98+
#[unstable(feature = "char_max_len", issue = "none")]
99+
pub const MAX_LEN_UTF8: usize = char::MAX_LEN_UTF8;
100+
101+
/// The maximum number of two-byte units required to [encode](char::encode_utf16) a `char`
102+
/// to UTF-16 encoding.
103+
#[unstable(feature = "char_max_len", issue = "none")]
104+
pub const MAX_LEN_UTF16: usize = char::MAX_LEN_UTF16;
105+
96106
/// `U+FFFD REPLACEMENT CHARACTER` (�) is used in Unicode to represent a
97107
/// decoding error. Use [`char::REPLACEMENT_CHARACTER`] instead.
98108
#[stable(feature = "decode_utf16", since = "1.9.0")]

library/core/src/fmt/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![stable(feature = "rust1", since = "1.0.0")]
44

55
use crate::cell::{Cell, Ref, RefCell, RefMut, SyncUnsafeCell, UnsafeCell};
6-
use crate::char::EscapeDebugExtArgs;
6+
use crate::char::{EscapeDebugExtArgs, MAX_LEN_UTF8};
77
use crate::iter;
88
use crate::marker::PhantomData;
99
use crate::mem;
@@ -164,7 +164,7 @@ pub trait Write {
164164
/// ```
165165
#[stable(feature = "fmt_write_char", since = "1.1.0")]
166166
fn write_char(&mut self, c: char) -> Result {
167-
self.write_str(c.encode_utf8(&mut [0; 4]))
167+
self.write_str(c.encode_utf8(&mut [0; MAX_LEN_UTF8]))
168168
}
169169

170170
/// Glue for usage of the [`write!`] macro with implementors of this trait.
@@ -2387,7 +2387,7 @@ impl Display for char {
23872387
if f.width.is_none() && f.precision.is_none() {
23882388
f.write_char(*self)
23892389
} else {
2390-
f.pad(self.encode_utf8(&mut [0; 4]))
2390+
f.pad(self.encode_utf8(&mut [0; MAX_LEN_UTF8]))
23912391
}
23922392
}
23932393
}

library/core/src/str/pattern.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
issue = "27721"
3939
)]
4040

41+
use crate::char::MAX_LEN_UTF8;
4142
use crate::cmp;
4243
use crate::cmp::Ordering;
4344
use crate::fmt;
@@ -541,7 +542,7 @@ impl<'a> Pattern<'a> for char {
541542

542543
#[inline]
543544
fn into_searcher(self, haystack: &'a str) -> Self::Searcher {
544-
let mut utf8_encoded = [0; 4];
545+
let mut utf8_encoded = [0; MAX_LEN_UTF8];
545546
let utf8_size = self.encode_utf8(&mut utf8_encoded).len();
546547
CharSearcher {
547548
haystack,

library/core/tests/char.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::char::MAX_LEN_UTF8;
12
use std::str::FromStr;
23
use std::{char, str};
34

@@ -259,7 +260,7 @@ fn test_escape_unicode() {
259260
#[test]
260261
fn test_encode_utf8() {
261262
fn check(input: char, expect: &[u8]) {
262-
let mut buf = [0; 4];
263+
let mut buf = [0; MAX_LEN_UTF8];
263264
let ptr = buf.as_ptr();
264265
let s = input.encode_utf8(&mut buf);
265266
assert_eq!(s.as_ptr() as usize, ptr as usize);

library/core/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(async_iterator)]
88
#![feature(bigint_helper_methods)]
99
#![feature(cell_update)]
10+
#![feature(char_max_len)]
1011
#![feature(const_align_offset)]
1112
#![feature(const_align_of_val_raw)]
1213
#![feature(const_black_box)]

0 commit comments

Comments
 (0)