use super::*;
pub struct StringNameSpace(pub(crate) Expr);
impl StringNameSpace {
#[cfg(feature = "regex")]
pub fn contains_literal(self, pat: Expr) -> Expr {
self.0.map_binary(
StringFunction::Contains {
literal: true,
strict: false,
},
pat,
)
}
#[cfg(feature = "regex")]
pub fn contains(self, pat: Expr, strict: bool) -> Expr {
self.0.map_binary(
StringFunction::Contains {
literal: false,
strict,
},
pat,
)
}
#[cfg(feature = "find_many")]
pub fn contains_any(self, patterns: Expr, ascii_case_insensitive: bool) -> Expr {
self.0.map_binary(
StringFunction::ContainsAny {
ascii_case_insensitive,
},
patterns,
)
}
#[cfg(feature = "find_many")]
pub fn replace_many(
self,
patterns: Expr,
replace_with: Expr,
ascii_case_insensitive: bool,
) -> Expr {
self.0.map_ternary(
StringFunction::ReplaceMany {
ascii_case_insensitive,
},
patterns,
replace_with,
)
}
#[cfg(feature = "find_many")]
pub fn extract_many(
self,
patterns: Expr,
ascii_case_insensitive: bool,
overlapping: bool,
) -> Expr {
self.0.map_binary(
StringFunction::ExtractMany {
ascii_case_insensitive,
overlapping,
},
patterns,
)
}
#[cfg(feature = "find_many")]
pub fn find_many(
self,
patterns: Expr,
ascii_case_insensitive: bool,
overlapping: bool,
) -> Expr {
self.0.map_binary(
StringFunction::FindMany {
ascii_case_insensitive,
overlapping,
},
patterns,
)
}
pub fn ends_with(self, sub: Expr) -> Expr {
self.0.map_binary(StringFunction::EndsWith, sub)
}
pub fn starts_with(self, sub: Expr) -> Expr {
self.0.map_binary(StringFunction::StartsWith, sub)
}
#[cfg(feature = "string_encoding")]
pub fn hex_encode(self) -> Expr {
self.0.map_unary(StringFunction::HexEncode)
}
#[cfg(feature = "binary_encoding")]
pub fn hex_decode(self, strict: bool) -> Expr {
self.0.map_unary(StringFunction::HexDecode(strict))
}
#[cfg(feature = "string_encoding")]
pub fn base64_encode(self) -> Expr {
self.0.map_unary(StringFunction::Base64Encode)
}
#[cfg(feature = "binary_encoding")]
pub fn base64_decode(self, strict: bool) -> Expr {
self.0.map_unary(StringFunction::Base64Decode(strict))
}
pub fn extract(self, pat: Expr, group_index: usize) -> Expr {
self.0.map_binary(StringFunction::Extract(group_index), pat)
}
#[cfg(feature = "extract_groups")]
pub fn extract_groups(self, pat: &str) -> PolarsResult<Expr> {
use polars_utils::format_pl_smallstr;
let reg = polars_utils::regex_cache::compile_regex(pat)?;
let names = reg
.capture_names()
.enumerate()
.skip(1)
.map(|(idx, opt_name)| {
opt_name
.map(PlSmallStr::from_str)
.unwrap_or_else(|| format_pl_smallstr!("{idx}"))
})
.collect::<Vec<_>>();
let dtype = DataType::Struct(
names
.iter()
.map(|name| Field::new(name.clone(), DataType::String))
.collect(),
);
Ok(self.0.map_unary(StringFunction::ExtractGroups {
dtype,
pat: pat.into(),
}))
}
#[cfg(feature = "string_pad")]
pub fn pad_start(self, length: Expr, fill_char: char) -> Expr {
self.0
.map_binary(StringFunction::PadStart { fill_char }, length)
}
#[cfg(feature = "string_pad")]
pub fn pad_end(self, length: Expr, fill_char: char) -> Expr {
self.0
.map_binary(StringFunction::PadEnd { fill_char }, length)
}
#[cfg(feature = "string_pad")]
pub fn zfill(self, length: Expr) -> Expr {
self.0.map_binary(StringFunction::ZFill, length)
}
#[cfg(feature = "regex")]
pub fn find_literal(self, pat: Expr) -> Expr {
self.0.map_binary(
StringFunction::Find {
literal: true,
strict: false,
},
pat,
)
}
#[cfg(feature = "regex")]
pub fn find(self, pat: Expr, strict: bool) -> Expr {
self.0.map_binary(
StringFunction::Find {
literal: false,
strict,
},
pat,
)
}
pub fn extract_all(self, pat: Expr) -> Expr {
self.0.map_binary(StringFunction::ExtractAll, pat)
}
pub fn count_matches(self, pat: Expr, literal: bool) -> Expr {
self.0
.map_binary(StringFunction::CountMatches(literal), pat)
}
#[cfg(feature = "temporal")]
pub fn strptime(
self,
dtype: impl Into<DataTypeExpr>,
options: StrptimeOptions,
ambiguous: Expr,
) -> Expr {
self.0
.map_binary(StringFunction::Strptime(dtype.into(), options), ambiguous)
}
#[cfg(feature = "dtype-date")]
pub fn to_date(self, options: StrptimeOptions) -> Expr {
self.strptime(DataType::Date, options, lit("raise"))
}
#[cfg(feature = "dtype-datetime")]
pub fn to_datetime(
self,
time_unit: Option<TimeUnit>,
time_zone: Option<TimeZone>,
options: StrptimeOptions,
ambiguous: Expr,
) -> Expr {
let time_unit = match (&options.format, time_unit) {
(_, Some(time_unit)) => time_unit,
(Some(format), None) => {
if format.contains("%.9f") || format.contains("%9f") {
TimeUnit::Nanoseconds
} else if format.contains("%.3f") || format.contains("%3f") {
TimeUnit::Milliseconds
} else {
TimeUnit::Microseconds
}
},
(None, None) => TimeUnit::Microseconds,
};
self.strptime(DataType::Datetime(time_unit, time_zone), options, ambiguous)
}
#[cfg(feature = "dtype-time")]
pub fn to_time(self, options: StrptimeOptions) -> Expr {
self.strptime(DataType::Time, options, lit("raise"))
}
#[cfg(feature = "dtype-decimal")]
pub fn to_decimal(self, scale: usize) -> Expr {
self.0.map_unary(StringFunction::ToDecimal { scale })
}
#[cfg(feature = "concat_str")]
pub fn join(self, delimiter: &str, ignore_nulls: bool) -> Expr {
self.0.map_unary(StringFunction::ConcatVertical {
delimiter: delimiter.into(),
ignore_nulls,
})
}
pub fn split(self, by: Expr) -> Expr {
self.0.map_binary(StringFunction::Split(false), by)
}
pub fn split_inclusive(self, by: Expr) -> Expr {
self.0.map_binary(StringFunction::Split(true), by)
}
#[cfg(feature = "dtype-struct")]
pub fn split_exact(self, by: Expr, n: usize) -> Expr {
self.0.map_binary(
StringFunction::SplitExact {
n,
inclusive: false,
},
by,
)
}
#[cfg(feature = "dtype-struct")]
pub fn split_exact_inclusive(self, by: Expr, n: usize) -> Expr {
self.0
.map_binary(StringFunction::SplitExact { n, inclusive: true }, by)
}
#[cfg(feature = "dtype-struct")]
pub fn splitn(self, by: Expr, n: usize) -> Expr {
self.0.map_binary(StringFunction::SplitN(n), by)
}
#[cfg(feature = "regex")]
pub fn replace(self, pat: Expr, value: Expr, literal: bool) -> Expr {
self.0
.map_ternary(StringFunction::Replace { n: 1, literal }, pat, value)
}
#[cfg(feature = "regex")]
pub fn replace_n(self, pat: Expr, value: Expr, literal: bool, n: i64) -> Expr {
self.0
.map_ternary(StringFunction::Replace { n, literal }, pat, value)
}
#[cfg(feature = "regex")]
pub fn replace_all(self, pat: Expr, value: Expr, literal: bool) -> Expr {
self.0
.map_ternary(StringFunction::Replace { n: -1, literal }, pat, value)
}
#[cfg(feature = "string_normalize")]
pub fn normalize(self, form: UnicodeForm) -> Expr {
self.0.map_unary(StringFunction::Normalize { form })
}
#[cfg(feature = "string_reverse")]
pub fn reverse(self) -> Expr {
self.0.map_unary(StringFunction::Reverse)
}
pub fn strip_chars(self, matches: Expr) -> Expr {
self.0.map_binary(StringFunction::StripChars, matches)
}
pub fn strip_chars_start(self, matches: Expr) -> Expr {
self.0.map_binary(StringFunction::StripCharsStart, matches)
}
pub fn strip_chars_end(self, matches: Expr) -> Expr {
self.0.map_binary(StringFunction::StripCharsEnd, matches)
}
pub fn strip_prefix(self, prefix: Expr) -> Expr {
self.0.map_binary(StringFunction::StripPrefix, prefix)
}
pub fn strip_suffix(self, suffix: Expr) -> Expr {
self.0.map_binary(StringFunction::StripSuffix, suffix)
}
pub fn to_lowercase(self) -> Expr {
self.0.map_unary(StringFunction::Lowercase)
}
pub fn to_uppercase(self) -> Expr {
self.0.map_unary(StringFunction::Uppercase)
}
#[cfg(feature = "nightly")]
pub fn to_titlecase(self) -> Expr {
self.0.map_unary(StringFunction::Titlecase)
}
#[cfg(feature = "string_to_integer")]
pub fn to_integer(self, base: Expr, dtype: Option<DataType>, strict: bool) -> Expr {
self.0
.map_binary(StringFunction::ToInteger { dtype, strict }, base)
}
pub fn len_bytes(self) -> Expr {
self.0.map_unary(StringFunction::LenBytes)
}
pub fn len_chars(self) -> Expr {
self.0.map_unary(StringFunction::LenChars)
}
pub fn slice(self, offset: Expr, length: Expr) -> Expr {
self.0.map_ternary(StringFunction::Slice, offset, length)
}
pub fn head(self, n: Expr) -> Expr {
self.0.map_binary(StringFunction::Head, n)
}
pub fn tail(self, n: Expr) -> Expr {
self.0.map_binary(StringFunction::Tail, n)
}
#[cfg(feature = "extract_jsonpath")]
pub fn json_decode(self, dtype: impl Into<DataTypeExpr>) -> Expr {
self.0.map_unary(StringFunction::JsonDecode(dtype.into()))
}
#[cfg(feature = "extract_jsonpath")]
pub fn json_path_match(self, pat: Expr) -> Expr {
self.0.map_binary(StringFunction::JsonPathMatch, pat)
}
#[cfg(feature = "regex")]
pub fn escape_regex(self) -> Expr {
self.0.map_unary(StringFunction::EscapeRegex)
}
}