Skip to content

Commit

Permalink
fix: use CRLF mode in regexes for match and search (#92)
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 5b3fd54
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions serde_json_path/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **fixed**: edge case where `.` in regexes for `match` and `search` functions was matching `\r\n` properly ([#92])

[#92]: https://github.com/hiltontj/serde_json_path/pull/92

# 0.6.7 (3 March 2024)

- **testing**: support tests for non-determinism in compliance test suite ([#85])
Expand Down
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 5b3fd54

Please sign in to comment.