void testInteger (IntType in) { std::string s; IntType out (in+1); expect (lexicalCastChecked (s, in)); expect (lexicalCastChecked (out, s)); expect (out == in); }
bool tryEdgeCase (std::string const& s) { T ret; bool const result = lexicalCastChecked (ret, s); if (!result) return false; return s == std::to_string (ret); }
bool isNumeric (std::string const& s) { int n; // Must be convertible to an integer if (!lexicalCastChecked (n, s)) return false; // Must not have leading zeroes return std::to_string (n) == s; }
bool chopUInt (int& value, int limit, std::string& input) { // Must not be empty if (input.empty ()) return false; auto left_iter = std::find_if_not (input.begin (), input.end (), [](std::string::value_type c) { return std::isdigit (c, std::locale::classic()); }); std::string item (input.begin (), left_iter); // Must not be empty if (item.empty ()) return false; int n; // Must be convertible to an integer if (!lexicalCastChecked (n, item)) return false; // Must not have leading zeroes if (std::to_string (n) != item) return false; // Must not be out of range if (n < 0 || n > limit) return false; input.erase (input.begin (), left_iter); value = n; return true; }
void testZero () { testcase ("zero conversion"); { std::int32_t out; expect (lexicalCastChecked (out, "-0"), "0"); expect (lexicalCastChecked (out, "0"), "0"); expect (lexicalCastChecked (out, "+0"), "0"); } { std::uint32_t out; expect (!lexicalCastChecked (out, "-0"), "0"); expect (lexicalCastChecked (out, "0"), "0"); expect (lexicalCastChecked (out, "+0"), "0"); } }
void tryBadConvert (std::string const& s) { T out; expect (!lexicalCastChecked (out, s), s); }