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(unnecessary_transmutes)]
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/// Returns true if the byte is an ASCII whitespace character.
81/// ASCII whitespace: space (0x20), tab (0x09), newline (0x0A),
82/// vertical tab (0x0B), form feed (0x0C), carriage return (0x0D).
83const fn is_ascii_whitespace(b: u8) -> bool {
84    matches!(b, b' ' | b'\t' | b'\n' | b'\x0B' | b'\x0C' | b'\r')
85}
86
87/// Trims ASCII whitespace from both ends of a string slice.
88pub const fn trim_ascii(s: &str) -> &str {
89    let bytes = s.as_bytes();
90    let len = bytes.len();
91
92    // Find start
93    let mut start = 0;
94    while start < len && is_ascii_whitespace(bytes[start]) {
95        start += 1;
96    }
97
98    // Find end
99    let mut end = len;
100    while end > start && is_ascii_whitespace(bytes[end - 1]) {
101        end -= 1;
102    }
103
104    let trimmed_bytes = crate::slice::subslice(bytes, start..end);
105    unsafe { core::str::from_utf8_unchecked(trimmed_bytes) }
106}
107
108/// Trims ASCII whitespace from the start of a string slice.
109pub const fn trim_ascii_start(s: &str) -> &str {
110    let bytes = s.as_bytes();
111    let len = bytes.len();
112
113    // Find start
114    let mut start = 0;
115    while start < len && is_ascii_whitespace(bytes[start]) {
116        start += 1;
117    }
118
119    let trimmed_bytes = crate::slice::advance(bytes, start);
120    unsafe { core::str::from_utf8_unchecked(trimmed_bytes) }
121}
122
123/// Trims ASCII whitespace from the end of a string slice.
124pub const fn trim_ascii_end(s: &str) -> &str {
125    let bytes = s.as_bytes();
126    let len = bytes.len();
127
128    // Find end
129    let mut end = len;
130    while end > 0 && is_ascii_whitespace(bytes[end - 1]) {
131        end -= 1;
132    }
133
134    let trimmed_bytes = crate::slice::subslice(bytes, 0..end);
135    unsafe { core::str::from_utf8_unchecked(trimmed_bytes) }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    #[test]
143    fn test_next_match() {
144        assert_eq!(next_match("abc", "ab"), Some((0, "c")));
145        assert_eq!(next_match("abc", "bc"), Some((1, "")));
146        assert_eq!(next_match("abc", "c"), Some((2, "")));
147        assert_eq!(next_match("abc", "d"), None);
148    }
149
150    #[test]
151    fn test_trim_ascii() {
152        assert_eq!(trim_ascii("  hello world  "), "hello world");
153        assert_eq!(trim_ascii("\t\n  hello\tworld\n  \r"), "hello\tworld");
154        assert_eq!(trim_ascii("   "), "");
155        assert_eq!(trim_ascii("hello"), "hello");
156        assert_eq!(trim_ascii(""), "");
157    }
158
159    #[test]
160    fn test_trim_ascii_start() {
161        assert_eq!(trim_ascii_start("  hello world  "), "hello world  ");
162        assert_eq!(trim_ascii_start("\t\n  hello\tworld"), "hello\tworld");
163        assert_eq!(trim_ascii_start("hello"), "hello");
164        assert_eq!(trim_ascii_start(""), "");
165        assert_eq!(trim_ascii_start("   "), "");
166    }
167
168    #[test]
169    fn test_trim_ascii_end() {
170        assert_eq!(trim_ascii_end("  hello world  "), "  hello world");
171        assert_eq!(trim_ascii_end("hello\tworld\n  \r"), "hello\tworld");
172        assert_eq!(trim_ascii_end("hello"), "hello");
173        assert_eq!(trim_ascii_end(""), "");
174        assert_eq!(trim_ascii_end("   "), "");
175    }
176}