summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortastytea2020-01-14 22:28:14 +0100
committertastytea2020-01-14 22:28:14 +0100
commitc192f352f004c3a24a8012c8fdc9ce83d7fbd15a (patch)
treedb63567ee015149cfda48164c8905561e0cec0b7
parentbd7952e901ab103051faa945f5aa49e9bb3b026d (diff)
downloadmastodonpp-c192f352f004c3a24a8012c8fdc9ce83d7fbd15a.tar
mastodonpp-c192f352f004c3a24a8012c8fdc9ce83d7fbd15a.tar.gz
mastodonpp-c192f352f004c3a24a8012c8fdc9ce83d7fbd15a.zip
Extract post formats via regex.
-rw-r--r--src/instance.cpp20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/instance.cpp b/src/instance.cpp
index ac2f308..fb8db81 100644
--- a/src/instance.cpp
+++ b/src/instance.cpp
@@ -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';
+
+ const regex re_format(R"(\s*"([^"]+)\"\s*,?)");
- while ((pos = formats.find('"', 1)) != string::npos)
+ 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';
}
}