Skip to content

Commit

Permalink
fix: use CRLF mode in regexes for match and search
Browse files Browse the repository at this point in the history
Update after latest CTS surfaced an edge case for i-regexp compliance.

Use CRLF mode in regexes for search and match functions so that the
line feed and new line characters are treated properly with a dot matcher
  • Loading branch information
hiltontj committed May 12, 2024
1 parent 5d4090c commit 50ea5bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions serde_json_path/src/parser/selector/function/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,25 @@ fn count(nodes: NodesType) -> ValueType {
#[serde_json_path_macros::register(name = "match", target = MATCH_FUNC)]
fn match_func(value: ValueType, rgx: ValueType) -> LogicalType {
match (value.as_value(), rgx.as_value()) {
(Some(Value::String(s)), Some(Value::String(r))) => Regex::new(format!("^({r})$").as_str())
.map(|r| r.is_match(s))
.map(Into::into)
.unwrap_or_default(),
(Some(Value::String(s)), Some(Value::String(r))) => {
Regex::new(format!("(?R)^({r})$").as_str())
.map(|r| r.is_match(s))
.map(Into::into)
.unwrap_or_default()
}
_ => LogicalType::False,
}
}

#[serde_json_path_macros::register(target = SEARCH_FUNC)]
fn search(value: ValueType, rgx: ValueType) -> LogicalType {
match (value.as_value(), rgx.as_value()) {
(Some(Value::String(s)), Some(Value::String(r))) => Regex::new(r.as_str())
.map(|r| r.is_match(s))
.map(Into::into)
.unwrap_or_default(),
(Some(Value::String(s)), Some(Value::String(r))) => {
Regex::new(format!("(?R)({r})").as_str())
.map(|r| r.is_match(s))
.map(Into::into)
.unwrap_or_default()
}
_ => LogicalType::False,
}
}
Expand Down

0 comments on commit 50ea5bd

Please sign in to comment.