Files
externals/linux/include/celoxica/strutils.h
T
2017-10-04 14:16:02 -04:00

758 lines
27 KiB
C++

#pragma once
#include <nullptr.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef __cplusplus
# include "itoa.h"
# include "intutils.h"
# include <string>
# include <vector>
# include <cstddef>
# include <algorithm>
namespace baseutils
{
static const int64_t stodError = static_cast<int64_t>(0x8000000000000000);
std::string nanosecToString(uint64_t p_nanosec, char const* p_format="%Y/%M/%D %h:%m:%s");
extern std::vector<std::string> split(std::string const& p_src, std::string const& p_delimiters, bool p_removeEmpty=false);
inline std::vector<std::string> split(std::string const& p_src, char p_delimiter, bool p_removeEmpty=false) { return(split(p_src, std::string(1, p_delimiter), p_removeEmpty)); }
/*--------------------------------------------------------------------------------------------------
// _ _
// (_) |_ ___ ___
// | | __/ _ \/ __|
// | | || (_) \__ \
// |_|\__\___/|___/
//
--------------------------------------------------------------------------------------------------*/
inline std::string itos(int8_t p_value)
{
char buffer[7];
int16toa(buffer,p_value);
return(buffer);
}
inline std::string itos(uint8_t p_value)
{
char buffer[8];
uint16toa(buffer,p_value);
return(buffer);
}
inline std::string itos(int16_t p_value)
{
char buffer[7];
int16toa(buffer,p_value);
return(buffer);
}
inline std::string itos(uint16_t p_value)
{
char buffer[8];
uint16toa(buffer,p_value);
return(buffer);
}
inline std::string itos(int32_t p_value)
{
char buffer[13];
int32toa(buffer,p_value);
return(buffer);
}
inline std::string itos(uint32_t p_value)
{
char buffer[13];
uint32toa(buffer,p_value);
return(buffer);
}
inline std::string itos(int64_t p_value)
{
char buffer[23];
int64toa(buffer,p_value);
return(buffer);
}
inline std::string itos(uint64_t p_value)
{
char buffer[23];
uint64toa(buffer,p_value);
return(buffer);
}
inline std::string itos(uint32_t p_value, char const* p_format)
{
char buffer[100];
::sprintf(buffer, p_format, p_value);
return(buffer);
}
inline std::string itos(int64_t p_value, char const* p_format)
{
char buffer[100];
::sprintf(buffer, p_format, p_value);
return(buffer);
}
inline std::string itos(double p_value, char const* p_format = "%lf")
{
char buffer[100];
::sprintf(buffer, p_format, p_value);
return(buffer);
}
/*--------------------------------------------------------------------------------------------------
// _ _
// _ __| |_ _ __(_)_ __ ___
// | '__| __| '__| | '_ ` _ \
// | | | |_| | | | | | | | |
// |_| \__|_| |_|_| |_| |_|
//
--------------------------------------------------------------------------------------------------*/
inline std::string rtrim(std::string const& p_src, std::string const& p_delimiters=" \f\n\r\t\v")
{
size_t pos = p_src.find_last_not_of(p_delimiters);
return(pos == std::string::npos ? "" : p_src.substr(0, pos + 1));
}
/*--------------------------------------------------------------------------------------------------
// _ _ _
// | | |_ _ __(_)_ __ ___
// | | __| '__| | '_ ` _ \
// | | |_| | | | | | | | |
// |_|\__|_| |_|_| |_| |_|
//
--------------------------------------------------------------------------------------------------*/
inline std::string ltrim(std::string const& p_src, std::string const& p_delimiters=" \f\n\r\t\v")
{
size_t pos = p_src.find_first_not_of(p_delimiters);
return(pos == std::string::npos ? "" : p_src.substr(pos));
}
/*--------------------------------------------------------------------------------------------------
// _ _
// | |_ _ __(_)_ __ ___
// | __| '__| | '_ ` _ \
// | |_| | | | | | | | |
// \__|_| |_|_| |_| |_|
//
--------------------------------------------------------------------------------------------------*/
inline std::string trim(std::string const& p_src, std::string const& p_delimiters=" \f\n\r\t\v")
{
return(ltrim(rtrim(p_src, p_delimiters), p_delimiters));
}
/*--------------------------------------------------------------------------------------------------
// _ __ _ _
// ___ ___| |/ _| _ __| |_ _ __(_)_ __ ___
// / __|/ _ \ | |_ | '__| __| '__| | '_ ` _ \
// \__ \ __/ | _| | | | |_| | | | | | | | |
// |___/\___|_|_|___|_| \__|_| |_|_| |_| |_|
// |_____|
--------------------------------------------------------------------------------------------------*/
inline std::string& self_rtrim(std::string& p_src, std::string const& p_delimiters=" \f\n\r\t\v")
{
return(p_src.erase(p_src.find_last_not_of(p_delimiters) + 1));
}
/*--------------------------------------------------------------------------------------------------
// _ __ _ _ _
// ___ ___| |/ _| | | |_ _ __(_)_ __ ___
// / __|/ _ \ | |_ | | __| '__| | '_ ` _ \
// \__ \ __/ | _| | | |_| | | | | | | | |
// |___/\___|_|_|___|_|\__|_| |_|_| |_| |_|
// |_____|
--------------------------------------------------------------------------------------------------*/
inline std::string& self_ltrim(std::string& p_src, std::string const& p_delimiters=" \f\n\r\t\v")
{
return(p_src.erase(0, p_src.find_first_not_of(p_delimiters)));
}
/*--------------------------------------------------------------------------------------------------
// _ __ _ _
// ___ ___| |/ _|| |_ _ __(_)_ __ ___
// / __|/ _ \ | |_ | __| '__| | '_ ` _ \
// \__ \ __/ | _|| |_| | | | | | | | |
// |___/\___|_|_|___\__|_| |_|_| |_| |_|
// |_____|
--------------------------------------------------------------------------------------------------*/
inline std::string& self_trim(std::string& p_src, std::string const& p_delimiters=" \f\n\r\t\v")
{
return(self_ltrim(self_rtrim(p_src, p_delimiters), p_delimiters));
}
/*--------------------------------------------------------------------------------------------------
// _
// ___ _ __ ___ _ __ __ _| |_ ___ _ __
// / _ \| '_ \ / _ \ '__/ _` | __/ _ \| '__|
// | (_) | |_) | __/ | | (_| | || (_) | |
// \___/| .__/ \___|_| \__,_|\__\___/|_|
// |_|
--------------------------------------------------------------------------------------------------*/
inline bool operator&=(std::string const& p_left, std::string const& p_right)
{
return(::strcasecmp(p_left.c_str(), p_right.c_str()) == 0);
}
//--------------------------------------------------------------------------------------------------
inline bool operator&=(std::string const& p_left, char const* p_right)
{
return(::strcasecmp(p_left.c_str(), p_right) == 0);
}
//--------------------------------------------------------------------------------------------------
inline bool operator&=(char const* p_left, std::string const& p_right)
{
return(::strcasecmp(p_left, p_right.c_str()) == 0);
}
/*--------------------------------------------------------------------------------------------------
// _ _____
// ___| |_ _ __| ____|_ __ _ __ ___ _ __
// / __| __| '__| _| | '__| '__/ _ \| '__|
// \__ \ |_| | | |___| | | | | (_) | |
// |___/\__|_| |_____|_| |_| \___/|_|
//
--------------------------------------------------------------------------------------------------*/
char const* const cg_errnoName[] =
{
"", // 0
"EPERM",
"ENOENT",
"ESRCH",
"EINTR",
"EIO",
"ENXIO",
"E2BIG",
"ENOEXEC",
"EBADF",
"ECHILD",
"EAGAIN",
"ENOMEM",
"EACCES",
"EFAULT",
"ENOTBLK",
"EBUSY",
"EEXIST",
"EXDEV",
"ENODEV",
"ENOTDIR",
"EISDIR",
"EINVAL",
"ENFILE",
"EMFILE",
"ENOTTY",
"ETXTBSY",
"EFBIG",
"ENOSPC",
"ESPIPE",
"EROFS",
"EMLINK",
"EPIPE",
"EDOM",
"ERANGE",
"EDEADLK",
"ENAMETOOLONG",
"ENOLCK",
"ENOSYS",
"ENOTEMPTY",
"ELOOP",
"EWOULDBLOCK",
"ENOMSG",
"EIDRM",
"ECHRNG",
"EL2NSYNC",
"EL3HLT",
"EL3RST",
"ELNRNG",
"EUNATCH",
"ENOCSI",
"EL2HLT",
"EBADE",
"EBADR",
"EXFULL",
"ENOANO",
"EBADRQC",
"EBADSLT",
"", // 58
"EBFONT",
"ENOSTR",
"ENODATA",
"ETIME",
"ENOSR",
"ENONET",
"ENOPKG",
"EREMOTE",
"ENOLINK",
"EADV",
"ESRMNT",
"ECOMM",
"EPROTO",
"EMULTIHOP",
"EDOTDOT",
"EBADMSG",
"EOVERFLOW",
"ENOTUNIQ",
"EBADFD",
"EREMCHG",
"ELIBACC",
"ELIBBAD",
"ELIBSCN",
"ELIBMAX",
"ELIBEXEC",
"EILSEQ",
"ERESTART",
"ESTRPIPE",
"EUSERS",
"ENOTSOCK",
"EDESTADDRREQ",
"EMSGSIZE",
"EPROTOTYPE",
"ENOPROTOOPT",
"EPROTONOSUPPORT",
"ESOCKTNOSUPPORT",
"EOPNOTSUPP",
"EPFNOSUPPORT",
"EAFNOSUPPORT",
"EADDRINUSE",
"EADDRNOTAVAIL",
"ENETDOWN",
"ENETUNREACH",
"ENETRESET",
"ECONNABORTED",
"ECONNRESET",
"ENOBUFS",
"EISCONN",
"ENOTCONN",
"ESHUTDOWN",
"ETOOMANYREFS",
"ETIMEDOUT",
"ECONNREFUSED",
"EHOSTDOWN",
"EHOSTUNREACH",
"EALREADY",
"EINPROGRESS",
"ESTALE",
"EUCLEAN",
"ENOTNAM",
"ENAVAIL",
"EISNAM",
"EREMOTEIO",
"EDQUOT",
"ENOMEDIUM",
"EMEDIUMTYPE",
"ECANCELED",
"ENOKEY",
"EKEYEXPIRED",
"EKEYREVOKED",
"EKEYREJECTED",
"EOWNERDEAD",
"ENOTRECOVERABLE",
"ERFKILL",
"EHWPOISON" // 133
};
inline std::string strError(int p_error)
{
char buf[200];
#ifdef _GNU_SOURCE
std::string s = strerror_r(p_error, buf, sizeof(buf));
#else
strerror_r(p_error, buf, sizeof(buf));
std::string s = buf;
#endif
s += " (" + baseutils::itos(p_error);
if(p_error > 0 and p_error < 134 and p_error != 58)
{
s += " - " + std::string(cg_errnoName[p_error]);
}
return(s + ")");
}
/*--------------------------------------------------------------------------------------------------
// _
// | | _____ _____ _ __
// | |/ _ \ \ /\ / / _ \ '__|
// | | (_) \ V V / __/ |
// |_|\___/ \_/\_/ \___|_|
//
--------------------------------------------------------------------------------------------------*/
inline std::string lower(std::string const& p_str)
{
std::string data = p_str;
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
return(data);
}
/*--------------------------------------------------------------------------------------------------
//
// _ _ _ __ _ __ ___ _ __
// | | | | '_ \| '_ \ / _ \ '__|
// | |_| | |_) | |_) | __/ |
// \__,_| .__/| .__/ \___|_|
// |_| |_|
--------------------------------------------------------------------------------------------------*/
inline std::string upper(std::string const& p_str)
{
std::string data = p_str;
std::transform(data.begin(), data.end(), data.begin(), ::toupper);
return(data);
}
/*--------------------------------------------------------------------------------------------------
// _
// ___ __ _ _ __ ___ ___| |
// / __/ _` | '_ ` _ \ / _ \ |
// | (_| (_| | | | | | | __/ |
// \___\__,_|_| |_| |_|\___|_|
//
--------------------------------------------------------------------------------------------------*/
inline std::string camel(std::string const& p_str)
{
std::string data = p_str;
if(data.length())
{
std::transform(data.begin(), data.begin() + 1, data.begin(), ::toupper);
}
return(data);
}
/*--------------------------------------------------------------------------------------------------
// _ _
// ___| |_ ___ __| |
// / __| __/ _ \ / _` |
// \__ \ || (_) | (_| |
// |___/\__\___/ \__,_|
//
--------------------------------------------------------------------------------------------------*/
// p_factor is the maximum number of decimals really used (extra decimals are ignored).
// This function returns stodError in case of conversion error.
extern int64_t stod(char const* p_ptr, uint8_t p_factor);
inline int64_t stod(char const* p_ptr, uint8_t p_factor, bool* po_valid)
{
int64_t rc = stod(p_ptr, p_factor);
*po_valid = rc != stodError;
return(rc);
}
template<typename T> inline bool stod(T& po_dest, char const* p_ptr, uint8_t p_factor)
{
int64_t rc = stod(p_ptr, p_factor);
if(rc == stodError or rc < NumericLimits<T>::smallest or rc > NumericLimits<T>::biggest)
{
return(false);
}
po_dest = static_cast<T>(rc);
return(true);
}
extern std::string dtos(int64_t p_val, uint32_t p_factorSize);
/*--------------------------------------------------------------------------------------------------
// _ _ _ _________
// __ _| |_ ___ _ _(_)_ __ | |_|___ /___ \
// / _` | __/ _ \| | | | | '_ \| __| |_ \ __) |
// | (_| | || (_) | |_| | | | | | |_ ___) / __/
// \__,_|\__\___/ \__,_|_|_| |_|\__|____/_____|
//
--------------------------------------------------------------------------------------------------*/
inline uint32_t atouint32(char const* p_src)
{
uint32_t val = 0;
while(*p_src)
{
val = val * 10 + (static_cast<unsigned char>(*p_src)) - '0';
++p_src;
}
return(val);
}
/*--------------------------------------------------------------------------------------------------
// _ _ _ _________
// __ _| |_ ___ (_)_ __ | |_|___ /___ \
// / _` | __/ _ \| | '_ \| __| |_ \ __) |
// | (_| | || (_) | | | | | |_ ___) / __/
// \__,_|\__\___/|_|_| |_|\__|____/_____|
//
--------------------------------------------------------------------------------------------------*/
inline int32_t atoint32(char const* p_src)
{
int32_t val = 0;
bool minus = *p_src == '-';
p_src += minus ? 1 : 0;
while(*p_src)
{
val = val * 10 + *p_src - '0';
++p_src;
}
return(val * (minus ? -1 : 1));
}
/*--------------------------------------------------------------------------------------------------
// _ _ _ __ _ _
// __ _| |_ ___ _ _(_)_ __ | |_ / /_ | || |
// / _` | __/ _ \| | | | | '_ \| __| '_ \| || |_
// | (_| | || (_) | |_| | | | | | |_| (_) |__ _|
// \__,_|\__\___/ \__,_|_|_| |_|\__|\___/ |_|
//
--------------------------------------------------------------------------------------------------*/
inline uint64_t atouint64(char const* p_src)
{
uint64_t val = 0;
while(*p_src)
{
val = val * 10 + (static_cast<unsigned char>(*p_src)) - '0';
++p_src;
}
return(val);
}
/*--------------------------------------------------------------------------------------------------
// _ _ _ __ _ _
// __ _| |_ ___ (_)_ __ | |_ / /_ | || |
// / _` | __/ _ \| | '_ \| __| '_ \| || |_
// | (_| | || (_) | | | | | |_| (_) |__ _|
// \__,_|\__\___/|_|_| |_|\__|\___/ |_|
//
--------------------------------------------------------------------------------------------------*/
inline int64_t atoint64(char const* p_src)
{
int64_t val = 0;
bool minus = *p_src == '-';
p_src += minus ? 1 : 0;
while(*p_src)
{
val = val * 10 + *p_src - '0';
++p_src;
}
return(val * (minus ? -1 : 1));
}
/*--------------------------------------------------------------------------------------------------
//
// _ __ ___ ___ _ __ ___ _ __ ___ _ __ _ _
// | '_ ` _ \ / _ \ '_ ` _ \| '_ \ / __| '_ \| | | |
// | | | | | | __/ | | | | | |_) | (__| |_) | |_| |
// |_| |_| |_|\___|_| |_| |_| .__/ \___| .__/ \__, |
// |_| |_| |___/
--------------------------------------------------------------------------------------------------*/
template<typename type_t, typename len_t> inline type_t* mempcpy(type_t* po_dest, void const* p_src, len_t p_srcSize)
{
return(static_cast<type_t*>(::mempcpy(po_dest, p_src, static_cast<size_t>(p_srcSize))));
}
/*--------------------------------------------------------------------------------------------------
// _
// | |_ __ ___ ___ _ __ ___ ___ _ __ _ _
// | | '_ ` _ \ / _ \ '_ ` _ \ / __| '_ \| | | |
// | | | | | | | __/ | | | | | (__| |_) | |_| |
// |_|_| |_| |_|\___|_| |_| |_|\___| .__/ \__, |
// |_| |___/
--------------------------------------------------------------------------------------------------*/
template<typename len_t> inline len_t lmemcpy(void* po_dest, void const* p_src, len_t p_srcSize)
{
::memcpy(po_dest, p_src, static_cast<size_t>(p_srcSize));
return(p_srcSize);
}
}
#endif // #ifdef __cplusplus
#ifdef __cplusplus
namespace baseutils
{
#endif
/*--------------------------------------------------------------------------------------------------
// _ _
// ___ _ __ _ __ _ __(_)_ __ | |_
// / __| '_ \| '_ \| '__| | '_ \| __|
// \__ \ | | | |_) | | | | | | | |_
// |___/_| |_| .__/|_| |_|_| |_|\__|
// |_|
--------------------------------------------------------------------------------------------------*/
size_t snprint(char* po_dest, size_t p_size, char const* p_fmt, ...) __attribute__ ((format (printf, 3, 4)));
inline size_t snprint(char* po_dest, size_t p_size, char const* p_fmt, ...)
{
va_list arg;
va_start(arg, p_fmt);
size_t len = static_cast<size_t>(::vsnprintf(po_dest, p_size, p_fmt, arg));
va_end(arg);
if(len > p_size)
{
return(p_size);
}
return(len);
}
/*--------------------------------------------------------------------------------------------------
// _ _
// __ _____ _ __ _ __ _ __(_)_ __ | |_
// \ \ / / __| '_ \| '_ \| '__| | '_ \| __|
// \ V /\__ \ | | | |_) | | | | | | | |_
// \_/ |___/_| |_| .__/|_| |_|_| |_|\__|
// |_|
--------------------------------------------------------------------------------------------------*/
inline size_t vsnprint(char* po_dest, size_t p_size, char const* p_fmt, va_list p_args)
{
size_t len = static_cast<size_t>(vsnprintf(po_dest, p_size, p_fmt, p_args));
if(len > p_size)
{
return(p_size);
}
return(len);
}
/*--------------------------------------------------------------------------------------------------
// _ _
// | |___| |_ _ __ ___ _ __ _ _
// | / __| __| '__/ __| '_ \| | | |
// | \__ \ |_| | | (__| |_) | |_| |
// |_|___/\__|_| \___| .__/ \__, |
// |_| |___/
--------------------------------------------------------------------------------------------------*/
// Like strcpy, but returns the length of p_src.
// p_src must be null-terminated.
// p_dest must large enough to receive p_src.
// Slow if compiled without optimization.
inline size_t lstrcpy(char* po_dest, char const* p_src)
{
return(static_cast<size_t>(::stpcpy(po_dest, p_src) - po_dest));
}
/*--------------------------------------------------------------------------------------------------
// _ _
// | |___| |_ _ __ _ __ ___ _ __ _ _
// | / __| __| '__| '_ \ / __| '_ \| | | |
// | \__ \ |_| | | | | | (__| |_) | |_| |
// |_|___/\__|_| |_| |_|\___| .__/ \__, |
// |_| |___/
--------------------------------------------------------------------------------------------------*/
// Like strncpy, but returns the number of bytes copied into p_dest (without the terminating 0 in
// the case of there is suffiscient space in po_dest).
inline size_t lstrncpy(register char* po_dest, register char const* p_src, size_t p_destSize)
{
if(p_destSize > 0)
{
size_t i;
for(i = 0; i < p_destSize; ++i)
{
if(*p_src == 0)
{
*po_dest = 0;
break;
}
*po_dest++ = *p_src++;
}
return(i);
}
return(0);
}
/*--------------------------------------------------------------------------------------------------
// _ _
// | |___| |_ _ __ _ __ _______ _ __ _ _
// | / __| __| '__| '_ \|_ / __| '_ \| | | |
// | \__ \ |_| | | | | |/ / (__| |_) | |_| |
// |_|___/\__|_| |_| |_/___\___| .__/ \__, |
// |_| |___/
--------------------------------------------------------------------------------------------------*/
// Like strncpy except that:
// - po_dest is always null terminated (except if po_destSize is 0)
// - if p_src is smaller than po_dest, the remaining bytes of po_dest after the null terminating byte are not set to 0.
// - the value returned is the size of po_dest (without the null terminating byte).
inline size_t lstrnzcpy(char* po_dest, char const* p_src, size_t p_destSize)
{
if(p_destSize > 1)
{
--p_destSize;
size_t i;
for(i = 0; i < p_destSize and *p_src != '\0'; ++i)
{
*po_dest++ = *p_src++;
}
*po_dest = '\0';
return(i);
}
if(p_destSize == 1)
{
*po_dest = '\0';
}
return(0);
}
/*--------------------------------------------------------------------------------------------------
// _
// ___| |_ _ __ _ __ _______ _ __ _ _
// / __| __| '__| '_ \|_ / __| '_ \| | | |
// \__ \ |_| | | | | |/ / (__| |_) | |_| |
// |___/\__|_| |_| |_/___\___| .__/ \__, |
// |_| |___/
--------------------------------------------------------------------------------------------------*/
// Like strncpy, but po_dest is always null-terminated.
inline char* strnzcpy(register char* po_dest, register char const* p_src, size_t p_max)
{
lstrnzcpy(po_dest, p_src, p_max);
return(po_dest);
}
/*--------------------------------------------------------------------------------------------------
// _
// ___| |_ _ __ _ __ ___ _ __ _ ___ ______
// / __| __| '_ \| '_ \ / __| '_ \| | | \ \ /\ / /_ /
// \__ \ |_| |_) | | | | (__| |_) | |_| |\ V V / / /
// |___/\__| .__/|_| |_|\___| .__/ \__, | \_/\_/ /___|
// |_| |_| |___/
--------------------------------------------------------------------------------------------------*/
// Like the standard glibc function stpncpy except than the null terminating byte is never added to po_dest.
// wz for Without Zero
template<typename len_t> inline char* stpncpywz(char* po_dest, char const* p_src, len_t p_length)
{
char const* end = p_src + p_length;
while(*p_src and p_src != end)
{
*po_dest++ = *p_src++;
}
return(po_dest);
}
#ifdef __cplusplus
}
#endif