Skip to content

Commit c4e7357

Browse files
committed
optimization related with std::string allocation in TelegramParser::tryExtractJsonValueStr
1 parent 0a7580b commit c4e7357

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

vpr/src/server/telegramparser.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@ namespace comm {
77

88
std::optional<std::string> TelegramParser::tryExtractJsonValueStr(const std::string& jsonString, const std::string& key)
99
{
10+
static const std::string end_key_pattern{"\":\""};
11+
1012
// Find the position of the key
11-
size_t keyPos = jsonString.find("\"" + key + "\":");
13+
size_t keyPos = jsonString.find('\"' + key + end_key_pattern);
1214

1315
if (keyPos == std::string::npos) {
1416
// Key not found
1517
return std::nullopt;
1618
}
1719

1820
// Find the position of the value after the key
19-
size_t valuePosStart = jsonString.find("\"", keyPos + key.length() + std::string("\":\"").size());
21+
size_t valuePosStart = jsonString.find('\"', keyPos + key.length() + end_key_pattern.size());
2022

2123
if (valuePosStart == std::string::npos) {
2224
// Value not found
2325
return std::nullopt;
2426
}
2527

2628
// Find the position of the closing quote for the value
27-
size_t valueEnd = jsonString.find("\"", valuePosStart + std::string("\"").size());
29+
size_t valueEnd = jsonString.find('\"', valuePosStart + sizeof('\"'));
2830

2931
if (valueEnd == std::string::npos) {
3032
// Closing quote not found

0 commit comments

Comments
 (0)