std_mel/text/compare/
mod.rs

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