| 1 | //! Substring method for string types. |
| 2 | //! |
| 3 | //! This crate provides a [`substring()`] method on Rust string types. The method takes a start and |
| 4 | //! end character index and returns a string slice of the characters within that range. |
| 5 | //! |
| 6 | //! The method is provided via the [`Substring`] trait which is implemented on the [`str`] |
| 7 | //! primitive. |
| 8 | //! |
| 9 | //! # Example |
| 10 | //! ``` |
| 11 | //! use substring::Substring; |
| 12 | //! |
| 13 | //! // Works on a string slice. |
| 14 | //! assert_eq!("foobar".substring(2,5), "oba"); |
| 15 | //! |
| 16 | //! // Also works on a String. |
| 17 | //! assert_eq!("foobar".to_string().substring(1,6), "oobar"); |
| 18 | //! ``` |
| 19 | //! |
| 20 | //! As Rust strings are UTF-8 encoded, the algorithm for finding a character substring is `O(n)`, |
| 21 | //! where `n` is the byte length of the string. This is due to characters not being of predictible |
| 22 | //! byte lengths. |
| 23 | //! |
| 24 | //! # Note |
| 25 | //! The indexing of substrings is based on [*Unicode Scalar Value*]. As such, substrings may not |
| 26 | //! always match your intuition: |
| 27 | //! |
| 28 | //! ``` |
| 29 | //! use substring::Substring; |
| 30 | //! |
| 31 | //! assert_eq!("ã".substring(0, 1), "a"); // As opposed to "ã". |
| 32 | //! assert_eq!("ã".substring(1, 2), "\u{0303}") |
| 33 | //! ``` |
| 34 | //! |
| 35 | //! The above example occurs because "ã" is technically made up of two UTF-8 scalar values. |
| 36 | //! |
| 37 | //! [`str`]: https://doc.rust-lang.org/std/primitive.str.html |
| 38 | //! [`Substring`]: trait.Substring.html |
| 39 | //! [`substring()`]: trait.Substring.html#tymethod.substring |
| 40 | //! |
| 41 | //! [*Unicode Scalar Value*]: http://www.unicode.org/glossary/#unicode_scalar_value |
| 42 | |
| 43 | #![deny(missing_docs)] |
| 44 | // Since the MSRV is 1.0.0, allowing usage of deprecated items is ok, as the replacements are likely |
| 45 | // not available in early versions. |
| 46 | #![allow(deprecated)] |
| 47 | #![cfg_attr(rustc_1_6, no_std)] |
| 48 | |
| 49 | #[cfg(not(rustc_1_6))] |
| 50 | extern crate std as core; |
| 51 | |
| 52 | /// Provides a [`substring()`] method. |
| 53 | /// |
| 54 | /// The [`substring()`] method obtains a string slice of characters within the range specified by |
| 55 | /// `start_index` and `end_index`. |
| 56 | /// |
| 57 | /// [`substring()`]: trait.Substring.html#tymethod.substring |
| 58 | pub trait Substringrust-analyzer cargo substring 1.4.5 Substring# { |
| 59 | /// Obtains a string slice containing the characters within the range specified by |
| 60 | /// `start_index` and `end_index`. |
| 61 | /// |
| 62 | /// The range specified is a character range, not a byte range. |
| 63 | fn substringrust-analyzer cargo substring 1.4.5 Substring#substring().(&self, start_index: usize, end_index: usize) -> &str; |
| 64 | } |
| 65 | |
| 66 | /// Implements a [`substring()`] method for [`str`]. |
| 67 | /// |
| 68 | /// Note that structs which implement `Deref<Target=str>` (such as [`String`]) will also have |
| 69 | /// access to this implementation. |
| 70 | /// |
| 71 | /// [`str`]: https://doc.rust-lang.org/std/primitive.str.html |
| 72 | /// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html |
| 73 | /// [`substring()`]: trait.Substring.html#method.substring |
| 74 | impl Substringrust-analyzer cargo substring 1.4.5 Substring# for str { |
| 75 | /// Obtain a slice of the characters within the range of `start_index` and `end_index`. |
| 76 | /// |
| 77 | /// As this is by character index, rather than byte index, the temporal complexity of finding a |
| 78 | /// substring is *O(n)*, where *n* is the byte length of the string. |
| 79 | /// |
| 80 | /// Example: |
| 81 | /// ``` |
| 82 | /// use substring::Substring; |
| 83 | /// |
| 84 | /// assert_eq!("foobar".substring(2,5), "oba"); |
| 85 | /// ``` |
| 86 | #[must_use] |
| 87 | fn substringrust-analyzer cargo substring 1.4.5 impl#[str][Substring]substring().(&self, start_index: usize, end_index: usize) -> &str { |
| 88 | if end_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().start_index { |
| 89 | return ""; |
| 90 | } |
| 91 | |
| 92 | let mut indices = self.char_indicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]char_indices().(); |
| 93 | |
| 94 | let obtain_index = |(index, _char)| index; |
| 95 | let str_len = self.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(); |
| 96 | |
| 97 | unsafe { |
| 98 | // SAFETY: Since `indices` iterates over the `CharIndices` of `self`, we can guarantee |
| 99 | // that the indices obtained from it will always be within the bounds of `self` and they |
| 100 | // will always lie on UTF-8 sequence boundaries. |
| 101 | self.slice_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]slice_unchecked().( |
| 102 | indices.nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(start_index).map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(str_len, &obtain_index), |
| 103 | indices |
| 104 | .nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(end_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().start_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) |
| 105 | .map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(str_len, &obtain_index), |
| 106 | ) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod testsrust-analyzer cargo substring 1.4.5 tests/ { |
| 113 | use Substringrust-analyzer cargo substring 1.4.5 Substring#; |
| 114 | |
| 115 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 116 | fn test_substringrust-analyzer cargo substring 1.4.5 tests/test_substring().() { |
| 117 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("foobar".substringrust-analyzer cargo substring 1.4.5 impl#[str][Substring]substring().(0, 3), "foo"); |
| 118 | } |
| 119 | |
| 120 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 121 | fn test_out_of_boundsrust-analyzer cargo substring 1.4.5 tests/test_out_of_bounds().() { |
| 122 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("foobar".substringrust-analyzer cargo substring 1.4.5 impl#[str][Substring]substring().(0, 10), "foobar"); |
| 123 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("foobar".substringrust-analyzer cargo substring 1.4.5 impl#[str][Substring]substring().(6, 10), ""); |
| 124 | } |
| 125 | |
| 126 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 127 | fn test_start_less_than_endrust-analyzer cargo substring 1.4.5 tests/test_start_less_than_end().() { |
| 128 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("foobar".substringrust-analyzer cargo substring 1.4.5 impl#[str][Substring]substring().(3, 2), ""); |
| 129 | } |
| 130 | |
| 131 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 132 | fn test_start_and_end_equalrust-analyzer cargo substring 1.4.5 tests/test_start_and_end_equal().() { |
| 133 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("foobar".substringrust-analyzer cargo substring 1.4.5 impl#[str][Substring]substring().(3, 3), ""); |
| 134 | } |
| 135 | |
| 136 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 137 | fn test_multiple_byte_charactersrust-analyzer cargo substring 1.4.5 tests/test_multiple_byte_characters().() { |
| 138 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("fõøbα®".substringrust-analyzer cargo substring 1.4.5 impl#[str][Substring]substring().(2, 5), "øbα"); |
| 139 | } |
| 140 | } |