25#ifndef __JOIN_UTILS_HPP__
26#define __JOIN_UTILS_HPP__
46#define JOIN_LIKELY(x) __builtin_expect(!!(x), 1)
47#define JOIN_UNLIKELY(x) __builtin_expect(!!(x), 0)
49#define OUT_ENUM(a) case a : return #a
55 template <
typename Type,
size_t sz>
60 throw std::out_of_range (
"data size");
64 template <
typename Type>
73 template <
typename Type>
78 if (BYTE_ORDER == LITTLE_ENDIAN)
80 return (val >> 8) | (val << 8);
86 template <
typename Type>
91 if (BYTE_ORDER == LITTLE_ENDIAN)
93 return ((val & 0xff000000) >> 24) |
94 ((val & 0x00ff0000) >> 8 ) |
95 ((val & 0x0000ff00) << 8 ) |
96 ((val & 0x000000ff) << 24);
102 template <
typename Type>
107 if (BYTE_ORDER == LITTLE_ENDIAN)
109 return ((val & 0xff00000000000000ull) >> 56) |
110 ((val & 0x00ff000000000000ull) >> 40) |
111 ((val & 0x0000ff0000000000ull) >> 24) |
112 ((val & 0x000000ff00000000ull) >> 8 ) |
113 ((val & 0x00000000ff000000ull) << 8 ) |
114 ((val & 0x0000000000ff0000ull) << 24) |
115 ((val & 0x000000000000ff00ull) << 40) |
116 ((val & 0x00000000000000ffull) << 56);
127 if (BYTE_ORDER == LITTLE_ENDIAN)
129 union {
float f; uint32_t i; } tmp; tmp.f = val;
130 tmp.i = _byteswap <uint32_t, sizeof (uint32_t)> ()(tmp.i);
142 if (BYTE_ORDER == LITTLE_ENDIAN)
144 union {
double f; uint64_t i; } tmp; tmp.f = val;
145 tmp.i = _byteswap <uint64_t, sizeof (uint64_t)> ()(tmp.i);
152 template <
class Type>
157 return _byteswap <Type, sizeof (Type)> ()(val);
163 bool operator () (
const std::string& a,
const std::string& b)
const noexcept
165 return ::strcasecmp (a.c_str (), b.c_str ()) < 0;
175 template <
class Type>
176 __inline__ Type&
swap (Type& val)
178 val = details::_swap <Type> ()(val);
190 return ((a.size () == b.size ()) &&
191 std::equal (a.begin (), a.end (), b.begin (), [] (
char c1,
char c2) {return std::toupper (c1) == std::toupper (c2);}));
201 return s.erase (0, s.find_first_not_of (
"\f\t\v\r\n "));
211 return s.erase (s.find_last_not_of (
" \f\t\v\r\n") + 1);
219 __inline__ std::string&
trim (std::string& s)
231 __inline__ std::string&
replaceAll (std::string& str,
const std::string &toReplace,
const std::string &by)
235 while ((pos = str.find (toReplace, pos)) != std::string::npos)
237 str.replace (pos, toReplace.length (), by);
250 __inline__ std::vector <std::string>
split (
const std::string& in,
const std::string& delim)
252 std::vector <std::string> tokens;
255 size_t beg = 0, end = in.find (delim);
256 size_t sz = delim.size();
257 while (end != std::string::npos)
259 tokens.push_back (in.substr (beg, end - beg));
261 end = in.find (delim, beg);
263 tokens.push_back (in.substr (beg));
274 __inline__ std::vector <std::string>
rsplit (
const std::string& in,
const std::string& delim)
276 std::vector <std::string> tokens;
279 size_t beg = in.rfind (delim), end = std::string::npos;
280 size_t sz = delim.size();
281 while (beg != std::string::npos)
283 tokens.push_back (in.substr (beg + sz, end - beg - sz));
285 beg = in.rfind (delim, end - sz);
287 tokens.push_back (in.substr (0, end));
299 __inline__
bool getline (std::istream& in, std::string& line, std::streamsize max = 1024)
336 __inline__
bool getline (std::streambuf& in, std::string& line, std::streamsize max = 1024)
338 std::istream istream (&in);
339 return getline (istream, line, max);
347 __inline__
void dump (
const void* data,
unsigned long size, std::ostream& out = std::cout)
349 const uint8_t *buf =
reinterpret_cast <const uint8_t *
> (data);
351 for (
int i = 0; i < int (size); i += 16)
353 out << std::hex << std::uppercase << std::setw (8);
354 out << std::setfill (
'0') << i << std::dec <<
":";
356 for (
int j = 0; j < 16; ++j)
359 out << std::dec <<
" ";
361 if (i + j <
int (size))
363 out << std::hex << std::uppercase << std::setw (2);
364 out << std::setfill ('0') << static_cast <int> (buf[i + j]);
367 out << std::dec <<
" ";
370 out << std::dec <<
" ";
372 for (
int j = 0; j < 16; ++j)
374 if (i + j <
int (size))
376 if (isprint (buf[i + j]))
393 template <
typename Type>
394 std::enable_if_t <std::numeric_limits <Type>::is_integer, Type>
397 std::random_device rnd;
398 std::uniform_int_distribution <Type> dist {};
408 template <
class Func,
class... Args>
409 std::chrono::milliseconds
benchmark (Func&& func, Args&&... args)
411 auto beg = std::chrono::high_resolution_clock::now ();
412 func (std::forward <Args> (args)...);
413 auto end = std::chrono::high_resolution_clock::now ();
414 return std::chrono::duration_cast <std::chrono::milliseconds> (end - beg);
422 template <
typename Clock,
typename Duration>
423 struct timespec
toTimespec (
std::chrono::time_point <Clock, Duration> timePoint)
425 auto secs = std::chrono::time_point_cast <std::chrono::seconds> (timePoint);
426 auto ns = std::chrono::time_point_cast <std::chrono::nanoseconds> (timePoint) -
427 std::chrono::time_point_cast <std::chrono::nanoseconds> (secs);
429 auto scount = secs.time_since_epoch ().count ();
430 auto ncount = ns.count ();
432 return { .tv_sec = scount, .tv_nsec = ncount };
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code)
Create an std::error_code object.
Definition error.cpp:154
__inline__ std::string & trimRight(std::string &s)
trim right.
Definition utils.hpp:209
__inline__ std::string & trim(std::string &s)
trim.
Definition utils.hpp:219
std::chrono::milliseconds benchmark(Func &&func, Args &&... args)
benchmark function call.
Definition utils.hpp:409
__inline__ std::vector< std::string > split(const std::string &in, const std::string &delim)
split a string using a delimiter.
Definition utils.hpp:250
struct timespec toTimespec(std::chrono::time_point< Clock, Duration > timePoint)
converts time_point to timespec.
Definition utils.hpp:423
std::enable_if_t< std::numeric_limits< Type >::is_integer, Type > randomize()
create a random number.
Definition utils.hpp:395
__inline__ std::string & replaceAll(std::string &str, const std::string &toReplace, const std::string &by)
replace all occurrences of a substring.
Definition utils.hpp:231
__inline__ Type & swap(Type &val)
swaps byte orders.
Definition utils.hpp:176
__inline__ std::vector< std::string > rsplit(const std::string &in, const std::string &delim)
split a string in reverse order using a delimiter.
Definition utils.hpp:274
__inline__ bool getline(std::istream &in, std::string &line, std::streamsize max=1024)
read line (delimiter "\r\n").
Definition utils.hpp:299
__inline__ std::string & trimLeft(std::string &s)
trim left.
Definition utils.hpp:199
__inline__ bool compareNoCase(const std::string &a, const std::string &b)
case insensitive string comparison.
Definition utils.hpp:188
thread_local std::error_code lastError
last error.
Definition error.cpp:32
__inline__ void dump(const void *data, unsigned long size, std::ostream &out=std::cout)
dump data to standard output stream.
Definition utils.hpp:347
__inline__ Type operator()(Type val)
Definition utils.hpp:58
__inline__ Type operator()(Type val)
Definition utils.hpp:155
bool operator()(const std::string &a, const std::string &b) const noexcept
Definition utils.hpp:163