core/str/mod.rs
1//! String manipulation.
2//!
3//! For more details, see the [`std::str`] module.
4//!
5//! [`std::str`]: ../../std/str/index.html
6
7#![stable(feature = "rust1", since = "1.0.0")]
8
9mod converts;
10mod count;
11mod error;
12mod iter;
13mod traits;
14mod validations;
15
16use self::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
17use crate::char::{self, EscapeDebugExtArgs};
18use crate::hint::assert_unchecked;
19use crate::range::Range;
20use crate::slice::{self, SliceIndex};
21use crate::ub_checks::assert_unsafe_precondition;
22use crate::{ascii, mem};
23
24pub mod pattern;
25
26mod lossy;
27#[unstable(feature = "str_from_raw_parts", issue = "119206")]
28pub use converts::{from_raw_parts, from_raw_parts_mut};
29#[stable(feature = "rust1", since = "1.0.0")]
30pub use converts::{from_utf8, from_utf8_unchecked};
31#[stable(feature = "str_mut_extras", since = "1.20.0")]
32pub use converts::{from_utf8_mut, from_utf8_unchecked_mut};
33#[doc(hidden)]
34#[unstable(feature = "ferrocene_test", issue = "none")]
35pub use count::ferrocene_test::test_do_count_chars;
36#[stable(feature = "rust1", since = "1.0.0")]
37pub use error::{ParseBoolError, Utf8Error};
38#[stable(feature = "encode_utf16", since = "1.8.0")]
39pub use iter::EncodeUtf16;
40#[stable(feature = "rust1", since = "1.0.0")]
41#[allow(deprecated)]
42pub use iter::LinesAny;
43#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
44pub use iter::SplitAsciiWhitespace;
45#[stable(feature = "split_inclusive", since = "1.51.0")]
46pub use iter::SplitInclusive;
47#[stable(feature = "rust1", since = "1.0.0")]
48pub use iter::{Bytes, CharIndices, Chars, Lines, SplitWhitespace};
49#[stable(feature = "str_escape", since = "1.34.0")]
50pub use iter::{EscapeDebug, EscapeDefault, EscapeUnicode};
51#[stable(feature = "str_match_indices", since = "1.5.0")]
52pub use iter::{MatchIndices, RMatchIndices};
53use iter::{MatchIndicesInternal, MatchesInternal, SplitInternal, SplitNInternal};
54#[stable(feature = "str_matches", since = "1.2.0")]
55pub use iter::{Matches, RMatches};
56#[stable(feature = "rust1", since = "1.0.0")]
57pub use iter::{RSplit, RSplitTerminator, Split, SplitTerminator};
58#[stable(feature = "rust1", since = "1.0.0")]
59pub use iter::{RSplitN, SplitN};
60#[stable(feature = "utf8_chunks", since = "1.79.0")]
61pub use lossy::{Utf8Chunk, Utf8Chunks};
62#[stable(feature = "rust1", since = "1.0.0")]
63pub use traits::FromStr;
64#[unstable(feature = "str_internals", issue = "none")]
65pub use validations::{next_code_point, utf8_char_width};
66
67#[inline(never)]
68#[cold]
69#[track_caller]
70#[rustc_allow_const_fn_unstable(const_eval_select)]
71#[cfg(not(panic = "immediate-abort"))]
72#[ferrocene::prevalidated]
73const fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
74 crate::intrinsics::const_eval_select((s, begin, end), slice_error_fail_ct, slice_error_fail_rt)
75}
76
77#[cfg(panic = "immediate-abort")]
78const fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
79 slice_error_fail_ct(s, begin, end)
80}
81
82#[track_caller]
83#[ferrocene::annotation("Cannot be covered as this only runs during compilation.")]
84#[ferrocene::prevalidated]
85const fn slice_error_fail_ct(_: &str, _: usize, _: usize) -> ! {
86 panic!("failed to slice string");
87}
88
89#[track_caller]
90#[ferrocene::prevalidated]
91fn slice_error_fail_rt(s: &str, begin: usize, end: usize) -> ! {
92 let len = s.len();
93
94 // 1. begin is OOB.
95 if begin > len {
96 panic!("start byte index {begin} is out of bounds for string of length {len}");
97 }
98
99 // 2. end is OOB.
100 if end > len {
101 panic!("end byte index {end} is out of bounds for string of length {len}");
102 }
103
104 // 3. range is backwards.
105 if begin > end {
106 panic!("byte range starts at {begin} but ends at {end}");
107 }
108
109 // 4. begin is inside a character.
110 if !s.is_char_boundary(begin) {
111 let floor = s.floor_char_boundary(begin);
112 let ceil = s.ceil_char_boundary(begin);
113 let range = floor..ceil;
114 let ch = s[floor..ceil].chars().next().unwrap();
115 panic!(
116 "start byte index {begin} is not a char boundary; it is inside {ch:?} (bytes {range:?} of string)"
117 )
118 }
119
120 // 5. end is inside a character.
121 if !s.is_char_boundary(end) {
122 let floor = s.floor_char_boundary(end);
123 let ceil = s.ceil_char_boundary(end);
124 let range = floor..ceil;
125 let ch = s[floor..ceil].chars().next().unwrap();
126 panic!(
127 "end byte index {end} is not a char boundary; it is inside {ch:?} (bytes {range:?} of string)"
128 )
129 }
130
131 // 6. end is OOB and range is inclusive (end == len).
132 // This test cannot be combined with 2. above because for cases like
133 // `"abcαβγ"[4..9]` the error is that 4 is inside 'α', not that 9 is OOB.
134 debug_assert_eq!(end, len);
135 panic!("end byte index {end} is out of bounds for string of length {len}");
136}
137
138impl str {
139 /// Returns the length of `self`.
140 ///
141 /// This length is in bytes, not [`char`]s or graphemes. In other words,
142 /// it might not be what a human considers the length of the string.
143 ///
144 /// [`char`]: prim@char
145 ///
146 /// # Examples
147 ///
148 /// ```
149 /// let len = "foo".len();
150 /// assert_eq!(3, len);
151 ///
152 /// assert_eq!("ƒoo".len(), 4); // fancy f!
153 /// assert_eq!("ƒoo".chars().count(), 3);
154 /// ```
155 #[stable(feature = "rust1", since = "1.0.0")]
156 #[rustc_const_stable(feature = "const_str_len", since = "1.39.0")]
157 #[rustc_diagnostic_item = "str_len"]
158 #[rustc_no_implicit_autorefs]
159 #[must_use]
160 #[inline]
161 #[ferrocene::prevalidated]
162 pub const fn len(&self) -> usize {
163 self.as_bytes().len()
164 }
165
166 /// Returns `true` if `self` has a length of zero bytes.
167 ///
168 /// # Examples
169 ///
170 /// ```
171 /// let s = "";
172 /// assert!(s.is_empty());
173 ///
174 /// let s = "not empty";
175 /// assert!(!s.is_empty());
176 /// ```
177 #[stable(feature = "rust1", since = "1.0.0")]
178 #[rustc_const_stable(feature = "const_str_is_empty", since = "1.39.0")]
179 #[rustc_no_implicit_autorefs]
180 #[must_use]
181 #[inline]
182 #[ferrocene::prevalidated]
183 pub const fn is_empty(&self) -> bool {
184 self.len() == 0
185 }
186
187 /// Converts a slice of bytes to a string slice.
188 ///
189 /// A string slice ([`&str`]) is made of bytes ([`u8`]), and a byte slice
190 /// ([`&[u8]`][byteslice]) is made of bytes, so this function converts between
191 /// the two. Not all byte slices are valid string slices, however: [`&str`] requires
192 /// that it is valid UTF-8. `from_utf8()` checks to ensure that the bytes are valid
193 /// UTF-8, and then does the conversion.
194 ///
195 /// [`&str`]: str
196 /// [byteslice]: prim@slice
197 ///
198 /// If you are sure that the byte slice is valid UTF-8, and you don't want to
199 /// incur the overhead of the validity check, there is an unsafe version of
200 /// this function, [`from_utf8_unchecked`], which has the same
201 /// behavior but skips the check.
202 ///
203 /// If you need a `String` instead of a `&str`, consider
204 /// [`String::from_utf8`][string].
205 ///
206 /// [string]: ../std/string/struct.String.html#method.from_utf8
207 ///
208 /// Because you can stack-allocate a `[u8; N]`, and you can take a
209 /// [`&[u8]`][byteslice] of it, this function is one way to have a
210 /// stack-allocated string. There is an example of this in the
211 /// examples section below.
212 ///
213 /// [byteslice]: slice
214 ///
215 /// # Errors
216 ///
217 /// Returns `Err` if the slice is not UTF-8 with a description as to why the
218 /// provided slice is not UTF-8.
219 ///
220 /// # Examples
221 ///
222 /// Basic usage:
223 ///
224 /// ```
225 /// // some bytes, in a vector
226 /// let sparkle_heart = vec![240, 159, 146, 150];
227 ///
228 /// // We can use the ? (try) operator to check if the bytes are valid
229 /// let sparkle_heart = str::from_utf8(&sparkle_heart)?;
230 ///
231 /// assert_eq!("💖", sparkle_heart);
232 /// # Ok::<_, std::str::Utf8Error>(())
233 /// ```
234 ///
235 /// Incorrect bytes:
236 ///
237 /// ```
238 /// // some invalid bytes, in a vector
239 /// let sparkle_heart = vec![0, 159, 146, 150];
240 ///
241 /// assert!(str::from_utf8(&sparkle_heart).is_err());
242 /// ```
243 ///
244 /// See the docs for [`Utf8Error`] for more details on the kinds of
245 /// errors that can be returned.
246 ///
247 /// A "stack allocated string":
248 ///
249 /// ```
250 /// // some bytes, in a stack-allocated array
251 /// let sparkle_heart = [240, 159, 146, 150];
252 ///
253 /// // We know these bytes are valid, so just use `unwrap()`.
254 /// let sparkle_heart: &str = str::from_utf8(&sparkle_heart).unwrap();
255 ///
256 /// assert_eq!("💖", sparkle_heart);
257 /// ```
258 #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
259 #[rustc_const_stable(feature = "inherent_str_constructors", since = "1.87.0")]
260 #[rustc_diagnostic_item = "str_inherent_from_utf8"]
261 #[ferrocene::prevalidated]
262 pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
263 converts::from_utf8(v)
264 }
265
266 /// Converts a mutable slice of bytes to a mutable string slice.
267 ///
268 /// # Examples
269 ///
270 /// Basic usage:
271 ///
272 /// ```
273 /// // "Hello, Rust!" as a mutable vector
274 /// let mut hellorust = vec![72, 101, 108, 108, 111, 44, 32, 82, 117, 115, 116, 33];
275 ///
276 /// // As we know these bytes are valid, we can use `unwrap()`
277 /// let outstr = str::from_utf8_mut(&mut hellorust).unwrap();
278 ///
279 /// assert_eq!("Hello, Rust!", outstr);
280 /// ```
281 ///
282 /// Incorrect bytes:
283 ///
284 /// ```
285 /// // Some invalid bytes in a mutable vector
286 /// let mut invalid = vec![128, 223];
287 ///
288 /// assert!(str::from_utf8_mut(&mut invalid).is_err());
289 /// ```
290 /// See the docs for [`Utf8Error`] for more details on the kinds of
291 /// errors that can be returned.
292 #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
293 #[rustc_const_stable(feature = "const_str_from_utf8", since = "1.87.0")]
294 #[rustc_diagnostic_item = "str_inherent_from_utf8_mut"]
295 #[ferrocene::prevalidated]
296 pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
297 converts::from_utf8_mut(v)
298 }
299
300 /// Converts a slice of bytes to a string slice without checking
301 /// that the string contains valid UTF-8.
302 ///
303 /// See the safe version, [`from_utf8`], for more information.
304 ///
305 /// # Safety
306 ///
307 /// The bytes passed in must be valid UTF-8.
308 ///
309 /// # Examples
310 ///
311 /// Basic usage:
312 ///
313 /// ```
314 /// // some bytes, in a vector
315 /// let sparkle_heart = vec![240, 159, 146, 150];
316 ///
317 /// let sparkle_heart = unsafe {
318 /// str::from_utf8_unchecked(&sparkle_heart)
319 /// };
320 ///
321 /// assert_eq!("💖", sparkle_heart);
322 /// ```
323 #[inline]
324 #[must_use]
325 #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
326 #[rustc_const_stable(feature = "inherent_str_constructors", since = "1.87.0")]
327 #[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked"]
328 #[ferrocene::prevalidated]
329 pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
330 // SAFETY: converts::from_utf8_unchecked has the same safety requirements as this function.
331 unsafe { converts::from_utf8_unchecked(v) }
332 }
333
334 /// Converts a slice of bytes to a string slice without checking
335 /// that the string contains valid UTF-8; mutable version.
336 ///
337 /// See the immutable version, [`from_utf8_unchecked()`] for documentation and safety requirements.
338 ///
339 /// # Examples
340 ///
341 /// Basic usage:
342 ///
343 /// ```
344 /// let mut heart = vec![240, 159, 146, 150];
345 /// let heart = unsafe { str::from_utf8_unchecked_mut(&mut heart) };
346 ///
347 /// assert_eq!("💖", heart);
348 /// ```
349 #[inline]
350 #[must_use]
351 #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
352 #[rustc_const_stable(feature = "inherent_str_constructors", since = "1.87.0")]
353 #[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked_mut"]
354 #[ferrocene::prevalidated]
355 pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
356 // SAFETY: converts::from_utf8_unchecked_mut has the same safety requirements as this function.
357 unsafe { converts::from_utf8_unchecked_mut(v) }
358 }
359
360 /// Checks that `index`-th byte is the first byte in a UTF-8 code point
361 /// sequence or the end of the string.
362 ///
363 /// The start and end of the string (when `index == self.len()`) are
364 /// considered to be boundaries.
365 ///
366 /// Returns `false` if `index` is greater than `self.len()`.
367 ///
368 /// # Examples
369 ///
370 /// ```
371 /// let s = "Löwe 老虎 Léopard";
372 /// assert!(s.is_char_boundary(0));
373 /// // start of `老`
374 /// assert!(s.is_char_boundary(6));
375 /// assert!(s.is_char_boundary(s.len()));
376 ///
377 /// // second byte of `ö`
378 /// assert!(!s.is_char_boundary(2));
379 ///
380 /// // third byte of `老`
381 /// assert!(!s.is_char_boundary(8));
382 /// ```
383 #[must_use]
384 #[stable(feature = "is_char_boundary", since = "1.9.0")]
385 #[rustc_const_stable(feature = "const_is_char_boundary", since = "1.86.0")]
386 #[inline]
387 #[ferrocene::prevalidated]
388 pub const fn is_char_boundary(&self, index: usize) -> bool {
389 // 0 is always ok.
390 // Test for 0 explicitly so that it can optimize out the check
391 // easily and skip reading string data for that case.
392 // Note that optimizing `self.get(..index)` relies on this.
393 if index == 0 {
394 return true;
395 }
396
397 if index >= self.len() {
398 // For `true` we have two options:
399 //
400 // - index == self.len()
401 // Empty strings are valid, so return true
402 // - index > self.len()
403 // In this case return false
404 //
405 // The check is placed exactly here, because it improves generated
406 // code on higher opt-levels. See PR #84751 for more details.
407 index == self.len()
408 } else {
409 self.as_bytes()[index].is_utf8_char_boundary()
410 }
411 }
412
413 /// Finds the closest `x` not exceeding `index` where [`is_char_boundary(x)`] is `true`.
414 ///
415 /// This method can help you truncate a string so that it's still valid UTF-8, but doesn't
416 /// exceed a given number of bytes. Note that this is done purely at the character level
417 /// and can still visually split graphemes, even though the underlying characters aren't
418 /// split. For example, the emoji 🧑🔬 (scientist) could be split so that the string only
419 /// includes 🧑 (person) instead.
420 ///
421 /// [`is_char_boundary(x)`]: Self::is_char_boundary
422 ///
423 /// # Examples
424 ///
425 /// ```
426 /// let s = "❤️🧡💛💚💙💜";
427 /// assert_eq!(s.len(), 26);
428 /// assert!(!s.is_char_boundary(13));
429 ///
430 /// let closest = s.floor_char_boundary(13);
431 /// assert_eq!(closest, 10);
432 /// assert_eq!(&s[..closest], "❤️🧡");
433 /// ```
434 #[stable(feature = "round_char_boundary", since = "1.91.0")]
435 #[rustc_const_stable(feature = "round_char_boundary", since = "1.91.0")]
436 #[inline]
437 #[ferrocene::prevalidated]
438 pub const fn floor_char_boundary(&self, index: usize) -> usize {
439 if index >= self.len() {
440 return self.len();
441 }
442 if self.as_bytes()[index].is_utf8_char_boundary() {
443 return index;
444 }
445 // Unlike `ceil_char_boundary`, the loop is unrolled manually to prevent the compiler from
446 // generating excessive unrolled loop bodies when `index` is statically known.
447
448 // The first byte of `&str` must always be a char boundary, so we can assume `i > 0` below
449 // for any `i` where `self.as_bytes()[i]` is not a char boundary.
450 debug_assert!(self.as_bytes()[0].is_utf8_char_boundary());
451
452 // SAFETY: `self.as_bytes()[0]` is always a char boundary with valid `&str`
453 unsafe { assert_unchecked(index >= 1) };
454 if self.as_bytes()[index - 1].is_utf8_char_boundary() {
455 return index - 1;
456 }
457
458 // SAFETY: `self.as_bytes()[0]` is always a char boundary with valid `&str`
459 unsafe { assert_unchecked(index >= 2) };
460 if self.as_bytes()[index - 2].is_utf8_char_boundary() {
461 return index - 2;
462 }
463
464 // `self.as_bytes()[0]` is always a char boundary with valid `&str`
465 debug_assert!(index >= 3);
466 // The character boundary will be within four bytes of the index
467 debug_assert!(self.as_bytes()[index - 3].is_utf8_char_boundary());
468 index - 3
469 }
470
471 /// Finds the closest `x` not below `index` where [`is_char_boundary(x)`] is `true`.
472 ///
473 /// If `index` is greater than the length of the string, this returns the length of the string.
474 ///
475 /// This method is the natural complement to [`floor_char_boundary`]. See that method
476 /// for more details.
477 ///
478 /// [`floor_char_boundary`]: str::floor_char_boundary
479 /// [`is_char_boundary(x)`]: Self::is_char_boundary
480 ///
481 /// # Examples
482 ///
483 /// ```
484 /// let s = "❤️🧡💛💚💙💜";
485 /// assert_eq!(s.len(), 26);
486 /// assert!(!s.is_char_boundary(13));
487 ///
488 /// let closest = s.ceil_char_boundary(13);
489 /// assert_eq!(closest, 14);
490 /// assert_eq!(&s[..closest], "❤️🧡💛");
491 /// ```
492 #[stable(feature = "round_char_boundary", since = "1.91.0")]
493 #[rustc_const_stable(feature = "round_char_boundary", since = "1.91.0")]
494 #[inline]
495 #[ferrocene::prevalidated]
496 pub const fn ceil_char_boundary(&self, index: usize) -> usize {
497 if index >= self.len() {
498 self.len()
499 } else {
500 let mut i = index;
501 while !self.as_bytes()[i].is_utf8_char_boundary() {
502 i += 1;
503 if i >= self.len() {
504 break;
505 }
506 }
507
508 // The character boundary will be within four bytes of the index
509 debug_assert!(i <= index + 3);
510
511 i
512 }
513 }
514
515 /// Converts a string slice to a byte slice. To convert the byte slice back
516 /// into a string slice, use the [`from_utf8`] function.
517 ///
518 /// # Examples
519 ///
520 /// ```
521 /// let bytes = "bors".as_bytes();
522 /// assert_eq!(b"bors", bytes);
523 /// ```
524 #[stable(feature = "rust1", since = "1.0.0")]
525 #[rustc_const_stable(feature = "str_as_bytes", since = "1.39.0")]
526 #[must_use]
527 #[inline(always)]
528 #[allow(unused_attributes)]
529 #[ferrocene::prevalidated]
530 pub const fn as_bytes(&self) -> &[u8] {
531 // SAFETY: const sound because we transmute two types with the same layout
532 unsafe { mem::transmute(self) }
533 }
534
535 /// Converts a mutable string slice to a mutable byte slice.
536 ///
537 /// # Safety
538 ///
539 /// The caller must ensure that the content of the slice is valid UTF-8
540 /// before the borrow ends and the underlying `str` is used.
541 ///
542 /// Use of a `str` whose contents are not valid UTF-8 is undefined behavior.
543 ///
544 /// # Examples
545 ///
546 /// Basic usage:
547 ///
548 /// ```
549 /// let mut s = String::from("Hello");
550 /// let bytes = unsafe { s.as_bytes_mut() };
551 ///
552 /// assert_eq!(b"Hello", bytes);
553 /// ```
554 ///
555 /// Mutability:
556 ///
557 /// ```
558 /// let mut s = String::from("🗻∈🌏");
559 ///
560 /// unsafe {
561 /// let bytes = s.as_bytes_mut();
562 ///
563 /// bytes[0] = 0xF0;
564 /// bytes[1] = 0x9F;
565 /// bytes[2] = 0x8D;
566 /// bytes[3] = 0x94;
567 /// }
568 ///
569 /// assert_eq!("🍔∈🌏", s);
570 /// ```
571 #[stable(feature = "str_mut_extras", since = "1.20.0")]
572 #[rustc_const_stable(feature = "const_str_as_mut", since = "1.83.0")]
573 #[must_use]
574 #[inline(always)]
575 #[ferrocene::prevalidated]
576 pub const unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
577 // SAFETY: the cast from `&str` to `&[u8]` is safe since `str`
578 // has the same layout as `&[u8]` (only std can make this guarantee).
579 // The pointer dereference is safe since it comes from a mutable reference which
580 // is guaranteed to be valid for writes.
581 unsafe { &mut *(self as *mut str as *mut [u8]) }
582 }
583
584 /// Converts a string slice to a raw pointer.
585 ///
586 /// As string slices are a slice of bytes, the raw pointer points to a
587 /// [`u8`]. This pointer will be pointing to the first byte of the string
588 /// slice.
589 ///
590 /// The caller must ensure that the returned pointer is never written to.
591 /// If you need to mutate the contents of the string slice, use [`as_mut_ptr`].
592 ///
593 /// [`as_mut_ptr`]: str::as_mut_ptr
594 ///
595 /// # Examples
596 ///
597 /// ```
598 /// let s = "Hello";
599 /// let ptr = s.as_ptr();
600 /// ```
601 #[stable(feature = "rust1", since = "1.0.0")]
602 #[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
603 #[rustc_never_returns_null_ptr]
604 #[rustc_as_ptr]
605 #[must_use]
606 #[inline(always)]
607 #[ferrocene::prevalidated]
608 pub const fn as_ptr(&self) -> *const u8 {
609 self as *const str as *const u8
610 }
611
612 /// Converts a mutable string slice to a raw pointer.
613 ///
614 /// As string slices are a slice of bytes, the raw pointer points to a
615 /// [`u8`]. This pointer will be pointing to the first byte of the string
616 /// slice.
617 ///
618 /// It is your responsibility to make sure that the string slice only gets
619 /// modified in a way that it remains valid UTF-8.
620 #[ferrocene::prevalidated]
621 #[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
622 #[rustc_const_stable(feature = "const_str_as_mut", since = "1.83.0")]
623 #[rustc_never_returns_null_ptr]
624 #[rustc_as_ptr]
625 #[must_use]
626 #[inline(always)]
627 #[rustc_no_writable]
628 pub const fn as_mut_ptr(&mut self) -> *mut u8 {
629 self as *mut str as *mut u8
630 }
631
632 /// Returns a subslice of `str`.
633 ///
634 /// This is the non-panicking alternative to indexing the `str`. Returns
635 /// [`None`] whenever equivalent indexing operation would panic.
636 ///
637 /// # Examples
638 ///
639 /// ```
640 /// let v = String::from("🗻∈🌏");
641 ///
642 /// assert_eq!(Some("🗻"), v.get(0..4));
643 ///
644 /// // indices not on UTF-8 sequence boundaries
645 /// assert!(v.get(1..).is_none());
646 /// assert!(v.get(..8).is_none());
647 ///
648 /// // out of bounds
649 /// assert!(v.get(..42).is_none());
650 /// ```
651 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
652 #[rustc_const_unstable(feature = "const_index", issue = "143775")]
653 #[inline]
654 pub const fn get<I: [const] SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
655 i.get(self)
656 }
657
658 /// Returns a mutable subslice of `str`.
659 ///
660 /// This is the non-panicking alternative to indexing the `str`. Returns
661 /// [`None`] whenever equivalent indexing operation would panic.
662 ///
663 /// # Examples
664 ///
665 /// ```
666 /// let mut v = String::from("hello");
667 /// // correct length
668 /// assert!(v.get_mut(0..5).is_some());
669 /// // out of bounds
670 /// assert!(v.get_mut(..42).is_none());
671 /// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v));
672 ///
673 /// assert_eq!("hello", v);
674 /// {
675 /// let s = v.get_mut(0..2);
676 /// let s = s.map(|s| {
677 /// s.make_ascii_uppercase();
678 /// &*s
679 /// });
680 /// assert_eq!(Some("HE"), s);
681 /// }
682 /// assert_eq!("HEllo", v);
683 /// ```
684 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
685 #[rustc_const_unstable(feature = "const_index", issue = "143775")]
686 #[inline]
687 pub const fn get_mut<I: [const] SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
688 i.get_mut(self)
689 }
690
691 /// Returns an unchecked subslice of `str`.
692 ///
693 /// This is the unchecked alternative to indexing the `str`.
694 ///
695 /// # Safety
696 ///
697 /// Callers of this function are responsible that these preconditions are
698 /// satisfied:
699 ///
700 /// * The starting index must not exceed the ending index;
701 /// * Indexes must be within bounds of the original slice;
702 /// * Indexes must lie on UTF-8 sequence boundaries.
703 ///
704 /// Failing that, the returned string slice may reference invalid memory or
705 /// violate the invariants communicated by the `str` type.
706 ///
707 /// # Examples
708 ///
709 /// ```
710 /// let v = "🗻∈🌏";
711 /// unsafe {
712 /// assert_eq!("🗻", v.get_unchecked(0..4));
713 /// assert_eq!("∈", v.get_unchecked(4..7));
714 /// assert_eq!("🌏", v.get_unchecked(7..11));
715 /// }
716 /// ```
717 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
718 #[inline]
719 #[ferrocene::prevalidated]
720 pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
721 // SAFETY: the caller must uphold the safety contract for `get_unchecked`;
722 // the slice is dereferenceable because `self` is a safe reference.
723 // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
724 unsafe { &*i.get_unchecked(self) }
725 }
726
727 /// Returns a mutable, unchecked subslice of `str`.
728 ///
729 /// This is the unchecked alternative to indexing the `str`.
730 ///
731 /// # Safety
732 ///
733 /// Callers of this function are responsible that these preconditions are
734 /// satisfied:
735 ///
736 /// * The starting index must not exceed the ending index;
737 /// * Indexes must be within bounds of the original slice;
738 /// * Indexes must lie on UTF-8 sequence boundaries.
739 ///
740 /// Failing that, the returned string slice may reference invalid memory or
741 /// violate the invariants communicated by the `str` type.
742 ///
743 /// # Examples
744 ///
745 /// ```
746 /// let mut v = String::from("🗻∈🌏");
747 /// unsafe {
748 /// assert_eq!("🗻", v.get_unchecked_mut(0..4));
749 /// assert_eq!("∈", v.get_unchecked_mut(4..7));
750 /// assert_eq!("🌏", v.get_unchecked_mut(7..11));
751 /// }
752 /// ```
753 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
754 #[inline]
755 pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
756 // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`;
757 // the slice is dereferenceable because `self` is a safe reference.
758 // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
759 unsafe { &mut *i.get_unchecked_mut(self) }
760 }
761
762 /// Creates a string slice from another string slice, bypassing safety
763 /// checks.
764 ///
765 /// This is generally not recommended, use with caution! For a safe
766 /// alternative see [`str`] and [`Index`].
767 ///
768 /// [`Index`]: crate::ops::Index
769 ///
770 /// This new slice goes from `begin` to `end`, including `begin` but
771 /// excluding `end`.
772 ///
773 /// To get a mutable string slice instead, see the
774 /// [`slice_mut_unchecked`] method.
775 ///
776 /// [`slice_mut_unchecked`]: str::slice_mut_unchecked
777 ///
778 /// # Safety
779 ///
780 /// Callers of this function are responsible that three preconditions are
781 /// satisfied:
782 ///
783 /// * `begin` must not exceed `end`.
784 /// * `begin` and `end` must be byte positions within the string slice.
785 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
786 ///
787 /// # Examples
788 ///
789 /// ```
790 /// let s = "Löwe 老虎 Léopard";
791 ///
792 /// unsafe {
793 /// assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
794 /// }
795 ///
796 /// let s = "Hello, world!";
797 ///
798 /// unsafe {
799 /// assert_eq!("world", s.slice_unchecked(7, 12));
800 /// }
801 /// ```
802 #[stable(feature = "rust1", since = "1.0.0")]
803 #[deprecated(since = "1.29.0", note = "use `get_unchecked(begin..end)` instead")]
804 #[must_use]
805 #[inline]
806 pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
807 // SAFETY: the caller must uphold the safety contract for `get_unchecked`;
808 // the slice is dereferenceable because `self` is a safe reference.
809 // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
810 unsafe { &*(begin..end).get_unchecked(self) }
811 }
812
813 /// Creates a string slice from another string slice, bypassing safety
814 /// checks.
815 ///
816 /// This is generally not recommended, use with caution! For a safe
817 /// alternative see [`str`] and [`IndexMut`].
818 ///
819 /// [`IndexMut`]: crate::ops::IndexMut
820 ///
821 /// This new slice goes from `begin` to `end`, including `begin` but
822 /// excluding `end`.
823 ///
824 /// To get an immutable string slice instead, see the
825 /// [`slice_unchecked`] method.
826 ///
827 /// [`slice_unchecked`]: str::slice_unchecked
828 ///
829 /// # Safety
830 ///
831 /// Callers of this function are responsible that three preconditions are
832 /// satisfied:
833 ///
834 /// * `begin` must not exceed `end`.
835 /// * `begin` and `end` must be byte positions within the string slice.
836 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
837 #[stable(feature = "str_slice_mut", since = "1.5.0")]
838 #[deprecated(since = "1.29.0", note = "use `get_unchecked_mut(begin..end)` instead")]
839 #[inline]
840 pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
841 // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`;
842 // the slice is dereferenceable because `self` is a safe reference.
843 // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
844 unsafe { &mut *(begin..end).get_unchecked_mut(self) }
845 }
846
847 /// Divides one string slice into two at an index.
848 ///
849 /// The argument, `mid`, should be a byte offset from the start of the
850 /// string. It must also be on the boundary of a UTF-8 code point.
851 ///
852 /// The two slices returned go from the start of the string slice to `mid`,
853 /// and from `mid` to the end of the string slice.
854 ///
855 /// To get mutable string slices instead, see the [`split_at_mut`]
856 /// method.
857 ///
858 /// [`split_at_mut`]: str::split_at_mut
859 ///
860 /// # Panics
861 ///
862 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is past
863 /// the end of the last code point of the string slice. For a non-panicking
864 /// alternative see [`split_at_checked`](str::split_at_checked).
865 ///
866 /// # Examples
867 ///
868 /// ```
869 /// let s = "Per Martin-Löf";
870 ///
871 /// let (first, last) = s.split_at(3);
872 ///
873 /// assert_eq!("Per", first);
874 /// assert_eq!(" Martin-Löf", last);
875 /// ```
876 #[inline]
877 #[must_use]
878 #[stable(feature = "str_split_at", since = "1.4.0")]
879 #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
880 pub const fn split_at(&self, mid: usize) -> (&str, &str) {
881 match self.split_at_checked(mid) {
882 None => slice_error_fail(self, 0, mid),
883 Some(pair) => pair,
884 }
885 }
886
887 /// Divides one mutable string slice into two at an index.
888 ///
889 /// The argument, `mid`, should be a byte offset from the start of the
890 /// string. It must also be on the boundary of a UTF-8 code point.
891 ///
892 /// The two slices returned go from the start of the string slice to `mid`,
893 /// and from `mid` to the end of the string slice.
894 ///
895 /// To get immutable string slices instead, see the [`split_at`] method.
896 ///
897 /// [`split_at`]: str::split_at
898 ///
899 /// # Panics
900 ///
901 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is past
902 /// the end of the last code point of the string slice. For a non-panicking
903 /// alternative see [`split_at_mut_checked`](str::split_at_mut_checked).
904 ///
905 /// # Examples
906 ///
907 /// ```
908 /// let mut s = "Per Martin-Löf".to_string();
909 /// {
910 /// let (first, last) = s.split_at_mut(3);
911 /// first.make_ascii_uppercase();
912 /// assert_eq!("PER", first);
913 /// assert_eq!(" Martin-Löf", last);
914 /// }
915 /// assert_eq!("PER Martin-Löf", s);
916 /// ```
917 #[inline]
918 #[must_use]
919 #[stable(feature = "str_split_at", since = "1.4.0")]
920 #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
921 pub const fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
922 // is_char_boundary checks that the index is in [0, .len()]
923 if self.is_char_boundary(mid) {
924 // SAFETY: just checked that `mid` is on a char boundary.
925 unsafe { self.split_at_mut_unchecked(mid) }
926 } else {
927 slice_error_fail(self, 0, mid)
928 }
929 }
930
931 /// Divides one string slice into two at an index.
932 ///
933 /// The argument, `mid`, should be a valid byte offset from the start of the
934 /// string. It must also be on the boundary of a UTF-8 code point. The
935 /// method returns `None` if that’s not the case.
936 ///
937 /// The two slices returned go from the start of the string slice to `mid`,
938 /// and from `mid` to the end of the string slice.
939 ///
940 /// To get mutable string slices instead, see the [`split_at_mut_checked`]
941 /// method.
942 ///
943 /// [`split_at_mut_checked`]: str::split_at_mut_checked
944 ///
945 /// # Examples
946 ///
947 /// ```
948 /// let s = "Per Martin-Löf";
949 ///
950 /// let (first, last) = s.split_at_checked(3).unwrap();
951 /// assert_eq!("Per", first);
952 /// assert_eq!(" Martin-Löf", last);
953 ///
954 /// assert_eq!(None, s.split_at_checked(13)); // Inside “ö”
955 /// assert_eq!(None, s.split_at_checked(16)); // Beyond the string length
956 /// ```
957 #[inline]
958 #[must_use]
959 #[stable(feature = "split_at_checked", since = "1.80.0")]
960 #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
961 pub const fn split_at_checked(&self, mid: usize) -> Option<(&str, &str)> {
962 // is_char_boundary checks that the index is in [0, .len()]
963 if self.is_char_boundary(mid) {
964 // SAFETY: just checked that `mid` is on a char boundary.
965 Some(unsafe { self.split_at_unchecked(mid) })
966 } else {
967 None
968 }
969 }
970
971 /// Divides one mutable string slice into two at an index.
972 ///
973 /// The argument, `mid`, should be a valid byte offset from the start of the
974 /// string. It must also be on the boundary of a UTF-8 code point. The
975 /// method returns `None` if that’s not the case.
976 ///
977 /// The two slices returned go from the start of the string slice to `mid`,
978 /// and from `mid` to the end of the string slice.
979 ///
980 /// To get immutable string slices instead, see the [`split_at_checked`] method.
981 ///
982 /// [`split_at_checked`]: str::split_at_checked
983 ///
984 /// # Examples
985 ///
986 /// ```
987 /// let mut s = "Per Martin-Löf".to_string();
988 /// if let Some((first, last)) = s.split_at_mut_checked(3) {
989 /// first.make_ascii_uppercase();
990 /// assert_eq!("PER", first);
991 /// assert_eq!(" Martin-Löf", last);
992 /// }
993 /// assert_eq!("PER Martin-Löf", s);
994 ///
995 /// assert_eq!(None, s.split_at_mut_checked(13)); // Inside “ö”
996 /// assert_eq!(None, s.split_at_mut_checked(16)); // Beyond the string length
997 /// ```
998 #[inline]
999 #[must_use]
1000 #[stable(feature = "split_at_checked", since = "1.80.0")]
1001 #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
1002 pub const fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut str, &mut str)> {
1003 // is_char_boundary checks that the index is in [0, .len()]
1004 if self.is_char_boundary(mid) {
1005 // SAFETY: just checked that `mid` is on a char boundary.
1006 Some(unsafe { self.split_at_mut_unchecked(mid) })
1007 } else {
1008 None
1009 }
1010 }
1011
1012 /// Divides one string slice into two at an index.
1013 ///
1014 /// # Safety
1015 ///
1016 /// The caller must ensure that `mid` is a valid byte offset from the start
1017 /// of the string and falls on the boundary of a UTF-8 code point.
1018 #[inline]
1019 const unsafe fn split_at_unchecked(&self, mid: usize) -> (&str, &str) {
1020 let len = self.len();
1021 let ptr = self.as_ptr();
1022 // SAFETY: caller guarantees `mid` is on a char boundary.
1023 unsafe {
1024 (
1025 from_utf8_unchecked(slice::from_raw_parts(ptr, mid)),
1026 from_utf8_unchecked(slice::from_raw_parts(ptr.add(mid), len - mid)),
1027 )
1028 }
1029 }
1030
1031 /// Divides one string slice into two at an index.
1032 ///
1033 /// # Safety
1034 ///
1035 /// The caller must ensure that `mid` is a valid byte offset from the start
1036 /// of the string and falls on the boundary of a UTF-8 code point.
1037 const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut str, &mut str) {
1038 let len = self.len();
1039 let ptr = self.as_mut_ptr();
1040 // SAFETY: caller guarantees `mid` is on a char boundary.
1041 unsafe {
1042 (
1043 from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, mid)),
1044 from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr.add(mid), len - mid)),
1045 )
1046 }
1047 }
1048
1049 /// Returns an iterator over the [`char`]s of a string slice.
1050 ///
1051 /// As a string slice consists of valid UTF-8, we can iterate through a
1052 /// string slice by [`char`]. This method returns such an iterator.
1053 ///
1054 /// It's important to remember that [`char`] represents a Unicode Scalar
1055 /// Value, and might not match your idea of what a 'character' is. Iteration
1056 /// over grapheme clusters may be what you actually want. This functionality
1057 /// is not provided by Rust's standard library, check crates.io instead.
1058 ///
1059 /// # Examples
1060 ///
1061 /// Basic usage:
1062 ///
1063 /// ```
1064 /// let word = "goodbye";
1065 ///
1066 /// let count = word.chars().count();
1067 /// assert_eq!(7, count);
1068 ///
1069 /// let mut chars = word.chars();
1070 ///
1071 /// assert_eq!(Some('g'), chars.next());
1072 /// assert_eq!(Some('o'), chars.next());
1073 /// assert_eq!(Some('o'), chars.next());
1074 /// assert_eq!(Some('d'), chars.next());
1075 /// assert_eq!(Some('b'), chars.next());
1076 /// assert_eq!(Some('y'), chars.next());
1077 /// assert_eq!(Some('e'), chars.next());
1078 ///
1079 /// assert_eq!(None, chars.next());
1080 /// ```
1081 ///
1082 /// Remember, [`char`]s might not match your intuition about characters:
1083 ///
1084 /// [`char`]: prim@char
1085 ///
1086 /// ```
1087 /// let y = "y̆";
1088 ///
1089 /// let mut chars = y.chars();
1090 ///
1091 /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
1092 /// assert_eq!(Some('\u{0306}'), chars.next());
1093 ///
1094 /// assert_eq!(None, chars.next());
1095 /// ```
1096 #[stable(feature = "rust1", since = "1.0.0")]
1097 #[inline]
1098 #[rustc_diagnostic_item = "str_chars"]
1099 #[ferrocene::prevalidated]
1100 pub fn chars(&self) -> Chars<'_> {
1101 Chars { iter: self.as_bytes().iter() }
1102 }
1103
1104 /// Returns an iterator over the [`char`]s of a string slice, and their
1105 /// positions.
1106 ///
1107 /// As a string slice consists of valid UTF-8, we can iterate through a
1108 /// string slice by [`char`]. This method returns an iterator of both
1109 /// these [`char`]s, as well as their byte positions.
1110 ///
1111 /// The iterator yields tuples. The position is first, the [`char`] is
1112 /// second.
1113 ///
1114 /// # Examples
1115 ///
1116 /// Basic usage:
1117 ///
1118 /// ```
1119 /// let word = "goodbye";
1120 ///
1121 /// let count = word.char_indices().count();
1122 /// assert_eq!(7, count);
1123 ///
1124 /// let mut char_indices = word.char_indices();
1125 ///
1126 /// assert_eq!(Some((0, 'g')), char_indices.next());
1127 /// assert_eq!(Some((1, 'o')), char_indices.next());
1128 /// assert_eq!(Some((2, 'o')), char_indices.next());
1129 /// assert_eq!(Some((3, 'd')), char_indices.next());
1130 /// assert_eq!(Some((4, 'b')), char_indices.next());
1131 /// assert_eq!(Some((5, 'y')), char_indices.next());
1132 /// assert_eq!(Some((6, 'e')), char_indices.next());
1133 ///
1134 /// assert_eq!(None, char_indices.next());
1135 /// ```
1136 ///
1137 /// Remember, [`char`]s might not match your intuition about characters:
1138 ///
1139 /// [`char`]: prim@char
1140 ///
1141 /// ```
1142 /// let yes = "y̆es";
1143 ///
1144 /// let mut char_indices = yes.char_indices();
1145 ///
1146 /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
1147 /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
1148 ///
1149 /// // note the 3 here - the previous character took up two bytes
1150 /// assert_eq!(Some((3, 'e')), char_indices.next());
1151 /// assert_eq!(Some((4, 's')), char_indices.next());
1152 ///
1153 /// assert_eq!(None, char_indices.next());
1154 /// ```
1155 #[stable(feature = "rust1", since = "1.0.0")]
1156 #[inline]
1157 #[ferrocene::prevalidated]
1158 pub fn char_indices(&self) -> CharIndices<'_> {
1159 CharIndices { front_offset: 0, iter: self.chars() }
1160 }
1161
1162 /// Returns an iterator over the bytes of a string slice.
1163 ///
1164 /// As a string slice consists of a sequence of bytes, we can iterate
1165 /// through a string slice by byte. This method returns such an iterator.
1166 ///
1167 /// # Examples
1168 ///
1169 /// ```
1170 /// let mut bytes = "bors".bytes();
1171 ///
1172 /// assert_eq!(Some(b'b'), bytes.next());
1173 /// assert_eq!(Some(b'o'), bytes.next());
1174 /// assert_eq!(Some(b'r'), bytes.next());
1175 /// assert_eq!(Some(b's'), bytes.next());
1176 ///
1177 /// assert_eq!(None, bytes.next());
1178 /// ```
1179 #[stable(feature = "rust1", since = "1.0.0")]
1180 #[inline]
1181 #[ferrocene::prevalidated]
1182 pub fn bytes(&self) -> Bytes<'_> {
1183 Bytes(self.as_bytes().iter().copied())
1184 }
1185
1186 /// Splits a string slice by whitespace.
1187 ///
1188 /// The iterator returned will return string slices that are sub-slices of
1189 /// the original string slice, separated by any amount of whitespace.
1190 ///
1191 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1192 /// Core Property `White_Space`. If you only want to split on ASCII whitespace
1193 /// instead, use [`split_ascii_whitespace`].
1194 ///
1195 /// [`split_ascii_whitespace`]: str::split_ascii_whitespace
1196 ///
1197 /// # Examples
1198 ///
1199 /// Basic usage:
1200 ///
1201 /// ```
1202 /// let mut iter = "A few words".split_whitespace();
1203 ///
1204 /// assert_eq!(Some("A"), iter.next());
1205 /// assert_eq!(Some("few"), iter.next());
1206 /// assert_eq!(Some("words"), iter.next());
1207 ///
1208 /// assert_eq!(None, iter.next());
1209 /// ```
1210 ///
1211 /// All kinds of whitespace are considered:
1212 ///
1213 /// ```
1214 /// let mut iter = " Mary had\ta\u{2009}little \n\t lamb".split_whitespace();
1215 /// assert_eq!(Some("Mary"), iter.next());
1216 /// assert_eq!(Some("had"), iter.next());
1217 /// assert_eq!(Some("a"), iter.next());
1218 /// assert_eq!(Some("little"), iter.next());
1219 /// assert_eq!(Some("lamb"), iter.next());
1220 ///
1221 /// assert_eq!(None, iter.next());
1222 /// ```
1223 ///
1224 /// If the string is empty or all whitespace, the iterator yields no string slices:
1225 /// ```
1226 /// assert_eq!("".split_whitespace().next(), None);
1227 /// assert_eq!(" ".split_whitespace().next(), None);
1228 /// ```
1229 #[must_use = "this returns the split string as an iterator, \
1230 without modifying the original"]
1231 #[stable(feature = "split_whitespace", since = "1.1.0")]
1232 #[rustc_diagnostic_item = "str_split_whitespace"]
1233 #[inline]
1234 pub fn split_whitespace(&self) -> SplitWhitespace<'_> {
1235 SplitWhitespace { inner: self.split(IsWhitespace).filter(IsNotEmpty) }
1236 }
1237
1238 /// Splits a string slice by ASCII whitespace.
1239 ///
1240 /// The iterator returned will return string slices that are sub-slices of
1241 /// the original string slice, separated by any amount of ASCII whitespace.
1242 ///
1243 /// This uses the same definition as [`char::is_ascii_whitespace`].
1244 /// To split by Unicode `Whitespace` instead, use [`split_whitespace`].
1245 /// Note that because of this difference in definition, even if `s.is_ascii()`
1246 /// is `true`, `s.split_ascii_whitespace()` behavior will differ from `s.split_whitespace()`
1247 /// if `s` contains U+000B VERTICAL TAB.
1248 ///
1249 /// [`split_whitespace`]: str::split_whitespace
1250 ///
1251 /// # Examples
1252 ///
1253 /// Basic usage:
1254 ///
1255 /// ```
1256 /// let mut iter = "A few words".split_ascii_whitespace();
1257 ///
1258 /// assert_eq!(Some("A"), iter.next());
1259 /// assert_eq!(Some("few"), iter.next());
1260 /// assert_eq!(Some("words"), iter.next());
1261 ///
1262 /// assert_eq!(None, iter.next());
1263 /// ```
1264 ///
1265 /// Various kinds of ASCII whitespace are considered
1266 /// (see [`char::is_ascii_whitespace`]):
1267 ///
1268 /// ```
1269 /// let mut iter = " Mary had\ta little \n\t lamb".split_ascii_whitespace();
1270 /// assert_eq!(Some("Mary"), iter.next());
1271 /// assert_eq!(Some("had"), iter.next());
1272 /// assert_eq!(Some("a"), iter.next());
1273 /// assert_eq!(Some("little"), iter.next());
1274 /// assert_eq!(Some("lamb"), iter.next());
1275 ///
1276 /// assert_eq!(None, iter.next());
1277 /// ```
1278 ///
1279 /// If the string is empty or all ASCII whitespace, the iterator yields no string slices:
1280 /// ```
1281 /// assert_eq!("".split_ascii_whitespace().next(), None);
1282 /// assert_eq!(" ".split_ascii_whitespace().next(), None);
1283 /// ```
1284 #[must_use = "this returns the split string as an iterator, \
1285 without modifying the original"]
1286 #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
1287 #[inline]
1288 pub fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_> {
1289 let inner =
1290 self.as_bytes().split(IsAsciiWhitespace).filter(BytesIsNotEmpty).map(UnsafeBytesToStr);
1291 SplitAsciiWhitespace { inner }
1292 }
1293
1294 /// Returns an iterator over the lines of a string, as string slices.
1295 ///
1296 /// Lines are split at line endings that are either newlines (`\n`) or
1297 /// sequences of a carriage return followed by a line feed (`\r\n`).
1298 ///
1299 /// Line terminators are not included in the lines returned by the iterator.
1300 ///
1301 /// Note that any carriage return (`\r`) not immediately followed by a
1302 /// line feed (`\n`) does not split a line. These carriage returns are
1303 /// thereby included in the produced lines.
1304 ///
1305 /// The final line ending is optional. A string that ends with a final line
1306 /// ending will return the same lines as an otherwise identical string
1307 /// without a final line ending.
1308 ///
1309 /// An empty string returns an empty iterator.
1310 ///
1311 /// # Examples
1312 ///
1313 /// Basic usage:
1314 ///
1315 /// ```
1316 /// let text = "foo\r\nbar\n\nbaz\r";
1317 /// let mut lines = text.lines();
1318 ///
1319 /// assert_eq!(Some("foo"), lines.next());
1320 /// assert_eq!(Some("bar"), lines.next());
1321 /// assert_eq!(Some(""), lines.next());
1322 /// // Trailing carriage return is included in the last line
1323 /// assert_eq!(Some("baz\r"), lines.next());
1324 ///
1325 /// assert_eq!(None, lines.next());
1326 /// ```
1327 ///
1328 /// The final line does not require any ending:
1329 ///
1330 /// ```
1331 /// let text = "foo\nbar\n\r\nbaz";
1332 /// let mut lines = text.lines();
1333 ///
1334 /// assert_eq!(Some("foo"), lines.next());
1335 /// assert_eq!(Some("bar"), lines.next());
1336 /// assert_eq!(Some(""), lines.next());
1337 /// assert_eq!(Some("baz"), lines.next());
1338 ///
1339 /// assert_eq!(None, lines.next());
1340 /// ```
1341 ///
1342 /// An empty string returns an empty iterator:
1343 ///
1344 /// ```
1345 /// let text = "";
1346 /// let mut lines = text.lines();
1347 ///
1348 /// assert_eq!(lines.next(), None);
1349 /// ```
1350 #[stable(feature = "rust1", since = "1.0.0")]
1351 #[inline]
1352 pub fn lines(&self) -> Lines<'_> {
1353 Lines(self.split_inclusive('\n').map(LinesMap))
1354 }
1355
1356 /// Returns an iterator over the lines of a string.
1357 #[stable(feature = "rust1", since = "1.0.0")]
1358 #[deprecated(since = "1.4.0", note = "use lines() instead now", suggestion = "lines")]
1359 #[inline]
1360 #[allow(deprecated)]
1361 pub fn lines_any(&self) -> LinesAny<'_> {
1362 LinesAny(self.lines())
1363 }
1364
1365 /// Returns an iterator of `u16` over the string encoded
1366 /// as native endian UTF-16 (without byte-order mark).
1367 ///
1368 /// # Examples
1369 ///
1370 /// ```
1371 /// let text = "Zażółć gęślą jaźń";
1372 ///
1373 /// let utf8_len = text.len();
1374 /// let utf16_len = text.encode_utf16().count();
1375 ///
1376 /// assert!(utf16_len <= utf8_len);
1377 /// ```
1378 #[must_use = "this returns the encoded string as an iterator, \
1379 without modifying the original"]
1380 #[stable(feature = "encode_utf16", since = "1.8.0")]
1381 pub fn encode_utf16(&self) -> EncodeUtf16<'_> {
1382 EncodeUtf16 { chars: self.chars(), extra: 0 }
1383 }
1384
1385 /// Returns `true` if the given pattern matches a sub-slice of
1386 /// this string slice.
1387 ///
1388 /// Returns `false` if it does not.
1389 ///
1390 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1391 /// function or closure that determines if a character matches.
1392 ///
1393 /// [`char`]: prim@char
1394 /// [pattern]: self::pattern
1395 ///
1396 /// # Examples
1397 ///
1398 /// ```
1399 /// let bananas = "bananas";
1400 ///
1401 /// assert!(bananas.contains("nana"));
1402 /// assert!(!bananas.contains("apples"));
1403 /// ```
1404 #[stable(feature = "rust1", since = "1.0.0")]
1405 #[inline]
1406 pub fn contains<P: Pattern>(&self, pat: P) -> bool {
1407 pat.is_contained_in(self)
1408 }
1409
1410 /// Returns `true` if the given pattern matches a prefix of this
1411 /// string slice.
1412 ///
1413 /// Returns `false` if it does not.
1414 ///
1415 /// The [pattern] can be a `&str`, in which case this function will return true if
1416 /// the `&str` is a prefix of this string slice.
1417 ///
1418 /// The [pattern] can also be a [`char`], a slice of [`char`]s, or a
1419 /// function or closure that determines if a character matches.
1420 /// These will only be checked against the first character of this string slice.
1421 /// Look at the second example below regarding behavior for slices of [`char`]s.
1422 ///
1423 /// [`char`]: prim@char
1424 /// [pattern]: self::pattern
1425 ///
1426 /// # Examples
1427 ///
1428 /// ```
1429 /// let bananas = "bananas";
1430 ///
1431 /// assert!(bananas.starts_with("bana"));
1432 /// assert!(!bananas.starts_with("nana"));
1433 /// ```
1434 ///
1435 /// ```
1436 /// let bananas = "bananas";
1437 ///
1438 /// // Note that both of these assert successfully.
1439 /// assert!(bananas.starts_with(&['b', 'a', 'n', 'a']));
1440 /// assert!(bananas.starts_with(&['a', 'b', 'c', 'd']));
1441 /// ```
1442 #[stable(feature = "rust1", since = "1.0.0")]
1443 #[rustc_diagnostic_item = "str_starts_with"]
1444 #[ferrocene::prevalidated]
1445 pub fn starts_with<P: Pattern>(&self, pat: P) -> bool {
1446 pat.is_prefix_of(self)
1447 }
1448
1449 /// Returns `true` if the given pattern matches a suffix of this
1450 /// string slice.
1451 ///
1452 /// Returns `false` if it does not.
1453 ///
1454 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1455 /// function or closure that determines if a character matches.
1456 ///
1457 /// [`char`]: prim@char
1458 /// [pattern]: self::pattern
1459 ///
1460 /// # Examples
1461 ///
1462 /// ```
1463 /// let bananas = "bananas";
1464 ///
1465 /// assert!(bananas.ends_with("anas"));
1466 /// assert!(!bananas.ends_with("nana"));
1467 /// ```
1468 #[stable(feature = "rust1", since = "1.0.0")]
1469 #[rustc_diagnostic_item = "str_ends_with"]
1470 #[ferrocene::prevalidated]
1471 pub fn ends_with<P: Pattern>(&self, pat: P) -> bool
1472 where
1473 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1474 {
1475 pat.is_suffix_of(self)
1476 }
1477
1478 /// Returns the byte index of the first character of this string slice that
1479 /// matches the pattern.
1480 ///
1481 /// Returns [`None`] if the pattern doesn't match.
1482 ///
1483 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1484 /// function or closure that determines if a character matches.
1485 ///
1486 /// [`char`]: prim@char
1487 /// [pattern]: self::pattern
1488 ///
1489 /// # Examples
1490 ///
1491 /// Simple patterns:
1492 ///
1493 /// ```
1494 /// let s = "Löwe 老虎 Léopard Gepardi";
1495 ///
1496 /// assert_eq!(s.find('L'), Some(0));
1497 /// assert_eq!(s.find('é'), Some(14));
1498 /// assert_eq!(s.find("pard"), Some(17));
1499 /// ```
1500 ///
1501 /// More complex patterns using point-free style and closures:
1502 ///
1503 /// ```
1504 /// let s = "Löwe 老虎 Léopard";
1505 ///
1506 /// assert_eq!(s.find(char::is_whitespace), Some(5));
1507 /// assert_eq!(s.find(char::is_lowercase), Some(1));
1508 /// assert_eq!(s.find(|c: char| c.is_whitespace() || c.is_lowercase()), Some(1));
1509 /// assert_eq!(s.find(|c: char| (c < 'o') && (c > 'a')), Some(4));
1510 /// ```
1511 ///
1512 /// Not finding the pattern:
1513 ///
1514 /// ```
1515 /// let s = "Löwe 老虎 Léopard";
1516 /// let x: &[_] = &['1', '2'];
1517 ///
1518 /// assert_eq!(s.find(x), None);
1519 /// ```
1520 #[stable(feature = "rust1", since = "1.0.0")]
1521 #[inline]
1522 #[ferrocene::prevalidated]
1523 pub fn find<P: Pattern>(&self, pat: P) -> Option<usize> {
1524 pat.into_searcher(self).next_match().map(|(i, _)| i)
1525 }
1526
1527 /// Returns the byte index for the first character of the last match of the pattern in
1528 /// this string slice.
1529 ///
1530 /// Returns [`None`] if the pattern doesn't match.
1531 ///
1532 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1533 /// function or closure that determines if a character matches.
1534 ///
1535 /// [`char`]: prim@char
1536 /// [pattern]: self::pattern
1537 ///
1538 /// # Examples
1539 ///
1540 /// Simple patterns:
1541 ///
1542 /// ```
1543 /// let s = "Löwe 老虎 Léopard Gepardi";
1544 ///
1545 /// assert_eq!(s.rfind('L'), Some(13));
1546 /// assert_eq!(s.rfind('é'), Some(14));
1547 /// assert_eq!(s.rfind("pard"), Some(24));
1548 /// ```
1549 ///
1550 /// More complex patterns with closures:
1551 ///
1552 /// ```
1553 /// let s = "Löwe 老虎 Léopard";
1554 ///
1555 /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
1556 /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
1557 /// ```
1558 ///
1559 /// Not finding the pattern:
1560 ///
1561 /// ```
1562 /// let s = "Löwe 老虎 Léopard";
1563 /// let x: &[_] = &['1', '2'];
1564 ///
1565 /// assert_eq!(s.rfind(x), None);
1566 /// ```
1567 #[stable(feature = "rust1", since = "1.0.0")]
1568 #[inline]
1569 pub fn rfind<P: Pattern>(&self, pat: P) -> Option<usize>
1570 where
1571 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1572 {
1573 pat.into_searcher(self).next_match_back().map(|(i, _)| i)
1574 }
1575
1576 /// Returns an iterator over substrings of this string slice, separated by
1577 /// characters matched by a pattern.
1578 ///
1579 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1580 /// function or closure that determines if a character matches.
1581 ///
1582 /// If there are no matches the full string slice is returned as the only
1583 /// item in the iterator.
1584 ///
1585 /// [`char`]: prim@char
1586 /// [pattern]: self::pattern
1587 ///
1588 /// # Iterator behavior
1589 ///
1590 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1591 /// allows a reverse search and forward/reverse search yields the same
1592 /// elements. This is true for, e.g., [`char`], but not for `&str`.
1593 ///
1594 /// If the pattern allows a reverse search but its results might differ
1595 /// from a forward search, the [`rsplit`] method can be used.
1596 ///
1597 /// [`rsplit`]: str::rsplit
1598 ///
1599 /// # Examples
1600 ///
1601 /// Simple patterns:
1602 ///
1603 /// ```
1604 /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
1605 /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
1606 ///
1607 /// let v: Vec<&str> = "".split('X').collect();
1608 /// assert_eq!(v, [""]);
1609 ///
1610 /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
1611 /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
1612 ///
1613 /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
1614 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1615 ///
1616 /// let v: Vec<&str> = "AABBCC".split("DD").collect();
1617 /// assert_eq!(v, ["AABBCC"]);
1618 ///
1619 /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
1620 /// assert_eq!(v, ["abc", "def", "ghi"]);
1621 ///
1622 /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
1623 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1624 /// ```
1625 ///
1626 /// If the pattern is a slice of chars, split on each occurrence of any of the characters:
1627 ///
1628 /// ```
1629 /// let v: Vec<&str> = "2020-11-03 23:59".split(&['-', ' ', ':', '@'][..]).collect();
1630 /// assert_eq!(v, ["2020", "11", "03", "23", "59"]);
1631 /// ```
1632 ///
1633 /// A more complex pattern, using a closure:
1634 ///
1635 /// ```
1636 /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
1637 /// assert_eq!(v, ["abc", "def", "ghi"]);
1638 /// ```
1639 ///
1640 /// If a string contains multiple contiguous separators, you will end up
1641 /// with empty strings in the output:
1642 ///
1643 /// ```
1644 /// let x = "||||a||b|c".to_string();
1645 /// let d: Vec<_> = x.split('|').collect();
1646 ///
1647 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1648 /// ```
1649 ///
1650 /// Contiguous separators are separated by the empty string.
1651 ///
1652 /// ```
1653 /// let x = "(///)".to_string();
1654 /// let d: Vec<_> = x.split('/').collect();
1655 ///
1656 /// assert_eq!(d, &["(", "", "", ")"]);
1657 /// ```
1658 ///
1659 /// Separators at the start or end of a string are neighbored
1660 /// by empty strings.
1661 ///
1662 /// ```
1663 /// let d: Vec<_> = "010".split("0").collect();
1664 /// assert_eq!(d, &["", "1", ""]);
1665 /// ```
1666 ///
1667 /// When the empty string is used as a separator, it separates
1668 /// every character in the string, along with the beginning
1669 /// and end of the string.
1670 ///
1671 /// ```
1672 /// let f: Vec<_> = "rust".split("").collect();
1673 /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
1674 /// ```
1675 ///
1676 /// Contiguous separators can lead to possibly surprising behavior
1677 /// when whitespace is used as the separator. This code is correct:
1678 ///
1679 /// ```
1680 /// let x = " a b c".to_string();
1681 /// let d: Vec<_> = x.split(' ').collect();
1682 ///
1683 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1684 /// ```
1685 ///
1686 /// It does _not_ give you:
1687 ///
1688 /// ```,ignore
1689 /// assert_eq!(d, &["a", "b", "c"]);
1690 /// ```
1691 ///
1692 /// Use [`split_whitespace`] for this behavior.
1693 ///
1694 /// [`split_whitespace`]: str::split_whitespace
1695 #[stable(feature = "rust1", since = "1.0.0")]
1696 #[inline]
1697 pub fn split<P: Pattern>(&self, pat: P) -> Split<'_, P> {
1698 Split(SplitInternal {
1699 start: 0,
1700 end: self.len(),
1701 matcher: pat.into_searcher(self),
1702 allow_trailing_empty: true,
1703 finished: false,
1704 })
1705 }
1706
1707 /// Returns an iterator over substrings of this string slice, separated by
1708 /// characters matched by a pattern.
1709 ///
1710 /// Differs from the iterator produced by `split` in that `split_inclusive`
1711 /// leaves the matched part as the terminator of the substring.
1712 ///
1713 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1714 /// function or closure that determines if a character matches.
1715 ///
1716 /// [`char`]: prim@char
1717 /// [pattern]: self::pattern
1718 ///
1719 /// # Examples
1720 ///
1721 /// ```
1722 /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb."
1723 /// .split_inclusive('\n').collect();
1724 /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]);
1725 /// ```
1726 ///
1727 /// If the last element of the string is matched,
1728 /// that element will be considered the terminator of the preceding substring.
1729 /// That substring will be the last item returned by the iterator.
1730 ///
1731 /// ```
1732 /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb.\n"
1733 /// .split_inclusive('\n').collect();
1734 /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb.\n"]);
1735 /// ```
1736 #[stable(feature = "split_inclusive", since = "1.51.0")]
1737 #[inline]
1738 #[ferrocene::prevalidated]
1739 pub fn split_inclusive<P: Pattern>(&self, pat: P) -> SplitInclusive<'_, P> {
1740 SplitInclusive(SplitInternal {
1741 start: 0,
1742 end: self.len(),
1743 matcher: pat.into_searcher(self),
1744 allow_trailing_empty: false,
1745 finished: false,
1746 })
1747 }
1748
1749 /// Returns an iterator over substrings of the given string slice, separated
1750 /// by characters matched by a pattern and yielded in reverse order.
1751 ///
1752 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1753 /// function or closure that determines if a character matches.
1754 ///
1755 /// [`char`]: prim@char
1756 /// [pattern]: self::pattern
1757 ///
1758 /// # Iterator behavior
1759 ///
1760 /// The returned iterator requires that the pattern supports a reverse
1761 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1762 /// search yields the same elements.
1763 ///
1764 /// For iterating from the front, the [`split`] method can be used.
1765 ///
1766 /// [`split`]: str::split
1767 ///
1768 /// # Examples
1769 ///
1770 /// Simple patterns:
1771 ///
1772 /// ```
1773 /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
1774 /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
1775 ///
1776 /// let v: Vec<&str> = "".rsplit('X').collect();
1777 /// assert_eq!(v, [""]);
1778 ///
1779 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
1780 /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
1781 ///
1782 /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
1783 /// assert_eq!(v, ["leopard", "tiger", "lion"]);
1784 /// ```
1785 ///
1786 /// A more complex pattern, using a closure:
1787 ///
1788 /// ```
1789 /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
1790 /// assert_eq!(v, ["ghi", "def", "abc"]);
1791 /// ```
1792 #[stable(feature = "rust1", since = "1.0.0")]
1793 #[inline]
1794 pub fn rsplit<P: Pattern>(&self, pat: P) -> RSplit<'_, P>
1795 where
1796 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1797 {
1798 RSplit(self.split(pat).0)
1799 }
1800
1801 /// Returns an iterator over substrings of the given string slice, separated
1802 /// by characters matched by a pattern.
1803 ///
1804 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1805 /// function or closure that determines if a character matches.
1806 ///
1807 /// [`char`]: prim@char
1808 /// [pattern]: self::pattern
1809 ///
1810 /// Equivalent to [`split`], except that the trailing substring
1811 /// is skipped if empty.
1812 ///
1813 /// [`split`]: str::split
1814 ///
1815 /// This method can be used for string data that is _terminated_,
1816 /// rather than _separated_ by a pattern.
1817 ///
1818 /// # Iterator behavior
1819 ///
1820 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1821 /// allows a reverse search and forward/reverse search yields the same
1822 /// elements. This is true for, e.g., [`char`], but not for `&str`.
1823 ///
1824 /// If the pattern allows a reverse search but its results might differ
1825 /// from a forward search, the [`rsplit_terminator`] method can be used.
1826 ///
1827 /// [`rsplit_terminator`]: str::rsplit_terminator
1828 ///
1829 /// # Examples
1830 ///
1831 /// ```
1832 /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
1833 /// assert_eq!(v, ["A", "B"]);
1834 ///
1835 /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
1836 /// assert_eq!(v, ["A", "", "B", ""]);
1837 ///
1838 /// let v: Vec<&str> = "A.B:C.D".split_terminator(&['.', ':'][..]).collect();
1839 /// assert_eq!(v, ["A", "B", "C", "D"]);
1840 /// ```
1841 #[stable(feature = "rust1", since = "1.0.0")]
1842 #[inline]
1843 pub fn split_terminator<P: Pattern>(&self, pat: P) -> SplitTerminator<'_, P> {
1844 SplitTerminator(SplitInternal { allow_trailing_empty: false, ..self.split(pat).0 })
1845 }
1846
1847 /// Returns an iterator over substrings of `self`, separated by characters
1848 /// matched by a pattern and yielded in reverse order.
1849 ///
1850 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1851 /// function or closure that determines if a character matches.
1852 ///
1853 /// [`char`]: prim@char
1854 /// [pattern]: self::pattern
1855 ///
1856 /// Equivalent to [`split`], except that the trailing substring is
1857 /// skipped if empty.
1858 ///
1859 /// [`split`]: str::split
1860 ///
1861 /// This method can be used for string data that is _terminated_,
1862 /// rather than _separated_ by a pattern.
1863 ///
1864 /// # Iterator behavior
1865 ///
1866 /// The returned iterator requires that the pattern supports a
1867 /// reverse search, and it will be double ended if a forward/reverse
1868 /// search yields the same elements.
1869 ///
1870 /// For iterating from the front, the [`split_terminator`] method can be
1871 /// used.
1872 ///
1873 /// [`split_terminator`]: str::split_terminator
1874 ///
1875 /// # Examples
1876 ///
1877 /// ```
1878 /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
1879 /// assert_eq!(v, ["B", "A"]);
1880 ///
1881 /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
1882 /// assert_eq!(v, ["", "B", "", "A"]);
1883 ///
1884 /// let v: Vec<&str> = "A.B:C.D".rsplit_terminator(&['.', ':'][..]).collect();
1885 /// assert_eq!(v, ["D", "C", "B", "A"]);
1886 /// ```
1887 #[stable(feature = "rust1", since = "1.0.0")]
1888 #[inline]
1889 pub fn rsplit_terminator<P: Pattern>(&self, pat: P) -> RSplitTerminator<'_, P>
1890 where
1891 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1892 {
1893 RSplitTerminator(self.split_terminator(pat).0)
1894 }
1895
1896 /// Returns an iterator over substrings of the given string slice, separated
1897 /// by a pattern, restricted to returning at most `n` items.
1898 ///
1899 /// If `n` substrings are returned, the last substring (the `n`th substring)
1900 /// will contain the remainder of the string.
1901 ///
1902 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1903 /// function or closure that determines if a character matches.
1904 ///
1905 /// [`char`]: prim@char
1906 /// [pattern]: self::pattern
1907 ///
1908 /// # Iterator behavior
1909 ///
1910 /// The returned iterator will not be double ended, because it is
1911 /// not efficient to support.
1912 ///
1913 /// If the pattern allows a reverse search, the [`rsplitn`] method can be
1914 /// used.
1915 ///
1916 /// [`rsplitn`]: str::rsplitn
1917 ///
1918 /// # Examples
1919 ///
1920 /// Simple patterns:
1921 ///
1922 /// ```
1923 /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
1924 /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
1925 ///
1926 /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
1927 /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
1928 ///
1929 /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
1930 /// assert_eq!(v, ["abcXdef"]);
1931 ///
1932 /// let v: Vec<&str> = "".splitn(1, 'X').collect();
1933 /// assert_eq!(v, [""]);
1934 /// ```
1935 ///
1936 /// A more complex pattern, using a closure:
1937 ///
1938 /// ```
1939 /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
1940 /// assert_eq!(v, ["abc", "defXghi"]);
1941 /// ```
1942 #[stable(feature = "rust1", since = "1.0.0")]
1943 #[inline]
1944 pub fn splitn<P: Pattern>(&self, n: usize, pat: P) -> SplitN<'_, P> {
1945 SplitN(SplitNInternal { iter: self.split(pat).0, count: n })
1946 }
1947
1948 /// Returns an iterator over substrings of this string slice, separated by a
1949 /// pattern, starting from the end of the string, restricted to returning at
1950 /// most `n` items.
1951 ///
1952 /// If `n` substrings are returned, the last substring (the `n`th substring)
1953 /// will contain the remainder of the string.
1954 ///
1955 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1956 /// function or closure that determines if a character matches.
1957 ///
1958 /// [`char`]: prim@char
1959 /// [pattern]: self::pattern
1960 ///
1961 /// # Iterator behavior
1962 ///
1963 /// The returned iterator will not be double ended, because it is not
1964 /// efficient to support.
1965 ///
1966 /// For splitting from the front, the [`splitn`] method can be used.
1967 ///
1968 /// [`splitn`]: str::splitn
1969 ///
1970 /// # Examples
1971 ///
1972 /// Simple patterns:
1973 ///
1974 /// ```
1975 /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
1976 /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
1977 ///
1978 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
1979 /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
1980 ///
1981 /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
1982 /// assert_eq!(v, ["leopard", "lion::tiger"]);
1983 /// ```
1984 ///
1985 /// A more complex pattern, using a closure:
1986 ///
1987 /// ```
1988 /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
1989 /// assert_eq!(v, ["ghi", "abc1def"]);
1990 /// ```
1991 #[stable(feature = "rust1", since = "1.0.0")]
1992 #[inline]
1993 pub fn rsplitn<P: Pattern>(&self, n: usize, pat: P) -> RSplitN<'_, P>
1994 where
1995 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1996 {
1997 RSplitN(self.splitn(n, pat).0)
1998 }
1999
2000 /// Splits the string on the first occurrence of the specified delimiter and
2001 /// returns prefix before delimiter and suffix after delimiter.
2002 ///
2003 /// # Examples
2004 ///
2005 /// ```
2006 /// assert_eq!("cfg".split_once('='), None);
2007 /// assert_eq!("cfg=".split_once('='), Some(("cfg", "")));
2008 /// assert_eq!("cfg=foo".split_once('='), Some(("cfg", "foo")));
2009 /// assert_eq!("cfg=foo=bar".split_once('='), Some(("cfg", "foo=bar")));
2010 /// ```
2011 #[stable(feature = "str_split_once", since = "1.52.0")]
2012 #[inline]
2013 pub fn split_once<P: Pattern>(&self, delimiter: P) -> Option<(&'_ str, &'_ str)> {
2014 let (start, end) = delimiter.into_searcher(self).next_match()?;
2015 // SAFETY: `Searcher` is known to return valid indices.
2016 unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
2017 }
2018
2019 /// Splits the string on the last occurrence of the specified delimiter and
2020 /// returns prefix before delimiter and suffix after delimiter.
2021 ///
2022 /// # Examples
2023 ///
2024 /// ```
2025 /// assert_eq!("cfg".rsplit_once('='), None);
2026 /// assert_eq!("cfg=".rsplit_once('='), Some(("cfg", "")));
2027 /// assert_eq!("cfg=foo".rsplit_once('='), Some(("cfg", "foo")));
2028 /// assert_eq!("cfg=foo=bar".rsplit_once('='), Some(("cfg=foo", "bar")));
2029 /// ```
2030 #[stable(feature = "str_split_once", since = "1.52.0")]
2031 #[inline]
2032 pub fn rsplit_once<P: Pattern>(&self, delimiter: P) -> Option<(&'_ str, &'_ str)>
2033 where
2034 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2035 {
2036 let (start, end) = delimiter.into_searcher(self).next_match_back()?;
2037 // SAFETY: `Searcher` is known to return valid indices.
2038 unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
2039 }
2040
2041 /// Returns an iterator over the disjoint matches of a pattern within the
2042 /// given string slice.
2043 ///
2044 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2045 /// function or closure that determines if a character matches.
2046 ///
2047 /// [`char`]: prim@char
2048 /// [pattern]: self::pattern
2049 ///
2050 /// # Iterator behavior
2051 ///
2052 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
2053 /// allows a reverse search and forward/reverse search yields the same
2054 /// elements. This is true for, e.g., [`char`], but not for `&str`.
2055 ///
2056 /// If the pattern allows a reverse search but its results might differ
2057 /// from a forward search, the [`rmatches`] method can be used.
2058 ///
2059 /// [`rmatches`]: str::rmatches
2060 ///
2061 /// # Examples
2062 ///
2063 /// ```
2064 /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
2065 /// assert_eq!(v, ["abc", "abc", "abc"]);
2066 ///
2067 /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
2068 /// assert_eq!(v, ["1", "2", "3"]);
2069 /// ```
2070 #[stable(feature = "str_matches", since = "1.2.0")]
2071 #[inline]
2072 pub fn matches<P: Pattern>(&self, pat: P) -> Matches<'_, P> {
2073 Matches(MatchesInternal(pat.into_searcher(self)))
2074 }
2075
2076 /// Returns an iterator over the disjoint matches of a pattern within this
2077 /// string slice, yielded in reverse order.
2078 ///
2079 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2080 /// function or closure that determines if a character matches.
2081 ///
2082 /// [`char`]: prim@char
2083 /// [pattern]: self::pattern
2084 ///
2085 /// # Iterator behavior
2086 ///
2087 /// The returned iterator requires that the pattern supports a reverse
2088 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
2089 /// search yields the same elements.
2090 ///
2091 /// For iterating from the front, the [`matches`] method can be used.
2092 ///
2093 /// [`matches`]: str::matches
2094 ///
2095 /// # Examples
2096 ///
2097 /// ```
2098 /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
2099 /// assert_eq!(v, ["abc", "abc", "abc"]);
2100 ///
2101 /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
2102 /// assert_eq!(v, ["3", "2", "1"]);
2103 /// ```
2104 #[stable(feature = "str_matches", since = "1.2.0")]
2105 #[inline]
2106 pub fn rmatches<P: Pattern>(&self, pat: P) -> RMatches<'_, P>
2107 where
2108 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2109 {
2110 RMatches(self.matches(pat).0)
2111 }
2112
2113 /// Returns an iterator over the disjoint matches of a pattern within this string
2114 /// slice as well as the index that the match starts at.
2115 ///
2116 /// For matches of `pat` within `self` that overlap, only the indices
2117 /// corresponding to the first match are returned.
2118 ///
2119 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2120 /// function or closure that determines if a character matches.
2121 ///
2122 /// [`char`]: prim@char
2123 /// [pattern]: self::pattern
2124 ///
2125 /// # Iterator behavior
2126 ///
2127 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
2128 /// allows a reverse search and forward/reverse search yields the same
2129 /// elements. This is true for, e.g., [`char`], but not for `&str`.
2130 ///
2131 /// If the pattern allows a reverse search but its results might differ
2132 /// from a forward search, the [`rmatch_indices`] method can be used.
2133 ///
2134 /// [`rmatch_indices`]: str::rmatch_indices
2135 ///
2136 /// # Examples
2137 ///
2138 /// ```
2139 /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
2140 /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
2141 ///
2142 /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
2143 /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
2144 ///
2145 /// let v: Vec<_> = "ababa".match_indices("aba").collect();
2146 /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
2147 /// ```
2148 #[stable(feature = "str_match_indices", since = "1.5.0")]
2149 #[inline]
2150 pub fn match_indices<P: Pattern>(&self, pat: P) -> MatchIndices<'_, P> {
2151 MatchIndices(MatchIndicesInternal(pat.into_searcher(self)))
2152 }
2153
2154 /// Returns an iterator over the disjoint matches of a pattern within `self`,
2155 /// yielded in reverse order along with the index of the match.
2156 ///
2157 /// For matches of `pat` within `self` that overlap, only the indices
2158 /// corresponding to the last match are returned.
2159 ///
2160 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2161 /// function or closure that determines if a character matches.
2162 ///
2163 /// [`char`]: prim@char
2164 /// [pattern]: self::pattern
2165 ///
2166 /// # Iterator behavior
2167 ///
2168 /// The returned iterator requires that the pattern supports a reverse
2169 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
2170 /// search yields the same elements.
2171 ///
2172 /// For iterating from the front, the [`match_indices`] method can be used.
2173 ///
2174 /// [`match_indices`]: str::match_indices
2175 ///
2176 /// # Examples
2177 ///
2178 /// ```
2179 /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
2180 /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
2181 ///
2182 /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
2183 /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
2184 ///
2185 /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
2186 /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
2187 /// ```
2188 #[stable(feature = "str_match_indices", since = "1.5.0")]
2189 #[inline]
2190 pub fn rmatch_indices<P: Pattern>(&self, pat: P) -> RMatchIndices<'_, P>
2191 where
2192 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2193 {
2194 RMatchIndices(self.match_indices(pat).0)
2195 }
2196
2197 /// Returns a string slice with leading and trailing whitespace removed.
2198 ///
2199 /// 'Whitespace' is defined according to the terms of the Unicode Derived
2200 /// Core Property `White_Space`, which includes newlines.
2201 ///
2202 /// # Examples
2203 ///
2204 /// ```
2205 /// let s = "\n Hello\tworld\t\n";
2206 ///
2207 /// assert_eq!("Hello\tworld", s.trim());
2208 /// ```
2209 #[inline]
2210 #[must_use = "this returns the trimmed string as a slice, \
2211 without modifying the original"]
2212 #[stable(feature = "rust1", since = "1.0.0")]
2213 #[rustc_diagnostic_item = "str_trim"]
2214 pub fn trim(&self) -> &str {
2215 self.trim_matches(char::is_whitespace)
2216 }
2217
2218 /// Returns a string slice with leading whitespace removed.
2219 ///
2220 /// 'Whitespace' is defined according to the terms of the Unicode Derived
2221 /// Core Property `White_Space`, which includes newlines.
2222 ///
2223 /// # Text directionality
2224 ///
2225 /// A string is a sequence of bytes. `start` in this context means the first
2226 /// position of that byte string; for a left-to-right language like English or
2227 /// Russian, this will be left side, and for right-to-left languages like
2228 /// Arabic or Hebrew, this will be the right side.
2229 ///
2230 /// # Examples
2231 ///
2232 /// Basic usage:
2233 ///
2234 /// ```
2235 /// let s = "\n Hello\tworld\t\n";
2236 /// assert_eq!("Hello\tworld\t\n", s.trim_start());
2237 /// ```
2238 ///
2239 /// Directionality:
2240 ///
2241 /// ```
2242 /// let s = " English ";
2243 /// assert!(Some('E') == s.trim_start().chars().next());
2244 ///
2245 /// let s = " עברית ";
2246 /// assert!(Some('ע') == s.trim_start().chars().next());
2247 /// ```
2248 #[inline]
2249 #[must_use = "this returns the trimmed string as a new slice, \
2250 without modifying the original"]
2251 #[stable(feature = "trim_direction", since = "1.30.0")]
2252 #[rustc_diagnostic_item = "str_trim_start"]
2253 pub fn trim_start(&self) -> &str {
2254 self.trim_start_matches(char::is_whitespace)
2255 }
2256
2257 /// Returns a string slice with trailing whitespace removed.
2258 ///
2259 /// 'Whitespace' is defined according to the terms of the Unicode Derived
2260 /// Core Property `White_Space`, which includes newlines.
2261 ///
2262 /// # Text directionality
2263 ///
2264 /// A string is a sequence of bytes. `end` in this context means the last
2265 /// position of that byte string; for a left-to-right language like English or
2266 /// Russian, this will be right side, and for right-to-left languages like
2267 /// Arabic or Hebrew, this will be the left side.
2268 ///
2269 /// # Examples
2270 ///
2271 /// Basic usage:
2272 ///
2273 /// ```
2274 /// let s = "\n Hello\tworld\t\n";
2275 /// assert_eq!("\n Hello\tworld", s.trim_end());
2276 /// ```
2277 ///
2278 /// Directionality:
2279 ///
2280 /// ```
2281 /// let s = " English ";
2282 /// assert!(Some('h') == s.trim_end().chars().rev().next());
2283 ///
2284 /// let s = " עברית ";
2285 /// assert!(Some('ת') == s.trim_end().chars().rev().next());
2286 /// ```
2287 #[inline]
2288 #[must_use = "this returns the trimmed string as a new slice, \
2289 without modifying the original"]
2290 #[stable(feature = "trim_direction", since = "1.30.0")]
2291 #[rustc_diagnostic_item = "str_trim_end"]
2292 pub fn trim_end(&self) -> &str {
2293 self.trim_end_matches(char::is_whitespace)
2294 }
2295
2296 /// Returns a string slice with leading whitespace removed.
2297 ///
2298 /// 'Whitespace' is defined according to the terms of the Unicode Derived
2299 /// Core Property `White_Space`.
2300 ///
2301 /// # Text directionality
2302 ///
2303 /// A string is a sequence of bytes. 'Left' in this context means the first
2304 /// position of that byte string; for a language like Arabic or Hebrew
2305 /// which are 'right to left' rather than 'left to right', this will be
2306 /// the _right_ side, not the left.
2307 ///
2308 /// # Examples
2309 ///
2310 /// Basic usage:
2311 ///
2312 /// ```
2313 /// let s = " Hello\tworld\t";
2314 ///
2315 /// assert_eq!("Hello\tworld\t", s.trim_left());
2316 /// ```
2317 ///
2318 /// Directionality:
2319 ///
2320 /// ```
2321 /// let s = " English";
2322 /// assert!(Some('E') == s.trim_left().chars().next());
2323 ///
2324 /// let s = " עברית";
2325 /// assert!(Some('ע') == s.trim_left().chars().next());
2326 /// ```
2327 #[must_use = "this returns the trimmed string as a new slice, \
2328 without modifying the original"]
2329 #[inline]
2330 #[stable(feature = "rust1", since = "1.0.0")]
2331 #[deprecated(since = "1.33.0", note = "superseded by `trim_start`", suggestion = "trim_start")]
2332 pub fn trim_left(&self) -> &str {
2333 self.trim_start()
2334 }
2335
2336 /// Returns a string slice with trailing whitespace removed.
2337 ///
2338 /// 'Whitespace' is defined according to the terms of the Unicode Derived
2339 /// Core Property `White_Space`.
2340 ///
2341 /// # Text directionality
2342 ///
2343 /// A string is a sequence of bytes. 'Right' in this context means the last
2344 /// position of that byte string; for a language like Arabic or Hebrew
2345 /// which are 'right to left' rather than 'left to right', this will be
2346 /// the _left_ side, not the right.
2347 ///
2348 /// # Examples
2349 ///
2350 /// Basic usage:
2351 ///
2352 /// ```
2353 /// let s = " Hello\tworld\t";
2354 ///
2355 /// assert_eq!(" Hello\tworld", s.trim_right());
2356 /// ```
2357 ///
2358 /// Directionality:
2359 ///
2360 /// ```
2361 /// let s = "English ";
2362 /// assert!(Some('h') == s.trim_right().chars().rev().next());
2363 ///
2364 /// let s = "עברית ";
2365 /// assert!(Some('ת') == s.trim_right().chars().rev().next());
2366 /// ```
2367 #[must_use = "this returns the trimmed string as a new slice, \
2368 without modifying the original"]
2369 #[inline]
2370 #[stable(feature = "rust1", since = "1.0.0")]
2371 #[deprecated(since = "1.33.0", note = "superseded by `trim_end`", suggestion = "trim_end")]
2372 pub fn trim_right(&self) -> &str {
2373 self.trim_end()
2374 }
2375
2376 /// Returns a string slice with all prefixes and suffixes that match a
2377 /// pattern repeatedly removed.
2378 ///
2379 /// The [pattern] can be a [`char`], a slice of [`char`]s, or a function
2380 /// or closure that determines if a character matches.
2381 ///
2382 /// [`char`]: prim@char
2383 /// [pattern]: self::pattern
2384 ///
2385 /// # Examples
2386 ///
2387 /// Simple patterns:
2388 ///
2389 /// ```
2390 /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
2391 /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
2392 ///
2393 /// let x: &[_] = &['1', '2'];
2394 /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
2395 /// ```
2396 ///
2397 /// A more complex pattern, using a closure:
2398 ///
2399 /// ```
2400 /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
2401 /// ```
2402 #[must_use = "this returns the trimmed string as a new slice, \
2403 without modifying the original"]
2404 #[stable(feature = "rust1", since = "1.0.0")]
2405 pub fn trim_matches<P: Pattern>(&self, pat: P) -> &str
2406 where
2407 for<'a> P::Searcher<'a>: DoubleEndedSearcher<'a>,
2408 {
2409 let mut i = 0;
2410 let mut j = 0;
2411 let mut matcher = pat.into_searcher(self);
2412 if let Some((a, b)) = matcher.next_reject() {
2413 i = a;
2414 j = b; // Remember earliest known match, correct it below if
2415 // last match is different
2416 }
2417 if let Some((_, b)) = matcher.next_reject_back() {
2418 j = b;
2419 }
2420 // SAFETY: `Searcher` is known to return valid indices.
2421 unsafe { self.get_unchecked(i..j) }
2422 }
2423
2424 /// Returns a string slice with all prefixes that match a pattern
2425 /// repeatedly removed.
2426 ///
2427 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2428 /// function or closure that determines if a character matches.
2429 ///
2430 /// [`char`]: prim@char
2431 /// [pattern]: self::pattern
2432 ///
2433 /// # Text directionality
2434 ///
2435 /// A string is a sequence of bytes. `start` in this context means the first
2436 /// position of that byte string; for a left-to-right language like English or
2437 /// Russian, this will be left side, and for right-to-left languages like
2438 /// Arabic or Hebrew, this will be the right side.
2439 ///
2440 /// # Examples
2441 ///
2442 /// ```
2443 /// assert_eq!("11foo1bar11".trim_start_matches('1'), "foo1bar11");
2444 /// assert_eq!("123foo1bar123".trim_start_matches(char::is_numeric), "foo1bar123");
2445 ///
2446 /// let x: &[_] = &['1', '2'];
2447 /// assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12");
2448 /// ```
2449 #[must_use = "this returns the trimmed string as a new slice, \
2450 without modifying the original"]
2451 #[stable(feature = "trim_direction", since = "1.30.0")]
2452 pub fn trim_start_matches<P: Pattern>(&self, pat: P) -> &str {
2453 let mut i = self.len();
2454 let mut matcher = pat.into_searcher(self);
2455 if let Some((a, _)) = matcher.next_reject() {
2456 i = a;
2457 }
2458 // SAFETY: `Searcher` is known to return valid indices.
2459 unsafe { self.get_unchecked(i..self.len()) }
2460 }
2461
2462 /// Returns a string slice with the prefix removed.
2463 ///
2464 /// If the string starts with the pattern `prefix`, returns the substring after the prefix,
2465 /// wrapped in `Some`. Unlike [`trim_start_matches`], this method removes the prefix exactly once.
2466 ///
2467 /// If the string does not start with `prefix`, returns `None`.
2468 ///
2469 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2470 /// function or closure that determines if a character matches.
2471 ///
2472 /// [`char`]: prim@char
2473 /// [pattern]: self::pattern
2474 /// [`trim_start_matches`]: Self::trim_start_matches
2475 ///
2476 /// # Examples
2477 ///
2478 /// ```
2479 /// assert_eq!("foo:bar".strip_prefix("foo:"), Some("bar"));
2480 /// assert_eq!("foo:bar".strip_prefix("bar"), None);
2481 /// assert_eq!("foofoo".strip_prefix("foo"), Some("foo"));
2482 /// ```
2483 #[must_use = "this returns the remaining substring as a new slice, \
2484 without modifying the original"]
2485 #[stable(feature = "str_strip", since = "1.45.0")]
2486 pub fn strip_prefix<P: Pattern>(&self, prefix: P) -> Option<&str> {
2487 prefix.strip_prefix_of(self)
2488 }
2489
2490 /// Returns a string slice with the suffix removed.
2491 ///
2492 /// If the string ends with the pattern `suffix`, returns the substring before the suffix,
2493 /// wrapped in `Some`. Unlike [`trim_end_matches`], this method removes the suffix exactly once.
2494 ///
2495 /// If the string does not end with `suffix`, returns `None`.
2496 ///
2497 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2498 /// function or closure that determines if a character matches.
2499 ///
2500 /// [`char`]: prim@char
2501 /// [pattern]: self::pattern
2502 /// [`trim_end_matches`]: Self::trim_end_matches
2503 ///
2504 /// # Examples
2505 ///
2506 /// ```
2507 /// assert_eq!("bar:foo".strip_suffix(":foo"), Some("bar"));
2508 /// assert_eq!("bar:foo".strip_suffix("bar"), None);
2509 /// assert_eq!("foofoo".strip_suffix("foo"), Some("foo"));
2510 /// ```
2511 #[must_use = "this returns the remaining substring as a new slice, \
2512 without modifying the original"]
2513 #[stable(feature = "str_strip", since = "1.45.0")]
2514 #[ferrocene::prevalidated]
2515 pub fn strip_suffix<P: Pattern>(&self, suffix: P) -> Option<&str>
2516 where
2517 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2518 {
2519 suffix.strip_suffix_of(self)
2520 }
2521
2522 /// Returns a string slice with the prefix and suffix removed.
2523 ///
2524 /// If the string starts with the pattern `prefix` and ends with the pattern `suffix`, returns
2525 /// the substring after the prefix and before the suffix, wrapped in `Some`.
2526 /// Unlike [`trim_start_matches`] and [`trim_end_matches`], this method removes both the prefix
2527 /// and suffix exactly once.
2528 ///
2529 /// If the string does not start with `prefix` or does not end with `suffix`, returns `None`.
2530 ///
2531 /// Each [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2532 /// function or closure that determines if a character matches.
2533 ///
2534 /// [`char`]: prim@char
2535 /// [pattern]: self::pattern
2536 /// [`trim_start_matches`]: Self::trim_start_matches
2537 /// [`trim_end_matches`]: Self::trim_end_matches
2538 ///
2539 /// # Examples
2540 ///
2541 /// ```
2542 /// assert_eq!("bar:hello:foo".strip_circumfix("bar:", ":foo"), Some("hello"));
2543 /// assert_eq!("bar:foo".strip_circumfix("foo", "foo"), None);
2544 /// assert_eq!("foo:bar;".strip_circumfix("foo:", ';'), Some("bar"));
2545 /// ```
2546 #[must_use = "this returns the remaining substring as a new slice, \
2547 without modifying the original"]
2548 #[stable(feature = "strip_circumfix", since = "CURRENT_RUSTC_VERSION")]
2549 pub fn strip_circumfix<P: Pattern, S: Pattern>(&self, prefix: P, suffix: S) -> Option<&str>
2550 where
2551 for<'a> S::Searcher<'a>: ReverseSearcher<'a>,
2552 {
2553 self.strip_prefix(prefix)?.strip_suffix(suffix)
2554 }
2555
2556 /// Returns a string slice with the optional prefix removed.
2557 ///
2558 /// If the string starts with the pattern `prefix`, returns the substring after the prefix.
2559 /// Unlike [`strip_prefix`], this method always returns `&str` for easy method chaining,
2560 /// instead of returning [`Option<&str>`].
2561 ///
2562 /// If the string does not start with `prefix`, returns the original string unchanged.
2563 ///
2564 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2565 /// function or closure that determines if a character matches.
2566 ///
2567 /// [`char`]: prim@char
2568 /// [pattern]: self::pattern
2569 /// [`strip_prefix`]: Self::strip_prefix
2570 ///
2571 /// # Examples
2572 ///
2573 /// ```
2574 /// #![feature(trim_prefix_suffix)]
2575 ///
2576 /// // Prefix present - removes it
2577 /// assert_eq!("foo:bar".trim_prefix("foo:"), "bar");
2578 /// assert_eq!("foofoo".trim_prefix("foo"), "foo");
2579 ///
2580 /// // Prefix absent - returns original string
2581 /// assert_eq!("foo:bar".trim_prefix("bar"), "foo:bar");
2582 ///
2583 /// // Method chaining example
2584 /// assert_eq!("<https://example.com/>".trim_prefix('<').trim_suffix('>'), "https://example.com/");
2585 /// ```
2586 #[must_use = "this returns the remaining substring as a new slice, \
2587 without modifying the original"]
2588 #[unstable(feature = "trim_prefix_suffix", issue = "142312")]
2589 pub fn trim_prefix<P: Pattern>(&self, prefix: P) -> &str {
2590 prefix.strip_prefix_of(self).unwrap_or(self)
2591 }
2592
2593 /// Returns a string slice with the optional suffix removed.
2594 ///
2595 /// If the string ends with the pattern `suffix`, returns the substring before the suffix.
2596 /// Unlike [`strip_suffix`], this method always returns `&str` for easy method chaining,
2597 /// instead of returning [`Option<&str>`].
2598 ///
2599 /// If the string does not end with `suffix`, returns the original string unchanged.
2600 ///
2601 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2602 /// function or closure that determines if a character matches.
2603 ///
2604 /// [`char`]: prim@char
2605 /// [pattern]: self::pattern
2606 /// [`strip_suffix`]: Self::strip_suffix
2607 ///
2608 /// # Examples
2609 ///
2610 /// ```
2611 /// #![feature(trim_prefix_suffix)]
2612 ///
2613 /// // Suffix present - removes it
2614 /// assert_eq!("bar:foo".trim_suffix(":foo"), "bar");
2615 /// assert_eq!("foofoo".trim_suffix("foo"), "foo");
2616 ///
2617 /// // Suffix absent - returns original string
2618 /// assert_eq!("bar:foo".trim_suffix("bar"), "bar:foo");
2619 ///
2620 /// // Method chaining example
2621 /// assert_eq!("<https://example.com/>".trim_prefix('<').trim_suffix('>'), "https://example.com/");
2622 /// ```
2623 #[must_use = "this returns the remaining substring as a new slice, \
2624 without modifying the original"]
2625 #[unstable(feature = "trim_prefix_suffix", issue = "142312")]
2626 pub fn trim_suffix<P: Pattern>(&self, suffix: P) -> &str
2627 where
2628 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2629 {
2630 suffix.strip_suffix_of(self).unwrap_or(self)
2631 }
2632
2633 /// Returns a string slice with all suffixes that match a pattern
2634 /// repeatedly removed.
2635 ///
2636 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2637 /// function or closure that determines if a character matches.
2638 ///
2639 /// [`char`]: prim@char
2640 /// [pattern]: self::pattern
2641 ///
2642 /// # Text directionality
2643 ///
2644 /// A string is a sequence of bytes. `end` in this context means the last
2645 /// position of that byte string; for a left-to-right language like English or
2646 /// Russian, this will be right side, and for right-to-left languages like
2647 /// Arabic or Hebrew, this will be the left side.
2648 ///
2649 /// # Examples
2650 ///
2651 /// Simple patterns:
2652 ///
2653 /// ```
2654 /// assert_eq!("11foo1bar11".trim_end_matches('1'), "11foo1bar");
2655 /// assert_eq!("123foo1bar123".trim_end_matches(char::is_numeric), "123foo1bar");
2656 ///
2657 /// let x: &[_] = &['1', '2'];
2658 /// assert_eq!("12foo1bar12".trim_end_matches(x), "12foo1bar");
2659 /// ```
2660 ///
2661 /// A more complex pattern, using a closure:
2662 ///
2663 /// ```
2664 /// assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo");
2665 /// ```
2666 #[must_use = "this returns the trimmed string as a new slice, \
2667 without modifying the original"]
2668 #[stable(feature = "trim_direction", since = "1.30.0")]
2669 pub fn trim_end_matches<P: Pattern>(&self, pat: P) -> &str
2670 where
2671 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2672 {
2673 let mut j = 0;
2674 let mut matcher = pat.into_searcher(self);
2675 if let Some((_, b)) = matcher.next_reject_back() {
2676 j = b;
2677 }
2678 // SAFETY: `Searcher` is known to return valid indices.
2679 unsafe { self.get_unchecked(0..j) }
2680 }
2681
2682 /// Returns a string slice with all prefixes that match a pattern
2683 /// repeatedly removed.
2684 ///
2685 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2686 /// function or closure that determines if a character matches.
2687 ///
2688 /// [`char`]: prim@char
2689 /// [pattern]: self::pattern
2690 ///
2691 /// # Text directionality
2692 ///
2693 /// A string is a sequence of bytes. 'Left' in this context means the first
2694 /// position of that byte string; for a language like Arabic or Hebrew
2695 /// which are 'right to left' rather than 'left to right', this will be
2696 /// the _right_ side, not the left.
2697 ///
2698 /// # Examples
2699 ///
2700 /// ```
2701 /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
2702 /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
2703 ///
2704 /// let x: &[_] = &['1', '2'];
2705 /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
2706 /// ```
2707 #[stable(feature = "rust1", since = "1.0.0")]
2708 #[deprecated(
2709 since = "1.33.0",
2710 note = "superseded by `trim_start_matches`",
2711 suggestion = "trim_start_matches"
2712 )]
2713 pub fn trim_left_matches<P: Pattern>(&self, pat: P) -> &str {
2714 self.trim_start_matches(pat)
2715 }
2716
2717 /// Returns a string slice with all suffixes that match a pattern
2718 /// repeatedly removed.
2719 ///
2720 /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2721 /// function or closure that determines if a character matches.
2722 ///
2723 /// [`char`]: prim@char
2724 /// [pattern]: self::pattern
2725 ///
2726 /// # Text directionality
2727 ///
2728 /// A string is a sequence of bytes. 'Right' in this context means the last
2729 /// position of that byte string; for a language like Arabic or Hebrew
2730 /// which are 'right to left' rather than 'left to right', this will be
2731 /// the _left_ side, not the right.
2732 ///
2733 /// # Examples
2734 ///
2735 /// Simple patterns:
2736 ///
2737 /// ```
2738 /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
2739 /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
2740 ///
2741 /// let x: &[_] = &['1', '2'];
2742 /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
2743 /// ```
2744 ///
2745 /// A more complex pattern, using a closure:
2746 ///
2747 /// ```
2748 /// assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo");
2749 /// ```
2750 #[stable(feature = "rust1", since = "1.0.0")]
2751 #[deprecated(
2752 since = "1.33.0",
2753 note = "superseded by `trim_end_matches`",
2754 suggestion = "trim_end_matches"
2755 )]
2756 pub fn trim_right_matches<P: Pattern>(&self, pat: P) -> &str
2757 where
2758 for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2759 {
2760 self.trim_end_matches(pat)
2761 }
2762
2763 /// Parses this string slice into another type.
2764 ///
2765 /// Because `parse` is so general, it can cause problems with type
2766 /// inference. As such, `parse` is one of the few times you'll see
2767 /// the syntax affectionately known as the 'turbofish': `::<>`. This
2768 /// helps the inference algorithm understand specifically which type
2769 /// you're trying to parse into.
2770 ///
2771 /// `parse` can parse into any type that implements the [`FromStr`] trait.
2772 ///
2773 /// # Errors
2774 ///
2775 /// Will return [`Err`] if it's not possible to parse this string slice into
2776 /// the desired type.
2777 ///
2778 /// [`Err`]: FromStr::Err
2779 ///
2780 /// # Examples
2781 ///
2782 /// Basic usage:
2783 ///
2784 /// ```
2785 /// let four: u32 = "4".parse().unwrap();
2786 ///
2787 /// assert_eq!(4, four);
2788 /// ```
2789 ///
2790 /// Using the 'turbofish' instead of annotating `four`:
2791 ///
2792 /// ```
2793 /// let four = "4".parse::<u32>();
2794 ///
2795 /// assert_eq!(Ok(4), four);
2796 /// ```
2797 ///
2798 /// Failing to parse:
2799 ///
2800 /// ```
2801 /// let nope = "j".parse::<u32>();
2802 ///
2803 /// assert!(nope.is_err());
2804 /// ```
2805 #[inline]
2806 #[stable(feature = "rust1", since = "1.0.0")]
2807 #[ferrocene::prevalidated]
2808 pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
2809 FromStr::from_str(self)
2810 }
2811
2812 /// Checks if all characters in this string are within the ASCII range.
2813 ///
2814 /// An empty string returns `true`.
2815 ///
2816 /// # Examples
2817 ///
2818 /// ```
2819 /// let ascii = "hello!\n";
2820 /// let non_ascii = "Grüße, Jürgen ❤";
2821 ///
2822 /// assert!(ascii.is_ascii());
2823 /// assert!(!non_ascii.is_ascii());
2824 /// ```
2825 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2826 #[rustc_const_stable(feature = "const_slice_is_ascii", since = "1.74.0")]
2827 #[must_use]
2828 #[inline]
2829 #[ferrocene::prevalidated]
2830 pub const fn is_ascii(&self) -> bool {
2831 // We can treat each byte as character here: all multibyte characters
2832 // start with a byte that is not in the ASCII range, so we will stop
2833 // there already.
2834 self.as_bytes().is_ascii()
2835 }
2836
2837 /// If this string slice [`is_ascii`](Self::is_ascii), returns it as a slice
2838 /// of [ASCII characters](`ascii::Char`), otherwise returns `None`.
2839 #[unstable(feature = "ascii_char", issue = "110998")]
2840 #[must_use]
2841 #[inline]
2842 pub const fn as_ascii(&self) -> Option<&[ascii::Char]> {
2843 // Like in `is_ascii`, we can work on the bytes directly.
2844 self.as_bytes().as_ascii()
2845 }
2846
2847 /// Converts this string slice into a slice of [ASCII characters](ascii::Char),
2848 /// without checking whether they are valid.
2849 ///
2850 /// # Safety
2851 ///
2852 /// Every character in this string must be ASCII, or else this is UB.
2853 #[unstable(feature = "ascii_char", issue = "110998")]
2854 #[must_use]
2855 #[inline]
2856 pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char] {
2857 assert_unsafe_precondition!(
2858 check_library_ub,
2859 "as_ascii_unchecked requires that the string is valid ASCII",
2860 (it: &str = self) => it.is_ascii()
2861 );
2862
2863 // SAFETY: the caller promised that every byte of this string slice
2864 // is ASCII.
2865 unsafe { self.as_bytes().as_ascii_unchecked() }
2866 }
2867
2868 /// Checks that two strings are an ASCII case-insensitive match.
2869 ///
2870 /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
2871 /// but without allocating and copying temporaries.
2872 ///
2873 /// For Unicode-aware case-insensitive matching, consider
2874 /// [`str::eq_ignore_case_unnormalized`].
2875 ///
2876 /// # Examples
2877 ///
2878 /// ```
2879 /// assert!("Ferris".eq_ignore_ascii_case("FERRIS"));
2880 /// assert!("Ferrös".eq_ignore_ascii_case("FERRöS"));
2881 /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
2882 /// ```
2883 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2884 #[rustc_const_stable(feature = "const_eq_ignore_ascii_case", since = "1.89.0")]
2885 #[must_use]
2886 #[inline]
2887 #[ferrocene::prevalidated]
2888 pub const fn eq_ignore_ascii_case(&self, other: &str) -> bool {
2889 self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
2890 }
2891
2892 /// Checks that two strings are a caseless match, according to
2893 /// [Definition 144] in Chapter 3 of the Unicode Standard.
2894 ///
2895 /// [Definition 144]: https://www.unicode.org/versions/latest/core-spec/chapter-3/#G53513
2896 ///
2897 /// Same as `a.to_casefold_unnormalized() == b.to_casefold_unnormalized()`,
2898 /// but without allocating. See that method's documentation,
2899 /// as well as [`char::to_casefold_unnormalized()`],
2900 /// for more information about case folding.
2901 ///
2902 /// No [normalization] (e.g. NFC) is performed, so visually and semantically identical strings
2903 /// might still compare unequal. For example, `"Å"` (U+00C5 LATIN CAPITAL LETTER A WITH RING ABOVE)
2904 /// is considered distinct from `"Å"` (A followed by U+030A COMBINING RING ABOVE),
2905 /// even though Unicode considers them canonically equivalent.
2906 ///
2907 /// In addition, this method is independent of language/locale,
2908 /// so the special behavior of I/ı/İ/i in Turkish and Azeri is not handled.
2909 ///
2910 /// # Examples
2911 ///
2912 /// ```
2913 /// #![feature(casefold)]
2914 /// assert!("Ferris".eq_ignore_case_unnormalized("FERRIS"));
2915 /// assert!("Ferrös".eq_ignore_case_unnormalized("FERRÖS"));
2916 /// assert!("ẞ".eq_ignore_case_unnormalized("ss"));
2917 /// ```
2918 ///
2919 /// No NFC [normalization] is performed:
2920 ///
2921 /// ```rust
2922 /// #![feature(casefold)]
2923 /// // These two strings are visually and semantically identical...
2924 /// let comp = "Å";
2925 /// let decomp = "Å";
2926 ///
2927 /// // ... but not codepoint-for-codepoint equal.
2928 /// assert_eq!(comp, "\u{C5}");
2929 /// assert_eq!(decomp, "A\u{030A}");
2930 ///
2931 /// // Their case-foldings are likewise unequal:
2932 /// assert!(!comp.eq_ignore_case_unnormalized(decomp));
2933 /// ```
2934 ///
2935 /// [normalization]: https://www.unicode.org/faq/normalization.html
2936 #[unstable(feature = "casefold", issue = "154742")]
2937 #[must_use]
2938 #[inline]
2939 pub fn eq_ignore_case_unnormalized(&self, other: &str) -> bool {
2940 self.chars()
2941 .flat_map(char::to_casefold_unnormalized)
2942 .eq(other.chars().flat_map(char::to_casefold_unnormalized))
2943 }
2944
2945 /// Converts this string to its ASCII upper case equivalent in-place.
2946 ///
2947 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
2948 /// but non-ASCII letters are unchanged.
2949 ///
2950 /// To return a new uppercased value without modifying the existing one, use
2951 /// [`to_ascii_uppercase()`].
2952 ///
2953 /// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase
2954 ///
2955 /// # Examples
2956 ///
2957 /// ```
2958 /// let mut s = String::from("Grüße, Jürgen ❤");
2959 ///
2960 /// s.make_ascii_uppercase();
2961 ///
2962 /// assert_eq!("GRüßE, JüRGEN ❤", s);
2963 /// ```
2964 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2965 #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
2966 #[inline]
2967 pub const fn make_ascii_uppercase(&mut self) {
2968 // SAFETY: changing ASCII letters only does not invalidate UTF-8.
2969 let me = unsafe { self.as_bytes_mut() };
2970 me.make_ascii_uppercase()
2971 }
2972
2973 /// Converts this string to its ASCII lower case equivalent in-place.
2974 ///
2975 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
2976 /// but non-ASCII letters are unchanged.
2977 ///
2978 /// To return a new lowercased value without modifying the existing one, use
2979 /// [`to_ascii_lowercase()`].
2980 ///
2981 /// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase
2982 ///
2983 /// # Examples
2984 ///
2985 /// ```
2986 /// let mut s = String::from("GRÜßE, JÜRGEN ❤");
2987 ///
2988 /// s.make_ascii_lowercase();
2989 ///
2990 /// assert_eq!("grÜße, jÜrgen ❤", s);
2991 /// ```
2992 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2993 #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
2994 #[inline]
2995 pub const fn make_ascii_lowercase(&mut self) {
2996 // SAFETY: changing ASCII letters only does not invalidate UTF-8.
2997 let me = unsafe { self.as_bytes_mut() };
2998 me.make_ascii_lowercase()
2999 }
3000
3001 /// Returns a string slice with leading ASCII whitespace removed.
3002 ///
3003 /// 'Whitespace' refers to the definition used by
3004 /// [`u8::is_ascii_whitespace`]. Importantly, this definition excludes
3005 /// the U+000B code point even though it has the Unicode [`White_Space`] property
3006 /// and is removed by [`str::trim_start`].
3007 ///
3008 /// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
3009 /// [`White_Space`]: https://www.unicode.org/reports/tr44/#White_Space
3010 ///
3011 /// # Examples
3012 ///
3013 /// ```
3014 /// assert_eq!(" \t \u{3000}hello world\n".trim_ascii_start(), "\u{3000}hello world\n");
3015 /// assert_eq!(" ".trim_ascii_start(), "");
3016 /// assert_eq!("".trim_ascii_start(), "");
3017 /// ```
3018 #[must_use = "this returns the trimmed string as a new slice, \
3019 without modifying the original"]
3020 #[stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3021 #[rustc_const_stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3022 #[inline]
3023 pub const fn trim_ascii_start(&self) -> &str {
3024 // SAFETY: Removing ASCII characters from a `&str` does not invalidate
3025 // UTF-8.
3026 unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_start()) }
3027 }
3028
3029 /// Returns a string slice with trailing ASCII whitespace removed.
3030 ///
3031 /// 'Whitespace' refers to the definition used by
3032 /// [`u8::is_ascii_whitespace`]. Importantly, this definition excludes
3033 /// the U+000B code point even though it has the Unicode [`White_Space`] property
3034 /// and is removed by [`str::trim_end`].
3035 ///
3036 /// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
3037 /// [`White_Space`]: https://www.unicode.org/reports/tr44/#White_Space
3038 ///
3039 /// # Examples
3040 ///
3041 /// ```
3042 /// assert_eq!("\r hello world\u{3000}\n ".trim_ascii_end(), "\r hello world\u{3000}");
3043 /// assert_eq!(" ".trim_ascii_end(), "");
3044 /// assert_eq!("".trim_ascii_end(), "");
3045 /// ```
3046 #[must_use = "this returns the trimmed string as a new slice, \
3047 without modifying the original"]
3048 #[stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3049 #[rustc_const_stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3050 #[inline]
3051 pub const fn trim_ascii_end(&self) -> &str {
3052 // SAFETY: Removing ASCII characters from a `&str` does not invalidate
3053 // UTF-8.
3054 unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_end()) }
3055 }
3056
3057 /// Returns a string slice with leading and trailing ASCII whitespace
3058 /// removed.
3059 ///
3060 /// 'Whitespace' refers to the definition used by
3061 /// [`u8::is_ascii_whitespace`]. Importantly, this definition excludes
3062 /// the U+000B code point even though it has the Unicode [`White_Space`] property
3063 /// and is removed by [`str::trim`].
3064 ///
3065 /// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
3066 /// [`White_Space`]: https://www.unicode.org/reports/tr44/#White_Space
3067 ///
3068 /// # Examples
3069 ///
3070 /// ```
3071 /// assert_eq!("\r hello world\n ".trim_ascii(), "hello world");
3072 /// assert_eq!(" ".trim_ascii(), "");
3073 /// assert_eq!("".trim_ascii(), "");
3074 /// ```
3075 #[must_use = "this returns the trimmed string as a new slice, \
3076 without modifying the original"]
3077 #[stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3078 #[rustc_const_stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3079 #[inline]
3080 pub const fn trim_ascii(&self) -> &str {
3081 // SAFETY: Removing ASCII characters from a `&str` does not invalidate
3082 // UTF-8.
3083 unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii()) }
3084 }
3085
3086 /// Returns an iterator that escapes each char in `self` with [`char::escape_debug`].
3087 ///
3088 /// Note: only extended grapheme codepoints that begin the string will be
3089 /// escaped.
3090 ///
3091 /// # Examples
3092 ///
3093 /// As an iterator:
3094 ///
3095 /// ```
3096 /// for c in "❤\n!".escape_debug() {
3097 /// print!("{c}");
3098 /// }
3099 /// println!();
3100 /// ```
3101 ///
3102 /// Using `println!` directly:
3103 ///
3104 /// ```
3105 /// println!("{}", "❤\n!".escape_debug());
3106 /// ```
3107 ///
3108 ///
3109 /// Both are equivalent to:
3110 ///
3111 /// ```
3112 /// println!("❤\\n!");
3113 /// ```
3114 ///
3115 /// Using `to_string`:
3116 ///
3117 /// ```
3118 /// assert_eq!("❤\n!".escape_debug().to_string(), "❤\\n!");
3119 /// ```
3120 #[must_use = "this returns the escaped string as an iterator, \
3121 without modifying the original"]
3122 #[stable(feature = "str_escape", since = "1.34.0")]
3123 pub fn escape_debug(&self) -> EscapeDebug<'_> {
3124 let mut chars = self.chars();
3125 EscapeDebug {
3126 inner: chars
3127 .next()
3128 .map(|first| first.escape_debug_ext(EscapeDebugExtArgs::ESCAPE_ALL))
3129 .into_iter()
3130 .flatten()
3131 .chain(chars.flat_map(CharEscapeDebugContinue)),
3132 }
3133 }
3134
3135 /// Returns an iterator that escapes each char in `self` with [`char::escape_default`].
3136 ///
3137 /// # Examples
3138 ///
3139 /// As an iterator:
3140 ///
3141 /// ```
3142 /// for c in "❤\n!".escape_default() {
3143 /// print!("{c}");
3144 /// }
3145 /// println!();
3146 /// ```
3147 ///
3148 /// Using `println!` directly:
3149 ///
3150 /// ```
3151 /// println!("{}", "❤\n!".escape_default());
3152 /// ```
3153 ///
3154 ///
3155 /// Both are equivalent to:
3156 ///
3157 /// ```
3158 /// println!("\\u{{2764}}\\n!");
3159 /// ```
3160 ///
3161 /// Using `to_string`:
3162 ///
3163 /// ```
3164 /// assert_eq!("❤\n!".escape_default().to_string(), "\\u{2764}\\n!");
3165 /// ```
3166 #[must_use = "this returns the escaped string as an iterator, \
3167 without modifying the original"]
3168 #[stable(feature = "str_escape", since = "1.34.0")]
3169 pub fn escape_default(&self) -> EscapeDefault<'_> {
3170 EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
3171 }
3172
3173 /// Returns an iterator that escapes each char in `self` with [`char::escape_unicode`].
3174 ///
3175 /// # Examples
3176 ///
3177 /// As an iterator:
3178 ///
3179 /// ```
3180 /// for c in "❤\n!".escape_unicode() {
3181 /// print!("{c}");
3182 /// }
3183 /// println!();
3184 /// ```
3185 ///
3186 /// Using `println!` directly:
3187 ///
3188 /// ```
3189 /// println!("{}", "❤\n!".escape_unicode());
3190 /// ```
3191 ///
3192 ///
3193 /// Both are equivalent to:
3194 ///
3195 /// ```
3196 /// println!("\\u{{2764}}\\u{{a}}\\u{{21}}");
3197 /// ```
3198 ///
3199 /// Using `to_string`:
3200 ///
3201 /// ```
3202 /// assert_eq!("❤\n!".escape_unicode().to_string(), "\\u{2764}\\u{a}\\u{21}");
3203 /// ```
3204 #[must_use = "this returns the escaped string as an iterator, \
3205 without modifying the original"]
3206 #[stable(feature = "str_escape", since = "1.34.0")]
3207 pub fn escape_unicode(&self) -> EscapeUnicode<'_> {
3208 EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }
3209 }
3210
3211 /// Returns the range that a substring points to.
3212 ///
3213 /// Returns `None` if `substr` does not point within `self`.
3214 ///
3215 /// Unlike [`str::find`], **this does not search through the string**.
3216 /// Instead, it uses pointer arithmetic to find where in the string
3217 /// `substr` is derived from.
3218 ///
3219 /// This is useful for extending [`str::split`] and similar methods.
3220 ///
3221 /// Note that this method may return false positives (typically either
3222 /// `Some(0..0)` or `Some(self.len()..self.len())`) if `substr` is a
3223 /// zero-length `str` that points at the beginning or end of another,
3224 /// independent, `str`.
3225 ///
3226 /// # Examples
3227 /// ```
3228 /// use core::range::Range;
3229 ///
3230 /// let data = "a, b, b, a";
3231 /// let mut iter = data.split(", ").map(|s| data.substr_range(s).unwrap());
3232 ///
3233 /// assert_eq!(iter.next(), Some(Range { start: 0, end: 1 }));
3234 /// assert_eq!(iter.next(), Some(Range { start: 3, end: 4 }));
3235 /// assert_eq!(iter.next(), Some(Range { start: 6, end: 7 }));
3236 /// assert_eq!(iter.next(), Some(Range { start: 9, end: 10 }));
3237 /// ```
3238 #[must_use]
3239 #[stable(feature = "substr_range", since = "CURRENT_RUSTC_VERSION")]
3240 pub fn substr_range(&self, substr: &str) -> Option<Range<usize>> {
3241 self.as_bytes().subslice_range(substr.as_bytes())
3242 }
3243
3244 /// Returns the same string as a string slice `&str`.
3245 ///
3246 /// This method is redundant when used directly on `&str`, but
3247 /// it helps dereferencing other string-like types to string slices,
3248 /// for example references to `Box<str>` or `Arc<str>`.
3249 #[inline]
3250 #[unstable(feature = "str_as_str", issue = "130366")]
3251 #[ferrocene::prevalidated]
3252 pub const fn as_str(&self) -> &str {
3253 self
3254 }
3255}
3256
3257#[stable(feature = "rust1", since = "1.0.0")]
3258#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3259const impl AsRef<[u8]> for str {
3260 #[inline]
3261 #[ferrocene::prevalidated]
3262 fn as_ref(&self) -> &[u8] {
3263 self.as_bytes()
3264 }
3265}
3266
3267#[stable(feature = "rust1", since = "1.0.0")]
3268#[rustc_const_unstable(feature = "const_default", issue = "143894")]
3269const impl Default for &str {
3270 /// Creates an empty str
3271 #[inline]
3272 #[ferrocene::prevalidated]
3273 fn default() -> Self {
3274 ""
3275 }
3276}
3277
3278#[stable(feature = "default_mut_str", since = "1.28.0")]
3279#[rustc_const_unstable(feature = "const_default", issue = "143894")]
3280const impl Default for &mut str {
3281 /// Creates an empty mutable str
3282 #[inline]
3283 fn default() -> Self {
3284 // SAFETY: The empty string is valid UTF-8.
3285 unsafe { from_utf8_unchecked_mut(&mut []) }
3286 }
3287}
3288
3289impl_fn_for_zst! {
3290 /// A nameable, cloneable fn type
3291 #[derive(Clone)]
3292 struct LinesMap impl<'a> Fn = |line: &'a str| -> &'a str {
3293 let Some(line) = line.strip_suffix('\n') else { return line };
3294 let Some(line) = line.strip_suffix('\r') else { return line };
3295 line
3296 };
3297
3298 #[derive(Clone)]
3299 struct CharEscapeDebugContinue impl Fn = |c: char| -> char::EscapeDebug {
3300 c.escape_debug_ext(EscapeDebugExtArgs {
3301 escape_grapheme_extender: false,
3302 escape_single_quote: true,
3303 escape_double_quote: true
3304 })
3305 };
3306
3307 #[derive(Clone)]
3308 struct CharEscapeUnicode impl Fn = |c: char| -> char::EscapeUnicode {
3309 c.escape_unicode()
3310 };
3311 #[derive(Clone)]
3312 struct CharEscapeDefault impl Fn = |c: char| -> char::EscapeDefault {
3313 c.escape_default()
3314 };
3315
3316 #[derive(Clone)]
3317 struct IsWhitespace impl Fn = |c: char| -> bool {
3318 c.is_whitespace()
3319 };
3320
3321 #[derive(Clone)]
3322 struct IsAsciiWhitespace impl Fn = |byte: &u8| -> bool {
3323 byte.is_ascii_whitespace()
3324 };
3325
3326 #[derive(Clone)]
3327 struct IsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b str| -> bool {
3328 !s.is_empty()
3329 };
3330
3331 #[derive(Clone)]
3332 struct BytesIsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b [u8]| -> bool {
3333 !s.is_empty()
3334 };
3335
3336 #[derive(Clone)]
3337 struct UnsafeBytesToStr impl<'a> Fn = |bytes: &'a [u8]| -> &'a str {
3338 // SAFETY: not safe
3339 unsafe { from_utf8_unchecked(bytes) }
3340 };
3341}
3342
3343// This is required to make `impl From<&str> for Box<dyn Error>` and `impl<E> From<E> for Box<dyn Error>` not overlap.
3344#[stable(feature = "error_in_core_neg_impl", since = "1.65.0")]
3345impl !crate::error::Error for &str {}