Skip to main content

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//
130// # Safety notes
131//
132// `Borrow` is a safe trait, and implementations are not required
133// to be deterministic. An inconsistent `Borrow` implementation could return slices
134// of different lengths on consecutive calls (e.g. by using interior mutability).
135//
136// This implementation calls `borrow()` multiple times:
137// 1. To calculate `reserved_len`, all elements are borrowed once.
138// 2. All elements, except the first, are borrowed a second time when building the mapped iterator.
139//
140// Risks and Mitigations:
141// - If elements 2..N GROW on their second borrow, the target slice bounds set by `checked_sub`
142//   means that `split_at_mut` inside `copy_slice_and_advance!` will correctly panic.
143// - If elements SHRINK on their second borrow, the spare space is never written, and the final
144//   length set via `set_len` masks trailing uninitialized bytes.
145#[cfg(not(no_global_oom_handling))]
146fn join_generic_copy<B, T, S>(slice: &[S], sep: &[T]) -> Vec<T>
147where
148    T: Copy,
149    B: AsRef<[T]> + ?Sized,
150    S: Borrow<B>,
151{
152    let sep_len = sep.len();
153    let mut iter = slice.iter();
154
155    // the first slice is the only one without a separator preceding it
156    // we take care to only borrow this once during the length calculation
157    // to avoid inconsistent Borrow implementations from breaking our assumptions
158    let first = match iter.next() {
159        Some(first) => first.borrow().as_ref(),
160        None => return vec![],
161    };
162
163    // compute the exact total length of the joined Vec
164    // if the `len` calculation overflows, we'll panic
165    // we would have run out of memory anyway and the rest of the function requires
166    // the entire Vec pre-allocated for safety
167    let reserved_len = sep_len
168        .checked_mul(iter.len())
169        .and_then(|n| n.checked_add(first.len()))
170        .and_then(|n| {
171            // iter starts from the second element as we've already taken the first
172            // it's cloned so we can reuse the same iterator below
173            iter.clone().map(|s| s.borrow().as_ref().len()).try_fold(n, usize::checked_add)
174        })
175        .expect("attempt to join into collection with len > usize::MAX");
176
177    // prepare an uninitialized buffer
178    let mut result = Vec::with_capacity(reserved_len);
179    debug_assert!(result.capacity() >= reserved_len);
180
181    result.extend_from_slice(first);
182
183    unsafe {
184        let pos = result.len();
185        debug_assert!(reserved_len >= pos);
186        let target = result.spare_capacity_mut().get_unchecked_mut(..reserved_len - pos);
187
188        // Convert the separator and slices to slices of MaybeUninit
189        // to simplify implementation in specialize_for_lengths.
190        let sep_uninit = core::slice::from_raw_parts(sep.as_ptr().cast(), sep.len());
191        let iter_uninit = iter.map(|it| {
192            let it = it.borrow().as_ref();
193            core::slice::from_raw_parts(it.as_ptr().cast(), it.len())
194        });
195
196        // copy separator and slices over without bounds checks.
197        // `specialize_for_lengths!` internally calls `s.borrow()`, but because it uses
198        // the bounds-checked `split_at_mut` any misbehaving implementation
199        // will not write out of bounds.
200        let remain = specialize_for_lengths!(sep_uninit, target, iter_uninit; 0, 1, 2, 3, 4);
201
202        // A weird borrow implementation may return different
203        // slices for the length calculation and the actual copy.
204        // Make sure we don't expose uninitialized bytes to the caller.
205        let result_len = reserved_len - remain.len();
206        result.set_len(result_len);
207    }
208    result
209}
210
211/// Helper for final sigma lowercase
212#[cfg(not(no_global_oom_handling))]
213fn map_uppercase_sigma(from: &str, i: usize) -> char {
214    fn case_ignorable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
215        match iter.skip_while(|&c| c.is_case_ignorable()).next() {
216            Some(c) => c.is_cased(),
217            None => false,
218        }
219    }
220
221    // See https://www.unicode.org/versions/latest/core-spec/chapter-3/#G54277
222    // for the definition of `Final_Sigma`.
223    let is_word_final = case_ignorable_then_cased(from[..i].chars().rev())
224        && !case_ignorable_then_cased(from[i + const { 'Σ'.len_utf8() }..].chars());
225    if is_word_final { 'ς' } else { 'σ' }
226}
227
228#[stable(feature = "rust1", since = "1.0.0")]
229impl Borrow<str> for String {
230    #[inline]
231    fn borrow(&self) -> &str {
232        &self[..]
233    }
234}
235
236#[stable(feature = "string_borrow_mut", since = "1.36.0")]
237impl BorrowMut<str> for String {
238    #[inline]
239    fn borrow_mut(&mut self) -> &mut str {
240        &mut self[..]
241    }
242}
243
244#[cfg(not(no_global_oom_handling))]
245#[stable(feature = "rust1", since = "1.0.0")]
246impl ToOwned for str {
247    type Owned = String;
248
249    #[inline]
250    fn to_owned(&self) -> String {
251        unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
252    }
253
254    #[inline]
255    fn clone_into(&self, target: &mut String) {
256        target.clear();
257        target.push_str(self);
258    }
259}
260
261/// Methods for string slices.
262impl str {
263    /// Converts a `Box<str>` into a `Box<[u8]>` without copying or allocating.
264    ///
265    /// # Examples
266    ///
267    /// ```
268    /// let s = "this is a string";
269    /// let boxed_str = s.to_owned().into_boxed_str();
270    /// let boxed_bytes = boxed_str.into_boxed_bytes();
271    /// assert_eq!(*boxed_bytes, *s.as_bytes());
272    /// ```
273    #[rustc_allow_incoherent_impl]
274    #[stable(feature = "str_box_extras", since = "1.20.0")]
275    #[must_use = "`self` will be dropped if the result is not used"]
276    #[inline]
277    pub fn into_boxed_bytes(self: Box<Self>) -> Box<[u8]> {
278        self.into()
279    }
280
281    /// Replaces all matches of a pattern with another string.
282    ///
283    /// `replace` creates a new [`String`], and copies the data from this string slice into it.
284    /// While doing so, it attempts to find matches of a pattern. If it finds any, it
285    /// replaces them with the replacement string slice.
286    ///
287    /// # Examples
288    ///
289    /// ```
290    /// let s = "this is old";
291    ///
292    /// assert_eq!("this is new", s.replace("old", "new"));
293    /// assert_eq!("than an old", s.replace("is", "an"));
294    /// ```
295    ///
296    /// When the pattern doesn't match, it returns this string slice as [`String`]:
297    ///
298    /// ```
299    /// let s = "this is old";
300    /// assert_eq!(s, s.replace("cookie monster", "little lamb"));
301    /// ```
302    #[cfg(not(no_global_oom_handling))]
303    #[rustc_allow_incoherent_impl]
304    #[must_use = "this returns the replaced string as a new allocation, \
305                  without modifying the original"]
306    #[stable(feature = "rust1", since = "1.0.0")]
307    #[inline]
308    pub fn replace<P: Pattern>(&self, from: P, to: &str) -> String {
309        // Fast path for replacing a single ASCII character with another.
310        if let Some(from_byte) = match from.as_utf8_pattern() {
311            Some(Utf8Pattern::StringPattern(s)) => match s.as_bytes() {
312                [from_byte] => Some(*from_byte),
313                _ => None,
314            },
315            Some(Utf8Pattern::CharPattern(c)) => c.as_ascii().map(|ascii_char| ascii_char.to_u8()),
316            _ => None,
317        } {
318            if let [to_byte] = to.as_bytes() {
319                return unsafe { replace_ascii(self.as_bytes(), from_byte, *to_byte) };
320            }
321        }
322        // Set result capacity to self.len() when from.len() <= to.len()
323        let default_capacity = match from.as_utf8_pattern() {
324            Some(Utf8Pattern::StringPattern(s)) if s.len() <= to.len() => self.len(),
325            Some(Utf8Pattern::CharPattern(c)) if c.len_utf8() <= to.len() => self.len(),
326            _ => 0,
327        };
328        let mut result = String::with_capacity(default_capacity);
329        let mut last_end = 0;
330        for (start, part) in self.match_indices(from) {
331            result.push_str(unsafe { self.get_unchecked(last_end..start) });
332            result.push_str(to);
333            last_end = start + part.len();
334        }
335        result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
336        result
337    }
338
339    /// Replaces first N matches of a pattern with another string.
340    ///
341    /// `replacen` creates a new [`String`], and copies the data from this string slice into it.
342    /// While doing so, it attempts to find matches of a pattern. If it finds any, it
343    /// replaces them with the replacement string slice at most `count` times.
344    ///
345    /// # Examples
346    ///
347    /// ```
348    /// let s = "foo foo 123 foo";
349    /// assert_eq!("new new 123 foo", s.replacen("foo", "new", 2));
350    /// assert_eq!("faa fao 123 foo", s.replacen('o', "a", 3));
351    /// assert_eq!("foo foo new23 foo", s.replacen(char::is_numeric, "new", 1));
352    /// ```
353    ///
354    /// When the pattern doesn't match, it returns this string slice as [`String`]:
355    ///
356    /// ```
357    /// let s = "this is old";
358    /// assert_eq!(s, s.replacen("cookie monster", "little lamb", 10));
359    /// ```
360    #[cfg(not(no_global_oom_handling))]
361    #[rustc_allow_incoherent_impl]
362    #[doc(alias = "replace_first")]
363    #[must_use = "this returns the replaced string as a new allocation, \
364                  without modifying the original"]
365    #[stable(feature = "str_replacen", since = "1.16.0")]
366    pub fn replacen<P: Pattern>(&self, pat: P, to: &str, count: usize) -> String {
367        // Hope to reduce the times of re-allocation
368        let mut result = String::with_capacity(32);
369        let mut last_end = 0;
370        for (start, part) in self.match_indices(pat).take(count) {
371            result.push_str(unsafe { self.get_unchecked(last_end..start) });
372            result.push_str(to);
373            last_end = start + part.len();
374        }
375        result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
376        result
377    }
378
379    /// Returns the lowercase equivalent of this string slice, as a new [`String`].
380    ///
381    /// 'Lowercase' is defined according to the terms of
382    /// [Chapter 3 (Conformance)](https://www.unicode.org/versions/latest/core-spec/chapter-3/#G34432)
383    /// of the Unicode standard.
384    ///
385    /// Since some characters can expand into multiple characters when changing
386    /// the case, this function returns a [`String`] instead of modifying the
387    /// parameter in-place.
388    ///
389    /// Unlike [`char::to_lowercase()`], this method fully handles the context-dependent
390    /// casing of Greek sigma. However, like that method, it does not handle locale-specific
391    /// casing, like Turkish and Azeri I/ı/İ/i. See its documentation
392    /// for more information.
393    ///
394    /// # Examples
395    ///
396    /// Basic usage:
397    ///
398    /// ```
399    /// let s = "HELLO WORLD";
400    ///
401    /// assert_eq!("hello world", s.to_lowercase());
402    /// ```
403    ///
404    /// Tricky examples, with sigma:
405    ///
406    /// ```
407    /// let sigma = "Σ";
408    ///
409    /// assert_eq!("σ", sigma.to_lowercase());
410    ///
411    /// // but at the end of a word, it's ς, not σ:
412    /// let odysseus = "ὈΔΥΣΣΕΎΣ";
413    ///
414    /// assert_eq!("ὀδυσσεύς", odysseus.to_lowercase());
415    ///
416    /// let odysseus_king_of_ithaca = "Ο ΟΔΥΣΣΈΑΣ ΒΑΣΙΛΙΆΣ ΤΗΣ ΙΘΆΚΗΣ";
417    ///
418    /// assert_eq!("ο οδυσσέας βασιλιάς της ιθάκης", odysseus_king_of_ithaca.to_lowercase());
419    /// ```
420    ///
421    /// Languages without case are not changed:
422    ///
423    /// ```
424    /// let new_year = "农历新年";
425    ///
426    /// assert_eq!(new_year, new_year.to_lowercase());
427    /// ```
428    #[cfg(not(no_global_oom_handling))]
429    #[rustc_allow_incoherent_impl]
430    #[must_use = "this returns the lowercase string as a new String, \
431                  without modifying the original"]
432    #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
433    pub fn to_lowercase(&self) -> String {
434        // SAFETY: `to_ascii_lowercase` preserves ASCII bytes, so the converted
435        // prefix remains valid UTF-8.
436        let (mut s, rest) = unsafe { convert_while_ascii(self, u8::to_ascii_lowercase) };
437
438        let prefix_len = s.len();
439
440        for (i, c) in rest.char_indices() {
441            if c == 'Σ' {
442                // Σ maps to σ, except at the end of a word where it maps to ς.
443                // This is the only conditional (contextual) but language-independent mapping
444                // in `SpecialCasing.txt`,
445                // so hard-code it rather than have a generic "condition" mechanism.
446                // See https://github.com/rust-lang/rust/issues/26035
447                let sigma_lowercase = map_uppercase_sigma(self, prefix_len + i);
448                s.push(sigma_lowercase);
449            } else {
450                match conversions::to_lower(c) {
451                    [a, '\0', _] => s.push(a),
452                    [a, b, '\0'] => {
453                        s.push(a);
454                        s.push(b);
455                    }
456                    [a, b, c] => {
457                        s.push(a);
458                        s.push(b);
459                        s.push(c);
460                    }
461                }
462            }
463        }
464        return s;
465    }
466
467    /// Returns the titlecase equivalent of this string slice,
468    /// which is assumed to represent a single word,
469    /// as a new [`String`].
470    ///
471    /// Essentially, this consists of uppercasing the first cased letter
472    /// (with [`char::to_titlecase()`]), and lowercasing everything that follows.
473    ///
474    /// 'Titlecase' is defined according to the terms of
475    /// [Chapter 3 (Conformance)](https://www.unicode.org/versions/latest/core-spec/chapter-3/#G34082)
476    /// of the Unicode standard.
477    ///
478    /// Since some characters can expand into multiple characters when changing
479    /// the case, this function returns a [`String`] instead of modifying the
480    /// parameter in-place.
481    ///
482    /// Unlike [`char::to_lowercase()`], this method fully handles the context-dependent
483    /// casing of Greek sigma. However, like that method, it does not handle locale-specific
484    /// casing, like Turkish and Azeri I/ı/İ/i. See its documentation
485    /// for more information.
486    ///
487    /// This method does not perform any kind of word segmentation.
488    ///
489    /// # Examples
490    ///
491    /// Basic usage:
492    ///
493    /// ```
494    /// #![feature(titlecase)]
495    /// let s = "HELLO WORLD";
496    ///
497    /// assert_eq!("Hello world", s.word_to_titlecase());
498    /// ```
499    ///
500    /// The first *cased* letter is uppercased:
501    ///
502    /// ```
503    /// #![feature(titlecase)]
504    /// let the_night_before_christmas = "'twas";
505    ///
506    /// assert_eq!("'Twas", the_night_before_christmas.word_to_titlecase());
507    /// ```
508    ///
509    /// Languages without case are not changed:
510    ///
511    /// ```
512    /// #![feature(titlecase)]
513    /// let new_year = "农历新年";
514    ///
515    /// assert_eq!(new_year, new_year.word_to_titlecase());
516    /// ```
517    ///
518    /// Georgian uppercase ("Mtavruli") letters are not used in titlecase:
519    ///
520    /// ```
521    /// #![feature(titlecase)]
522    /// let georgian = "ერთობაშია";
523    ///
524    /// assert_eq!(georgian, georgian.word_to_titlecase());
525    /// ```
526    ///
527    /// No word segmentation is performed,
528    /// so only the first cased letter in the whole string gets uppercased:
529    ///
530    /// ```
531    /// #![feature(titlecase)]
532    /// let blazingly_fast = "ferris and I";
533    ///
534    /// assert_eq!("Ferris and i", blazingly_fast.word_to_titlecase());
535    /// ```
536    ///
537    /// Tricky examples, with sigma:
538    ///
539    /// ```
540    /// #![feature(titlecase)]
541    /// let odysseus = "ὈΔΥΣΣΕΎΣ";
542    ///
543    /// assert_eq!("Ὀδυσσεύς", odysseus.word_to_titlecase());
544    ///
545    /// let odysseus_king_of_ithaca = "Ο ΟΔΥΣΣΈΑΣ ΒΑΣΙΛΙΆΣ ΤΗΣ ΙΘΆΚΗΣ";
546    ///
547    /// assert_eq!("Ο οδυσσέας βασιλιάς της ιθάκης", odysseus_king_of_ithaca.word_to_titlecase());
548    /// ```
549    #[cfg(not(no_global_oom_handling))]
550    #[rustc_allow_incoherent_impl]
551    #[must_use = "this returns the titlecase word as a new String, \
552                  without modifying the original"]
553    #[unstable(feature = "titlecase", issue = "153892")]
554    pub fn word_to_titlecase(&self) -> String {
555        let mut s = String::with_capacity(self.len());
556        let mut chars = self.char_indices();
557
558        // The first cased character is title-cased; leading uncased characters pass through.
559        'until_first_cased_char: for (_, c) in chars.by_ref() {
560            if c.is_cased() {
561                s.extend(c.to_titlecase());
562                break 'until_first_cased_char;
563            } else {
564                s.push(c);
565            }
566        }
567
568        // Everything after the first cased character is lower-cased. Use the ASCII fast
569        // path (auto-vectorized) for its ASCII prefix, mirroring `to_lowercase`.
570        let remainder = chars.as_str();
571        let rest_start = self.len() - remainder.len();
572        // SAFETY: `to_ascii_lowercase` preserves ASCII bytes, so the prefix stays valid UTF-8.
573        let (ascii, rest) = unsafe { convert_while_ascii(remainder, u8::to_ascii_lowercase) };
574        s.push_str(&ascii);
575        let prefix_len = rest_start + ascii.len();
576
577        for (i, c) in rest.char_indices() {
578            if c == 'Σ' {
579                // Σ maps to σ, except at the end of a word where it maps to ς.
580                // This is the only conditional (contextual) but language-independent mapping
581                // in `SpecialCasing.txt`,
582                // so hard-code it rather than have a generic "condition" mechanism.
583                // See https://github.com/rust-lang/rust/issues/26035
584                let sigma_lowercase = map_uppercase_sigma(self, prefix_len + i);
585                s.push(sigma_lowercase);
586            } else {
587                match conversions::to_lower(c) {
588                    [a, '\0', _] => s.push(a),
589                    [a, b, '\0'] => {
590                        s.push(a);
591                        s.push(b);
592                    }
593                    [a, b, c] => {
594                        s.push(a);
595                        s.push(b);
596                        s.push(c);
597                    }
598                }
599            }
600        }
601
602        s
603    }
604
605    /// Returns the uppercase equivalent of this string slice, as a new [`String`].
606    ///
607    /// 'Uppercase' is defined according to the terms of
608    /// [Chapter 3 (Conformance)](https://www.unicode.org/versions/latest/core-spec/chapter-3/#G34431)
609    /// of the Unicode standard.
610    ///
611    /// Since some characters can expand into multiple characters when changing
612    /// the case, this function returns a [`String`] instead of modifying the
613    /// parameter in-place.
614    ///
615    /// Like [`char::to_uppercase()`] this method does not handle language-specific
616    /// casing, like Turkish and Azeri I/ı/İ/i. See that method's documentation
617    /// for more information.
618    ///
619    /// # Examples
620    ///
621    /// Basic usage:
622    ///
623    /// ```
624    /// let s = "hello world";
625    ///
626    /// assert_eq!("HELLO WORLD", s.to_uppercase());
627    /// ```
628    ///
629    /// Scripts without case are not changed:
630    ///
631    /// ```
632    /// let new_year = "农历新年";
633    ///
634    /// assert_eq!(new_year, new_year.to_uppercase());
635    /// ```
636    ///
637    /// One character can become multiple:
638    /// ```
639    /// let s = "tschüß";
640    ///
641    /// assert_eq!("TSCHÜSS", s.to_uppercase());
642    /// ```
643    #[cfg(not(no_global_oom_handling))]
644    #[rustc_allow_incoherent_impl]
645    #[must_use = "this returns the uppercase string as a new String, \
646                  without modifying the original"]
647    #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
648    pub fn to_uppercase(&self) -> String {
649        // SAFETY: `to_ascii_uppercase` preserves ASCII bytes, so the converted
650        // prefix remains valid UTF-8.
651        let (mut s, rest) = unsafe { convert_while_ascii(self, u8::to_ascii_uppercase) };
652
653        for c in rest.chars() {
654            match conversions::to_upper(c) {
655                [a, '\0', _] => s.push(a),
656                [a, b, '\0'] => {
657                    s.push(a);
658                    s.push(b);
659                }
660                [a, b, c] => {
661                    s.push(a);
662                    s.push(b);
663                    s.push(c);
664                }
665            }
666        }
667        s
668    }
669
670    /// Returns the case-folded equivalent of this string slice, as a new [`String`].
671    ///
672    /// Case folding is a transformation, mostly matching lowercase, that is meant to be used
673    /// for case-insensitive string comparisons. Case-folded strings should not usually
674    /// be exposed directly to users.
675    ///
676    /// For the precise specification of case folding, see
677    /// [Chapter 3 (Conformance)](https://www.unicode.org/versions/latest/core-spec/chapter-3/#G63737)
678    /// of the Unicode standard.
679    ///
680    /// Since some characters can expand into multiple characters when case folding,
681    /// this function returns a [`String`] instead of modifying the parameter in-place.
682    ///
683    /// No [normalization] (e.g. NFC) is performed, so visually and semantically identical strings
684    /// might still casefold differently. For example, `"Å"` (U+00C5 LATIN CAPITAL LETTER A WITH RING ABOVE)
685    /// is considered distinct from `"Å"` (A followed by U+030A COMBINING RING ABOVE),
686    /// even though Unicode considers them canonically equivalent.
687    ///
688    /// Like [`char::to_casefold_unnormalized()`] this method does not handle language-specific
689    /// casing, like Turkish and Azeri I/ı/İ/i. See that method's documentation
690    /// for more information.
691    ///
692    /// # Examples
693    ///
694    /// Basic usage:
695    ///
696    /// ```
697    /// #![feature(casefold)]
698    /// let s0 = "HELLO";
699    /// let s1 = "Hello";
700    ///
701    /// assert_eq!(s0.to_casefold_unnormalized(), s1.to_casefold_unnormalized());
702    /// assert_eq!(s0.to_casefold_unnormalized(), "hello")
703    /// ```
704    ///
705    /// Scripts without case are not changed:
706    ///
707    /// ```
708    /// #![feature(casefold)]
709    /// let new_year = "农历新年";
710    ///
711    /// assert_eq!(new_year, new_year.to_casefold_unnormalized());
712    /// ```
713    ///
714    /// One character can become multiple:
715    ///
716    /// ```
717    /// #![feature(casefold)]
718    /// let s0 = "TSCHÜẞ";
719    /// let s1 = "TSCHÜSS";
720    /// let s2 = "tschüß";
721    ///
722    /// assert_eq!(s0.to_casefold_unnormalized(), s1.to_casefold_unnormalized());
723    /// assert_eq!(s0.to_casefold_unnormalized(), s2.to_casefold_unnormalized());
724    /// assert_eq!(s0.to_casefold_unnormalized(), "tschüss");
725    /// ```
726    ///
727    /// No NFC [normalization] is performed:
728    ///
729    /// ```rust
730    /// #![feature(casefold)]
731    /// // These two strings are visually and semantically identical...
732    /// let comp = "Å";
733    /// let decomp = "Å";
734    ///
735    /// // ... but not codepoint-for-codepoint equal.
736    /// assert_eq!(comp, "\u{C5}");
737    /// assert_eq!(decomp, "A\u{030A}");
738    ///
739    /// // Their case-foldings are likewise unequal:
740    /// assert_eq!(comp.to_casefold_unnormalized(), "\u{E5}");
741    /// assert_eq!(decomp.to_casefold_unnormalized(), "a\u{030A}");
742    /// ```
743    ///
744    /// [normalization]: https://www.unicode.org/faq/normalization.html
745    #[cfg(not(no_global_oom_handling))]
746    #[rustc_allow_incoherent_impl]
747    #[must_use = "this returns the case-folded string as a new String, \
748                  without modifying the original"]
749    #[unstable(feature = "casefold", issue = "157000")]
750    pub fn to_casefold_unnormalized(&self) -> String {
751        // SAFETY: `to_ascii_lowercase` preserves ASCII bytes, so the converted
752        // prefix remains valid UTF-8.
753        let (mut s, rest) = unsafe { convert_while_ascii(self, u8::to_ascii_lowercase) };
754
755        for c in rest.chars() {
756            match conversions::to_casefold(c) {
757                [a, '\0', _] => s.push(a),
758                [a, b, '\0'] => {
759                    s.push(a);
760                    s.push(b);
761                }
762                [a, b, c] => {
763                    s.push(a);
764                    s.push(b);
765                    s.push(c);
766                }
767            }
768        }
769        s
770    }
771
772    /// Converts a [`Box<str>`] into a [`String`] without copying or allocating.
773    ///
774    /// # Examples
775    ///
776    /// ```
777    /// let string = String::from("birthday gift");
778    /// let boxed_str = string.clone().into_boxed_str();
779    ///
780    /// assert_eq!(boxed_str.into_string(), string);
781    /// ```
782    #[stable(feature = "box_str", since = "1.4.0")]
783    #[rustc_allow_incoherent_impl]
784    #[must_use = "`self` will be dropped if the result is not used"]
785    #[inline]
786    pub fn into_string(self: Box<Self>) -> String {
787        let slice = Box::<[u8]>::from(self);
788        unsafe { String::from_utf8_unchecked(slice.into_vec()) }
789    }
790
791    /// Creates a new [`String`] by repeating a string `n` times.
792    ///
793    /// # Panics
794    ///
795    /// This function will panic if the capacity would overflow.
796    ///
797    /// # Examples
798    ///
799    /// Basic usage:
800    ///
801    /// ```
802    /// assert_eq!("abc".repeat(4), String::from("abcabcabcabc"));
803    /// ```
804    ///
805    /// A panic upon overflow:
806    ///
807    /// ```should_panic
808    /// // this will panic at runtime
809    /// let huge = "0123456789abcdef".repeat(usize::MAX);
810    /// ```
811    #[cfg(not(no_global_oom_handling))]
812    #[rustc_allow_incoherent_impl]
813    #[must_use]
814    #[stable(feature = "repeat_str", since = "1.16.0")]
815    #[inline]
816    pub fn repeat(&self, n: usize) -> String {
817        unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) }
818    }
819
820    /// Returns a copy of this string where each character is mapped to its
821    /// ASCII upper case equivalent.
822    ///
823    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
824    /// but non-ASCII letters are unchanged.
825    ///
826    /// To uppercase the value in-place, use [`make_ascii_uppercase`].
827    ///
828    /// To uppercase ASCII characters in addition to non-ASCII characters, use
829    /// [`to_uppercase`].
830    ///
831    /// # Examples
832    ///
833    /// ```
834    /// let s = "Grüße, Jürgen ❤";
835    ///
836    /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
837    /// ```
838    ///
839    /// [`make_ascii_uppercase`]: str::make_ascii_uppercase
840    /// [`to_uppercase`]: #method.to_uppercase
841    #[cfg(not(no_global_oom_handling))]
842    #[rustc_allow_incoherent_impl]
843    #[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
844    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
845    #[inline]
846    pub fn to_ascii_uppercase(&self) -> String {
847        let bytes = self.as_bytes().to_ascii_uppercase();
848        // SAFETY: ASCII case conversion only maps a-z to A-Z and leaves
849        // all other bytes unchanged as valid UTF-8
850        unsafe { String::from_utf8_unchecked(bytes) }
851    }
852
853    /// Returns a copy of this string where each character is mapped to its
854    /// ASCII lower case equivalent.
855    ///
856    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
857    /// but non-ASCII letters are unchanged.
858    ///
859    /// To lowercase the value in-place, use [`make_ascii_lowercase`].
860    ///
861    /// To lowercase ASCII characters in addition to non-ASCII characters, use
862    /// [`to_lowercase`].
863    ///
864    /// # Examples
865    ///
866    /// ```
867    /// let s = "Grüße, Jürgen ❤";
868    ///
869    /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
870    /// ```
871    ///
872    /// [`make_ascii_lowercase`]: str::make_ascii_lowercase
873    /// [`to_lowercase`]: #method.to_lowercase
874    #[cfg(not(no_global_oom_handling))]
875    #[rustc_allow_incoherent_impl]
876    #[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
877    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
878    #[inline]
879    pub fn to_ascii_lowercase(&self) -> String {
880        let bytes = self.as_bytes().to_ascii_lowercase();
881        // SAFETY: ASCII case conversion only maps A-Z to a-z and leaves
882        // all other bytes unchanged as valid UTF-8
883        unsafe { String::from_utf8_unchecked(bytes) }
884    }
885}
886
887/// Converts a boxed slice of bytes to a boxed string slice without checking
888/// that the string contains valid UTF-8.
889///
890/// # Safety
891///
892/// * The provided bytes must contain a valid UTF-8 sequence.
893///
894/// # Examples
895///
896/// ```
897/// let smile_utf8 = Box::new([226, 152, 186]);
898/// let smile = unsafe { std::str::from_boxed_utf8_unchecked(smile_utf8) };
899///
900/// assert_eq!("☺", &*smile);
901/// ```
902#[stable(feature = "str_box_extras", since = "1.20.0")]
903#[must_use]
904#[inline]
905pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
906    unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }
907}
908
909/// Converts leading ascii bytes in `s` by calling the `convert` function.
910///
911/// For better average performance, this happens in chunks of `2*size_of::<usize>()`.
912///
913/// Returns a tuple of the converted prefix and the remainder starting from
914/// the first non-ascii character.
915///
916/// This function is only public so that it can be verified in a codegen test,
917/// see `issue-123712-str-to-lower-autovectorization.rs`.
918///
919/// # Safety
920///
921/// `convert` must return an ASCII byte for every ASCII input byte.
922#[unstable(feature = "str_internals", issue = "none")]
923#[doc(hidden)]
924#[inline]
925#[cfg(not(no_global_oom_handling))]
926pub unsafe fn convert_while_ascii(s: &str, convert: fn(&u8) -> u8) -> (String, &str) {
927    // Process the input in chunks of 16 bytes to enable auto-vectorization.
928    // Previously the chunk size depended on the size of `usize`,
929    // but on 32-bit platforms with sse or neon is also the better choice.
930    // The only downside on other platforms would be a bit more loop-unrolling.
931    const N: usize = 16;
932
933    let mut slice = s.as_bytes();
934    let mut out = Vec::with_capacity(slice.len());
935    let mut out_slice = out.spare_capacity_mut();
936
937    let mut ascii_prefix_len = 0_usize;
938    let mut is_ascii = [false; N];
939
940    while slice.len() >= N {
941        // SAFETY: checked in loop condition
942        let chunk = unsafe { slice.get_unchecked(..N) };
943        // SAFETY: out_slice has at least same length as input slice and gets sliced with the same offsets
944        let out_chunk = unsafe { out_slice.get_unchecked_mut(..N) };
945
946        for j in 0..N {
947            is_ascii[j] = chunk[j] <= 127;
948        }
949
950        // Auto-vectorization for this check is a bit fragile, sum and comparing against the chunk
951        // size gives the best result, specifically a pmovmsk instruction on x86.
952        // See https://github.com/llvm/llvm-project/issues/96395 for why llvm currently does not
953        // currently recognize other similar idioms.
954        if is_ascii.iter().map(|x| *x as u8).sum::<u8>() as usize != N {
955            break;
956        }
957
958        for j in 0..N {
959            out_chunk[j] = MaybeUninit::new(convert(&chunk[j]));
960        }
961
962        ascii_prefix_len += N;
963        slice = unsafe { slice.get_unchecked(N..) };
964        out_slice = unsafe { out_slice.get_unchecked_mut(N..) };
965    }
966
967    // handle the remainder as individual bytes
968    while slice.len() > 0 {
969        let byte = slice[0];
970        if byte > 127 {
971            break;
972        }
973        // SAFETY: out_slice has at least same length as input slice
974        unsafe {
975            *out_slice.get_unchecked_mut(0) = MaybeUninit::new(convert(&byte));
976        }
977        ascii_prefix_len += 1;
978        slice = unsafe { slice.get_unchecked(1..) };
979        out_slice = unsafe { out_slice.get_unchecked_mut(1..) };
980    }
981
982    unsafe {
983        // SAFETY: ascii_prefix_len bytes have been initialized above
984        out.set_len(ascii_prefix_len);
985
986        // SAFETY: We have written only valid ascii to the output vec
987        let ascii_string = String::from_utf8_unchecked(out);
988
989        // SAFETY: we know this is a valid char boundary
990        // since we only skipped over leading ascii bytes
991        let rest = core::str::from_utf8_unchecked(slice);
992
993        (ascii_string, rest)
994    }
995}
996#[inline]
997#[cfg(not(no_global_oom_handling))]
998#[allow(dead_code)]
999/// Faster implementation of string replacement for ASCII to ASCII cases.
1000/// Should produce fast vectorized code.
1001unsafe fn replace_ascii(utf8_bytes: &[u8], from: u8, to: u8) -> String {
1002    let result: Vec<u8> = utf8_bytes.iter().map(|b| if *b == from { to } else { *b }).collect();
1003    // SAFETY: We replaced ascii with ascii on valid utf8 strings.
1004    unsafe { String::from_utf8_unchecked(result) }
1005}