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