forked from mia/Aegisub
Fix string vs color logic in the option parsing
This commit is contained in:
parent
28b056e422
commit
0483c1cfbe
2 changed files with 27 additions and 2 deletions
|
@ -31,6 +31,8 @@
|
|||
#include <libaegisub/option_value.h>
|
||||
#include <libaegisub/scoped_ptr.h>
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
|
||||
namespace agi {
|
||||
|
||||
ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors, bool replace)
|
||||
|
@ -117,7 +119,12 @@ void ConfigVisitor::Visit(const json::Double& number) {
|
|||
}
|
||||
|
||||
void ConfigVisitor::Visit(const json::String& string) {
|
||||
if (string.size() && (string.find("rgb(") == 0 || string[0] == '#' || string[0] == '&')) {
|
||||
size_t size = string.size();
|
||||
if ((size == 4 && string[0] == '#') ||
|
||||
(size == 7 && string[0] == '#') ||
|
||||
(size >= 10 && boost::starts_with(string, "rgb(")) ||
|
||||
((size == 9 || size == 10) && boost::starts_with(string, "&H")))
|
||||
{
|
||||
AddOptionValue(new OptionValueColor(name, string));
|
||||
} else {
|
||||
AddOptionValue(new OptionValueString(name, string));
|
||||
|
|
|
@ -261,3 +261,21 @@ TEST_F(lagi_option, empty_array_decays_to_first_used_type) {
|
|||
EXPECT_THROW(opt.Get("arr")->GetListInt(), agi::OptionValueErrorInvalidListType);
|
||||
}
|
||||
}
|
||||
|
||||
#define CHECK_TYPE(str, type) \
|
||||
do { \
|
||||
agi::Options opt("", "{ \"" str "\" : \"" str "\" }", agi::Options::FLUSH_SKIP); \
|
||||
EXPECT_NO_THROW(opt.Get(str)->Get##type()); \
|
||||
} while (false)
|
||||
|
||||
TEST_F(lagi_option, color_vs_string) {
|
||||
CHECK_TYPE("#", String);
|
||||
CHECK_TYPE("#a", String);
|
||||
CHECK_TYPE("#abc", Color);
|
||||
CHECK_TYPE("#aabbcc", Color);
|
||||
CHECK_TYPE("#aabb", String);
|
||||
|
||||
CHECK_TYPE("&", String);
|
||||
CHECK_TYPE("&H000000&", Color);
|
||||
CHECK_TYPE("&H00000000", Color);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue