add MAX_LEN_UTF8 and MAX_LEN_UTF16 constants

This commit is contained in:
HTGAzureX1212
2024-02-02 20:49:20 +08:00
parent a5db378dc1
commit eec49bbf59
14 changed files with 49 additions and 18 deletions

View File

@@ -71,6 +71,16 @@ impl char {
#[stable(feature = "assoc_char_consts", since = "1.52.0")]
pub const MAX: char = '\u{10FFFF}';
/// The maximum number of bytes required to [encode](char::encode_utf8) a `char` to
/// UTF-8 encoding.
#[unstable(feature = "char_max_len", issue = "121714")]
pub const MAX_LEN_UTF8: usize = 4;
/// The maximum number of two-byte units required to [encode](char::encode_utf16) a `char`
/// to UTF-16 encoding.
#[unstable(feature = "char_max_len", issue = "121714")]
pub const MAX_LEN_UTF16: usize = 2;
/// `U+FFFD REPLACEMENT CHARACTER` (<28>) is used in Unicode to represent a
/// decoding error.
///

View File

@@ -95,6 +95,16 @@ const MAX_THREE_B: u32 = 0x10000;
#[stable(feature = "rust1", since = "1.0.0")]
pub const MAX: char = char::MAX;
/// The maximum number of bytes required to [encode](char::encode_utf8) a `char` to
/// UTF-8 encoding.
#[unstable(feature = "char_max_len", issue = "121714")]
pub const MAX_LEN_UTF8: usize = char::MAX_LEN_UTF8;
/// The maximum number of two-byte units required to [encode](char::encode_utf16) a `char`
/// to UTF-16 encoding.
#[unstable(feature = "char_max_len", issue = "121714")]
pub const MAX_LEN_UTF16: usize = char::MAX_LEN_UTF16;
/// `U+FFFD REPLACEMENT CHARACTER` (<28>) is used in Unicode to represent a
/// decoding error. Use [`char::REPLACEMENT_CHARACTER`] instead.
#[stable(feature = "decode_utf16", since = "1.9.0")]

View File

@@ -3,7 +3,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
use crate::cell::{Cell, Ref, RefCell, RefMut, SyncUnsafeCell, UnsafeCell};
use crate::char::EscapeDebugExtArgs;
use crate::char::{EscapeDebugExtArgs, MAX_LEN_UTF8};
use crate::marker::PhantomData;
use crate::num::fmt as numfmt;
use crate::ops::Deref;
@@ -187,7 +187,7 @@ pub trait Write {
/// ```
#[stable(feature = "fmt_write_char", since = "1.1.0")]
fn write_char(&mut self, c: char) -> Result {
self.write_str(c.encode_utf8(&mut [0; 4]))
self.write_str(c.encode_utf8(&mut [0; MAX_LEN_UTF8]))
}
/// Glue for usage of the [`write!`] macro with implementors of this trait.
@@ -2768,7 +2768,7 @@ impl Display for char {
if f.options.width.is_none() && f.options.precision.is_none() {
f.write_char(*self)
} else {
f.pad(self.encode_utf8(&mut [0; 4]))
f.pad(self.encode_utf8(&mut [0; MAX_LEN_UTF8]))
}
}
}

View File

@@ -38,6 +38,7 @@
issue = "27721"
)]
use crate::char::MAX_LEN_UTF8;
use crate::cmp::Ordering;
use crate::convert::TryInto as _;
use crate::slice::memchr;
@@ -561,8 +562,8 @@ impl Pattern for char {
type Searcher<'a> = CharSearcher<'a>;
#[inline]
fn into_searcher(self, haystack: &str) -> Self::Searcher<'_> {
let mut utf8_encoded = [0; 4];
fn into_searcher<'a>(self, haystack: &'a str) -> Self::Searcher<'a> {
let mut utf8_encoded = [0; MAX_LEN_UTF8];
let utf8_size = self
.encode_utf8(&mut utf8_encoded)
.len()