std_mel/text/compare/
mod.rs

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