const_str/
str.rs

1#![allow(unsafe_code)]
2
3use core::cmp::Ordering;
4
5use crate::slice::advance;
6
7pub const fn equal(lhs: &str, rhs: &str) -> bool {
8    crate::bytes::equal(lhs.as_bytes(), rhs.as_bytes())
9}
10
11pub const fn compare(lhs: &str, rhs: &str) -> Ordering {
12    crate::bytes::compare(lhs.as_bytes(), rhs.as_bytes())
13}
14
15pub const unsafe fn char_from_u32(x: u32) -> char {
16    #[cfg(not(feature = "unstable"))]
17    #[allow(clippy::transmute_int_to_char)]
18    {
19        core::mem::transmute(x)
20    }
21    #[cfg(feature = "unstable")] // feature(const_char_from_u32_unchecked)
22    {
23        core::char::from_u32_unchecked(x)
24    }
25}
26
27pub const fn contains(haystack: &str, needle: &str) -> bool {
28    crate::bytes::contains(haystack.as_bytes(), needle.as_bytes())
29}
30
31pub const fn starts_with(haystack: &str, needle: &str) -> bool {
32    crate::bytes::starts_with(haystack.as_bytes(), needle.as_bytes())
33}
34
35pub const fn ends_with(haystack: &str, needle: &str) -> bool {
36    crate::bytes::ends_with(haystack.as_bytes(), needle.as_bytes())
37}
38
39pub const fn strip_prefix<'s>(s: &'s str, prefix: &str) -> Option<&'s str> {
40    match crate::bytes::strip_prefix(s.as_bytes(), prefix.as_bytes()) {
41        Some(remain) => Some(unsafe { core::str::from_utf8_unchecked(remain) }),
42        None => None,
43    }
44}
45
46pub const fn strip_suffix<'s>(s: &'s str, suffix: &str) -> Option<&'s str> {
47    match crate::bytes::strip_suffix(s.as_bytes(), suffix.as_bytes()) {
48        Some(remain) => Some(unsafe { core::str::from_utf8_unchecked(remain) }),
49        None => None,
50    }
51}
52
53pub const fn next_match<'h>(haystack: &'h str, needle: &str) -> Option<(usize, &'h str)> {
54    assert!(!needle.is_empty());
55
56    let lhs = haystack.as_bytes();
57    let rhs = needle.as_bytes();
58
59    let mut i = 0;
60    while i + rhs.len() <= lhs.len() {
61        let mut j = 0;
62        while j < rhs.len() {
63            if lhs[i + j] != rhs[j] {
64                break;
65            }
66            j += 1;
67        }
68        if j == rhs.len() {
69            let remain = advance(lhs, i + rhs.len());
70            let remain = unsafe { core::str::from_utf8_unchecked(remain) };
71            return Some((i, remain));
72        }
73
74        i += 1;
75    }
76
77    None
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_next_match() {
86        assert_eq!(next_match("abc", "ab"), Some((0, "c")));
87        assert_eq!(next_match("abc", "bc"), Some((1, "")));
88        assert_eq!(next_match("abc", "c"), Some((2, "")));
89        assert_eq!(next_match("abc", "d"), None);
90    }
91}