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.recv_many_as::<string>().await {
19        check!(
20            contains
21                .send_many_as(
22                    values
23                        .iter()
24                        .map(|val| val.contains(&substring))
25                        .collect::<Vec<bool>>()
26                )
27                .await
28        )
29    }
30}
31
32/// Tells if strings exactly matches a pattern.
33#[mel_treatment(
34    input text Stream<string>
35    output matches Stream<bool>
36)]
37pub async fn exact(pattern: string) {
38    while let Ok(text) = text.recv_many_as::<string>().await {
39        check!(
40            matches
41                .send_many_as(
42                    text.into_iter()
43                        .map(|txt| txt == pattern)
44                        .collect::<Vec<_>>()
45                )
46                .await
47        );
48    }
49}
50
51/// Tells if string exactly matches a pattern.
52#[mel_function]
53pub fn exact(text: string, pattern: string) -> bool {
54    text == pattern
55}
56
57/// Tells if strings starts with a pattern.
58#[mel_treatment(
59    input text Stream<string>
60    output matches Stream<bool>
61)]
62pub async fn starts_with(pattern: string) {
63    while let Ok(text) = text.recv_many_as::<string>().await {
64        check!(
65            matches
66                .send_many_as(
67                    text.into_iter()
68                        .map(|txt| txt.starts_with(&pattern))
69                        .collect::<Vec<_>>()
70                )
71                .await
72        );
73    }
74}
75
76/// Tells if string starts with a pattern.
77#[mel_function]
78pub fn starts_with(text: string, pattern: string) -> bool {
79    text.starts_with(&pattern)
80}
81
82/// Tells if strings ends with a pattern.
83#[mel_treatment(
84    input text Stream<string>
85    output matches Stream<bool>
86)]
87pub async fn ends_with(pattern: string) {
88    while let Ok(text) = text.recv_many_as::<string>().await {
89        check!(
90            matches
91                .send_many_as(
92                    text.into_iter()
93                        .map(|txt| txt.ends_with(&pattern))
94                        .collect::<Vec<_>>()
95                )
96                .await
97        );
98    }
99}
100
101/// Tells if string ends with a pattern.
102#[mel_function]
103pub fn ends_with(text: string, pattern: string) -> bool {
104    text.ends_with(&pattern)
105}