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