risingwave_expr_impl/scalar/
trim.rs1use std::fmt::Write;
16
17use risingwave_expr::function;
18
19#[function("trim(varchar) -> varchar")]
20pub fn trim(s: &str, writer: &mut impl Write) {
21 writer.write_str(s.trim()).unwrap();
22}
23
24#[function("ltrim(varchar) -> varchar")]
28pub fn ltrim(s: &str, writer: &mut impl Write) {
29 writer.write_str(s.trim_start()).unwrap();
30}
31
32#[function("rtrim(varchar) -> varchar")]
36pub fn rtrim(s: &str, writer: &mut impl Write) {
37 writer.write_str(s.trim_end()).unwrap();
38}
39
40#[function("trim(varchar, varchar) -> varchar")]
41pub fn trim_characters(s: &str, characters: &str, writer: &mut impl Write) {
42 let pattern = |c| characters.chars().any(|ch| ch == c);
43 writer.write_str(s.trim_matches(pattern)).unwrap();
47}
48
49#[function("ltrim(varchar, varchar) -> varchar")]
50pub fn ltrim_characters(s: &str, characters: &str, writer: &mut impl Write) {
51 let pattern = |c| characters.chars().any(|ch| ch == c);
52 writer.write_str(s.trim_start_matches(pattern)).unwrap();
53}
54
55#[function("rtrim(varchar, varchar) -> varchar")]
56pub fn rtrim_characters(s: &str, characters: &str, writer: &mut impl Write) {
57 let pattern = |c| characters.chars().any(|ch| ch == c);
58 writer.write_str(s.trim_end_matches(pattern)).unwrap();
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_trim() {
67 let cases = [
68 (" Hello\tworld\t", "Hello\tworld"),
69 (" 空I ❤️ databases空 ", "空I ❤️ databases空"),
70 ];
71
72 for (s, expected) in cases {
73 let mut writer = String::new();
74 trim(s, &mut writer);
75 assert_eq!(writer, expected);
76 }
77 }
78
79 #[test]
80 fn test_ltrim() {
81 let cases = [
82 (" \tHello\tworld\t", "Hello\tworld\t"),
83 (" \t空I ❤️ databases空 ", "空I ❤️ databases空 "),
84 ];
85
86 for (s, expected) in cases {
87 let mut writer = String::new();
88 ltrim(s, &mut writer);
89 assert_eq!(writer, expected);
90 }
91 }
92
93 #[test]
94 fn test_rtrim() {
95 let cases = [
96 (" \tHello\tworld\t ", " \tHello\tworld"),
97 (" \t空I ❤️ databases空\t ", " \t空I ❤️ databases空"),
98 ];
99
100 for (s, expected) in cases {
101 let mut writer = String::new();
102 rtrim(s, &mut writer);
103 assert_eq!(writer, expected);
104 }
105 }
106
107 #[test]
108 fn test_trim_characters() {
109 let cases = [
110 ("Hello world", "Hdl", "ello wor"),
111 ("abcde", "aae", "bcd"),
112 ("zxy", "yxz", ""),
113 ];
114
115 for (s, characters, expected) in cases {
116 let mut writer = String::new();
117 trim_characters(s, characters, &mut writer);
118 assert_eq!(writer, expected);
119 }
120 }
121
122 #[test]
123 fn test_ltrim_characters() {
124 let cases = [
125 ("Hello world", "Hdl", "ello world"),
126 ("abcde", "aae", "bcde"),
127 ("zxy", "yxz", ""),
128 ];
129
130 for (s, characters, expected) in cases {
131 let mut writer = String::new();
132 ltrim_characters(s, characters, &mut writer);
133 assert_eq!(writer, expected);
134 }
135 }
136
137 #[test]
138 fn test_rtrim_characters() {
139 let cases = [
140 ("Hello world", "Hdl", "Hello wor"),
141 ("abcde", "aae", "abcd"),
142 ("zxy", "yxz", ""),
143 ];
144
145 for (s, characters, expected) in cases {
146 let mut writer = String::new();
147 rtrim_characters(s, characters, &mut writer);
148 assert_eq!(writer, expected);
149 }
150 }
151}