-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added
escape_regex
operation to the str
namespace and as a …
…global function (#19257)
- Loading branch information
Showing
21 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
crates/polars-ops/src/chunked_array/strings/escape_regex.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use polars_core::prelude::{StringChunked, StringChunkedBuilder}; | ||
|
||
#[inline] | ||
pub fn escape_regex_str(s: &str) -> String { | ||
regex_syntax::escape(s) | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
builder.finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ Miscellaneous | |
|
||
align_frames | ||
concat | ||
escape_regex | ||
|
||
Parallelization | ||
~~~~~~~~~~~~~~~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 `Expr`, you may want use `Expr.str.escape_regex` instead" | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters