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