-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added escape_regex
operation to the str
namespace and as a global function
#19257
base: main
Are you sure you want to change the base?
Changes from 2 commits
9b42c1f
6da46d8
27d460b
32e4daa
622d233
2d29893
2e48326
040d2c7
b3a078a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use polars_core::prelude::arity::unary_elementwise; | ||
use polars_core::prelude::StringChunked; | ||
use regex::escape; | ||
|
||
#[inline] | ||
pub fn escape_regex_str(s: &str) -> String { | ||
escape(s) | ||
} | ||
|
||
fn escape_regex_helper(s: Option<&str>) -> Option<String> { | ||
s.map(escape_regex_str) | ||
} | ||
|
||
pub fn escape_regex(ca: &StringChunked) -> StringChunked { | ||
unary_elementwise(ca, escape_regex_helper) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a dependency on pub fn escape_regex(ca: &StringChunked) -> StringChunked {
let mut buffer = String::new();
let mut builder = StringChunkedBuilder::new(ca.name().clone(), ca.len());
for opt_s in ca.iter() {
if let Some(s) = opt_s {
buffer.clear();
regex_syntax::escape_into(s, &mut buffer);
builder.append_value(&buffer);
} else {
builder.append_null();
}
}
} This prevents us from having to allocate for each string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had some Cargo.lock mess, will get to it tommorow evening. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[pyfunction] | ||
pub fn escape_regex(s: &str) -> PyResult<String> { | ||
let escaped_s = polars_ops::chunked_array::strings::escape_regex_str(s); | ||
Ok(escaped_s) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
import contextlib | ||
|
||
with contextlib.suppress(ImportError): # Module not available when building docs | ||
import polars.polars as plr | ||
import polars._reexport as pl | ||
|
||
|
||
def escape_regex(s: str) -> str: | ||
r""" | ||
Escapes string regex meta characters. | ||
Parameters | ||
---------- | ||
s | ||
The string that all of its meta characters will be escaped. | ||
""" | ||
if isinstance(s, pl.Expr): | ||
msg = "escape_regex function is unsupported for `Exp`, you may want use `Expr.str.escape_regex` instead" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
raise TypeError(msg) | ||
elif not isinstance(s, str): | ||
msg = f"escape_regex function supports only `str` type, got `{type(s)}`" | ||
raise TypeError(msg) | ||
|
||
return plr.escape_regex(s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this function so
pl.escape_regex
andstr.escape_regex
will be coupled by same implementation.