Fix string vs color logic in the option parsing

This commit is contained in:
Thomas Goyne 2012-11-24 10:37:37 -08:00
parent 28b056e422
commit 0483c1cfbe
2 changed files with 27 additions and 2 deletions

View File

@ -31,6 +31,8 @@
#include <libaegisub/option_value.h> #include <libaegisub/option_value.h>
#include <libaegisub/scoped_ptr.h> #include <libaegisub/scoped_ptr.h>
#include <boost/algorithm/string/predicate.hpp>
namespace agi { namespace agi {
ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors, bool replace) 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) { 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)); AddOptionValue(new OptionValueColor(name, string));
} else { } else {
AddOptionValue(new OptionValueString(name, string)); AddOptionValue(new OptionValueString(name, string));

View File

@ -1,4 +1,4 @@
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org> // Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
@ -261,3 +261,21 @@ TEST_F(lagi_option, empty_array_decays_to_first_used_type) {
EXPECT_THROW(opt.Get("arr")->GetListInt(), agi::OptionValueErrorInvalidListType); 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);
}