alloc/str.rs
1//! Utilities for the `str` primitive type.
2//!
3//! *[See also the `str` primitive type](str).*
4
5#![stable(feature = "rust1", since = "1.0.0")]
6// Many of the usings in this module are only used in the test configuration.
7// It's cleaner to just turn off the unused_imports warning than to fix them.
8#![allow(unused_imports)]
9
10use core::borrow::{Borrow, BorrowMut};
11use core::iter::FusedIterator;
12use core::mem::MaybeUninit;
13#[stable(feature = "encode_utf16", since = "1.8.0")]
14pub use core::str::EncodeUtf16;
15#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
16pub use core::str::SplitAsciiWhitespace;
17#[stable(feature = "split_inclusive", since = "1.51.0")]
18pub use core::str::SplitInclusive;
19#[stable(feature = "rust1", since = "1.0.0")]
20pub use core::str::SplitWhitespace;
21#[stable(feature = "rust1", since = "1.0.0")]
22pub use core::str::pattern;
23use core::str::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher, Utf8Pattern};
24#[stable(feature = "rust1", since = "1.0.0")]
25pub use core::str::{Bytes, CharIndices, Chars, from_utf8, from_utf8_mut};
26#[stable(feature = "str_escape", since = "1.34.0")]
27pub use core::str::{EscapeDebug, EscapeDefault, EscapeUnicode};
28#[stable(feature = "rust1", since = "1.0.0")]
29pub use core::str::{FromStr, Utf8Error};
30#[allow(deprecated)]
31#[stable(feature = "rust1", since = "1.0.0")]
32pub use core::str::{Lines, LinesAny};
33#[stable(feature = "rust1", since = "1.0.0")]
34pub use core::str::{MatchIndices, RMatchIndices};
35#[stable(feature = "rust1", since = "1.0.0")]
36pub use core::str::{Matches, RMatches};
37#[stable(feature = "rust1", since = "1.0.0")]
38pub use core::str::{ParseBoolError, from_utf8_unchecked, from_utf8_unchecked_mut};
39#[stable(feature = "rust1", since = "1.0.0")]
40pub use core::str::{RSplit, Split};
41#[stable(feature = "rust1", since = "1.0.0")]
42pub use core::str::{RSplitN, SplitN};
43#[stable(feature = "rust1", since = "1.0.0")]
44pub use core::str::{RSplitTerminator, SplitTerminator};
45#[stable(feature = "utf8_chunks", since = "1.79.0")]
46pub use core::str::{Utf8Chunk, Utf8Chunks};
47#[unstable(feature = "str_from_raw_parts", issue = "119206")]
48pub use core::str::{from_raw_parts, from_raw_parts_mut};
49use core::unicode::conversions;
50use core::{mem, ptr};
51
52use crate::borrow::ToOwned;
53use crate::boxed::Box;
54use crate::slice::{Concat, Join, SliceIndex};
55use crate::string::String;
56use crate::vec::Vec;
57
58/// Note: `str` in `Concat<str>` is not meaningful here.
59/// This type parameter of the trait only exists to enable another impl.
60#[cfg(not(no_global_oom_handling))]
61#[unstable(feature = "slice_concat_ext", issue = "27747")]
62impl<S: Borrow<str>> Concat<str> for [S] {
63 type Output = String;
64
65 fn concat(slice: &Self) -> String {
66 Join::join(slice, "")
67 }
68}
69
70#[cfg(not(no_global_oom_handling))]
71#[unstable(feature = "slice_concat_ext", issue = "27747")]
72impl<S: Borrow<str>> Join<&str> for [S] {
73 type Output = String;
74
75 fn join(slice: &Self, sep: &str) -> String {
76 unsafe { String::from_utf8_unchecked(join_generic_copy(slice, sep.as_bytes())) }
77 }
78}
79
80#[cfg(not(no_global_oom_handling))]
81macro_rules! specialize_for_lengths {
82 ($separator:expr, $target:expr, $iter:expr; $($num:expr),*) => {{
83 let mut target = $target;
84 let iter = $iter;
85 let sep_bytes = $separator;
86 match $separator.len() {
87 $(
88 // loops with hardcoded sizes run much faster
89 // specialize the cases with small separator lengths
90 $num => {
91 for s in iter {
92 copy_slice_and_advance!(target, sep_bytes);
93 let content_bytes = s.borrow().as_ref();
94 copy_slice_and_advance!(target, content_bytes);
95 }
96 },
97 )*
98 _ => {
99 // arbitrary non-zero size fallback
100 for s in iter {
101 copy_slice_and_advance!(target, sep_bytes);
102 let content_bytes = s.borrow().as_ref();
103 copy_slice_and_advance!(target, content_bytes);
104 }
105 }
106 }
107 target
108 }}
109}
110
111#[cfg(not(no_global_oom_handling))]
112macro_rules! copy_slice_and_advance {
113 ($target:expr, $bytes:expr) => {
114 let len = $bytes.len();
115 let (head, tail) = { $target }.split_at_mut(len);
116 head.copy_from_slice($bytes);
117 $target = tail;
118 };
119}
120
121// Optimized join implementation that works for both Vec<T> (T: Copy) and String's inner vec
122// Currently (2018-05-13) there is a bug with type inference and specialization (see issue #36262)
123// For this reason SliceConcat<T> is not specialized for T: Copy and SliceConcat<str> is the
124// only user of this function. It is left in place for the time when that is fixed.
125//
126// the bounds for String-join are S: Borrow<str> and for Vec-join Borrow<[T]>
127// [T] and str both impl AsRef<[T]> for some T
128// => s.borrow().as_ref() and we always have slices
129#[cfg(not(no_global_oom_handling))]
130fn join_generic_copy<B, T, S>(slice: &[S], sep: &[T]) -> Vec<T>
131where
132 T: Copy,
133 B: AsRef<[T]> + ?Sized,
134 S: Borrow<B>,
135{
136 let sep_len = sep.len();
137 let mut iter = slice.iter();
138
139 // the first slice is the only one without a separator preceding it
140 let first = match iter.next() {
141 Some(first) => first,
142 None => return vec![],
143 };
144
145 // compute the exact total length of the joined Vec
146 // if the `len` calculation overflows, we'll panic
147 // we would have run out of memory anyway and the rest of the function requires
148 // the entire Vec pre-allocated for safety
149 let reserved_len = sep_len
150 .checked_mul(iter.len())
151 .and_then(|n| {
152 slice.iter().map(|s| s.borrow().as_ref().len()).try_fold(n, usize::checked_add)
153 })
154 .expect("attempt to join into collection with len > usize::MAX");
155
156 // prepare an uninitialized buffer
157 let mut result = Vec::with_capacity(reserved_len);
158 debug_assert!(result.capacity() >= reserved_len);
159
160 result.extend_from_slice(first.borrow().as_ref());
161
162 unsafe {
163 let pos = result.len();
164 let target = result.spare_capacity_mut().get_unchecked_mut(..reserved_len - pos);
165
166 // Convert the separator and slices to slices of MaybeUninit
167 // to simplify implementation in specialize_for_lengths
168 let sep_uninit = core::slice::from_raw_parts(sep.as_ptr().cast(), sep.len());
169 let iter_uninit = iter.map(|it| {
170 let it = it.borrow().as_ref();
171 core::slice::from_raw_parts(it.as_ptr().cast(), it.len())
172 });
173
174 // copy separator and slices over without bounds checks
175 // generate loops with hardcoded offsets for small separators
176 // massive improvements possible (~ x2)
177 let remain = specialize_for_lengths!(sep_uninit, target, iter_uninit; 0, 1, 2, 3, 4);
178
179 // A weird borrow implementation may return different
180 // slices for the length calculation and the actual copy.
181 // Make sure we don't expose uninitialized bytes to the caller.
182 let result_len = reserved_len - remain.len();
183 result.set_len(result_len);
184 }
185 result
186}
187
188#[stable(feature = "rust1", since = "1.0.0")]
189impl Borrow<str> for String {
190 #[inline]
191 fn borrow(&self) -> &str {
192 &self[..]
193 }
194}
195
196#[stable(feature = "string_borrow_mut", since = "1.36.0")]
197impl BorrowMut<str> for String {
198 #[inline]
199 fn borrow_mut(&mut self) -> &mut str {
200 &mut self[..]
201 }
202}
203
204#[cfg(not(no_global_oom_handling))]
205#[stable(feature = "rust1", since = "1.0.0")]
206impl ToOwned for str {
207 type Owned = String;
208
209 #[inline]
210 fn to_owned(&self) -> String {
211 unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
212 }
213
214 #[inline]
215 fn clone_into(&self, target: &mut String) {
216 target.clear();
217 target.push_str(self);
218 }
219}
220
221/// Methods for string slices.
222impl str {
223 /// Converts a `Box<str>` into a `Box<[u8]>` without copying or allocating.
224 ///
225 /// # Examples
226 ///
227 /// ```
228 /// let s = "this is a string";
229 /// let boxed_str = s.to_owned().into_boxed_str();
230 /// let boxed_bytes = boxed_str.into_boxed_bytes();
231 /// assert_eq!(*boxed_bytes, *s.as_bytes());
232 /// ```
233 #[rustc_allow_incoherent_impl]
234 #[stable(feature = "str_box_extras", since = "1.20.0")]
235 #[must_use = "`self` will be dropped if the result is not used"]
236 #[inline]
237 pub fn into_boxed_bytes(self: Box<Self>) -> Box<[u8]> {
238 self.into()
239 }
240
241 /// Replaces all matches of a pattern with another string.
242 ///
243 /// `replace` creates a new [`String`], and copies the data from this string slice into it.
244 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
245 /// replaces them with the replacement string slice.
246 ///
247 /// # Examples
248 ///
249 /// ```
250 /// let s = "this is old";
251 ///
252 /// assert_eq!("this is new", s.replace("old", "new"));
253 /// assert_eq!("than an old", s.replace("is", "an"));
254 /// ```
255 ///
256 /// When the pattern doesn't match, it returns this string slice as [`String`]:
257 ///
258 /// ```
259 /// let s = "this is old";
260 /// assert_eq!(s, s.replace("cookie monster", "little lamb"));
261 /// ```
262 #[cfg(not(no_global_oom_handling))]
263 #[rustc_allow_incoherent_impl]
264 #[must_use = "this returns the replaced string as a new allocation, \
265 without modifying the original"]
266 #[stable(feature = "rust1", since = "1.0.0")]
267 #[inline]
268 pub fn replace<P: Pattern>(&self, from: P, to: &str) -> String {
269 // Fast path for replacing a single ASCII character with another.
270 if let Some(from_byte) = match from.as_utf8_pattern() {
271 Some(Utf8Pattern::StringPattern([from_byte])) => Some(*from_byte),
272 Some(Utf8Pattern::CharPattern(c)) => c.as_ascii().map(|ascii_char| ascii_char.to_u8()),
273 _ => None,
274 } {
275 if let [to_byte] = to.as_bytes() {
276 return unsafe { replace_ascii(self.as_bytes(), from_byte, *to_byte) };
277 }
278 }
279 // Set result capacity to self.len() when from.len() <= to.len()
280 let default_capacity = match from.as_utf8_pattern() {
281 Some(Utf8Pattern::StringPattern(s)) if s.len() <= to.len() => self.len(),
282 Some(Utf8Pattern::CharPattern(c)) if c.len_utf8() <= to.len() => self.len(),
283 _ => 0,
284 };
285 let mut result = String::with_capacity(default_capacity);
286 let mut last_end = 0;
287 for (start, part) in self.match_indices(from) {
288 result.push_str(unsafe { self.get_unchecked(last_end..start) });
289 result.push_str(to);
290 last_end = start + part.len();
291 }
292 result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
293 result
294 }
295
296 /// Replaces first N matches of a pattern with another string.
297 ///
298 /// `replacen` creates a new [`String`], and copies the data from this string slice into it.
299 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
300 /// replaces them with the replacement string slice at most `count` times.
301 ///
302 /// # Examples
303 ///
304 /// ```
305 /// let s = "foo foo 123 foo";
306 /// assert_eq!("new new 123 foo", s.replacen("foo", "new", 2));
307 /// assert_eq!("faa fao 123 foo", s.replacen('o', "a", 3));
308 /// assert_eq!("foo foo new23 foo", s.replacen(char::is_numeric, "new", 1));
309 /// ```
310 ///
311 /// When the pattern doesn't match, it returns this string slice as [`String`]:
312 ///
313 /// ```
314 /// let s = "this is old";
315 /// assert_eq!(s, s.replacen("cookie monster", "little lamb", 10));
316 /// ```
317 #[cfg(not(no_global_oom_handling))]
318 #[rustc_allow_incoherent_impl]
319 #[doc(alias = "replace_first")]
320 #[must_use = "this returns the replaced string as a new allocation, \
321 without modifying the original"]
322 #[stable(feature = "str_replacen", since = "1.16.0")]
323 pub fn replacen<P: Pattern>(&self, pat: P, to: &str, count: usize) -> String {
324 // Hope to reduce the times of re-allocation
325 let mut result = String::with_capacity(32);
326 let mut last_end = 0;
327 for (start, part) in self.match_indices(pat).take(count) {
328 result.push_str(unsafe { self.get_unchecked(last_end..start) });
329 result.push_str(to);
330 last_end = start + part.len();
331 }
332 result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
333 result
334 }
335
336 /// Returns the lowercase equivalent of this string slice, as a new [`String`].
337 ///
338 /// 'Lowercase' is defined according to the terms of
339 /// [Chapter 3 (Conformance)](https://www.unicode.org/versions/latest/core-spec/chapter-3/#G34432)
340 /// of the Unicode standard.
341 ///
342 /// Since some characters can expand into multiple characters when changing
343 /// the case, this function returns a [`String`] instead of modifying the
344 /// parameter in-place.
345 ///
346 /// Unlike [`char::to_lowercase()`], this method fully handles the context-dependent
347 /// casing of Greek sigma. However, like that method, it does not handle locale-specific
348 /// casing, like Turkish and Azeri I/ı/İ/i. See that method's documentation
349 /// for more information.
350 ///
351 /// # Examples
352 ///
353 /// Basic usage:
354 ///
355 /// ```
356 /// let s = "HELLO";
357 ///
358 /// assert_eq!("hello", s.to_lowercase());
359 /// ```
360 ///
361 /// A tricky example, with sigma:
362 ///
363 /// ```
364 /// let sigma = "Σ";
365 ///
366 /// assert_eq!("σ", sigma.to_lowercase());
367 ///
368 /// // but at the end of a word, it's ς, not σ:
369 /// let odysseus = "ὈΔΥΣΣΕΎΣ";
370 ///
371 /// assert_eq!("ὀδυσσεύς", odysseus.to_lowercase());
372 /// ```
373 ///
374 /// Languages without case are not changed:
375 ///
376 /// ```
377 /// let new_year = "农历新年";
378 ///
379 /// assert_eq!(new_year, new_year.to_lowercase());
380 /// ```
381 #[cfg(not(no_global_oom_handling))]
382 #[rustc_allow_incoherent_impl]
383 #[must_use = "this returns the lowercase string as a new String, \
384 without modifying the original"]
385 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
386 pub fn to_lowercase(&self) -> String {
387 // SAFETY: `to_ascii_lowercase` preserves ASCII bytes, so the converted
388 // prefix remains valid UTF-8.
389 let (mut s, rest) = unsafe { convert_while_ascii(self, u8::to_ascii_lowercase) };
390
391 let prefix_len = s.len();
392
393 for (i, c) in rest.char_indices() {
394 if c == 'Σ' {
395 // Σ maps to σ, except at the end of a word where it maps to ς.
396 // This is the only conditional (contextual) but language-independent mapping
397 // in `SpecialCasing.txt`,
398 // so hard-code it rather than have a generic "condition" mechanism.
399 // See https://github.com/rust-lang/rust/issues/26035
400 let sigma_lowercase = map_uppercase_sigma(self, prefix_len + i);
401 s.push(sigma_lowercase);
402 } else {
403 match conversions::to_lower(c) {
404 [a, '\0', _] => s.push(a),
405 [a, b, '\0'] => {
406 s.push(a);
407 s.push(b);
408 }
409 [a, b, c] => {
410 s.push(a);
411 s.push(b);
412 s.push(c);
413 }
414 }
415 }
416 }
417 return s;
418
419 fn map_uppercase_sigma(from: &str, i: usize) -> char {
420 // See https://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
421 // for the definition of `Final_Sigma`.
422 let is_word_final = case_ignorable_then_cased(from[..i].chars().rev())
423 && !case_ignorable_then_cased(from[i + const { 'Σ'.len_utf8() }..].chars());
424 if is_word_final { 'ς' } else { 'σ' }
425 }
426
427 fn case_ignorable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
428 match iter.skip_while(|&c| c.is_case_ignorable()).next() {
429 Some(c) => c.is_cased(),
430 None => false,
431 }
432 }
433 }
434
435 /// Returns the uppercase equivalent of this string slice, as a new [`String`].
436 ///
437 /// 'Uppercase' is defined according to the terms of
438 /// [Chapter 3 (Conformance)](https://www.unicode.org/versions/latest/core-spec/chapter-3/#G34431)
439 /// of the Unicode standard.
440 ///
441 /// Since some characters can expand into multiple characters when changing
442 /// the case, this function returns a [`String`] instead of modifying the
443 /// parameter in-place.
444 ///
445 /// Like [`char::to_uppercase()`] this method does not handle language-specific
446 /// casing, like Turkish and Azeri I/ı/İ/i. See that method's documentation
447 /// for more information.
448 ///
449 /// # Examples
450 ///
451 /// Basic usage:
452 ///
453 /// ```
454 /// let s = "hello";
455 ///
456 /// assert_eq!("HELLO", s.to_uppercase());
457 /// ```
458 ///
459 /// Scripts without case are not changed:
460 ///
461 /// ```
462 /// let new_year = "农历新年";
463 ///
464 /// assert_eq!(new_year, new_year.to_uppercase());
465 /// ```
466 ///
467 /// One character can become multiple:
468 /// ```
469 /// let s = "tschüß";
470 ///
471 /// assert_eq!("TSCHÜSS", s.to_uppercase());
472 /// ```
473 #[cfg(not(no_global_oom_handling))]
474 #[rustc_allow_incoherent_impl]
475 #[must_use = "this returns the uppercase string as a new String, \
476 without modifying the original"]
477 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
478 pub fn to_uppercase(&self) -> String {
479 // SAFETY: `to_ascii_uppercase` preserves ASCII bytes, so the converted
480 // prefix remains valid UTF-8.
481 let (mut s, rest) = unsafe { convert_while_ascii(self, u8::to_ascii_uppercase) };
482
483 for c in rest.chars() {
484 match conversions::to_upper(c) {
485 [a, '\0', _] => s.push(a),
486 [a, b, '\0'] => {
487 s.push(a);
488 s.push(b);
489 }
490 [a, b, c] => {
491 s.push(a);
492 s.push(b);
493 s.push(c);
494 }
495 }
496 }
497 s
498 }
499
500 /// Converts a [`Box<str>`] into a [`String`] without copying or allocating.
501 ///
502 /// # Examples
503 ///
504 /// ```
505 /// let string = String::from("birthday gift");
506 /// let boxed_str = string.clone().into_boxed_str();
507 ///
508 /// assert_eq!(boxed_str.into_string(), string);
509 /// ```
510 #[stable(feature = "box_str", since = "1.4.0")]
511 #[rustc_allow_incoherent_impl]
512 #[must_use = "`self` will be dropped if the result is not used"]
513 #[inline]
514 pub fn into_string(self: Box<Self>) -> String {
515 let slice = Box::<[u8]>::from(self);
516 unsafe { String::from_utf8_unchecked(slice.into_vec()) }
517 }
518
519 /// Creates a new [`String`] by repeating a string `n` times.
520 ///
521 /// # Panics
522 ///
523 /// This function will panic if the capacity would overflow.
524 ///
525 /// # Examples
526 ///
527 /// Basic usage:
528 ///
529 /// ```
530 /// assert_eq!("abc".repeat(4), String::from("abcabcabcabc"));
531 /// ```
532 ///
533 /// A panic upon overflow:
534 ///
535 /// ```should_panic
536 /// // this will panic at runtime
537 /// let huge = "0123456789abcdef".repeat(usize::MAX);
538 /// ```
539 #[cfg(not(no_global_oom_handling))]
540 #[rustc_allow_incoherent_impl]
541 #[must_use]
542 #[stable(feature = "repeat_str", since = "1.16.0")]
543 #[inline]
544 pub fn repeat(&self, n: usize) -> String {
545 unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) }
546 }
547
548 /// Returns a copy of this string where each character is mapped to its
549 /// ASCII upper case equivalent.
550 ///
551 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
552 /// but non-ASCII letters are unchanged.
553 ///
554 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
555 ///
556 /// To uppercase ASCII characters in addition to non-ASCII characters, use
557 /// [`to_uppercase`].
558 ///
559 /// # Examples
560 ///
561 /// ```
562 /// let s = "Grüße, Jürgen ❤";
563 ///
564 /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
565 /// ```
566 ///
567 /// [`make_ascii_uppercase`]: str::make_ascii_uppercase
568 /// [`to_uppercase`]: #method.to_uppercase
569 #[cfg(not(no_global_oom_handling))]
570 #[rustc_allow_incoherent_impl]
571 #[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
572 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
573 #[inline]
574 pub fn to_ascii_uppercase(&self) -> String {
575 let mut s = self.to_owned();
576 s.make_ascii_uppercase();
577 s
578 }
579
580 /// Returns a copy of this string where each character is mapped to its
581 /// ASCII lower case equivalent.
582 ///
583 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
584 /// but non-ASCII letters are unchanged.
585 ///
586 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
587 ///
588 /// To lowercase ASCII characters in addition to non-ASCII characters, use
589 /// [`to_lowercase`].
590 ///
591 /// # Examples
592 ///
593 /// ```
594 /// let s = "Grüße, Jürgen ❤";
595 ///
596 /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
597 /// ```
598 ///
599 /// [`make_ascii_lowercase`]: str::make_ascii_lowercase
600 /// [`to_lowercase`]: #method.to_lowercase
601 #[cfg(not(no_global_oom_handling))]
602 #[rustc_allow_incoherent_impl]
603 #[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
604 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
605 #[inline]
606 pub fn to_ascii_lowercase(&self) -> String {
607 let mut s = self.to_owned();
608 s.make_ascii_lowercase();
609 s
610 }
611}
612
613/// Converts a boxed slice of bytes to a boxed string slice without checking
614/// that the string contains valid UTF-8.
615///
616/// # Safety
617///
618/// * The provided bytes must contain a valid UTF-8 sequence.
619///
620/// # Examples
621///
622/// ```
623/// let smile_utf8 = Box::new([226, 152, 186]);
624/// let smile = unsafe { std::str::from_boxed_utf8_unchecked(smile_utf8) };
625///
626/// assert_eq!("☺", &*smile);
627/// ```
628#[stable(feature = "str_box_extras", since = "1.20.0")]
629#[must_use]
630#[inline]
631pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
632 unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }
633}
634
635/// Converts leading ascii bytes in `s` by calling the `convert` function.
636///
637/// For better average performance, this happens in chunks of `2*size_of::<usize>()`.
638///
639/// Returns a tuple of the converted prefix and the remainder starting from
640/// the first non-ascii character.
641///
642/// This function is only public so that it can be verified in a codegen test,
643/// see `issue-123712-str-to-lower-autovectorization.rs`.
644///
645/// # Safety
646///
647/// `convert` must return an ASCII byte for every ASCII input byte.
648#[unstable(feature = "str_internals", issue = "none")]
649#[doc(hidden)]
650#[inline]
651#[cfg(not(no_global_oom_handling))]
652pub unsafe fn convert_while_ascii(s: &str, convert: fn(&u8) -> u8) -> (String, &str) {
653 // Process the input in chunks of 16 bytes to enable auto-vectorization.
654 // Previously the chunk size depended on the size of `usize`,
655 // but on 32-bit platforms with sse or neon is also the better choice.
656 // The only downside on other platforms would be a bit more loop-unrolling.
657 const N: usize = 16;
658
659 let mut slice = s.as_bytes();
660 let mut out = Vec::with_capacity(slice.len());
661 let mut out_slice = out.spare_capacity_mut();
662
663 let mut ascii_prefix_len = 0_usize;
664 let mut is_ascii = [false; N];
665
666 while slice.len() >= N {
667 // SAFETY: checked in loop condition
668 let chunk = unsafe { slice.get_unchecked(..N) };
669 // SAFETY: out_slice has at least same length as input slice and gets sliced with the same offsets
670 let out_chunk = unsafe { out_slice.get_unchecked_mut(..N) };
671
672 for j in 0..N {
673 is_ascii[j] = chunk[j] <= 127;
674 }
675
676 // Auto-vectorization for this check is a bit fragile, sum and comparing against the chunk
677 // size gives the best result, specifically a pmovmsk instruction on x86.
678 // See https://github.com/llvm/llvm-project/issues/96395 for why llvm currently does not
679 // currently recognize other similar idioms.
680 if is_ascii.iter().map(|x| *x as u8).sum::<u8>() as usize != N {
681 break;
682 }
683
684 for j in 0..N {
685 out_chunk[j] = MaybeUninit::new(convert(&chunk[j]));
686 }
687
688 ascii_prefix_len += N;
689 slice = unsafe { slice.get_unchecked(N..) };
690 out_slice = unsafe { out_slice.get_unchecked_mut(N..) };
691 }
692
693 // handle the remainder as individual bytes
694 while slice.len() > 0 {
695 let byte = slice[0];
696 if byte > 127 {
697 break;
698 }
699 // SAFETY: out_slice has at least same length as input slice
700 unsafe {
701 *out_slice.get_unchecked_mut(0) = MaybeUninit::new(convert(&byte));
702 }
703 ascii_prefix_len += 1;
704 slice = unsafe { slice.get_unchecked(1..) };
705 out_slice = unsafe { out_slice.get_unchecked_mut(1..) };
706 }
707
708 unsafe {
709 // SAFETY: ascii_prefix_len bytes have been initialized above
710 out.set_len(ascii_prefix_len);
711
712 // SAFETY: We have written only valid ascii to the output vec
713 let ascii_string = String::from_utf8_unchecked(out);
714
715 // SAFETY: we know this is a valid char boundary
716 // since we only skipped over leading ascii bytes
717 let rest = core::str::from_utf8_unchecked(slice);
718
719 (ascii_string, rest)
720 }
721}
722#[inline]
723#[cfg(not(no_global_oom_handling))]
724#[allow(dead_code)]
725/// Faster implementation of string replacement for ASCII to ASCII cases.
726/// Should produce fast vectorized code.
727unsafe fn replace_ascii(utf8_bytes: &[u8], from: u8, to: u8) -> String {
728 let result: Vec<u8> = utf8_bytes.iter().map(|b| if *b == from { to } else { *b }).collect();
729 // SAFETY: We replaced ascii with ascii on valid utf8 strings.
730 unsafe { String::from_utf8_unchecked(result) }
731}