29#ifndef _STRING_TOOLS_H_
30#define _STRING_TOOLS_H_
43#include <boost/lexical_cast.hpp>
44#include <boost/algorithm/string/predicate.hpp>
58#ifdef WINDOWS_PLATFORM
59#pragma comment (lib, "Rpcrt4.lib")
62static const constexpr unsigned char isx[256] =
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
97 unsigned char *dst = (
unsigned char *)&
res[0];
98 const unsigned char *src = (
const unsigned char *)s.
data();
99 for(
size_t i = 0; i < s.
size(); i += 2)
103 if (tmp == 0xff)
return false;
106 if (
t2 == 0xff)
return false;
107 *dst++ = (tmp << 4) |
t2;
117 res.resize(s.size() / 2);
124 template<
class XType>
127 if (std::is_integral<XType>::value && !std::numeric_limits<XType>::is_signed && !std::is_same<XType, bool>::value)
129 for (
char c : str_id)
138 val = boost::lexical_cast<XType>(str_id);
141 catch(
const std::exception& )
155 template<
class XType>
160 str = boost::lexical_cast<std::string>(val);
177 std::string::size_type p = addres.find(
':');
178 std::string ip_str, port_str;
179 if(p == std::string::npos)
186 ip_str = addres.substr(0, p);
187 port_str = addres.substr(p+1, addres.size());
208 return boost::lexical_cast<std::string>(val);
213 std::stringstream ss;
214 ss << std::hex << val;
224 return !boost::iequals(str1, str2);
229 static std::string module_name;
235 static std::string module_folder;
236 return module_folder;
240 inline std::string get_current_module_path()
242 char pname [5000] = {0};
243 GetModuleFileNameA( NULL, pname,
sizeof(pname));
244 pname[
sizeof(pname)-1] = 0;
251 std::string path_to_process = path_to_process_;
253 path_to_process = get_current_module_path();
255 std::string::size_type
a = path_to_process.rfind(
'\\' );
256 if(
a == std::string::npos )
258 a = path_to_process.rfind(
'/' );
260 if (
a != std::string::npos )
273 for(std::string::iterator it = str.begin(); it!= str.end() &&
isspace(
static_cast<unsigned char>(*it));)
274 str.erase(str.begin());
282 for(std::string::reverse_iterator it = str.rbegin(); it!= str.rend() &&
isspace(
static_cast<unsigned char>(*it));)
283 str.erase( --((it++).base()));
288 inline std::string&
trim(std::string& str)
296 inline std::string
trim(
const std::string& str_)
298 std::string str = str_;
304 inline std::string
pad_string(std::string s,
size_t n,
char c =
' ',
bool prepend =
false)
309 s = std::string(n - s.size(), c) + s;
311 s.append(n - s.size(), c);
316 template<
class t_pod_type>
319 static_assert(std::is_standard_layout<t_pod_type>(),
"expected standard layout type");
323 template<
class t_pod_type>
326 static_assert(std::is_pod<t_pod_type>::value,
"expected pod type");
327 if(
sizeof(s)*2 != hex_str.size())
333 template<
class t_pod_type>
334 bool hex_to_pod(
const std::string& hex_str, tools::scrubbed<t_pod_type>& s)
339 template<
class t_pod_type>
350 std::string::size_type pos = str.rfind(
'.');
351 if(std::string::npos == pos)
354 res = str.substr(pos+1, str.size()-pos);
361 std::string::size_type pos = str.rfind(
'.');
362 if(std::string::npos == pos)
365 res = str.substr(0, pos);
370 inline std::wstring utf8_to_utf16(
const std::string& str)
374 int wstr_size = MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), NULL, 0);
377 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).
message());
379 std::wstring wstr(wstr_size,
wchar_t{});
380 if (!MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), &wstr[0], wstr_size))
382 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).
message());
386 inline std::string utf16_to_utf8(
const std::wstring& wstr)
390 int str_size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), NULL, 0, NULL, NULL);
393 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).
message());
395 std::string str(str_size,
char{});
396 if (!WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &str[0], str_size, NULL, NULL))
398 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).
message());
Non-owning sequence of data. Does not deep copy.
constexpr std::size_t size() const noexcept
constexpr pointer data() const noexcept
std::string message("Message requiring signing")
span< const std::uint8_t > to_byte_span(const span< const T > src) noexcept
T & unwrap(mlocked< T > &src)
constexpr span< const typename T::value_type > to_span(const T &src)
span< const std::uint8_t > as_byte_span(const T &src) noexcept
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
unsigned __int64 uint64_t
static std::string string(const span< const std::uint8_t > src)
#define DISABLE_GCC_WARNING