Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/function_expr/strings.rs
8420 views
1
#[cfg(feature = "serde")]
2
use serde::{Deserialize, Serialize};
3
4
use super::*;
5
6
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
8
#[derive(Clone, PartialEq, Debug, Hash)]
9
pub enum StringFunction {
10
Format {
11
format: PlSmallStr,
12
insertions: Arc<[usize]>,
13
},
14
#[cfg(feature = "concat_str")]
15
ConcatHorizontal {
16
delimiter: PlSmallStr,
17
ignore_nulls: bool,
18
},
19
#[cfg(feature = "concat_str")]
20
ConcatVertical {
21
delimiter: PlSmallStr,
22
ignore_nulls: bool,
23
},
24
#[cfg(feature = "regex")]
25
Contains {
26
literal: bool,
27
strict: bool,
28
},
29
CountMatches(bool),
30
EndsWith,
31
Extract(usize),
32
ExtractAll,
33
#[cfg(feature = "extract_groups")]
34
ExtractGroups {
35
dtype: DataType,
36
pat: PlSmallStr,
37
},
38
#[cfg(feature = "regex")]
39
Find {
40
literal: bool,
41
strict: bool,
42
},
43
#[cfg(feature = "string_to_integer")]
44
ToInteger {
45
dtype: Option<DataType>,
46
strict: bool,
47
},
48
LenBytes,
49
LenChars,
50
Lowercase,
51
#[cfg(feature = "extract_jsonpath")]
52
JsonDecode(DataTypeExpr),
53
#[cfg(feature = "extract_jsonpath")]
54
JsonPathMatch,
55
#[cfg(feature = "regex")]
56
Replace {
57
// negative is replace all
58
// how many matches to replace
59
n: i64,
60
literal: bool,
61
},
62
#[cfg(feature = "string_normalize")]
63
Normalize {
64
form: UnicodeForm,
65
},
66
#[cfg(feature = "string_reverse")]
67
Reverse,
68
#[cfg(feature = "string_pad")]
69
PadStart {
70
fill_char: char,
71
},
72
#[cfg(feature = "string_pad")]
73
PadEnd {
74
fill_char: char,
75
},
76
Slice,
77
Head,
78
Tail,
79
#[cfg(feature = "string_encoding")]
80
HexEncode,
81
#[cfg(feature = "binary_encoding")]
82
HexDecode(bool),
83
#[cfg(feature = "string_encoding")]
84
Base64Encode,
85
#[cfg(feature = "binary_encoding")]
86
Base64Decode(bool),
87
StartsWith,
88
StripChars,
89
StripCharsStart,
90
StripCharsEnd,
91
StripPrefix,
92
StripSuffix,
93
#[cfg(feature = "dtype-struct")]
94
SplitExact {
95
n: usize,
96
inclusive: bool,
97
},
98
#[cfg(feature = "dtype-struct")]
99
SplitN(usize),
100
#[cfg(feature = "temporal")]
101
Strptime(DataTypeExpr, StrptimeOptions),
102
Split(bool),
103
#[cfg(feature = "regex")]
104
SplitRegex {
105
inclusive: bool,
106
strict: bool,
107
},
108
#[cfg(feature = "dtype-decimal")]
109
ToDecimal {
110
scale: usize,
111
},
112
#[cfg(feature = "nightly")]
113
Titlecase,
114
Uppercase,
115
#[cfg(feature = "string_pad")]
116
ZFill,
117
#[cfg(feature = "find_many")]
118
ContainsAny {
119
ascii_case_insensitive: bool,
120
},
121
#[cfg(feature = "find_many")]
122
ReplaceMany {
123
ascii_case_insensitive: bool,
124
leftmost: bool,
125
},
126
#[cfg(feature = "find_many")]
127
ExtractMany {
128
ascii_case_insensitive: bool,
129
overlapping: bool,
130
leftmost: bool,
131
},
132
#[cfg(feature = "find_many")]
133
FindMany {
134
ascii_case_insensitive: bool,
135
overlapping: bool,
136
leftmost: bool,
137
},
138
#[cfg(feature = "regex")]
139
EscapeRegex,
140
}
141
142
impl Display for StringFunction {
143
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
144
use StringFunction::*;
145
let s = match self {
146
Format { .. } => "format",
147
#[cfg(feature = "regex")]
148
Contains { .. } => "contains",
149
CountMatches(_) => "count_matches",
150
EndsWith => "ends_with",
151
Extract(_) => "extract",
152
#[cfg(feature = "concat_str")]
153
ConcatHorizontal { .. } => "concat_horizontal",
154
#[cfg(feature = "concat_str")]
155
ConcatVertical { .. } => "concat_vertical",
156
ExtractAll => "extract_all",
157
#[cfg(feature = "extract_groups")]
158
ExtractGroups { .. } => "extract_groups",
159
#[cfg(feature = "string_to_integer")]
160
ToInteger { .. } => "to_integer",
161
#[cfg(feature = "regex")]
162
Find { .. } => "find",
163
Head => "head",
164
Tail => "tail",
165
#[cfg(feature = "extract_jsonpath")]
166
JsonDecode { .. } => "json_decode",
167
#[cfg(feature = "extract_jsonpath")]
168
JsonPathMatch => "json_path_match",
169
LenBytes => "len_bytes",
170
Lowercase => "to_lowercase",
171
LenChars => "len_chars",
172
#[cfg(feature = "string_pad")]
173
PadEnd { .. } => "pad_end",
174
#[cfg(feature = "string_pad")]
175
PadStart { .. } => "pad_start",
176
#[cfg(feature = "regex")]
177
Replace { .. } => "replace",
178
#[cfg(feature = "string_normalize")]
179
Normalize { .. } => "normalize",
180
#[cfg(feature = "string_reverse")]
181
Reverse => "reverse",
182
#[cfg(feature = "string_encoding")]
183
HexEncode => "hex_encode",
184
#[cfg(feature = "binary_encoding")]
185
HexDecode(_) => "hex_decode",
186
#[cfg(feature = "string_encoding")]
187
Base64Encode => "base64_encode",
188
#[cfg(feature = "binary_encoding")]
189
Base64Decode(_) => "base64_decode",
190
Slice => "slice",
191
StartsWith => "starts_with",
192
StripChars => "strip_chars",
193
StripCharsStart => "strip_chars_start",
194
StripCharsEnd => "strip_chars_end",
195
StripPrefix => "strip_prefix",
196
StripSuffix => "strip_suffix",
197
#[cfg(feature = "dtype-struct")]
198
SplitExact { inclusive, .. } => {
199
if *inclusive {
200
"split_exact_inclusive"
201
} else {
202
"split_exact"
203
}
204
},
205
#[cfg(feature = "dtype-struct")]
206
SplitN(_) => "splitn",
207
#[cfg(feature = "temporal")]
208
Strptime(_, _) => "strptime",
209
Split(inclusive) => {
210
if *inclusive {
211
"split_inclusive"
212
} else {
213
"split"
214
}
215
},
216
#[cfg(feature = "regex")]
217
SplitRegex { inclusive, .. } => {
218
if *inclusive {
219
"split_regex_inclusive"
220
} else {
221
"split_regex"
222
}
223
},
224
#[cfg(feature = "nightly")]
225
Titlecase => "to_titlecase",
226
#[cfg(feature = "dtype-decimal")]
227
ToDecimal { .. } => "to_decimal",
228
Uppercase => "to_uppercase",
229
#[cfg(feature = "string_pad")]
230
ZFill => "zfill",
231
#[cfg(feature = "find_many")]
232
ContainsAny { .. } => "contains_any",
233
#[cfg(feature = "find_many")]
234
ReplaceMany { .. } => "replace_many",
235
#[cfg(feature = "find_many")]
236
ExtractMany { .. } => "extract_many",
237
#[cfg(feature = "find_many")]
238
FindMany { .. } => "extract_many",
239
#[cfg(feature = "regex")]
240
EscapeRegex => "escape_regex",
241
};
242
write!(f, "str.{s}")
243
}
244
}
245
246
impl From<StringFunction> for FunctionExpr {
247
fn from(value: StringFunction) -> Self {
248
FunctionExpr::StringExpr(value)
249
}
250
}
251
252