Skip to main content

std_mel/text/compare/
mod.rs

1pub mod char;
2
3use melodium_core::*;
4use melodium_macro::{check, mel_function, mel_treatment};
5
6/// Return `true` if `text` contains `substring`.
7#[mel_function]
8pub fn contains(text: string, substring: string) -> bool {
9    text.contains(&substring)
10}
11
12/// For each string in `text`, emit `true` through `contains` if it contains `substring`.
13#[mel_treatment(
14    input text Stream<string>
15    output contains Stream<bool>
16)]
17pub async fn contains(substring: string) {
18    while let Ok(values) = text
19        .recv_many()
20        .await
21        .map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
22    {
23        check!(
24            contains
25                .send_many(
26                    values
27                        .iter()
28                        .map(|val| val.contains(&substring))
29                        .collect::<VecDeque<bool>>()
30                        .into()
31                )
32                .await
33        )
34    }
35}
36
37/// Tells if strings exactly matches a pattern.
38#[mel_treatment(
39    input text Stream<string>
40    output matches Stream<bool>
41)]
42pub async fn exact(pattern: string) {
43    while let Ok(text) = text
44        .recv_many()
45        .await
46        .map(|values: TransmissionValue| TryInto::<Vec<string>>::try_into(values).unwrap())
47    {
48        check!(
49            matches
50                .send_many(
51                    text.into_iter()
52                        .map(|txt| txt == pattern)
53                        .collect::<VecDeque<_>>()
54                        .into()
55                )
56                .await
57        );
58    }
59}
60
61/// Tells if string exactly matches a pattern.
62#[mel_function]
63pub fn exact(text: string, pattern: string) -> bool {
64    text == pattern
65}
66
67/// Tells if strings starts with a pattern.
68#[mel_treatment(
69    input text Stream<string>
70    output matches Stream<bool>
71)]
72pub async fn starts_with(pattern: string) {
73    while let Ok(text) = text
74        .recv_many()
75        .await
76        .map(|values: TransmissionValue| TryInto::<Vec<string>>::try_into(values).unwrap())
77    {
78        check!(
79            matches
80                .send_many(
81                    text.into_iter()
82                        .map(|txt| txt.starts_with(&pattern))
83                        .collect::<VecDeque<_>>()
84                        .into()
85                )
86                .await
87        );
88    }
89}
90
91/// Tells if string starts with a pattern.
92#[mel_function]
93pub fn starts_with(text: string, pattern: string) -> bool {
94    text.starts_with(&pattern)
95}
96
97/// Tells if strings ends with a pattern.
98#[mel_treatment(
99    input text Stream<string>
100    output matches Stream<bool>
101)]
102pub async fn ends_with(pattern: string) {
103    while let Ok(text) = text
104        .recv_many()
105        .await
106        .map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
107    {
108        check!(
109            matches
110                .send_many(
111                    text.into_iter()
112                        .map(|txt| txt.ends_with(&pattern))
113                        .collect::<VecDeque<_>>()
114                        .into()
115                )
116                .await
117        );
118    }
119}
120
121/// Tells if string ends with a pattern.
122#[mel_function]
123pub fn ends_with(text: string, pattern: string) -> bool {
124    text.ends_with(&pattern)
125}