Support JSON roundtripping via to_json()/from_json() for patterns

This needed a small tweak in the deserialization, since each roundtrip
would otherwise pad the prior pattern with an extra /^?(...)$?/.

This expands the language.set test to also verify serializing/unserializing for
sets, similarly to tables in the previous commit.
This commit is contained in:
Christian Kreibich 2024-06-28 17:27:47 -07:00
parent 92c1098e97
commit 0179a5e75c
3 changed files with 86 additions and 35 deletions

View file

@ -996,11 +996,17 @@ static std::variant<ValPtr, std::string> BuildVal(const rapidjson::Value& j, con
return mismatch_err();
std::string candidate(j.GetString(), j.GetStringLength());
// Remove any surrounding '/'s, not needed when creating an RE_matcher.
if ( candidate.size() > 2 && candidate.front() == candidate.back() && candidate.back() == '/' ) {
// Remove the '/'s
candidate.erase(0, 1);
candidate.erase(candidate.size() - 1);
}
// Remove any surrounding "^?(" and ")$?", automatically added below.
if ( candidate.size() > 6 && candidate.substr(0, 3) == "^?(" &&
candidate.substr(candidate.size() - 3, 3) == ")$?" ) {
candidate.erase(0, 3);
candidate.erase(candidate.size() - 3);
}
auto re = std::make_unique<RE_Matcher>(candidate.c_str());
if ( ! re->Compile() )