-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffoon.cc
35 lines (27 loc) · 1.09 KB
/
buffoon.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "buffoon.h"
#include <regex>
namespace Buffoon {
std::string Buffoon::findValueByKey(const std::string &json_like_string, const std::string &key) {
std::string escapedKey = escapeSpecialChars(key);
std::regex key_value_pattern("\"" + escapedKey + R"("\s*:\s*("[^"]*"|\d+|\-?\d+\.\d+|true|false|null))");
std::smatch match;
if (std::regex_search(json_like_string, match, key_value_pattern)) {
std::string value = match[1].str();
// If value is inside quotes, strip them
if (value.front() == '"' && value.back() == '"') {
return value.substr(1, value.length() - 2);
}
return value; // return the value as is (for numeric, boolean, or null)
}
return "";
}
std::string Buffoon::findUsername(const std::string &payload_value) {
size_t nPos = payload_value.find("n=");
if (nPos != std::string::npos) {
nPos += 2;
size_t commaPos = payload_value.find(',', nPos);
return payload_value.substr(nPos, commaPos - nPos);
}
return "";
}
} // namespace Buffoon