about summary refs log tree commit diff
path: root/library/alloc/src/string.rs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-19 21:16:01 +0100
committerGitHub <noreply@github.com>2025-02-19 21:16:01 +0100
commit84e9f2900758902fbbe6d09ec0f35546c220dedf (patch)
treed243042f4fb3b864cacbe50f6d6dddc9b798c0a9 /library/alloc/src/string.rs
parented49386d3aa3a445a9889707fd405df01723eced (diff)
parenteec49bbf59c922060a5785a47d885529e6cb2ac8 (diff)
downloadrust-84e9f2900758902fbbe6d09ec0f35546c220dedf.tar.gz
rust-84e9f2900758902fbbe6d09ec0f35546c220dedf.zip
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.
Diffstat (limited to 'library/alloc/src/string.rs')
-rw-r--r--library/alloc/src/string.rs8
1 files changed, 5 insertions, 3 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index 154da691078..4a1ec0c9895 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 {
@@ -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]))
     }
 }