Skip to content
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

Treat UTF-16 surrogate pairs as single characters for string min/maxLength #88

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions parser/src/json/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct Compiler {
pending_definitions: Vec<(String, NodeRef)>,

any_cache: Option<NodeRef>,
lexeme_cache: HashMap<String, NodeRef>,
lexeme_cache: HashMap<(String, bool), NodeRef>,
}

macro_rules! cache {
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Compiler {
message: reason.to_string(),
})),
Schema::Null => Ok(self.builder.string("null")),
Schema::Boolean => Ok(self.lexeme(r"true|false")),
Schema::Boolean => Ok(self.lexeme(r"true|false", false)),
Schema::Number {
minimum,
maximum,
Expand Down Expand Up @@ -285,13 +285,14 @@ impl Compiler {
}
}

fn lexeme(&mut self, rx: &str) -> NodeRef {
if self.lexeme_cache.contains_key(rx) {
return self.lexeme_cache[rx];
}
let r = self.builder.lexeme(mk_regex(rx), false);
self.lexeme_cache.insert(rx.to_string(), r);
r
fn lexeme(&mut self, rx: &str, json_quoted: bool) -> NodeRef {
let key = (rx.to_string(), json_quoted);
self.lexeme_cache
.entry(key)
.or_insert_with(||
self.builder.lexeme(mk_regex(rx), json_quoted)
)
.clone()
}

fn json_int(
Expand Down Expand Up @@ -333,7 +334,7 @@ impl Compiler {
minimum, maximum
)
})?;
Ok(self.lexeme(&rx))
Ok(self.lexeme(&rx, false))
}

fn json_number(
Expand All @@ -352,11 +353,11 @@ impl Compiler {
minimum, maximum
)
})?;
Ok(self.lexeme(&rx))
Ok(self.lexeme(&rx, false))
}

fn json_simple_string(&mut self) -> NodeRef {
self.lexeme(&format!("\"{}*\"", CHAR_REGEX))
self.lexeme("(?s:.*)", true)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this change is orthogonal to the underlying PR -- @mmoskal if using the CHAR_REGEX directly is marginally more performant, I can switch it back.

}

fn get_definition(&mut self, reference: &str) -> Result<NodeRef> {
Expand Down Expand Up @@ -568,12 +569,14 @@ impl Compiler {
let node = self.builder.lexeme(mk_regex(regex), true);
Ok(node)
} else {
Ok(self.lexeme(&format!(
"\"{}{{{},{}}}\"",
CHAR_REGEX,
min_length,
max_length.map_or("".to_string(), |v| v.to_string())
)))
Ok(self.lexeme(
&format!(
"(?s:.{{{},{}}})",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

derivre seems smart enough to match \uD83D\uDCA9 with ., so length is counted appropriately

min_length,
max_length.map_or("".to_string(), |v| v.to_string())
),
true,
))
}
}

Expand Down
Loading