Skip to main content

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