2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-08-27 10:59:01 +02:00
|
|
|
|
2017-09-23 01:53:46 +02:00
|
|
|
/// !!! NOTE !!!
|
|
|
|
///
|
|
|
|
/// Many functions implemented here need to be replaced with karma generators
|
|
|
|
/// similar to ircd::fmt. Both the boost and std lexical conversions are an
|
|
|
|
/// order of magnitude slower than the formal generators. Some tokenizations
|
|
|
|
/// can also be replaced.
|
|
|
|
///
|
|
|
|
|
2018-02-06 06:23:34 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2016-08-31 08:58:07 +02:00
|
|
|
|
2019-03-01 20:47:13 +01:00
|
|
|
decltype(ircd::LEX_CAST_BUFS)
|
|
|
|
ircd::LEX_CAST_BUFS
|
|
|
|
{
|
|
|
|
64 // plenty
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::LEX_CAST_BUFSIZE)
|
|
|
|
ircd::LEX_CAST_BUFSIZE
|
2017-09-23 00:50:28 +02:00
|
|
|
{
|
2019-03-01 20:47:13 +01:00
|
|
|
64
|
|
|
|
};
|
2017-09-23 00:50:28 +02:00
|
|
|
|
2019-03-01 20:47:13 +01:00
|
|
|
namespace ircd
|
|
|
|
{
|
2017-10-02 06:14:34 +02:00
|
|
|
/// This is a static "ring buffer" to simplify a majority of lex_cast uses.
|
|
|
|
/// If the lex_cast has binary input and string output, and no user buffer
|
|
|
|
/// is supplied, the next buffer here will be used instead. The returned
|
|
|
|
/// string_view of data from this buffer is only valid for several more
|
|
|
|
/// calls to lex_cast before it is overwritten.
|
|
|
|
thread_local char lex_cast_buf[LEX_CAST_BUFS][LEX_CAST_BUFSIZE];
|
|
|
|
thread_local uint lex_cast_cur;
|
2017-09-23 00:50:28 +02:00
|
|
|
|
2018-03-16 20:42:49 +01:00
|
|
|
[[noreturn]] static void throw_bad_lex_cast(const boost::bad_lexical_cast &, const std::type_info &);
|
2017-10-17 09:41:51 +02:00
|
|
|
template<size_t N, class T> static string_view _lex_cast(const T &i, mutable_buffer buf);
|
2017-10-02 06:14:34 +02:00
|
|
|
template<class T> static T _lex_cast(const string_view &s);
|
2017-09-23 00:50:28 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Internal template providing conversions from a number to a string;
|
|
|
|
/// potentially using the ring buffer if no user buffer is supplied.
|
2017-03-14 19:39:26 +01:00
|
|
|
template<size_t N,
|
|
|
|
class T>
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::_lex_cast(const T &i,
|
2017-10-17 09:41:51 +02:00
|
|
|
mutable_buffer buf)
|
2017-03-16 21:31:17 +01:00
|
|
|
try
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
using array = std::array<char, N>;
|
|
|
|
|
|
|
|
if(!buf)
|
|
|
|
{
|
2017-10-02 06:14:34 +02:00
|
|
|
buf = lex_cast_buf[lex_cast_cur++];
|
|
|
|
lex_cast_cur %= LEX_CAST_BUFS;
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 09:41:51 +02:00
|
|
|
assert(size(buf) >= N);
|
|
|
|
auto &a(*reinterpret_cast<array *>(data(buf)));
|
2017-03-14 19:39:26 +01:00
|
|
|
a = boost::lexical_cast<array>(i);
|
2017-10-17 09:41:51 +02:00
|
|
|
return { data(buf), strnlen(data(buf), size(buf)) };
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
2017-03-16 21:31:17 +01:00
|
|
|
catch(const boost::bad_lexical_cast &e)
|
|
|
|
{
|
2018-03-16 20:42:49 +01:00
|
|
|
throw_bad_lex_cast(e, typeid(T));
|
2017-03-16 21:31:17 +01:00
|
|
|
}
|
2017-03-14 19:39:26 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Internal template providing conversions from a string to a number;
|
|
|
|
/// the native object is returned directly; no ring buffer is consumed.
|
2017-03-14 19:39:26 +01:00
|
|
|
template<class T>
|
2017-10-02 06:14:34 +02:00
|
|
|
T
|
|
|
|
ircd::_lex_cast(const string_view &s)
|
2017-03-16 21:31:17 +01:00
|
|
|
try
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
2018-05-29 11:26:01 +02:00
|
|
|
return boost::lexical_cast<T>(data(s), size(s));
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
2017-03-16 21:31:17 +01:00
|
|
|
catch(const boost::bad_lexical_cast &e)
|
|
|
|
{
|
2018-03-16 20:42:49 +01:00
|
|
|
throw_bad_lex_cast(e, typeid(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::throw_bad_lex_cast(const boost::bad_lexical_cast &e,
|
|
|
|
const std::type_info &t)
|
|
|
|
{
|
|
|
|
throw ircd::bad_lex_cast
|
|
|
|
{
|
|
|
|
"%s: %s", e.what(), demangle(t.name())
|
|
|
|
};
|
2017-03-16 21:31:17 +01:00
|
|
|
}
|
2017-03-14 19:39:26 +01:00
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(bool i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(8);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(int8_t i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(8);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(uint8_t i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(8);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(short i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(8);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(ushort i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(8);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(int i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(16);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(uint i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(16);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(long i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(32);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(ulong i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(32);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(double i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(64);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(long double i,
|
2017-10-17 09:41:51 +02:00
|
|
|
const mutable_buffer &buf)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX(64);
|
2017-10-17 09:41:51 +02:00
|
|
|
return _lex_cast<MAX>(i, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(nanoseconds i,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
static const size_t MAX(64);
|
|
|
|
return _lex_cast<MAX>(i.count(), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(microseconds i,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
static const size_t MAX(64);
|
|
|
|
return _lex_cast<MAX>(i.count(), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(milliseconds i,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
static const size_t MAX(64);
|
|
|
|
return _lex_cast<MAX>(i.count(), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::string_view
|
|
|
|
ircd::lex_cast(seconds i,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
static const size_t MAX(64);
|
|
|
|
return _lex_cast<MAX>(i.count(), buf);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
2017-10-16 06:25:03 +02:00
|
|
|
return s == "true"? true:
|
|
|
|
s == "false"? false:
|
|
|
|
_lex_cast<bool>(s);
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> int8_t
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<char>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> uint8_t
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<unsigned char>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> short
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<short>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> unsigned short
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<unsigned short>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> int
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<int>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> unsigned int
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<unsigned int>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> long
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<long>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> unsigned long
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<unsigned long>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> double
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<double>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> long double
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return _lex_cast<long double>(s);
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:41:51 +02:00
|
|
|
template<> ircd::nanoseconds
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return std::chrono::duration<time_t, std::ratio<1L, 1000000000L>>(_lex_cast<time_t>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::microseconds
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return std::chrono::duration<time_t, std::ratio<1L, 1000000L>>(_lex_cast<time_t>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::milliseconds
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return std::chrono::duration<time_t, std::ratio<1L, 1000L>>(_lex_cast<time_t>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> ircd::seconds
|
|
|
|
ircd::lex_cast(const string_view &s)
|
|
|
|
{
|
|
|
|
return std::chrono::duration<time_t, std::ratio<1L, 1L>>(_lex_cast<time_t>(s));
|
|
|
|
}
|
|
|
|
|
2017-03-14 19:39:26 +01:00
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<bool>(const string_view &s)
|
|
|
|
{
|
|
|
|
bool i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<int8_t>(const string_view &s)
|
|
|
|
{
|
|
|
|
int8_t i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<uint8_t>(const string_view &s)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<short>(const string_view &s)
|
|
|
|
{
|
|
|
|
short i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<ushort>(const string_view &s)
|
|
|
|
{
|
|
|
|
ushort i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<int>(const string_view &s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<unsigned int>(const string_view &s)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<long>(const string_view &s)
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<unsigned long>(const string_view &s)
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<double>(const string_view &s)
|
|
|
|
{
|
|
|
|
double i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<long double>(const string_view &s)
|
|
|
|
{
|
|
|
|
long double i;
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:41:51 +02:00
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<ircd::nanoseconds>(const string_view &s)
|
|
|
|
{
|
|
|
|
time_t i; //TODO: XXX
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<ircd::microseconds>(const string_view &s)
|
|
|
|
{
|
|
|
|
time_t i; //TODO: XXX
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<ircd::milliseconds>(const string_view &s)
|
|
|
|
{
|
|
|
|
time_t i; //TODO: XXX
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> bool
|
|
|
|
ircd::try_lex_cast<ircd::seconds>(const string_view &s)
|
|
|
|
{
|
|
|
|
time_t i; //TODO: XXX
|
|
|
|
return boost::conversion::try_lexical_convert(s, i);
|
|
|
|
}
|