diff options
| author | HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> | 2024-02-02 20:49:20 +0800 | 
|---|---|---|
| committer | HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> | 2025-02-16 21:08:38 +0800 | 
| commit | eec49bbf59c922060a5785a47d885529e6cb2ac8 (patch) | |
| tree | 07dfb9a29f4561e401de15ad0ac593165ff0c476 /library/alloc | |
| parent | a5db378dc14a40dd1580c27fb8362156446382c3 (diff) | |
| download | rust-eec49bbf59c922060a5785a47d885529e6cb2ac8.tar.gz rust-eec49bbf59c922060a5785a47d885529e6cb2ac8.zip | |
add MAX_LEN_UTF8 and MAX_LEN_UTF16 constants
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/alloc/src/string.rs | 8 | ||||
| -rw-r--r-- | library/alloc/tests/lib.rs | 1 | ||||
| -rw-r--r-- | library/alloc/tests/str.rs | 5 | 
4 files changed, 10 insertions, 5 deletions
| diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 0bb7c432cc3..2795fe5c769 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -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)] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b29f740ef0f..37e9e05a6a1 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -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 { @@ -2771,7 +2773,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])) } } diff --git a/library/alloc/tests/lib.rs b/library/alloc/tests/lib.rs index 391ff04a4b8..708dfc582a7 100644 --- a/library/alloc/tests/lib.rs +++ b/library/alloc/tests/lib.rs @@ -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)] diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs index 6f930ab0853..aeb0e681c22 100644 --- a/library/alloc/tests/str.rs +++ b/library/alloc/tests/str.rs @@ -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() { | 
