1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::utf8::CharEncodeUtf8;

pub struct Contains<'a, P>(pub &'a str, pub P);

impl<'a, 'b> Contains<'a, &'b str> {
    pub const fn const_eval(&self) -> bool {
        crate::str::contains(self.0, self.1)
    }
}

impl<'a> Contains<'a, char> {
    pub const fn const_eval(&self) -> bool {
        let haystack = self.0;
        let ch = CharEncodeUtf8::new(self.1);
        let needle = ch.as_str();
        crate::str::contains(haystack, needle)
    }
}

/// Returns [`true`] if the given pattern matches a sub-slice of this string slice.
///
/// Returns [`false`] if it does not.
///
/// The pattern type must be one of
///
/// + [`&str`]
/// + [`char`]
///
/// # Examples
///
/// ```
/// const BANANAS: &str = "bananas";
/// const A: bool = const_str::contains!(BANANAS, "nana");
/// const B: bool = const_str::contains!(BANANAS, "apples");
/// const C: bool = const_str::contains!(BANANAS, 'c');
/// assert_eq!([A, B, C], [true, false, false]);
/// ```
///
#[macro_export]
macro_rules! contains {
    ($haystack: expr, $pattern: expr) => {{
        $crate::__ctfe::Contains($haystack, $pattern).const_eval()
    }};
}

pub struct StartsWith<'a, P>(pub &'a str, pub P);

impl<'a, 'b> StartsWith<'a, &'b str> {
    pub const fn const_eval(&self) -> bool {
        crate::str::starts_with(self.0, self.1)
    }
}

impl<'a> StartsWith<'a, char> {
    pub const fn const_eval(&self) -> bool {
        let haystack = self.0.as_bytes();
        let ch = CharEncodeUtf8::new(self.1);
        let needle = ch.as_bytes();
        crate::bytes::starts_with(haystack, needle)
    }
}

/// Returns [`true`] if the given pattern matches a prefix of this string slice.
///
/// Returns [`false`] if it does not.
///
/// The pattern type must be one of
///
/// + [`&str`]
/// + [`char`]
///
/// # Examples
///
/// ```
/// const BANANAS: &str = "bananas";
/// const A: bool = const_str::starts_with!(BANANAS, "bana");
/// const B: bool = const_str::starts_with!(BANANAS, "nana");
/// const C: bool = const_str::starts_with!(BANANAS, 'b');
/// assert_eq!([A, B, C], [true, false, true]);
/// ```
///
#[macro_export]
macro_rules! starts_with {
    ($haystack: expr, $pattern: expr) => {{
        $crate::__ctfe::StartsWith($haystack, $pattern).const_eval()
    }};
}

pub struct EndsWith<'a, P>(pub &'a str, pub P);

impl<'a, 'b> EndsWith<'a, &'b str> {
    pub const fn const_eval(&self) -> bool {
        crate::str::ends_with(self.0, self.1)
    }
}

impl<'a> EndsWith<'a, char> {
    pub const fn const_eval(&self) -> bool {
        let haystack = self.0.as_bytes();
        let ch = CharEncodeUtf8::new(self.1);
        let needle = ch.as_bytes();
        crate::bytes::ends_with(haystack, needle)
    }
}

/// Returns [`true`] if the given pattern matches a suffix of this string slice.
///
/// Returns [`false`] if it does not.
///
/// The pattern type must be one of
///
/// + [`&str`]
/// + [`char`]
///
/// # Examples
///
/// ```
/// const BANANAS: &str = "bananas";
/// const A: bool = const_str::ends_with!(BANANAS, "anas");
/// const B: bool = const_str::ends_with!(BANANAS, "nana");
/// const C: bool = const_str::ends_with!(BANANAS, 's');
/// assert_eq!([A, B, C], [true, false, true]);
/// ```
///
#[macro_export]
macro_rules! ends_with {
    ($haystack: expr, $pattern: expr) => {{
        $crate::__ctfe::EndsWith($haystack, $pattern).const_eval()
    }};
}

pub struct StripPrefix<'a, P>(pub &'a str, pub P);

impl<'a, 'b> StripPrefix<'a, &'b str> {
    pub const fn const_eval(&self) -> Option<&'a str> {
        crate::str::strip_prefix(self.0, self.1)
    }
}

pub struct StripSuffix<'a, P>(pub &'a str, pub P);

impl<'a, 'b> StripSuffix<'a, &'b str> {
    pub const fn const_eval(&self) -> Option<&'a str> {
        crate::str::strip_suffix(self.0, self.1)
    }
}

/// Returns a string slice with the prefix removed.
///
/// # Examples
///
/// ```
/// assert_eq!(const_str::strip_prefix!("foo:bar", "foo:"), Some("bar"));
/// assert_eq!(const_str::strip_prefix!("foo:bar", "bar"), None);
/// assert_eq!(const_str::strip_prefix!("foofoo", "foo"), Some("foo"));
///
/// const FOO_BAR: &str = "foo:bar";
/// const BAR: &str = const_str::unwrap!(const_str::strip_prefix!(FOO_BAR, "foo:"));
/// assert_eq!(BAR, "bar");
/// ```
///
#[macro_export]
macro_rules! strip_prefix {
    ($s: expr, $prefix: expr) => {{
        $crate::__ctfe::StripPrefix($s, $prefix).const_eval()
    }};
}

/// Returns a string slice with the suffix removed.
///
/// # Examples
///
/// ```
/// assert_eq!(const_str::strip_suffix!("bar:foo", ":foo"), Some("bar"));
/// assert_eq!(const_str::strip_suffix!("bar:foo", "bar"), None);
/// assert_eq!(const_str::strip_suffix!("foofoo", "foo"), Some("foo"));
///
/// const FOO_BAR: &str = "foo:bar";
/// const FOO: &str = const_str::unwrap!(const_str::strip_suffix!(FOO_BAR, ":bar"));
/// assert_eq!(FOO, "foo");
/// ```
///
#[macro_export]
macro_rules! strip_suffix {
    ($s: expr, $suffix: expr) => {{
        $crate::__ctfe::StripSuffix($s, $suffix).const_eval()
    }};
}