2011-10-30 05:19:41 +00:00
|
|
|
#pragma once
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2016-05-23 00:40:28 +00:00
|
|
|
#include <cmath>
|
2010-06-01 13:35:09 +00:00
|
|
|
#include <cstring>
|
2010-06-01 13:41:51 +00:00
|
|
|
#include <limits>
|
2010-05-28 19:13:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-01-08 16:33:28 +00:00
|
|
|
#include <type_traits>
|
2011-10-24 19:03:38 +00:00
|
|
|
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/Common.h>
|
|
|
|
#include <common/DateLUT.h>
|
2011-05-05 19:10:17 +00:00
|
|
|
|
2016-02-03 01:17:58 +00:00
|
|
|
#include <common/LocalDate.h>
|
|
|
|
#include <common/LocalDateTime.h>
|
2011-11-21 10:09:22 +00:00
|
|
|
|
2016-12-24 01:03:10 +00:00
|
|
|
#include <common/exp10.h>
|
|
|
|
|
2010-05-28 19:13:55 +00:00
|
|
|
#include <DB/Core/Types.h>
|
2016-09-28 18:45:28 +00:00
|
|
|
#include <DB/Core/StringRef.h>
|
2015-10-05 01:35:28 +00:00
|
|
|
#include <DB/Common/Exception.h>
|
2016-08-17 05:38:51 +00:00
|
|
|
#include <DB/Common/StringUtils.h>
|
2016-09-22 23:26:08 +00:00
|
|
|
#include <DB/Common/Arena.h>
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2010-06-04 18:25:25 +00:00
|
|
|
#include <DB/IO/ReadBuffer.h>
|
2017-01-03 01:42:17 +00:00
|
|
|
#include <DB/IO/ReadBufferFromMemory.h>
|
2011-08-19 18:31:14 +00:00
|
|
|
#include <DB/IO/VarInt.h>
|
2014-04-02 13:45:39 +00:00
|
|
|
#include <city.h>
|
2011-08-19 18:31:14 +00:00
|
|
|
|
|
|
|
#define DEFAULT_MAX_STRING_SIZE 0x00FFFFFFULL
|
2010-05-28 19:13:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_PARSE_DATE;
|
|
|
|
extern const int CANNOT_PARSE_DATETIME;
|
|
|
|
extern const int CANNOT_READ_ARRAY_FROM_TEXT;
|
|
|
|
}
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Helper functions for formatted input.
|
2010-06-01 13:35:09 +00:00
|
|
|
|
2015-11-25 03:11:17 +00:00
|
|
|
inline char parseEscapeSequence(char c)
|
2010-06-01 13:35:09 +00:00
|
|
|
{
|
|
|
|
switch(c)
|
2010-05-28 19:13:55 +00:00
|
|
|
{
|
2016-07-06 19:20:22 +00:00
|
|
|
case 'a':
|
|
|
|
return '\a';
|
2010-06-01 13:35:09 +00:00
|
|
|
case 'b':
|
|
|
|
return '\b';
|
|
|
|
case 'f':
|
|
|
|
return '\f';
|
|
|
|
case 'n':
|
|
|
|
return '\n';
|
|
|
|
case 'r':
|
|
|
|
return '\r';
|
|
|
|
case 't':
|
|
|
|
return '\t';
|
2016-07-06 19:20:22 +00:00
|
|
|
case 'v':
|
|
|
|
return '\v';
|
2010-06-01 13:35:09 +00:00
|
|
|
case '0':
|
|
|
|
return '\0';
|
|
|
|
default:
|
|
|
|
return c;
|
2010-05-28 19:13:55 +00:00
|
|
|
}
|
2010-06-01 13:35:09 +00:00
|
|
|
}
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2015-11-25 03:11:17 +00:00
|
|
|
inline char unhex(char c)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '0' ... '9':
|
|
|
|
return c - '0';
|
|
|
|
case 'a' ... 'f':
|
|
|
|
return c - 'a' + 10;
|
|
|
|
case 'A' ... 'F':
|
|
|
|
return c - 'A' + 10;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 18:57:49 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// These functions are located in VarInt.h
|
2014-11-15 18:57:49 +00:00
|
|
|
/// inline void throwReadAfterEOF()
|
2016-04-15 02:28:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
inline void readChar(char & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
if (!buf.eof())
|
|
|
|
{
|
|
|
|
x = *buf.position();
|
|
|
|
++buf.position();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throwReadAfterEOF();
|
|
|
|
}
|
2010-06-04 18:25:25 +00:00
|
|
|
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Read POD-type in native format
|
2011-07-04 18:22:37 +00:00
|
|
|
template <typename T>
|
2012-02-26 01:52:43 +00:00
|
|
|
inline void readPODBinary(T & x, ReadBuffer & buf)
|
2011-07-04 18:22:37 +00:00
|
|
|
{
|
|
|
|
buf.readStrict(reinterpret_cast<char *>(&x), sizeof(x));
|
|
|
|
}
|
|
|
|
|
2011-08-09 17:24:17 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void readIntBinary(T & x, ReadBuffer & buf)
|
|
|
|
{
|
2012-02-26 01:52:43 +00:00
|
|
|
readPODBinary(x, buf);
|
2011-08-09 17:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void readFloatBinary(T & x, ReadBuffer & buf)
|
|
|
|
{
|
2012-02-26 01:52:43 +00:00
|
|
|
readPODBinary(x, buf);
|
2011-08-09 17:24:17 +00:00
|
|
|
}
|
2011-07-04 18:22:37 +00:00
|
|
|
|
2011-08-19 18:31:14 +00:00
|
|
|
|
2013-10-10 23:06:51 +00:00
|
|
|
inline void readStringBinary(std::string & s, ReadBuffer & buf, size_t MAX_STRING_SIZE = DEFAULT_MAX_STRING_SIZE)
|
2011-08-19 18:31:14 +00:00
|
|
|
{
|
|
|
|
size_t size = 0;
|
2013-10-10 23:06:51 +00:00
|
|
|
readVarUInt(size, buf);
|
2011-08-19 18:31:14 +00:00
|
|
|
|
|
|
|
if (size > MAX_STRING_SIZE)
|
|
|
|
throw Poco::Exception("Too large string size.");
|
|
|
|
|
|
|
|
s.resize(size);
|
2013-02-18 08:43:48 +00:00
|
|
|
buf.readStrict(&s[0], size);
|
2011-08-19 18:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-22 23:26:08 +00:00
|
|
|
inline StringRef readStringBinaryInto(Arena & arena, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
readVarUInt(size, buf);
|
|
|
|
|
|
|
|
char * data = arena.alloc(size);
|
|
|
|
buf.readStrict(data, size);
|
|
|
|
|
|
|
|
return StringRef(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-10 20:33:08 +00:00
|
|
|
template <typename T>
|
|
|
|
void readVectorBinary(std::vector<T> & v, ReadBuffer & buf, size_t MAX_VECTOR_SIZE = DEFAULT_MAX_STRING_SIZE)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
2013-10-10 23:06:51 +00:00
|
|
|
readVarUInt(size, buf);
|
2013-10-10 20:33:08 +00:00
|
|
|
|
|
|
|
if (size > MAX_VECTOR_SIZE)
|
|
|
|
throw Poco::Exception("Too large vector size.");
|
|
|
|
|
|
|
|
v.resize(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
readBinary(v[i], buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-01 14:12:28 +00:00
|
|
|
void assertString(const char * s, ReadBuffer & buf);
|
2014-03-27 11:29:40 +00:00
|
|
|
void assertEOF(ReadBuffer & buf);
|
2015-06-03 15:32:06 +00:00
|
|
|
void assertChar(char symbol, ReadBuffer & buf);
|
2010-06-01 13:35:09 +00:00
|
|
|
|
2014-03-24 12:10:47 +00:00
|
|
|
inline void assertString(const String & s, ReadBuffer & buf)
|
2014-03-22 14:44:44 +00:00
|
|
|
{
|
|
|
|
assertString(s.c_str(), buf);
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:20:56 +00:00
|
|
|
bool checkString(const char * s, ReadBuffer & buf);
|
|
|
|
inline bool checkString(const String & s, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return checkString(s.c_str(), buf);
|
|
|
|
}
|
2011-11-21 10:09:22 +00:00
|
|
|
|
2015-11-19 21:48:17 +00:00
|
|
|
inline bool checkChar(char c, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
if (buf.eof() || *buf.position() != c)
|
|
|
|
return false;
|
|
|
|
++buf.position();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-30 05:13:14 +00:00
|
|
|
bool checkStringCaseInsensitive(const char * s, ReadBuffer & buf);
|
|
|
|
inline bool checkStringCaseInsensitive(const String & s, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return checkStringCaseInsensitive(s.c_str(), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assertStringCaseInsensitive(const char * s, ReadBuffer & buf);
|
|
|
|
inline void assertStringCaseInsensitive(const String & s, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return assertStringCaseInsensitive(s.c_str(), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check that next character in buf matches first character of s.
|
|
|
|
* If true, then check all characters in s and throw exception if it doesn't match.
|
|
|
|
* If false, then return false, and leave position in buffer unchanged.
|
|
|
|
*/
|
|
|
|
bool checkStringByFirstCharacterAndAssertTheRest(const char * s, ReadBuffer & buf);
|
|
|
|
bool checkStringByFirstCharacterAndAssertTheRestCaseInsensitive(const char * s, ReadBuffer & buf);
|
|
|
|
|
|
|
|
inline bool checkStringByFirstCharacterAndAssertTheRest(const String & s, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return checkStringByFirstCharacterAndAssertTheRest(s.c_str(), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool checkStringByFirstCharacterAndAssertTheRestCaseInsensitive(const String & s, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return checkStringByFirstCharacterAndAssertTheRestCaseInsensitive(s.c_str(), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-21 10:09:22 +00:00
|
|
|
inline void readBoolText(bool & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
char tmp = '0';
|
|
|
|
readChar(tmp, buf);
|
|
|
|
x = tmp != '0';
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:20:56 +00:00
|
|
|
template <typename T, typename ReturnType = void>
|
|
|
|
ReturnType readIntTextImpl(T & x, ReadBuffer & buf)
|
2010-06-01 13:35:09 +00:00
|
|
|
{
|
2015-10-05 14:20:56 +00:00
|
|
|
static constexpr bool throw_exception = std::is_same<ReturnType, void>::value;
|
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
bool negative = false;
|
|
|
|
x = 0;
|
2010-06-04 18:25:25 +00:00
|
|
|
if (buf.eof())
|
2015-10-05 14:20:56 +00:00
|
|
|
{
|
|
|
|
if (throw_exception)
|
|
|
|
throwReadAfterEOF();
|
|
|
|
else
|
|
|
|
return ReturnType(false);
|
|
|
|
}
|
2014-11-15 18:57:49 +00:00
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
while (!buf.eof())
|
|
|
|
{
|
|
|
|
switch (*buf.position())
|
2010-05-28 19:13:55 +00:00
|
|
|
{
|
2010-06-01 13:35:09 +00:00
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
case '-':
|
2017-03-13 18:01:46 +00:00
|
|
|
if (std::is_signed<T>::value)
|
2011-10-24 19:03:38 +00:00
|
|
|
negative = true;
|
|
|
|
else
|
2015-10-05 14:20:56 +00:00
|
|
|
return ReturnType(false);
|
2010-06-01 13:35:09 +00:00
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
x *= 10;
|
|
|
|
x += *buf.position() - '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (negative)
|
2010-05-28 19:13:55 +00:00
|
|
|
x = -x;
|
2015-10-05 14:20:56 +00:00
|
|
|
return ReturnType(true);
|
2010-05-28 19:13:55 +00:00
|
|
|
}
|
2010-06-01 13:35:09 +00:00
|
|
|
++buf.position();
|
2010-05-28 19:13:55 +00:00
|
|
|
}
|
2010-06-01 13:35:09 +00:00
|
|
|
if (negative)
|
|
|
|
x = -x;
|
2015-10-05 14:20:56 +00:00
|
|
|
|
|
|
|
return ReturnType(true);
|
2010-06-01 13:35:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 14:20:56 +00:00
|
|
|
template <typename T>
|
|
|
|
void readIntText(T & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
readIntTextImpl<T, void>(x, buf);
|
2010-06-01 13:35:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 14:20:56 +00:00
|
|
|
template <typename T>
|
|
|
|
bool tryReadIntText(T & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return readIntTextImpl<T, bool>(x, buf);
|
|
|
|
}
|
2013-01-05 14:16:05 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/** More efficient variant (about 1.5 times on real dataset).
|
|
|
|
* Differs in following:
|
|
|
|
* - for numbers starting with zero, parsed only zero;
|
|
|
|
* - symbol '+' before number is not supported;
|
|
|
|
* - symbols :;<=>? are parsed as some numbers.
|
2013-01-05 14:16:05 +00:00
|
|
|
*/
|
2016-03-03 14:00:57 +00:00
|
|
|
template <typename T, bool throw_on_error = true>
|
2013-01-05 14:16:05 +00:00
|
|
|
void readIntTextUnsafe(T & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
bool negative = false;
|
|
|
|
x = 0;
|
|
|
|
|
2016-03-03 14:00:57 +00:00
|
|
|
auto on_error = []
|
|
|
|
{
|
|
|
|
if (throw_on_error)
|
|
|
|
throwReadAfterEOF();
|
|
|
|
};
|
|
|
|
|
2013-01-05 14:16:05 +00:00
|
|
|
if (unlikely(buf.eof()))
|
2016-03-03 14:00:57 +00:00
|
|
|
return on_error();
|
2013-01-05 14:16:05 +00:00
|
|
|
|
2014-01-08 16:33:28 +00:00
|
|
|
if (std::is_signed<T>::value && *buf.position() == '-')
|
2013-01-05 14:16:05 +00:00
|
|
|
{
|
|
|
|
++buf.position();
|
|
|
|
negative = true;
|
2013-02-20 23:23:22 +00:00
|
|
|
if (unlikely(buf.eof()))
|
2016-03-03 14:00:57 +00:00
|
|
|
return on_error();
|
2013-01-05 14:16:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
if (*buf.position() == '0') /// There are many zeros in real datasets.
|
2013-01-05 14:16:05 +00:00
|
|
|
{
|
|
|
|
++buf.position();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!buf.eof())
|
|
|
|
{
|
2016-10-21 00:19:49 +00:00
|
|
|
if ((*buf.position() & 0xF0) == 0x30) /// It makes sense to have this condition inside loop.
|
2013-01-05 14:16:05 +00:00
|
|
|
{
|
|
|
|
x *= 10;
|
|
|
|
x += *buf.position() & 0x0F;
|
|
|
|
++buf.position();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-08 16:33:28 +00:00
|
|
|
if (std::is_signed<T>::value && negative)
|
2013-01-05 14:16:05 +00:00
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
|
2016-03-03 14:00:57 +00:00
|
|
|
template<typename T>
|
|
|
|
void tryReadIntTextUnsafe(T & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return readIntTextUnsafe<T, false>(x, buf);
|
|
|
|
}
|
|
|
|
|
2015-10-27 00:53:38 +00:00
|
|
|
|
2015-10-05 14:20:56 +00:00
|
|
|
template <bool throw_exception, class ExcepFun, class NoExcepFun, class... Args>
|
|
|
|
bool exceptionPolicySelector(ExcepFun && excep_f, NoExcepFun && no_excep_f, Args &&... args)
|
|
|
|
{
|
|
|
|
if (throw_exception)
|
|
|
|
{
|
|
|
|
excep_f(std::forward<Args>(args)...);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return no_excep_f(std::forward<Args>(args)...);
|
|
|
|
};
|
|
|
|
|
2013-01-05 14:16:05 +00:00
|
|
|
|
2016-08-16 21:23:53 +00:00
|
|
|
/// Returns true, iff parsed.
|
|
|
|
bool parseInfinity(ReadBuffer & buf);
|
|
|
|
bool parseNaN(ReadBuffer & buf);
|
|
|
|
|
|
|
|
void assertInfinity(ReadBuffer & buf);
|
|
|
|
void assertNaN(ReadBuffer & buf);
|
|
|
|
|
|
|
|
|
2016-08-16 20:21:28 +00:00
|
|
|
/// Rough: not exactly nearest machine representable number is returned.
|
2016-08-16 21:39:49 +00:00
|
|
|
/// Some garbage may be successfully parsed, examples: '.' parsed as 0; 123Inf parsed as inf.
|
2015-11-19 21:48:17 +00:00
|
|
|
template <typename T, typename ReturnType, char point_symbol = '.'>
|
2015-10-05 14:20:56 +00:00
|
|
|
ReturnType readFloatTextImpl(T & x, ReadBuffer & buf)
|
2010-06-01 13:35:09 +00:00
|
|
|
{
|
2015-10-05 14:20:56 +00:00
|
|
|
static constexpr bool throw_exception = std::is_same<ReturnType, void>::value;
|
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
bool negative = false;
|
|
|
|
x = 0;
|
|
|
|
bool after_point = false;
|
|
|
|
double power_of_ten = 1;
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2010-06-04 18:25:25 +00:00
|
|
|
if (buf.eof())
|
2015-10-06 12:04:50 +00:00
|
|
|
{
|
2015-10-06 10:40:24 +00:00
|
|
|
if (throw_exception)
|
|
|
|
throwReadAfterEOF();
|
|
|
|
else
|
|
|
|
return ReturnType(false);
|
2015-10-06 12:04:50 +00:00
|
|
|
}
|
2010-06-04 18:25:25 +00:00
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
while (!buf.eof())
|
2010-05-28 19:13:55 +00:00
|
|
|
{
|
2010-06-01 13:35:09 +00:00
|
|
|
switch (*buf.position())
|
2010-05-28 19:13:55 +00:00
|
|
|
{
|
2010-06-01 13:35:09 +00:00
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
negative = true;
|
|
|
|
break;
|
2015-11-20 15:41:39 +00:00
|
|
|
case point_symbol:
|
|
|
|
after_point = true;
|
|
|
|
break;
|
2010-06-01 13:35:09 +00:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
if (after_point)
|
|
|
|
{
|
|
|
|
power_of_ten /= 10;
|
|
|
|
x += (*buf.position() - '0') * power_of_ten;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x *= 10;
|
|
|
|
x += *buf.position() - '0';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
2010-05-28 19:13:55 +00:00
|
|
|
{
|
2010-06-01 13:35:09 +00:00
|
|
|
++buf.position();
|
|
|
|
Int32 exponent = 0;
|
2015-10-05 14:20:56 +00:00
|
|
|
bool res = exceptionPolicySelector<throw_exception>(readIntText<Int32>, tryReadIntText<Int32>, exponent, buf);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
x *= exp10(exponent);
|
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
return ReturnType(res);
|
2010-05-28 19:13:55 +00:00
|
|
|
}
|
2015-10-05 14:20:56 +00:00
|
|
|
|
2016-08-16 21:23:53 +00:00
|
|
|
case 'i':
|
2010-06-01 13:35:09 +00:00
|
|
|
case 'I':
|
2016-08-16 21:23:53 +00:00
|
|
|
{
|
|
|
|
bool res = exceptionPolicySelector<throw_exception>(assertInfinity, parseInfinity, buf);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
x = std::numeric_limits<T>::infinity();
|
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
return ReturnType(res);
|
|
|
|
}
|
2015-10-05 14:20:56 +00:00
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
case 'n':
|
|
|
|
case 'N':
|
2016-08-16 21:23:53 +00:00
|
|
|
{
|
|
|
|
bool res = exceptionPolicySelector<throw_exception>(assertNaN, parseNaN, buf);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
x = std::numeric_limits<T>::quiet_NaN();
|
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
return ReturnType(res);
|
|
|
|
}
|
2015-10-05 14:20:56 +00:00
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
default:
|
2016-08-16 21:23:53 +00:00
|
|
|
{
|
2010-06-01 13:35:09 +00:00
|
|
|
if (negative)
|
|
|
|
x = -x;
|
2015-10-05 14:20:56 +00:00
|
|
|
return ReturnType(true);
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
2010-05-28 19:13:55 +00:00
|
|
|
}
|
2010-06-01 13:35:09 +00:00
|
|
|
++buf.position();
|
2010-05-28 19:13:55 +00:00
|
|
|
}
|
2016-08-16 21:23:53 +00:00
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
if (negative)
|
|
|
|
x = -x;
|
2015-10-05 14:20:56 +00:00
|
|
|
|
|
|
|
return ReturnType(true);
|
2010-06-01 13:35:09 +00:00
|
|
|
}
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2015-10-05 14:20:56 +00:00
|
|
|
template <class T>
|
|
|
|
inline bool tryReadFloatText(T & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
return readFloatTextImpl<T, bool>(x, buf);
|
2010-06-01 13:35:09 +00:00
|
|
|
}
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2015-10-05 14:20:56 +00:00
|
|
|
template <class T>
|
|
|
|
inline void readFloatText(T & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
readFloatTextImpl<T, void>(x, buf);
|
|
|
|
}
|
2011-08-09 17:24:17 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// rough; all until '\n' or '\t'
|
2010-06-01 14:12:28 +00:00
|
|
|
void readString(String & s, ReadBuffer & buf);
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2010-06-01 14:12:28 +00:00
|
|
|
void readEscapedString(String & s, ReadBuffer & buf);
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2010-06-01 14:12:28 +00:00
|
|
|
void readQuotedString(String & s, ReadBuffer & buf);
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2011-06-15 18:54:18 +00:00
|
|
|
void readDoubleQuotedString(String & s, ReadBuffer & buf);
|
|
|
|
|
2016-02-18 11:44:50 +00:00
|
|
|
void readJSONString(String & s, ReadBuffer & buf);
|
|
|
|
|
2011-11-01 17:57:37 +00:00
|
|
|
void readBackQuotedString(String & s, ReadBuffer & buf);
|
|
|
|
|
2015-09-08 14:24:25 +00:00
|
|
|
void readStringUntilEOF(String & s, ReadBuffer & buf);
|
|
|
|
|
2010-05-28 19:13:55 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/** Read string in CSV format.
|
|
|
|
* Parsing rules:
|
|
|
|
* - string could be placed in quotes; quotes could be single: ' or double: ";
|
|
|
|
* - or string could be unquoted - this is determined by first character;
|
|
|
|
* - if string is unquoted, then it is read until next delimiter,
|
|
|
|
* either until end of line (CR or LF),
|
|
|
|
* or until end of stream;
|
|
|
|
* but spaces and tabs at begin and end of unquoted string are consumed but ignored (note that this behaviour differs from RFC).
|
|
|
|
* - if string is in quotes, then it will be read until closing quote,
|
|
|
|
* but sequences of two consecutive quotes are parsed as single quote inside string;
|
2016-02-07 08:42:21 +00:00
|
|
|
*/
|
|
|
|
void readCSVString(String & s, ReadBuffer & buf, const char delimiter = ',');
|
|
|
|
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Read and append result to array of characters.
|
2016-02-16 16:39:39 +00:00
|
|
|
template <typename Vector>
|
|
|
|
void readStringInto(Vector & s, ReadBuffer & buf);
|
|
|
|
|
|
|
|
template <typename Vector>
|
|
|
|
void readEscapedStringInto(Vector & s, ReadBuffer & buf);
|
|
|
|
|
|
|
|
template <typename Vector>
|
|
|
|
void readQuotedStringInto(Vector & s, ReadBuffer & buf);
|
|
|
|
|
|
|
|
template <typename Vector>
|
|
|
|
void readDoubleQuotedStringInto(Vector & s, ReadBuffer & buf);
|
|
|
|
|
|
|
|
template <typename Vector>
|
|
|
|
void readBackQuotedStringInto(Vector & s, ReadBuffer & buf);
|
|
|
|
|
|
|
|
template <typename Vector>
|
|
|
|
void readStringUntilEOFInto(Vector & s, ReadBuffer & buf);
|
|
|
|
|
|
|
|
template <typename Vector>
|
|
|
|
void readCSVStringInto(Vector & s, ReadBuffer & buf, const char delimiter = ',');
|
|
|
|
|
2016-02-18 11:44:50 +00:00
|
|
|
template <typename Vector>
|
|
|
|
void readJSONStringInto(Vector & s, ReadBuffer & buf);
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// This could be used as template parameter for functions above, if you want to just skip data.
|
2016-02-19 16:59:31 +00:00
|
|
|
struct NullSink
|
|
|
|
{
|
|
|
|
void append(const char *, size_t) {};
|
|
|
|
void push_back(char) {};
|
|
|
|
};
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// In YYYY-MM-DD format
|
2013-08-11 03:40:14 +00:00
|
|
|
inline void readDateText(DayNum_t & date, ReadBuffer & buf)
|
2011-05-05 19:10:17 +00:00
|
|
|
{
|
|
|
|
char s[10];
|
2011-05-27 18:46:29 +00:00
|
|
|
size_t size = buf.read(s, 10);
|
|
|
|
if (10 != size)
|
|
|
|
{
|
|
|
|
s[size] = 0;
|
|
|
|
throw Exception(std::string("Cannot parse date ") + s, ErrorCodes::CANNOT_PARSE_DATE);
|
|
|
|
}
|
2011-05-05 19:10:17 +00:00
|
|
|
|
|
|
|
UInt16 year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
|
|
|
|
UInt8 month = (s[5] - '0') * 10 + (s[6] - '0');
|
|
|
|
UInt8 day = (s[8] - '0') * 10 + (s[9] - '0');
|
|
|
|
|
2014-07-08 23:52:53 +00:00
|
|
|
date = DateLUT::instance().makeDayNum(year, month, day);
|
2011-05-05 19:10:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readDateText(LocalDate & date, ReadBuffer & buf)
|
2011-11-21 10:09:22 +00:00
|
|
|
{
|
|
|
|
char s[10];
|
|
|
|
size_t size = buf.read(s, 10);
|
|
|
|
if (10 != size)
|
|
|
|
{
|
|
|
|
s[size] = 0;
|
|
|
|
throw Exception(std::string("Cannot parse date ") + s, ErrorCodes::CANNOT_PARSE_DATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
date.year((s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0'));
|
|
|
|
date.month((s[5] - '0') * 10 + (s[6] - '0'));
|
|
|
|
date.day((s[8] - '0') * 10 + (s[9] - '0'));
|
|
|
|
}
|
|
|
|
|
2011-08-07 02:08:22 +00:00
|
|
|
|
2015-03-31 21:09:19 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T parse(const char * data, size_t size);
|
|
|
|
|
|
|
|
|
2017-01-22 08:33:16 +00:00
|
|
|
void readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut);
|
2015-04-01 02:55:52 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/** In YYYY-MM-DD hh:mm:ss format, according to current time zone.
|
|
|
|
* As an exception, also supported parsing of unix timestamp in form of decimal number.
|
2015-03-31 21:09:19 +00:00
|
|
|
*/
|
2017-01-22 08:33:16 +00:00
|
|
|
inline void readDateTimeText(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance())
|
2011-08-07 02:08:22 +00:00
|
|
|
{
|
2016-10-21 00:19:49 +00:00
|
|
|
/** Read 10 characters, that could represent unix timestamp.
|
|
|
|
* Only unix timestamp of 5-10 characters is supported.
|
|
|
|
* Then look at 5th charater. If it is a number - treat whole as unix timestamp.
|
|
|
|
* If it is not a number - then parse datetime in YYYY-MM-DD hh:mm:ss format.
|
2015-03-31 21:09:19 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Optimistic path, when whole value are in buffer.
|
2015-04-01 02:55:52 +00:00
|
|
|
const char * s = buf.position();
|
|
|
|
if (s + 19 < buf.buffer().end())
|
2015-03-31 21:09:19 +00:00
|
|
|
{
|
2015-04-01 02:55:52 +00:00
|
|
|
if (s[4] < '0' || s[4] > '9')
|
2015-03-31 21:09:19 +00:00
|
|
|
{
|
2015-04-01 02:55:52 +00:00
|
|
|
UInt16 year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
|
|
|
|
UInt8 month = (s[5] - '0') * 10 + (s[6] - '0');
|
|
|
|
UInt8 day = (s[8] - '0') * 10 + (s[9] - '0');
|
2015-03-31 21:09:19 +00:00
|
|
|
|
2015-04-01 02:55:52 +00:00
|
|
|
UInt8 hour = (s[11] - '0') * 10 + (s[12] - '0');
|
|
|
|
UInt8 minute = (s[14] - '0') * 10 + (s[15] - '0');
|
|
|
|
UInt8 second = (s[17] - '0') * 10 + (s[18] - '0');
|
2011-08-07 02:08:22 +00:00
|
|
|
|
2015-04-01 02:55:52 +00:00
|
|
|
if (unlikely(year == 0))
|
|
|
|
datetime = 0;
|
|
|
|
else
|
2017-01-22 08:33:16 +00:00
|
|
|
datetime = date_lut.makeDateTime(year, month, day, hour, minute, second);
|
2011-08-07 02:08:22 +00:00
|
|
|
|
2015-04-01 02:55:52 +00:00
|
|
|
buf.position() += 19;
|
|
|
|
}
|
2015-03-31 21:09:19 +00:00
|
|
|
else
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Why not readIntTextUnsafe? Because for needs of AdFox, parsing of unix timestamp with leading zeros is supported: 000...NNNN.
|
2015-10-27 00:53:38 +00:00
|
|
|
readIntText(datetime, buf);
|
2015-03-31 21:09:19 +00:00
|
|
|
}
|
2012-06-07 20:02:41 +00:00
|
|
|
else
|
2017-01-22 08:33:16 +00:00
|
|
|
readDateTimeTextFallback(datetime, buf, date_lut);
|
2011-08-07 02:08:22 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readDateTimeText(LocalDateTime & datetime, ReadBuffer & buf)
|
2011-11-21 10:09:22 +00:00
|
|
|
{
|
|
|
|
char s[19];
|
|
|
|
size_t size = buf.read(s, 19);
|
|
|
|
if (19 != size)
|
|
|
|
{
|
|
|
|
s[size] = 0;
|
|
|
|
throw Exception(std::string("Cannot parse datetime ") + s, ErrorCodes::CANNOT_PARSE_DATETIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
datetime.year((s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0'));
|
|
|
|
datetime.month((s[5] - '0') * 10 + (s[6] - '0'));
|
|
|
|
datetime.day((s[8] - '0') * 10 + (s[9] - '0'));
|
|
|
|
|
|
|
|
datetime.hour((s[11] - '0') * 10 + (s[12] - '0'));
|
|
|
|
datetime.minute((s[14] - '0') * 10 + (s[15] - '0'));
|
|
|
|
datetime.second((s[17] - '0') * 10 + (s[18] - '0'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Generic methods to read value in native binary format.
|
2017-03-13 19:23:56 +00:00
|
|
|
template <typename T>
|
|
|
|
inline typename std::enable_if<std::is_arithmetic<T>::value, void>::type
|
|
|
|
readBinary(T & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
|
|
|
|
2012-02-26 01:52:43 +00:00
|
|
|
inline void readBinary(String & x, ReadBuffer & buf) { readStringBinary(x, buf); }
|
2014-04-02 13:45:39 +00:00
|
|
|
inline void readBinary(uint128 & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readBinary(LocalDate & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
|
|
|
inline void readBinary(LocalDateTime & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
2011-11-21 10:09:22 +00:00
|
|
|
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Generic methods to read value in text tab-separated format.
|
2017-03-13 19:23:56 +00:00
|
|
|
template <typename T>
|
|
|
|
inline typename std::enable_if<std::is_integral<T>::value, void>::type
|
|
|
|
readText(T & x, ReadBuffer & buf) { readIntText(x, buf); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline typename std::enable_if<std::is_floating_point<T>::value, void>::type
|
|
|
|
readText(T & x, ReadBuffer & buf) { readFloatText(x, buf); }
|
2012-02-26 01:52:43 +00:00
|
|
|
|
2017-03-13 19:23:56 +00:00
|
|
|
inline void readText(bool & x, ReadBuffer & buf) { readBoolText(x, buf); }
|
|
|
|
inline void readText(String & x, ReadBuffer & buf) { readEscapedString(x, buf); }
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readText(LocalDate & x, ReadBuffer & buf) { readDateText(x, buf); }
|
|
|
|
inline void readText(LocalDateTime & x, ReadBuffer & buf) { readDateTimeText(x, buf); }
|
2011-08-07 02:08:22 +00:00
|
|
|
|
2012-01-24 16:45:50 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Generic methods to read value in text format,
|
|
|
|
/// possibly in single quotes (only for data types that use quotes in VALUES format of INSERT statement in SQL).
|
2017-03-13 19:23:56 +00:00
|
|
|
template <typename T>
|
|
|
|
inline typename std::enable_if<std::is_arithmetic<T>::value, void>::type
|
|
|
|
readQuoted(T & x, ReadBuffer & buf) { readText(x, buf); }
|
2012-02-26 01:52:43 +00:00
|
|
|
|
2017-03-13 19:23:56 +00:00
|
|
|
inline void readQuoted(String & x, ReadBuffer & buf) { readQuotedString(x, buf); }
|
2012-02-26 01:52:43 +00:00
|
|
|
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readQuoted(LocalDate & x, ReadBuffer & buf)
|
2012-01-24 16:45:50 +00:00
|
|
|
{
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('\'', buf);
|
2012-01-24 16:45:50 +00:00
|
|
|
readDateText(x, buf);
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('\'', buf);
|
2012-01-24 16:45:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readQuoted(LocalDateTime & x, ReadBuffer & buf)
|
2012-01-24 16:45:50 +00:00
|
|
|
{
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('\'', buf);
|
2012-01-24 16:45:50 +00:00
|
|
|
readDateTimeText(x, buf);
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('\'', buf);
|
2012-01-24 16:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Same as above, but in double quotes.
|
2017-03-13 19:23:56 +00:00
|
|
|
template <typename T>
|
|
|
|
inline typename std::enable_if<std::is_arithmetic<T>::value, void>::type
|
|
|
|
readDoubleQuoted(T & x, ReadBuffer & buf) { readText(x, buf); }
|
2012-11-06 13:58:29 +00:00
|
|
|
|
2017-03-13 19:23:56 +00:00
|
|
|
inline void readDoubleQuoted(String & x, ReadBuffer & buf) { readDoubleQuotedString(x, buf); }
|
2012-11-06 13:58:29 +00:00
|
|
|
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readDoubleQuoted(LocalDate & x, ReadBuffer & buf)
|
2012-11-06 13:58:29 +00:00
|
|
|
{
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('"', buf);
|
2012-11-06 13:58:29 +00:00
|
|
|
readDateText(x, buf);
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('"', buf);
|
2012-11-06 13:58:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 01:17:58 +00:00
|
|
|
inline void readDoubleQuoted(LocalDateTime & x, ReadBuffer & buf)
|
2012-11-06 13:58:29 +00:00
|
|
|
{
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('"', buf);
|
2012-11-06 13:58:29 +00:00
|
|
|
readDateTimeText(x, buf);
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('"', buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// CSV, for numbers, dates, datetimes: quotes are optional, no special escaping rules.
|
2016-02-07 08:42:21 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void readCSVSimple(T & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
if (buf.eof())
|
|
|
|
throwReadAfterEOF();
|
|
|
|
|
|
|
|
char maybe_quote = *buf.position();
|
|
|
|
|
|
|
|
if (maybe_quote == '\'' || maybe_quote == '\"')
|
|
|
|
++buf.position();
|
|
|
|
|
|
|
|
readText(x, buf);
|
|
|
|
|
|
|
|
if (maybe_quote == '\'' || maybe_quote == '\"')
|
|
|
|
assertChar(maybe_quote, buf);
|
2012-11-06 13:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 19:23:56 +00:00
|
|
|
template <typename T>
|
|
|
|
inline typename std::enable_if<std::is_arithmetic<T>::value, void>::type
|
|
|
|
readCSV(T & x, ReadBuffer & buf) { readCSVSimple(x, buf); }
|
|
|
|
|
2016-02-07 08:42:21 +00:00
|
|
|
inline void readCSV(String & x, ReadBuffer & buf, const char delimiter = ',') { readCSVString(x, buf, delimiter); }
|
|
|
|
inline void readCSV(LocalDate & x, ReadBuffer & buf) { readCSVSimple(x, buf); }
|
|
|
|
inline void readCSV(LocalDateTime & x, ReadBuffer & buf) { readCSVSimple(x, buf); }
|
|
|
|
|
2012-11-06 13:58:29 +00:00
|
|
|
|
2014-01-05 13:40:11 +00:00
|
|
|
template <typename T>
|
|
|
|
void readBinary(std::vector<T> & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
readVarUInt(size, buf);
|
|
|
|
|
|
|
|
if (size > DEFAULT_MAX_STRING_SIZE)
|
|
|
|
throw Poco::Exception("Too large vector size.");
|
|
|
|
|
|
|
|
x.resize(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
readBinary(x[i], buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void readQuoted(std::vector<T> & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
bool first = true;
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('[', buf);
|
2014-01-05 13:40:11 +00:00
|
|
|
while (!buf.eof() && *buf.position() != ']')
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
if (*buf.position() == ',')
|
|
|
|
++buf.position();
|
|
|
|
else
|
|
|
|
throw Exception("Cannot read array from text", ErrorCodes::CANNOT_READ_ARRAY_FROM_TEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
x.push_back(T());
|
|
|
|
readQuoted(x.back(), buf);
|
|
|
|
}
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar(']', buf);
|
2014-01-05 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void readDoubleQuoted(std::vector<T> & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
bool first = true;
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar('[', buf);
|
2014-01-05 13:40:11 +00:00
|
|
|
while (!buf.eof() && *buf.position() != ']')
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
if (*buf.position() == ',')
|
|
|
|
++buf.position();
|
|
|
|
else
|
|
|
|
throw Exception("Cannot read array from text", ErrorCodes::CANNOT_READ_ARRAY_FROM_TEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
x.push_back(T());
|
|
|
|
readDoubleQuoted(x.back(), buf);
|
|
|
|
}
|
2016-02-07 08:42:21 +00:00
|
|
|
assertChar(']', buf);
|
2014-01-05 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void readText(std::vector<T> & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
readQuoted(x, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Skip whitespace characters.
|
2011-10-30 05:19:41 +00:00
|
|
|
inline void skipWhitespaceIfAny(ReadBuffer & buf)
|
|
|
|
{
|
2016-08-17 05:38:51 +00:00
|
|
|
while (!buf.eof() && isWhitespaceASCII(*buf.position()))
|
2011-10-30 05:19:41 +00:00
|
|
|
++buf.position();
|
2010-05-28 19:13:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 13:11:03 +00:00
|
|
|
/// Skips json value. If the value contains objects (i.e. {...} sequence), an exception will be thrown.
|
2016-09-28 18:45:28 +00:00
|
|
|
void skipJSONFieldPlain(ReadBuffer & buf, const StringRef & name_of_filed);
|
2016-09-20 19:11:25 +00:00
|
|
|
|
2012-05-08 05:42:05 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/** Read serialized exception.
|
|
|
|
* During serialization/deserialization some information is lost
|
|
|
|
* (type is cut to base class, 'message' replaced by 'displayText', and stack trace is appended to 'message')
|
|
|
|
* Some additional message could be appended to exception (example: you could add information about from where it was received).
|
2012-05-08 05:42:05 +00:00
|
|
|
*/
|
|
|
|
void readException(Exception & e, ReadBuffer & buf, const String & additional_message = "");
|
|
|
|
void readAndThrowException(ReadBuffer & buf, const String & additional_message = "");
|
|
|
|
|
2012-11-19 18:28:22 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/** Helper function for implementation.
|
|
|
|
*/
|
2012-11-19 18:28:22 +00:00
|
|
|
template <typename T>
|
|
|
|
static inline const char * tryReadIntText(T & x, const char * pos, const char * end)
|
|
|
|
{
|
|
|
|
bool negative = false;
|
|
|
|
x = 0;
|
|
|
|
if (pos >= end)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
switch (*pos)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
case '-':
|
2014-01-08 16:33:28 +00:00
|
|
|
if (std::is_signed<T>::value)
|
2012-11-19 18:28:22 +00:00
|
|
|
negative = true;
|
|
|
|
else
|
|
|
|
return pos;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
x *= 10;
|
|
|
|
x += *pos - '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:34:19 +00:00
|
|
|
|
2016-10-21 00:19:49 +00:00
|
|
|
/// Convenient methods for reading something from string in text format.
|
2013-06-21 20:34:19 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T parse(const char * data, size_t size)
|
|
|
|
{
|
|
|
|
T res;
|
2017-01-03 01:42:17 +00:00
|
|
|
ReadBufferFromMemory buf(data, size);
|
2013-06-21 20:34:19 +00:00
|
|
|
readText(res, buf);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-06-21 21:05:16 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T parse(const char * data)
|
|
|
|
{
|
|
|
|
return parse<T>(data, strlen(data));
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:34:19 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T parse(const String & s)
|
|
|
|
{
|
|
|
|
return parse<T>(s.data(), s.size());
|
|
|
|
}
|
|
|
|
|
2016-06-23 19:39:20 +00:00
|
|
|
|
|
|
|
/** Skip UTF-8 BOM if it is under cursor.
|
|
|
|
* As BOM is usually located at start of stream, and buffer size is usually larger than three bytes,
|
|
|
|
* the function expects, that all three bytes of BOM is fully in buffer (otherwise it don't skip anything).
|
|
|
|
*/
|
|
|
|
inline void skipBOMIfExists(ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
if (!buf.eof()
|
|
|
|
&& buf.position() + 3 < buf.buffer().end()
|
|
|
|
&& buf.position()[0] == '\xEF'
|
|
|
|
&& buf.position()[1] == '\xBB'
|
|
|
|
&& buf.position()[2] == '\xBF')
|
|
|
|
{
|
|
|
|
buf.position() += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 04:29:47 +00:00
|
|
|
|
|
|
|
/// Skip to next character after next \n. If no \n in stream, skip to end.
|
|
|
|
void skipToNextLineOrEOF(ReadBuffer & buf);
|
|
|
|
|
|
|
|
/// Skip to next character after next unescaped \n. If no \n in stream, skip to end. Does not throw on invalid escape sequences.
|
|
|
|
void skipToUnescapedNextLineOrEOF(ReadBuffer & buf);
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|