Skip to content

Commit 8c1af11

Browse files
committed
add MAX_LEN_UTF8 and MAX_LEN_UTF16 constants
1 parent a5db378 commit 8c1af11

File tree

14 files changed

+65
-21
lines changed

14 files changed

+65
-21
lines changed

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#![feature(box_uninit_write)]
106106
#![feature(bstr)]
107107
#![feature(bstr_internals)]
108+
#![feature(char_max_len)]
108109
#![feature(clone_to_uninit)]
109110
#![feature(coerce_unsized)]
110111
#![feature(const_eval_select)]

library/alloc/src/string.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,9 @@ impl String {
14191419
pub fn push(&mut self, ch: char) {
14201420
match ch.len_utf8() {
14211421
1 => self.vec.push(ch as u8),
1422-
_ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()),
1422+
_ => {
1423+
self.vec.extend_from_slice(ch.encode_utf8(&mut [0; char::MAX_LEN_UTF8]).as_bytes())
1424+
}
14231425
}
14241426
}
14251427

@@ -1716,7 +1718,7 @@ impl String {
17161718
#[rustc_confusables("set")]
17171719
pub fn insert(&mut self, idx: usize, ch: char) {
17181720
assert!(self.is_char_boundary(idx));
1719-
let mut bits = [0; 4];
1721+
let mut bits = [0; char::MAX_LEN_UTF8];
17201722
let bits = ch.encode_utf8(&mut bits).as_bytes();
17211723

17221724
unsafe {
@@ -2771,7 +2773,7 @@ impl SpecToString for core::ascii::Char {
27712773
impl SpecToString for char {
27722774
#[inline]
27732775
fn spec_to_string(&self) -> String {
2774-
String::from(self.encode_utf8(&mut [0; 4]))
2776+
String::from(self.encode_utf8(&mut [0; char::MAX_LEN_UTF8]))
27752777
}
27762778
}
27772779

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(core_intrinsics)]
89
#![feature(downcast_unchecked)]

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

@@ -1231,7 +1232,7 @@ fn test_to_uppercase_rev_iterator() {
12311232
#[test]
12321233
#[cfg_attr(miri, ignore)] // Miri is too slow
12331234
fn test_chars_decoding() {
1234-
let mut bytes = [0; 4];
1235+
let mut bytes = [0; MAX_LEN_UTF8];
12351236
for c in (0..0x110000).filter_map(std::char::from_u32) {
12361237
let s = c.encode_utf8(&mut bytes);
12371238
if Some(c) != s.chars().next() {
@@ -1243,7 +1244,7 @@ fn test_chars_decoding() {
12431244
#[test]
12441245
#[cfg_attr(miri, ignore)] // Miri is too slow
12451246
fn test_chars_rev_decoding() {
1246-
let mut bytes = [0; 4];
1247+
let mut bytes = [0; MAX_LEN_UTF8];
12471248
for c in (0..0x110000).filter_map(std::char::from_u32) {
12481249
let s = c.encode_utf8(&mut bytes);
12491250
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
@@ -71,6 +71,16 @@ impl char {
7171
#[stable(feature = "assoc_char_consts", since = "1.52.0")]
7272
pub const MAX: char = '\u{10FFFF}';
7373

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

library/core/src/char/mod.rs

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

98+
/// The maximum number of bytes required to [encode](char::encode_utf8) a `char` to
99+
/// UTF-8 encoding.
100+
#[unstable(feature = "char_max_len", issue = "121714")]
101+
pub const MAX_LEN_UTF8: usize = char::MAX_LEN_UTF8;
102+
103+
/// The maximum number of two-byte units required to [encode](char::encode_utf16) a `char`
104+
/// to UTF-16 encoding.
105+
#[unstable(feature = "char_max_len", issue = "121714")]
106+
pub const MAX_LEN_UTF16: usize = char::MAX_LEN_UTF16;
107+
98108
/// `U+FFFD REPLACEMENT CHARACTER` (�) is used in Unicode to represent a
99109
/// decoding error. Use [`char::REPLACEMENT_CHARACTER`] instead.
100110
#[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::marker::PhantomData;
88
use crate::num::fmt as numfmt;
99
use crate::ops::Deref;
@@ -187,7 +187,7 @@ pub trait Write {
187187
/// ```
188188
#[stable(feature = "fmt_write_char", since = "1.1.0")]
189189
fn write_char(&mut self, c: char) -> Result {
190-
self.write_str(c.encode_utf8(&mut [0; 4]))
190+
self.write_str(c.encode_utf8(&mut [0; MAX_LEN_UTF8]))
191191
}
192192

193193
/// Glue for usage of the [`write!`] macro with implementors of this trait.
@@ -2768,7 +2768,7 @@ impl Display for char {
27682768
if f.options.width.is_none() && f.options.precision.is_none() {
27692769
f.write_char(*self)
27702770
} else {
2771-
f.pad(self.encode_utf8(&mut [0; 4]))
2771+
f.pad(self.encode_utf8(&mut [0; MAX_LEN_UTF8]))
27722772
}
27732773
}
27742774
}

library/core/src/str/pattern.rs

Lines changed: 3 additions & 2 deletions
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::Ordering;
4243
use crate::convert::TryInto as _;
4344
use crate::slice::memchr;
@@ -561,8 +562,8 @@ impl Pattern for char {
561562
type Searcher<'a> = CharSearcher<'a>;
562563

563564
#[inline]
564-
fn into_searcher(self, haystack: &str) -> Self::Searcher<'_> {
565-
let mut utf8_encoded = [0; 4];
565+
fn into_searcher<'a>(self, haystack: &'a str) -> Self::Searcher<'a> {
566+
let mut utf8_encoded = [0; MAX_LEN_UTF8];
566567
let utf8_size = self
567568
.encode_utf8(&mut utf8_encoded)
568569
.len()

library/coretests/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/coretests/tests/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@
1313
#![feature(bigint_helper_methods)]
1414
#![feature(bstr)]
1515
#![feature(cell_update)]
16+
#![feature(char_max_len)]
1617
#![feature(clone_to_uninit)]
1718
#![feature(const_eval_select)]
1819
#![feature(const_swap_nonoverlapping)]
20+
#![feature(const_align_offset)]
21+
#![feature(const_align_of_val_raw)]
22+
#![feature(const_black_box)]
23+
#![feature(const_caller_location)]
24+
#![feature(const_cell_into_inner)]
25+
#![feature(const_hash)]
26+
#![feature(const_heap)]
27+
#![feature(const_intrinsic_copy)]
28+
#![feature(const_maybe_uninit_as_mut_ptr)]
29+
#![feature(const_nonnull_new)]
30+
#![feature(const_pointer_is_aligned)]
31+
#![feature(const_ptr_as_ref)]
32+
#![feature(const_ptr_write)]
1933
#![feature(const_trait_impl)]
2034
#![feature(core_intrinsics)]
2135
#![feature(core_intrinsics_fallbacks)]

0 commit comments

Comments
 (0)