| 1 | pub trait StrExt { |
| 2 | fn as_str(&self) -> &str; |
| 3 | |
| 4 | /// Checks that these needles exist consequtively in self. |
| 5 | /// |
| 6 | /// Example: `"hello world".consecutive_in_set(&["he", "wor"])` returns `true`. |
| 7 | /// Example: `"hello world".consecutive_in_set(&["wor", "he"])` returns `false`. |
| 8 | fn consecutive_in_self(&self, needles: &[&str]) -> bool { |
| 9 | let mut rem = self.as_str(); |
| 10 | for needle in needles { |
| 11 | rem = match rem.find(needle) { |
| 12 | Some(next) => &rem[next + needle.len()..], |
| 13 | None => return false, |
| 14 | }; |
| 15 | } |
| 16 | true |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | impl StrExt for str { |
| 21 | #[inline(always)] |
| 22 | fn as_str(&self) -> &str { |
| 23 | self |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl StrExt for String { |
| 28 | #[inline(always)] |
| 29 | fn as_str(&self) -> &str { |
| 30 | self |
| 31 | } |
| 32 | } |