25#ifndef JOIN_CORE_UTILS_HPP
26#define JOIN_CORE_UTILS_HPP
46#define JOIN_LIKELY(x) __builtin_expect (!!(x), 1)
47#define JOIN_UNLIKELY(x) __builtin_expect (!!(x), 0)
57 template <
typename Type,
size_t sz>
62 throw std::out_of_range (
"data size");
66 template <
typename Type>
75 template <
typename Type>
80 if (BYTE_ORDER == LITTLE_ENDIAN)
82 return (val >> 8) | (val << 8);
88 template <
typename Type>
93 if (BYTE_ORDER == LITTLE_ENDIAN)
95 return ((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8) |
96 ((val & 0x000000ff) << 24);
102 template <
typename Type>
107 if (BYTE_ORDER == LITTLE_ENDIAN)
109 return ((val & 0xff00000000000000ull) >> 56) | ((val & 0x00ff000000000000ull) >> 40) |
110 ((val & 0x0000ff0000000000ull) >> 24) | ((val & 0x000000ff00000000ull) >> 8) |
111 ((val & 0x00000000ff000000ull) << 8) | ((val & 0x0000000000ff0000ull) << 24) |
112 ((val & 0x000000000000ff00ull) << 40) | ((val & 0x00000000000000ffull) << 56);
123 if (BYTE_ORDER == LITTLE_ENDIAN)
144 if (BYTE_ORDER == LITTLE_ENDIAN)
160 template <
class Type>
171 bool operator() (
const std::string& a,
const std::string& b)
const noexcept
173 return ::strcasecmp (a.c_str (), b.c_str ()) < 0;
183 template <
class Type>
198 return ((a.size () == b.size ()) && std::equal (a.begin (), a.end (), b.begin (), [] (
char c1,
char c2) {
199 return std::toupper (c1) == std::toupper (c2);
210 return s.erase (0, s.find_first_not_of (
"\f\t\v\r\n "));
220 return s.erase (s.find_last_not_of (
" \f\t\v\r\n") + 1);
228 inline std::string&
trim (std::string& s)
240 inline std::string&
replaceAll (std::string& str,
const std::string& toReplace,
const std::string& by)
244 while ((pos = str.find (toReplace, pos)) != std::string::npos)
246 str.replace (pos, toReplace.length (), by);
259 inline std::vector<std::string>
split (
const std::string& in,
const std::string& delim)
261 std::vector<std::string> tokens;
264 size_t beg = 0, end = in.find (delim);
265 size_t sz = delim.size ();
266 while (end != std::string::npos)
268 tokens.push_back (in.substr (beg, end - beg));
270 end = in.find (delim, beg);
272 tokens.push_back (in.substr (beg));
283 inline std::vector<std::string>
rsplit (
const std::string& in,
const std::string& delim)
285 std::vector<std::string> tokens;
288 size_t beg = in.rfind (delim), end = std::string::npos;
289 size_t sz = delim.size ();
290 while (beg != std::string::npos)
292 tokens.push_back (in.substr (beg + sz, end - beg - sz));
294 beg = in.rfind (delim, end - sz);
296 tokens.push_back (in.substr (0, end));
308 inline bool getline (std::istream& in, std::string& line, std::streamsize max = 1024)
345 inline bool getline (std::streambuf& in, std::string& line, std::streamsize max = 1024)
347 std::istream istream (&in);
348 return getline (istream, line, max);
356 inline void dump (
const void* data,
unsigned long size, std::ostream& out = std::cout)
358 const uint8_t* buf =
reinterpret_cast<const uint8_t*
> (data);
360 for (
int i = 0; i < int (size); i += 16)
362 out << std::hex << std::uppercase << std::setw (8);
363 out << std::setfill (
'0') << i << std::dec <<
":";
365 for (
int j = 0; j < 16; ++j)
368 out << std::dec <<
" ";
370 if (i + j <
int (size))
372 out << std::hex << std::uppercase << std::setw (2);
373 out << std::setfill ('0') << static_cast<int> (buf[i + j]);
376 out << std::dec <<
" ";
379 out << std::dec <<
" ";
381 for (
int j = 0; j < 16; ++j)
383 if (i + j <
int (size))
385 if (isprint (buf[i + j]))
402 template <
typename Type>
403 std::enable_if_t<std::numeric_limits<Type>::is_integer, Type>
randomize ()
405 std::random_device rnd;
406 std::uniform_int_distribution<Type> dist{};
416 template <
class Func,
class... Args>
417 std::chrono::milliseconds
benchmark (Func&& func, Args&&... args)
419 auto beg = std::chrono::high_resolution_clock::now ();
420 func (std::forward<Args> (args)...);
421 auto end = std::chrono::high_resolution_clock::now ();
422 return std::chrono::duration_cast<std::chrono::milliseconds> (end - beg);
430 template <
typename Clock,
typename Duration>
431 struct timespec
toTimespec (
std::chrono::time_point<Clock, Duration> timePoint)
433 auto secs = std::chrono::time_point_cast<std::chrono::seconds> (timePoint);
434 auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds> (timePoint) -
435 std::chrono::time_point_cast<std::chrono::nanoseconds> (secs);
437 auto scount = secs.time_since_epoch ().count ();
438 auto ncount = ns.count ();
440 return {.tv_sec = scount, .tv_nsec = ncount};
Definition acceptor.hpp:32
void dump(const void *data, unsigned long size, std::ostream &out=std::cout)
dump data to standard output stream.
Definition utils.hpp:356
Type & swap(Type &val)
swaps byte orders.
Definition utils.hpp:184
std::chrono::milliseconds benchmark(Func &&func, Args &&... args)
benchmark function call.
Definition utils.hpp:417
std::string & trim(std::string &s)
trim.
Definition utils.hpp:228
struct timespec toTimespec(std::chrono::time_point< Clock, Duration > timePoint)
converts time_point to timespec.
Definition utils.hpp:431
std::enable_if_t< std::numeric_limits< Type >::is_integer, Type > randomize()
create a random number.
Definition utils.hpp:403
bool compareNoCase(const std::string &a, const std::string &b)
case insensitive string comparison.
Definition utils.hpp:196
std::vector< std::string > split(const std::string &in, const std::string &delim)
split a string using a delimiter.
Definition utils.hpp:259
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:150
thread_local std::error_code lastError
last error.
Definition error.cpp:32
std::string & trimLeft(std::string &s)
trim left.
Definition utils.hpp:208
bool getline(std::istream &in, std::string &line, std::streamsize max=1024)
read line (delimiter "\r\n").
Definition utils.hpp:308
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:283
std::string & replaceAll(std::string &str, const std::string &toReplace, const std::string &by)
replace all occurrences of a substring.
Definition utils.hpp:240
std::string & trimRight(std::string &s)
trim right.
Definition utils.hpp:218
Type operator()(Type val)
Definition utils.hpp:60
Type operator()(Type val)
Definition utils.hpp:163
bool operator()(const std::string &a, const std::string &b) const noexcept
Definition utils.hpp:171