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