File tree Expand file tree Collapse file tree 1 file changed +22
-17
lines changed Expand file tree Collapse file tree 1 file changed +22
-17
lines changed Original file line number Diff line number Diff line change 1
1
#ifndef STRING_UTIL_HPP
2
2
#define STRING_UTIL_HPP
3
3
4
+ #include < array>
4
5
#include < cctype>
5
-
6
+ # include < cstddef >
6
7
#include < random>
7
8
#include < string>
8
9
#include < vector>
9
10
10
11
namespace osrm ::util
11
12
{
12
13
14
+ // implements Lemire's table-based escape needs check
15
+ // cf. https://lemire.me/blog/2024/05/31/quickly-checking-whether-a-string-needs-escaping/
16
+ inline static constexpr std::array<uint8_t , 256 > json_quotable_character = []() constexpr
17
+ {
18
+ std::array<uint8_t , 256 > result{};
19
+ for (auto i = 0 ; i < 32 ; i++)
20
+ {
21
+ result[i] = 1 ;
22
+ }
23
+ for (auto i : {' "' , ' \\ ' })
24
+ {
25
+ result[i] = 1 ;
26
+ }
27
+ return result;
28
+ }();
29
+
13
30
inline bool RequiresJSONStringEscaping (const std::string &string)
14
31
{
15
- for (const char letter : string)
32
+ uint8_t needs = 0 ;
33
+ for (uint8_t c : string)
16
34
{
17
- switch (letter)
18
- {
19
- case ' \\ ' :
20
- case ' "' :
21
- case ' /' :
22
- case ' \b ' :
23
- case ' \f ' :
24
- case ' \n ' :
25
- case ' \r ' :
26
- case ' \t ' :
27
- return true ;
28
- default :
29
- continue ;
30
- }
35
+ needs |= json_quotable_character[c];
31
36
}
32
- return false ;
37
+ return needs ;
33
38
}
34
39
35
40
inline void EscapeJSONString (const std::string &input, std::string &output)
You can’t perform that action at this time.
0 commit comments