]> git.lizzy.rs Git - rust.git/blob - src/liballoc/str.rs
Rollup merge of #68279 - GuillaumeGomez:clean-up-e0198, r=Dylan-DPC
[rust.git] / src / liballoc / str.rs
1 //! Unicode string slices.
2 //!
3 //! *[See also the `str` primitive type](../../std/primitive.str.html).*
4 //!
5 //! The `&str` type is one of the two main string types, the other being `String`.
6 //! Unlike its `String` counterpart, its contents are borrowed.
7 //!
8 //! # Basic Usage
9 //!
10 //! A basic string declaration of `&str` type:
11 //!
12 //! ```
13 //! let hello_world = "Hello, World!";
14 //! ```
15 //!
16 //! Here we have declared a string literal, also known as a string slice.
17 //! String literals have a static lifetime, which means the string `hello_world`
18 //! is guaranteed to be valid for the duration of the entire program.
19 //! We can explicitly specify `hello_world`'s lifetime as well:
20 //!
21 //! ```
22 //! let hello_world: &'static str = "Hello, world!";
23 //! ```
24
25 #![stable(feature = "rust1", since = "1.0.0")]
26 // Many of the usings in this module are only used in the test configuration.
27 // It's cleaner to just turn off the unused_imports warning than to fix them.
28 #![allow(unused_imports)]
29
30 use core::borrow::{Borrow, BorrowMut};
31 use core::iter::FusedIterator;
32 use core::mem;
33 use core::ptr;
34 use core::str::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
35 use core::unicode::conversions;
36
37 use crate::borrow::ToOwned;
38 use crate::boxed::Box;
39 use crate::slice::{Concat, Join, SliceIndex};
40 use crate::string::String;
41 use crate::vec::Vec;
42
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub use core::str::pattern;
45 #[stable(feature = "encode_utf16", since = "1.8.0")]
46 pub use core::str::EncodeUtf16;
47 #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
48 pub use core::str::SplitAsciiWhitespace;
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub use core::str::SplitWhitespace;
51 #[stable(feature = "rust1", since = "1.0.0")]
52 pub use core::str::{from_utf8, from_utf8_mut, Bytes, CharIndices, Chars};
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub use core::str::{from_utf8_unchecked, from_utf8_unchecked_mut, ParseBoolError};
55 #[stable(feature = "str_escape", since = "1.34.0")]
56 pub use core::str::{EscapeDebug, EscapeDefault, EscapeUnicode};
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub use core::str::{FromStr, Utf8Error};
59 #[allow(deprecated)]
60 #[stable(feature = "rust1", since = "1.0.0")]
61 pub use core::str::{Lines, LinesAny};
62 #[stable(feature = "rust1", since = "1.0.0")]
63 pub use core::str::{MatchIndices, RMatchIndices};
64 #[stable(feature = "rust1", since = "1.0.0")]
65 pub use core::str::{Matches, RMatches};
66 #[stable(feature = "rust1", since = "1.0.0")]
67 pub use core::str::{RSplit, Split};
68 #[stable(feature = "rust1", since = "1.0.0")]
69 pub use core::str::{RSplitN, SplitN};
70 #[stable(feature = "rust1", since = "1.0.0")]
71 pub use core::str::{RSplitTerminator, SplitTerminator};
72
73 /// Note: `str` in `Concat<str>` is not meaningful here.
74 /// This type parameter of the trait only exists to enable another impl.
75 #[unstable(feature = "slice_concat_ext", issue = "27747")]
76 impl<S: Borrow<str>> Concat<str> for [S] {
77     type Output = String;
78
79     fn concat(slice: &Self) -> String {
80         Join::join(slice, "")
81     }
82 }
83
84 #[unstable(feature = "slice_concat_ext", issue = "27747")]
85 impl<S: Borrow<str>> Join<&str> for [S] {
86     type Output = String;
87
88     fn join(slice: &Self, sep: &str) -> String {
89         unsafe { String::from_utf8_unchecked(join_generic_copy(slice, sep.as_bytes())) }
90     }
91 }
92
93 macro_rules! spezialize_for_lengths {
94     ($separator:expr, $target:expr, $iter:expr; $($num:expr),*) => {
95         let mut target = $target;
96         let iter = $iter;
97         let sep_bytes = $separator;
98         match $separator.len() {
99             $(
100                 // loops with hardcoded sizes run much faster
101                 // specialize the cases with small separator lengths
102                 $num => {
103                     for s in iter {
104                         copy_slice_and_advance!(target, sep_bytes);
105                         copy_slice_and_advance!(target, s.borrow().as_ref());
106                     }
107                 },
108             )*
109             _ => {
110                 // arbitrary non-zero size fallback
111                 for s in iter {
112                     copy_slice_and_advance!(target, sep_bytes);
113                     copy_slice_and_advance!(target, s.borrow().as_ref());
114                 }
115             }
116         }
117     };
118 }
119
120 macro_rules! copy_slice_and_advance {
121     ($target:expr, $bytes:expr) => {
122         let len = $bytes.len();
123         let (head, tail) = { $target }.split_at_mut(len);
124         head.copy_from_slice($bytes);
125         $target = tail;
126     };
127 }
128
129 // Optimized join implementation that works for both Vec<T> (T: Copy) and String's inner vec
130 // Currently (2018-05-13) there is a bug with type inference and specialization (see issue #36262)
131 // For this reason SliceConcat<T> is not specialized for T: Copy and SliceConcat<str> is the
132 // only user of this function. It is left in place for the time when that is fixed.
133 //
134 // the bounds for String-join are S: Borrow<str> and for Vec-join Borrow<[T]>
135 // [T] and str both impl AsRef<[T]> for some T
136 // => s.borrow().as_ref() and we always have slices
137 fn join_generic_copy<B, T, S>(slice: &[S], sep: &[T]) -> Vec<T>
138 where
139     T: Copy,
140     B: AsRef<[T]> + ?Sized,
141     S: Borrow<B>,
142 {
143     let sep_len = sep.len();
144     let mut iter = slice.iter();
145
146     // the first slice is the only one without a separator preceding it
147     let first = match iter.next() {
148         Some(first) => first,
149         None => return vec![],
150     };
151
152     // compute the exact total length of the joined Vec
153     // if the `len` calculation overflows, we'll panic
154     // we would have run out of memory anyway and the rest of the function requires
155     // the entire Vec pre-allocated for safety
156     let len = sep_len
157         .checked_mul(iter.len())
158         .and_then(|n| {
159             slice.iter().map(|s| s.borrow().as_ref().len()).try_fold(n, usize::checked_add)
160         })
161         .expect("attempt to join into collection with len > usize::MAX");
162
163     // crucial for safety
164     let mut result = Vec::with_capacity(len);
165     assert!(result.capacity() >= len);
166
167     result.extend_from_slice(first.borrow().as_ref());
168
169     unsafe {
170         {
171             let pos = result.len();
172             let target = result.get_unchecked_mut(pos..len);
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             spezialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
178         }
179         result.set_len(len);
180     }
181     result
182 }
183
184 #[stable(feature = "rust1", since = "1.0.0")]
185 impl Borrow<str> for String {
186     #[inline]
187     fn borrow(&self) -> &str {
188         &self[..]
189     }
190 }
191
192 #[stable(feature = "string_borrow_mut", since = "1.36.0")]
193 impl BorrowMut<str> for String {
194     #[inline]
195     fn borrow_mut(&mut self) -> &mut str {
196         &mut self[..]
197     }
198 }
199
200 #[stable(feature = "rust1", since = "1.0.0")]
201 impl ToOwned for str {
202     type Owned = String;
203     #[inline]
204     fn to_owned(&self) -> String {
205         unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
206     }
207
208     fn clone_into(&self, target: &mut String) {
209         let mut b = mem::take(target).into_bytes();
210         self.as_bytes().clone_into(&mut b);
211         *target = unsafe { String::from_utf8_unchecked(b) }
212     }
213 }
214
215 /// Methods for string slices.
216 #[lang = "str_alloc"]
217 #[cfg(not(test))]
218 impl str {
219     /// Converts a `Box<str>` into a `Box<[u8]>` without copying or allocating.
220     ///
221     /// # Examples
222     ///
223     /// Basic usage:
224     ///
225     /// ```
226     /// let s = "this is a string";
227     /// let boxed_str = s.to_owned().into_boxed_str();
228     /// let boxed_bytes = boxed_str.into_boxed_bytes();
229     /// assert_eq!(*boxed_bytes, *s.as_bytes());
230     /// ```
231     #[stable(feature = "str_box_extras", since = "1.20.0")]
232     #[inline]
233     pub fn into_boxed_bytes(self: Box<str>) -> Box<[u8]> {
234         self.into()
235     }
236
237     /// Replaces all matches of a pattern with another string.
238     ///
239     /// `replace` creates a new [`String`], and copies the data from this string slice into it.
240     /// While doing so, it attempts to find matches of a pattern. If it finds any, it
241     /// replaces them with the replacement string slice.
242     ///
243     /// [`String`]: string/struct.String.html
244     ///
245     /// # Examples
246     ///
247     /// Basic usage:
248     ///
249     /// ```
250     /// let s = "this is old";
251     ///
252     /// assert_eq!("this is new", s.replace("old", "new"));
253     /// ```
254     ///
255     /// When the pattern doesn't match:
256     ///
257     /// ```
258     /// let s = "this is old";
259     /// assert_eq!(s, s.replace("cookie monster", "little lamb"));
260     /// ```
261     #[must_use = "this returns the replaced string as a new allocation, \
262                   without modifying the original"]
263     #[stable(feature = "rust1", since = "1.0.0")]
264     #[inline]
265     pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
266         let mut result = String::new();
267         let mut last_end = 0;
268         for (start, part) in self.match_indices(from) {
269             result.push_str(unsafe { self.get_unchecked(last_end..start) });
270             result.push_str(to);
271             last_end = start + part.len();
272         }
273         result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
274         result
275     }
276
277     /// Replaces first N matches of a pattern with another string.
278     ///
279     /// `replacen` creates a new [`String`], and copies the data from this string slice into it.
280     /// While doing so, it attempts to find matches of a pattern. If it finds any, it
281     /// replaces them with the replacement string slice at most `count` times.
282     ///
283     /// [`String`]: string/struct.String.html
284     ///
285     /// # Examples
286     ///
287     /// Basic usage:
288     ///
289     /// ```
290     /// let s = "foo foo 123 foo";
291     /// assert_eq!("new new 123 foo", s.replacen("foo", "new", 2));
292     /// assert_eq!("faa fao 123 foo", s.replacen('o', "a", 3));
293     /// assert_eq!("foo foo new23 foo", s.replacen(char::is_numeric, "new", 1));
294     /// ```
295     ///
296     /// When the pattern doesn't match:
297     ///
298     /// ```
299     /// let s = "this is old";
300     /// assert_eq!(s, s.replacen("cookie monster", "little lamb", 10));
301     /// ```
302     #[must_use = "this returns the replaced string as a new allocation, \
303                   without modifying the original"]
304     #[stable(feature = "str_replacen", since = "1.16.0")]
305     pub fn replacen<'a, P: Pattern<'a>>(&'a self, pat: P, to: &str, count: usize) -> String {
306         // Hope to reduce the times of re-allocation
307         let mut result = String::with_capacity(32);
308         let mut last_end = 0;
309         for (start, part) in self.match_indices(pat).take(count) {
310             result.push_str(unsafe { self.get_unchecked(last_end..start) });
311             result.push_str(to);
312             last_end = start + part.len();
313         }
314         result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
315         result
316     }
317
318     /// Returns the lowercase equivalent of this string slice, as a new [`String`].
319     ///
320     /// 'Lowercase' is defined according to the terms of the Unicode Derived Core Property
321     /// `Lowercase`.
322     ///
323     /// Since some characters can expand into multiple characters when changing
324     /// the case, this function returns a [`String`] instead of modifying the
325     /// parameter in-place.
326     ///
327     /// [`String`]: string/struct.String.html
328     ///
329     /// # Examples
330     ///
331     /// Basic usage:
332     ///
333     /// ```
334     /// let s = "HELLO";
335     ///
336     /// assert_eq!("hello", s.to_lowercase());
337     /// ```
338     ///
339     /// A tricky example, with sigma:
340     ///
341     /// ```
342     /// let sigma = "Σ";
343     ///
344     /// assert_eq!("σ", sigma.to_lowercase());
345     ///
346     /// // but at the end of a word, it's ς, not σ:
347     /// let odysseus = "ὈΔΥΣΣΕΎΣ";
348     ///
349     /// assert_eq!("ὀδυσσεύς", odysseus.to_lowercase());
350     /// ```
351     ///
352     /// Languages without case are not changed:
353     ///
354     /// ```
355     /// let new_year = "农历新年";
356     ///
357     /// assert_eq!(new_year, new_year.to_lowercase());
358     /// ```
359     #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
360     pub fn to_lowercase(&self) -> String {
361         let mut s = String::with_capacity(self.len());
362         for (i, c) in self[..].char_indices() {
363             if c == 'Σ' {
364                 // Σ maps to σ, except at the end of a word where it maps to ς.
365                 // This is the only conditional (contextual) but language-independent mapping
366                 // in `SpecialCasing.txt`,
367                 // so hard-code it rather than have a generic "condition" mechanism.
368                 // See https://github.com/rust-lang/rust/issues/26035
369                 map_uppercase_sigma(self, i, &mut s)
370             } else {
371                 match conversions::to_lower(c) {
372                     [a, '\0', _] => s.push(a),
373                     [a, b, '\0'] => {
374                         s.push(a);
375                         s.push(b);
376                     }
377                     [a, b, c] => {
378                         s.push(a);
379                         s.push(b);
380                         s.push(c);
381                     }
382                 }
383             }
384         }
385         return s;
386
387         fn map_uppercase_sigma(from: &str, i: usize, to: &mut String) {
388             // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
389             // for the definition of `Final_Sigma`.
390             debug_assert!('Σ'.len_utf8() == 2);
391             let is_word_final = case_ignoreable_then_cased(from[..i].chars().rev())
392                 && !case_ignoreable_then_cased(from[i + 2..].chars());
393             to.push_str(if is_word_final { "ς" } else { "σ" });
394         }
395
396         fn case_ignoreable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
397             use core::unicode::derived_property::{Case_Ignorable, Cased};
398             match iter.skip_while(|&c| Case_Ignorable(c)).next() {
399                 Some(c) => Cased(c),
400                 None => false,
401             }
402         }
403     }
404
405     /// Returns the uppercase equivalent of this string slice, as a new [`String`].
406     ///
407     /// 'Uppercase' is defined according to the terms of the Unicode Derived Core Property
408     /// `Uppercase`.
409     ///
410     /// Since some characters can expand into multiple characters when changing
411     /// the case, this function returns a [`String`] instead of modifying the
412     /// parameter in-place.
413     ///
414     /// [`String`]: string/struct.String.html
415     ///
416     /// # Examples
417     ///
418     /// Basic usage:
419     ///
420     /// ```
421     /// let s = "hello";
422     ///
423     /// assert_eq!("HELLO", s.to_uppercase());
424     /// ```
425     ///
426     /// Scripts without case are not changed:
427     ///
428     /// ```
429     /// let new_year = "农历新年";
430     ///
431     /// assert_eq!(new_year, new_year.to_uppercase());
432     /// ```
433     ///
434     /// One character can become multiple:
435     /// ```
436     /// let s = "tschüß";
437     ///
438     /// assert_eq!("TSCHÜSS", s.to_uppercase());
439     /// ```
440     #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
441     pub fn to_uppercase(&self) -> String {
442         let mut s = String::with_capacity(self.len());
443         for c in self[..].chars() {
444             match conversions::to_upper(c) {
445                 [a, '\0', _] => s.push(a),
446                 [a, b, '\0'] => {
447                     s.push(a);
448                     s.push(b);
449                 }
450                 [a, b, c] => {
451                     s.push(a);
452                     s.push(b);
453                     s.push(c);
454                 }
455             }
456         }
457         s
458     }
459
460     /// Converts a [`Box<str>`] into a [`String`] without copying or allocating.
461     ///
462     /// [`String`]: string/struct.String.html
463     /// [`Box<str>`]: boxed/struct.Box.html
464     ///
465     /// # Examples
466     ///
467     /// Basic usage:
468     ///
469     /// ```
470     /// let string = String::from("birthday gift");
471     /// let boxed_str = string.clone().into_boxed_str();
472     ///
473     /// assert_eq!(boxed_str.into_string(), string);
474     /// ```
475     #[stable(feature = "box_str", since = "1.4.0")]
476     #[inline]
477     pub fn into_string(self: Box<str>) -> String {
478         let slice = Box::<[u8]>::from(self);
479         unsafe { String::from_utf8_unchecked(slice.into_vec()) }
480     }
481
482     /// Creates a new [`String`] by repeating a string `n` times.
483     ///
484     /// # Panics
485     ///
486     /// This function will panic if the capacity would overflow.
487     ///
488     /// [`String`]: string/struct.String.html
489     ///
490     /// # Examples
491     ///
492     /// Basic usage:
493     ///
494     /// ```
495     /// assert_eq!("abc".repeat(4), String::from("abcabcabcabc"));
496     /// ```
497     ///
498     /// A panic upon overflow:
499     ///
500     /// ```should_panic
501     /// // this will panic at runtime
502     /// "0123456789abcdef".repeat(usize::max_value());
503     /// ```
504     #[stable(feature = "repeat_str", since = "1.16.0")]
505     pub fn repeat(&self, n: usize) -> String {
506         unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) }
507     }
508
509     /// Returns a copy of this string where each character is mapped to its
510     /// ASCII upper case equivalent.
511     ///
512     /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
513     /// but non-ASCII letters are unchanged.
514     ///
515     /// To uppercase the value in-place, use [`make_ascii_uppercase`].
516     ///
517     /// To uppercase ASCII characters in addition to non-ASCII characters, use
518     /// [`to_uppercase`].
519     ///
520     /// # Examples
521     ///
522     /// ```
523     /// let s = "Grüße, Jürgen ❤";
524     ///
525     /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
526     /// ```
527     ///
528     /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
529     /// [`to_uppercase`]: #method.to_uppercase
530     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
531     #[inline]
532     pub fn to_ascii_uppercase(&self) -> String {
533         let mut bytes = self.as_bytes().to_vec();
534         bytes.make_ascii_uppercase();
535         // make_ascii_uppercase() preserves the UTF-8 invariant.
536         unsafe { String::from_utf8_unchecked(bytes) }
537     }
538
539     /// Returns a copy of this string where each character is mapped to its
540     /// ASCII lower case equivalent.
541     ///
542     /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
543     /// but non-ASCII letters are unchanged.
544     ///
545     /// To lowercase the value in-place, use [`make_ascii_lowercase`].
546     ///
547     /// To lowercase ASCII characters in addition to non-ASCII characters, use
548     /// [`to_lowercase`].
549     ///
550     /// # Examples
551     ///
552     /// ```
553     /// let s = "Grüße, Jürgen ❤";
554     ///
555     /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
556     /// ```
557     ///
558     /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
559     /// [`to_lowercase`]: #method.to_lowercase
560     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
561     #[inline]
562     pub fn to_ascii_lowercase(&self) -> String {
563         let mut bytes = self.as_bytes().to_vec();
564         bytes.make_ascii_lowercase();
565         // make_ascii_lowercase() preserves the UTF-8 invariant.
566         unsafe { String::from_utf8_unchecked(bytes) }
567     }
568 }
569
570 /// Converts a boxed slice of bytes to a boxed string slice without checking
571 /// that the string contains valid UTF-8.
572 ///
573 /// # Examples
574 ///
575 /// Basic usage:
576 ///
577 /// ```
578 /// let smile_utf8 = Box::new([226, 152, 186]);
579 /// let smile = unsafe { std::str::from_boxed_utf8_unchecked(smile_utf8) };
580 ///
581 /// assert_eq!("☺", &*smile);
582 /// ```
583 #[stable(feature = "str_box_extras", since = "1.20.0")]
584 #[inline]
585 pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
586     Box::from_raw(Box::into_raw(v) as *mut str)
587 }