From eec49bbf59c922060a5785a47d885529e6cb2ac8 Mon Sep 17 00:00:00 2001 From: HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> Date: Fri, 2 Feb 2024 20:49:20 +0800 Subject: add MAX_LEN_UTF8 and MAX_LEN_UTF16 constants --- library/alloc/src/string.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'library/alloc/src/string.rs') 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])) } } -- cgit 1.4.1-3-g733a5