This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-cpp.git
The following commit(s) were added to refs/heads/master by this push:
new 000b31f fix: keep quoted string literals unchanged in EscapeAssertion
000b31f is described below
commit 000b31f03a01d4c18a8481d72940256ff1b16cc8
Author: Yang Luo <[email protected]>
AuthorDate: Sat Oct 31 09:23:29 2026 +0800
fix: keep quoted string literals unchanged in EscapeAssertion
---
casbin/util/escape_assertion.cpp | 30 ++++++++++++++++--------------
tests/util_test.cpp | 5 +++++
2 files changed, 21 insertions(+), 14 deletions(-)
diff --git a/casbin/util/escape_assertion.cpp b/casbin/util/escape_assertion.cpp
index 5bac5c7..a766c10 100644
--- a/casbin/util/escape_assertion.cpp
+++ b/casbin/util/escape_assertion.cpp
@@ -32,22 +32,24 @@ namespace casbin {
* @return the escaped value.
*/
std::string EscapeAssertion(std::string s) {
- std::regex regex_s("[a-zA-Z0-9. ]+");
-
- std::sregex_iterator words_begin = std::sregex_iterator(s.begin(),
s.end(), regex_s);
- auto words_end = std::sregex_iterator();
-
- for (std::sregex_iterator k = words_begin; k != words_end; ++k) {
- std::smatch match = *k;
- std::string match_str = match.str();
- int pos = static_cast<int>(match_str.find("."));
- if (pos != -1) {
- std::string new_str = match_str.replace(pos, 1, "_");
- s = s.replace(match.position(), match.str().length(), new_str);
- }
+ static const std::regex regex_s("([()\\s|&,=!><+\\-*/]|^)([rp][0-9]*)\\.");
+
+ std::string res;
+ size_t start = 0;
+ while (start < s.size()) {
+ size_t quote = s.find_first_of("\"'", start);
+ res += std::regex_replace(s.substr(start, quote - start), regex_s,
"$1$2_");
+ if (quote == std::string::npos)
+ break;
+
+ size_t end = s.find(s[quote], quote + 1);
+ if (end == std::string::npos)
+ end = s.size() - 1;
+ res += s.substr(quote, end - quote + 1);
+ start = end + 1;
}
- return s;
+ return res;
}
} // namespace casbin
diff --git a/tests/util_test.cpp b/tests/util_test.cpp
index f728795..b0b31fb 100644
--- a/tests/util_test.cpp
+++ b/tests/util_test.cpp
@@ -40,6 +40,11 @@ TEST(TestUtil, TestEscapeAssertion) {
TestEscapeAssertionFn("g(r.sub, p.sub) == p.attr", "g(r_sub, p_sub) ==
p_attr");
TestEscapeAssertionFn("g(r.sub,p.sub) == p.attr", "g(r_sub,p_sub) ==
p_attr");
TestEscapeAssertionFn("(r.attp.value || p.attr)p.u", "(r_attp.value ||
p_attr)p_u");
+ TestEscapeAssertionFn("r.sub == \"r.my_resource\"", "r_sub ==
\"r.my_resource\"");
+ TestEscapeAssertionFn("p.obj == 'p.resource'", "p_obj == 'p.resource'");
+ TestEscapeAssertionFn("p.sub == p.obj && p.obj == \"r.something\"", "p_sub
== p_obj && p_obj == \"r.something\"");
+ TestEscapeAssertionFn("r.sub == \"hello p.world\" && r2.obj == p2.obj",
"r_sub == \"hello p.world\" && r2_obj == p2_obj");
+ TestEscapeAssertionFn("r.sub == \"a.p.p.l.e\"", "r_sub == \"a.p.p.l.e\"");
}
void TestRemoveCommentsFn(const std::string& s, const std::string& res) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]