std_mel/text/compare/
char.rs

1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4/// Tells if chars exactly matches a reference.
5#[mel_treatment(
6    input chars Stream<char>
7    output matches Stream<bool>
8)]
9pub async fn exact(reference: char) {
10    while let Ok(chars) = chars
11        .recv_many()
12        .await
13        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
14    {
15        check!(
16            matches
17                .send_many(
18                    chars
19                        .into_iter()
20                        .map(|char| char == reference)
21                        .collect::<VecDeque<_>>()
22                        .into()
23                )
24                .await
25        );
26    }
27}
28
29/// Tells if char exactly matches a reference.
30#[mel_function]
31pub fn exact(char: char, reference: char) -> bool {
32    char == reference
33}
34
35/// Tells if chars are alphabetic.
36#[mel_treatment(
37    input chars Stream<char>
38    output is Stream<bool>
39)]
40pub async fn is_alphabetic() {
41    while let Ok(chars) = chars
42        .recv_many()
43        .await
44        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
45    {
46        check!(
47            is.send_many(
48                chars
49                    .into_iter()
50                    .map(|char| char.is_alphabetic())
51                    .collect::<VecDeque<_>>()
52                    .into()
53            )
54            .await
55        );
56    }
57}
58
59/// Tells if char is alphabetic.
60#[mel_function]
61pub fn is_alphabetic(char: char) -> bool {
62    char.is_alphabetic()
63}
64
65/// Tells if chars are alphanumeric.
66#[mel_treatment(
67    input chars Stream<char>
68    output is Stream<bool>
69)]
70pub async fn is_alphanumeric() {
71    while let Ok(chars) = chars
72        .recv_many()
73        .await
74        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
75    {
76        check!(
77            is.send_many(
78                chars
79                    .into_iter()
80                    .map(|char| char.is_alphanumeric())
81                    .collect::<VecDeque<_>>()
82                    .into()
83            )
84            .await
85        );
86    }
87}
88
89/// Tells if char is alphanumeric.
90#[mel_function]
91pub fn is_alphanumeric(char: char) -> bool {
92    char.is_alphanumeric()
93}
94
95/// Tells if chars are ascii.
96#[mel_treatment(
97    input chars Stream<char>
98    output is Stream<bool>
99)]
100pub async fn is_ascii() {
101    while let Ok(chars) = chars
102        .recv_many()
103        .await
104        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
105    {
106        check!(
107            is.send_many(
108                chars
109                    .into_iter()
110                    .map(|char| char.is_ascii())
111                    .collect::<VecDeque<_>>()
112                    .into()
113            )
114            .await
115        );
116    }
117}
118
119/// Tells if char is ascii.
120#[mel_function]
121pub fn is_ascii(char: char) -> bool {
122    char.is_ascii()
123}
124
125/// Tells if chars are control.
126#[mel_treatment(
127    input chars Stream<char>
128    output is Stream<bool>
129)]
130pub async fn is_control() {
131    while let Ok(chars) = chars
132        .recv_many()
133        .await
134        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
135    {
136        check!(
137            is.send_many(
138                chars
139                    .into_iter()
140                    .map(|char| char.is_control())
141                    .collect::<VecDeque<_>>()
142                    .into()
143            )
144            .await
145        );
146    }
147}
148
149/// Tells if char is control.
150#[mel_function]
151pub fn is_control(char: char) -> bool {
152    char.is_control()
153}
154
155/// Tells if chars are digit.
156///
157/// - `base`: must be between 0 and 36, if over `is` will only be `false`.
158#[mel_treatment(
159    input chars Stream<char>
160    output is Stream<bool>
161)]
162pub async fn is_digit(base: u8) {
163    while let Ok(chars) = chars
164        .recv_many()
165        .await
166        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
167    {
168        if base <= 36 {
169            check!(
170                is.send_many(
171                    chars
172                        .into_iter()
173                        .map(|char| char.is_digit(base as u32))
174                        .collect::<VecDeque<_>>()
175                        .into()
176                )
177                .await
178            );
179        } else {
180            check!(is.send_many(vec![false; chars.len()].into()).await);
181        }
182    }
183}
184
185/// Tells if char is digit.
186///
187/// - `base`: must be between 0 and 36, if over function will return `false` in any case.
188#[mel_function]
189pub fn is_digit(char: char, base: u8) -> bool {
190    if base <= 36 {
191        char.is_digit(base as u32)
192    } else {
193        false
194    }
195}
196
197/// Tells if chars are lowercase.
198#[mel_treatment(
199    input chars Stream<char>
200    output is Stream<bool>
201)]
202pub async fn is_lowercase() {
203    while let Ok(chars) = chars
204        .recv_many()
205        .await
206        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
207    {
208        check!(
209            is.send_many(
210                chars
211                    .into_iter()
212                    .map(|char| char.is_lowercase())
213                    .collect::<VecDeque<_>>()
214                    .into()
215            )
216            .await
217        );
218    }
219}
220
221/// Tells if char is lowercase.
222#[mel_function]
223pub fn is_lowercase(char: char) -> bool {
224    char.is_lowercase()
225}
226
227/// Tells if chars are uppercase.
228#[mel_treatment(
229    input chars Stream<char>
230    output is Stream<bool>
231)]
232pub async fn is_uppercase() {
233    while let Ok(chars) = chars
234        .recv_many()
235        .await
236        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
237    {
238        check!(
239            is.send_many(
240                chars
241                    .into_iter()
242                    .map(|char| char.is_uppercase())
243                    .collect::<VecDeque<_>>()
244                    .into()
245            )
246            .await
247        );
248    }
249}
250
251/// Tells if char is uppercase.
252#[mel_function]
253pub fn is_uppercase(char: char) -> bool {
254    char.is_uppercase()
255}
256
257/// Tells if chars are whitespace.
258#[mel_treatment(
259    input chars Stream<char>
260    output is Stream<bool>
261)]
262pub async fn is_whitespace() {
263    while let Ok(chars) = chars
264        .recv_many()
265        .await
266        .map(|values| TryInto::<Vec<char>>::try_into(values).unwrap())
267    {
268        check!(
269            is.send_many(
270                chars
271                    .into_iter()
272                    .map(|char| char.is_whitespace())
273                    .collect::<VecDeque<_>>()
274                    .into()
275            )
276            .await
277        );
278    }
279}
280
281/// Tells if char is whitespace.
282#[mel_function]
283pub fn is_whitespace(char: char) -> bool {
284    char.is_whitespace()
285}