From 2725202771f079f0f04c5a55cc572410f72f0645 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Fri, 31 May 2024 19:58:57 +0200 Subject: [PATCH] Use Lemire's fast check whether to escape a JSON string (#6923) --- include/util/string_util.hpp | 39 ++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/include/util/string_util.hpp b/include/util/string_util.hpp index 874a1d284..4e6351137 100644 --- a/include/util/string_util.hpp +++ b/include/util/string_util.hpp @@ -1,8 +1,9 @@ #ifndef STRING_UTIL_HPP #define STRING_UTIL_HPP +#include #include - +#include #include #include #include @@ -10,26 +11,30 @@ namespace osrm::util { +// implements Lemire's table-based escape needs check +// cf. https://lemire.me/blog/2024/05/31/quickly-checking-whether-a-string-needs-escaping/ +inline static constexpr std::array json_quotable_character = []() constexpr +{ + std::array result{}; + for (auto i = 0; i < 32; i++) + { + result[i] = 1; + } + for (auto i : {'"', '\\'}) + { + result[i] = 1; + } + return result; +}(); + inline bool RequiresJSONStringEscaping(const std::string &string) { - for (const char letter : string) + uint8_t needs = 0; + for (uint8_t c : string) { - switch (letter) - { - case '\\': - case '"': - case '/': - case '\b': - case '\f': - case '\n': - case '\r': - case '\t': - return true; - default: - continue; - } + needs |= json_quotable_character[c]; } - return false; + return needs; } inline void EscapeJSONString(const std::string &input, std::string &output)