Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9c06ab0

Browse files
authoredFeb 20, 2025
Unrolled build for rust-lang#120580
Rollup merge of rust-lang#120580 - HTGAzureX1212:HTGAzureX1212/issue-45795, r=m-ou-se Add `MAX_LEN_UTF8` and `MAX_LEN_UTF16` Constants This pull request adds the `MAX_LEN_UTF8` and `MAX_LEN_UTF16` constants as per rust-lang#45795, gated behind the `char_max_len` feature. The constants are currently applied in the `alloc`, `core` and `std` libraries.
2 parents f280acf + eec49bb commit 9c06ab0

File tree

14 files changed

+49
-18
lines changed

14 files changed

+49
-18
lines changed
 

‎library/alloc/src/lib.rs

+1
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

+5-3
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 {
@@ -2797,7 +2799,7 @@ impl SpecToString for core::ascii::Char {
27972799
impl SpecToString for char {
27982800
#[inline]
27992801
fn spec_to_string(&self) -> String {
2800-
String::from(self.encode_utf8(&mut [0; 4]))
2802+
String::from(self.encode_utf8(&mut [0; char::MAX_LEN_UTF8]))
28012803
}
28022804
}
28032805

‎library/alloc/tests/lib.rs

+1
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

+3-2
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

+10
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

+10
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

+3-3
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

+3-2
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

+2-1
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

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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)]

‎library/std/src/fs/tests.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rand::RngCore;
22

3+
use crate::char::MAX_LEN_UTF8;
34
use crate::fs::{self, File, FileTimes, OpenOptions};
45
use crate::io::prelude::*;
56
use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
@@ -155,7 +156,7 @@ fn file_test_io_non_positional_read() {
155156
#[test]
156157
fn file_test_io_seek_and_tell_smoke_test() {
157158
let message = "ten-four";
158-
let mut read_mem = [0; 4];
159+
let mut read_mem = [0; MAX_LEN_UTF8];
159160
let set_cursor = 4 as u64;
160161
let tell_pos_pre_read;
161162
let tell_pos_post_read;
@@ -356,7 +357,7 @@ fn file_test_io_seek_shakedown() {
356357
let chunk_one: &str = "qwer";
357358
let chunk_two: &str = "asdf";
358359
let chunk_three: &str = "zxcv";
359-
let mut read_mem = [0; 4];
360+
let mut read_mem = [0; MAX_LEN_UTF8];
360361
let tmpdir = tmpdir();
361362
let filename = &tmpdir.join("file_rt_io_file_test_seek_shakedown.txt");
362363
{
@@ -621,7 +622,7 @@ fn file_test_directoryinfo_readdir() {
621622
check!(w.write(msg));
622623
}
623624
let files = check!(fs::read_dir(dir));
624-
let mut mem = [0; 4];
625+
let mut mem = [0; MAX_LEN_UTF8];
625626
for f in files {
626627
let f = f.unwrap().path();
627628
{

‎library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
#![feature(cfg_sanitizer_cfi)]
285285
#![feature(cfg_target_thread_local)]
286286
#![feature(cfi_encoding)]
287+
#![feature(char_max_len)]
287288
#![feature(concat_idents)]
288289
#![feature(decl_macro)]
289290
#![feature(deprecated_suggestion)]

‎library/std/src/sys/pal/windows/stdio.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![unstable(issue = "none", feature = "windows_stdio")]
22

3+
use core::char::MAX_LEN_UTF8;
34
use core::str::utf8_char_width;
45

56
use super::api::{self, WinError};
@@ -426,7 +427,7 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
426427

427428
impl IncompleteUtf8 {
428429
pub const fn new() -> IncompleteUtf8 {
429-
IncompleteUtf8 { bytes: [0; 4], len: 0 }
430+
IncompleteUtf8 { bytes: [0; MAX_LEN_UTF8], len: 0 }
430431
}
431432
}
432433

‎library/std/src/sys_common/wtf8.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#[cfg(test)]
1919
mod tests;
2020

21-
use core::char::{encode_utf8_raw, encode_utf16_raw};
21+
use core::char::{MAX_LEN_UTF8, MAX_LEN_UTF16, encode_utf8_raw, encode_utf16_raw};
2222
use core::clone::CloneToUninit;
2323
use core::str::next_code_point;
2424

@@ -240,7 +240,7 @@ impl Wtf8Buf {
240240
/// Copied from String::push
241241
/// This does **not** include the WTF-8 concatenation check or `is_known_utf8` check.
242242
fn push_code_point_unchecked(&mut self, code_point: CodePoint) {
243-
let mut bytes = [0; 4];
243+
let mut bytes = [0; MAX_LEN_UTF8];
244244
let bytes = encode_utf8_raw(code_point.value, &mut bytes);
245245
self.bytes.extend_from_slice(bytes)
246246
}
@@ -1001,7 +1001,7 @@ impl<'a> Iterator for EncodeWide<'a> {
10011001
return Some(tmp);
10021002
}
10031003

1004-
let mut buf = [0; 2];
1004+
let mut buf = [0; MAX_LEN_UTF16];
10051005
self.code_points.next().map(|code_point| {
10061006
let n = encode_utf16_raw(code_point.value, &mut buf).len();
10071007
if n == 2 {

0 commit comments

Comments
 (0)
Failed to load comments.