ClickHouse/dbms/src/IO/readFloatText.cpp

50 lines
1.1 KiB
C++
Raw Normal View History

2018-01-13 04:43:10 +00:00
#include <IO/readFloatText.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_PARSE_INPUT_ASSERTION_FAILED;
}
/** Must successfully parse inf, INF and Infinity.
* All other variants in different cases are also parsed for simplicity.
*/
bool parseInfinity(ReadBuffer & buf)
{
if (!checkStringCaseInsensitive("inf", buf))
return false;
/// Just inf.
if (buf.eof() || !isWordCharASCII(*buf.position()))
return true;
/// If word characters after inf, it should be infinity.
return checkStringCaseInsensitive("inity", buf);
}
/** Must successfully parse nan, NAN and NaN.
* All other variants in different cases are also parsed for simplicity.
*/
bool parseNaN(ReadBuffer & buf)
{
return checkStringCaseInsensitive("nan", buf);
}
void assertInfinity(ReadBuffer & buf)
{
if (!parseInfinity(buf))
throw Exception("Cannot parse infinity.", ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED);
}
void assertNaN(ReadBuffer & buf)
{
if (!parseNaN(buf))
throw Exception("Cannot parse NaN.", ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED);
}
}