fix bool lexical casts
oops
This commit is contained in:
parent
ca48a137ec
commit
3e8f914154
@ -10,4 +10,27 @@ void throwLexicalCastError(std::errc ec, const char* first, const char* last) {
|
|||||||
throw BadLexicalCast(strf("Lexical cast failed on '{}'", str));
|
throw BadLexicalCast(strf("Lexical cast failed on '{}'", str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
bool tryLexicalCast(bool& result, const char* first, const char* last) {
|
||||||
|
size_t len = last - first;
|
||||||
|
if (strncmp(first, "true", len) == 0)
|
||||||
|
result = true;
|
||||||
|
else if (strncmp(first, "false", len) != 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
result = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
bool lexicalCast(const char* first, const char* last) {
|
||||||
|
size_t len = last - first;
|
||||||
|
if (strncmp(first, "true", len) == 0)
|
||||||
|
return true;
|
||||||
|
else if (strncmp(first, "false", len) != 0)
|
||||||
|
throwLexicalCastError(std::errc(), first, last);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -19,6 +19,9 @@ bool tryLexicalCast(Type& result, const char* first, const char* last) {
|
|||||||
return res.ptr == last && (res.ec == std::errc() || res.ec == std::errc::result_out_of_range);
|
return res.ptr == last && (res.ec == std::errc() || res.ec == std::errc::result_out_of_range);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
bool tryLexicalCast(bool& result, const char* first, const char* last);
|
||||||
|
|
||||||
template <typename Type>
|
template <typename Type>
|
||||||
bool tryLexicalCast(Type& result, String const& s) {
|
bool tryLexicalCast(Type& result, String const& s) {
|
||||||
return tryLexicalCast<Type>(s.utf8Ptr(), s.utf8Ptr() + s.utf8Size());
|
return tryLexicalCast<Type>(s.utf8Ptr(), s.utf8Ptr() + s.utf8Size());
|
||||||
@ -43,7 +46,6 @@ Maybe<Type> maybeLexicalCast(StringView s) {
|
|||||||
return maybeLexicalCast<Type>(s.utf8Ptr(), s.utf8Ptr() + s.utf8Size());
|
return maybeLexicalCast<Type>(s.utf8Ptr(), s.utf8Ptr() + s.utf8Size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Type>
|
template <typename Type>
|
||||||
Type lexicalCast(const char* first, const char* last) {
|
Type lexicalCast(const char* first, const char* last) {
|
||||||
Type result{};
|
Type result{};
|
||||||
@ -54,6 +56,9 @@ Type lexicalCast(const char* first, const char* last) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
bool lexicalCast(const char* first, const char* last);
|
||||||
|
|
||||||
template <typename Type>
|
template <typename Type>
|
||||||
Type lexicalCast(StringView s) {
|
Type lexicalCast(StringView s) {
|
||||||
return lexicalCast<Type>(s.utf8Ptr(), s.utf8Ptr() + s.utf8Size());
|
return lexicalCast<Type>(s.utf8Ptr(), s.utf8Ptr() + s.utf8Size());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user