| 1 | # substring |
| 2 | |
| 3 | [](https://github.com/Anders429/substring/actions) |
| 4 | [](https://codecov.io/gh/Anders429/substring) |
| 5 | [](https://crates.io/crates/substring) |
| 6 | [](https://docs.rs/substring) |
| 7 | [](#minimum-supported-rust-version) |
| 8 | [](#license) |
| 9 | |
| 10 | Substring method for string types. |
| 11 | |
| 12 | This crate provides a `substring` method on Rust string types. The method takes a start and end |
| 13 | character index and returns a string slice containing the characters within that range. |
| 14 | |
| 15 | The method is provided via the `Substring` trait which is implemented on the |
| 16 | [`str`](https://doc.rust-lang.org/std/primitive.str.html) primitive. |
| 17 | |
| 18 | ## Usage |
| 19 | |
| 20 | To use this crate, simply bring the `Substring` trait into scope and call the `substring` method on |
| 21 | your string types. |
| 22 | |
| 23 | ```rust |
| 24 | use substring::Substring; |
| 25 | |
| 26 | assert_eq!("hello, world!".substring(7, 12), "world"); |
| 27 | ``` |
| 28 | |
| 29 | Note that the indexing of substrings is based on |
| 30 | [*Unicode Scalar Value*](http://www.unicode.org/glossary/#unicode_scalar_value). As such, |
| 31 | substrings may not always match your intuition: |
| 32 | |
| 33 | ```rust |
| 34 | use substring::Substring; |
| 35 | |
| 36 | assert_eq!("ã".substring(0, 1), "a"); // As opposed to "ã". |
| 37 | assert_eq!("ã".substring(1, 2), "\u{0303}") |
| 38 | ``` |
| 39 | |
| 40 | The above example occurs because "ã" is technically made up of two UTF-8 scalar values: the letter |
| 41 | "a" and a combining tilde. |
| 42 | |
| 43 | |
| 44 | ## Performance |
| 45 | |
| 46 | As Rust strings are UTF-8 encoded, the algorithm for finding a character substring has temporal |
| 47 | complexity *O(n)*, where *n* is the byte length of the string. This is due to characters not being |
| 48 | of predictible byte lengths. |
| 49 | |
| 50 | ## Minimum Supported Rust Version |
| 51 | This crate is guaranteed to compile on stable `rustc 1.0.0` and up. |
| 52 | |
| 53 | ## License |
| 54 | This project is licensed under either of |
| 55 | |
| 56 | * Apache License, Version 2.0 |
| 57 | ([LICENSE-APACHE](https://github.com/Anders429/substring/blob/HEAD/LICENSE-APACHE) or |
| 58 | http://www.apache.org/licenses/LICENSE-2.0) |
| 59 | * MIT license |
| 60 | ([LICENSE-MIT](https://github.com/Anders429/substring/blob/HEAD/LICENSE-MIT) or |
| 61 | http://opensource.org/licenses/MIT) |
| 62 | |
| 63 | at your option. |
| 64 | |
| 65 | ### Contribution |
| 66 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |