aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Piotr Jaroszyński <peper@gentoo.org> 2007-07-07 16:19:21 +0000
committerAvatar Piotr Jaroszyński <peper@gentoo.org> 2007-07-07 16:19:21 +0000
commit96f6b8b7bad21d59dc41bbfd19c29ecaf6f03d2c (patch)
treef2116b48386ad5f4e12585ffc1f26b1a0a9f75f1
parent26612768fb039ce1b50e952b30c46979eb821dfe (diff)
downloadpaludis-96f6b8b7bad21d59dc41bbfd19c29ecaf6f03d2c.tar.gz
paludis-96f6b8b7bad21d59dc41bbfd19c29ecaf6f03d2c.tar.xz
Improve destringification.
-rw-r--r--paludis/util/destringify.hh44
-rw-r--r--paludis/util/destringify_TEST.cc23
2 files changed, 41 insertions, 26 deletions
diff --git a/paludis/util/destringify.hh b/paludis/util/destringify.hh
index de5ffc2..c864412 100644
--- a/paludis/util/destringify.hh
+++ b/paludis/util/destringify.hh
@@ -110,20 +110,24 @@ namespace paludis
*/
static bool do_destringify(const std::string & s)
{
- int i;
- try
- {
- i = Destringifier<int, Exception_>::do_destringify(s);
- return i > 0;
- }
- catch (const DestringifyError &)
+ std::istringstream ss(s);
+ if ((s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z'))
{
bool b;
- std::istringstream ss(s);
ss >> std::boolalpha >> b;
- if (! ss.eof() || ss.bad())
+ if (ss.eof() && ! ss.bad())
+ return b;
+ else
+ throw Exception_(s);
+ }
+ else
+ {
+ int i;
+ ss >> i;
+ if (ss.eof() && ! ss.bad())
+ return i > 0;
+ else
throw Exception_(s);
- return b;
}
}
};
@@ -141,22 +145,10 @@ namespace paludis
*/
static char do_destringify(const std::string & s)
{
- try
- {
- std::string r(Destringifier<std::string, Exception_>::do_destringify(s));
- if (r.length() != 1)
- throw DestringifyError(s);
- return r[0];
- }
- catch (const DestringifyError &)
- {
- bool b;
- std::istringstream ss(s);
- ss >> std::boolalpha >> b;
- if (! ss.eof() || ss.bad())
- throw Exception_(s);
- return b;
- }
+ if (s.length() == 1)
+ return s[0];
+ else
+ throw Exception_(s);
}
};
}
diff --git a/paludis/util/destringify_TEST.cc b/paludis/util/destringify_TEST.cc
index d9aa421..51ec070 100644
--- a/paludis/util/destringify_TEST.cc
+++ b/paludis/util/destringify_TEST.cc
@@ -69,6 +69,7 @@ namespace test_cases
TEST_CHECK_EQUAL(destringify<float>("0.1"), 0.1f);
TEST_CHECK_EQUAL(destringify<float>("-1.54"), -1.54f);
TEST_CHECK_THROWS(destringify<float>("I am a fish"), DestringifyError);
+ TEST_CHECK_THROWS(destringify<float>(""), DestringifyError);
}
} test_case_destringify_float;
@@ -84,6 +85,7 @@ namespace test_cases
{
TEST_CHECK_EQUAL(destringify<std::string>("asdf"), "asdf");
TEST_CHECK_EQUAL(destringify<std::string>(" a f e b "), " a f e b ");
+ TEST_CHECK_THROWS(destringify<std::string>(""), DestringifyError);
}
} test_case_destringify_string;
@@ -105,7 +107,28 @@ namespace test_cases
TEST_CHECK(!destringify<bool>("-1"));
TEST_CHECK_THROWS(destringify<bool>("flase"), DestringifyError);
TEST_CHECK_THROWS(destringify<bool>("432.2413"), DestringifyError);
+ TEST_CHECK_THROWS(destringify<bool>(""), DestringifyError);
}
} test_case_destringify_bool;
+
+ /** \test
+ * Test destringify for char.
+ *
+ */
+ struct DestringifyCharTests : TestCase
+ {
+ DestringifyCharTests() : TestCase("destringify char") { }
+
+ void run()
+ {
+ TEST_CHECK_EQUAL(destringify<char>("x"), 'x');
+ TEST_CHECK_EQUAL(destringify<char>("0"), '0');
+ TEST_CHECK_THROWS(destringify<char>("aa"), DestringifyError);
+ TEST_CHECK_THROWS(destringify<char>("a a"), DestringifyError);
+ TEST_CHECK_THROWS(destringify<char>("11"), DestringifyError);
+ TEST_CHECK_THROWS(destringify<char>(""), DestringifyError);
+ }
+ } test_case_destringify_char;
+
}