1
Fork 0

Rollup merge of #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 #45795, gated behind the `char_max_len` feature.

The constants are currently applied in the `alloc`, `core` and `std` libraries.
This commit is contained in:
Matthias Krüger 2025-02-19 21:16:01 +01:00 committed by GitHub
commit 84e9f29007
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 49 additions and 18 deletions

View file

@ -105,6 +105,7 @@
#![feature(box_uninit_write)]
#![feature(bstr)]
#![feature(bstr_internals)]
#![feature(char_max_len)]
#![feature(clone_to_uninit)]
#![feature(coerce_unsized)]
#![feature(const_eval_select)]

View file

@ -1419,7 +1419,9 @@ impl String {
pub fn push(&mut self, ch: char) {
match ch.len_utf8() {
1 => self.vec.push(ch as u8),
_ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()),
_ => {
self.vec.extend_from_slice(ch.encode_utf8(&mut [0; char::MAX_LEN_UTF8]).as_bytes())
}
}
}
@ -1716,7 +1718,7 @@ impl String {
#[rustc_confusables("set")]
pub fn insert(&mut self, idx: usize, ch: char) {
assert!(self.is_char_boundary(idx));
let mut bits = [0; 4];
let mut bits = [0; char::MAX_LEN_UTF8];
let bits = ch.encode_utf8(&mut bits).as_bytes();
unsafe {
@ -2797,7 +2799,7 @@ impl SpecToString for core::ascii::Char {
impl SpecToString for char {
#[inline]
fn spec_to_string(&self) -> String {
String::from(self.encode_utf8(&mut [0; 4]))
String::from(self.encode_utf8(&mut [0; char::MAX_LEN_UTF8]))
}
}

View file

@ -3,6 +3,7 @@
#![feature(iter_array_chunks)]
#![feature(assert_matches)]
#![feature(btree_extract_if)]
#![feature(char_max_len)]
#![feature(cow_is_borrowed)]
#![feature(core_intrinsics)]
#![feature(downcast_unchecked)]

View file

@ -2,6 +2,7 @@
use std::assert_matches::assert_matches;
use std::borrow::Cow;
use std::char::MAX_LEN_UTF8;
use std::cmp::Ordering::{Equal, Greater, Less};
use std::str::{from_utf8, from_utf8_unchecked};
@ -1231,7 +1232,7 @@ fn test_to_uppercase_rev_iterator() {
#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_chars_decoding() {
let mut bytes = [0; 4];
let mut bytes = [0; MAX_LEN_UTF8];
for c in (0..0x110000).filter_map(std::char::from_u32) {
let s = c.encode_utf8(&mut bytes);
if Some(c) != s.chars().next() {
@ -1243,7 +1244,7 @@ fn test_chars_decoding() {
#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_chars_rev_decoding() {
let mut bytes = [0; 4];
let mut bytes = [0; MAX_LEN_UTF8];
for c in (0..0x110000).filter_map(std::char::from_u32) {
let s = c.encode_utf8(&mut bytes);
if Some(c) != s.chars().rev().next() {