Extract post formats via regex.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2020-01-14 22:28:14 +01:00
parent bd7952e901
commit c192f352f0
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 10 additions and 10 deletions

View File

@ -121,23 +121,23 @@ vector<string> Instance::get_post_formats() noexcept
return _post_formats;
}
constexpr string_view searchstring{R"("postFormats":[)"};
auto pos{answer.body.find(searchstring)};
if (pos == string::npos)
const regex re_allformats(R"("postFormats"\s*:\s*\[([^\]]+)\])");
smatch match;
if (!regex_search(answer.body, match, re_allformats))
{
debuglog << "Couldn't find metadata.postFormats.\n";
_post_formats = {default_value};
return _post_formats;
}
pos += searchstring.size();
auto endpos{answer.body.find("],", pos)};
string formats{answer.body.substr(pos, endpos - pos)};
debuglog << "Extracted postFormats: " << formats << '\n';
string allformats{match[1].str()};
debuglog << "Found postFormats: " << allformats << '\n';
while ((pos = formats.find('"', 1)) != string::npos)
const regex re_format(R"(\s*"([^"]+)\"\s*,?)");
while (regex_search(allformats, match, re_format))
{
_post_formats.push_back(formats.substr(1, pos - 1));
formats.erase(0, pos + 2); // 2 is the length of: ",
_post_formats.push_back(match[1].str());
allformats = match.suffix();
debuglog << "Found postFormat: " << _post_formats.back() << '\n';
}
}