| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#ifndef _STRING_TOOLS_H_ |
| 28 |
#define _STRING_TOOLS_H_ |
| 29 |
|
| 30 |
#include "hex.h" |
| 31 |
#include "mlocker.h" |
| 32 |
|
| 33 |
#include <boost/utility/string_ref.hpp> |
| 34 |
#include <boost/algorithm/string.hpp> |
| 35 |
#include <sstream> |
| 36 |
#include <string> |
| 37 |
#include <cstdint> |
| 38 |
|
| 39 |
#ifndef OUT |
| 40 |
#define OUT |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifdef WINDOWS_PLATFORM |
| 44 |
#pragma comment (lib, "Rpcrt4.lib") |
| 45 |
#endif |
| 46 |
|
| 47 |
namespace epee |
| 48 |
{ |
| 49 |
namespace string_tools |
| 50 |
{ |
| 51 |
|
| 52 |
inline std::string buff_to_hex_nodelimer(const std::string& src) |
| 53 |
{ |
| 54 |
return to_hex::string(to_byte_span(to_span(src))); |
| 55 |
} |
| 56 |
|
| 57 |
inline bool parse_hexstr_to_binbuff(const boost::string_ref s, std::string& res) |
| 58 |
{ |
| 59 |
return from_hex::to_string(res, s); |
| 60 |
} |
| 61 |
|
| 62 |
std::string get_ip_string_from_int32(uint32_t ip); |
| 63 |
bool get_ip_int32_from_string(uint32_t& ip, const std::string& ip_str); |
| 64 |
bool parse_peer_from_string(uint32_t& ip, uint16_t& port, const std::string& addres); |
| 65 |
std::string num_to_string_fast(int64_t val); |
| 66 |
|
| 67 |
bool compare_no_case(const std::string& str1, const std::string& str2); |
| 68 |
std::string& get_current_module_name(); |
| 69 |
std::string& get_current_module_folder(); |
| 70 |
#ifdef _WIN32 |
| 71 |
std::string get_current_module_path(); |
| 72 |
#endif |
| 73 |
void set_module_name_and_folder(const std::string& path_to_process_); |
| 74 |
|
| 75 |
inline std::string& trim(std::string& str) |
| 76 |
{ |
| 77 |
boost::trim(str); |
| 78 |
return str; |
| 79 |
} |
| 80 |
|
| 81 |
inline std::string trim(const std::string& str) |
| 82 |
{ |
| 83 |
return boost::trim_copy(str); |
| 84 |
} |
| 85 |
std::string pad_string(std::string s, size_t n, char c = ' ', bool prepend = false); |
| 86 |
|
| 87 |
|
| 88 |
template<class t_pod_type> |
| 89 |
std::string pod_to_hex(const t_pod_type& s) |
| 90 |
{ |
| 91 |
static_assert(std::is_standard_layout<t_pod_type>(), "expected standard layout type"); |
| 92 |
static_assert(alignof(t_pod_type) == 1, "type may have padding"); |
| 93 |
return to_hex::string(as_byte_span(s)); |
| 94 |
} |
| 95 |
|
| 96 |
template<class t_pod_type> |
| 97 |
bool hex_to_pod(const boost::string_ref hex_str, t_pod_type& s) |
| 98 |
{ |
| 99 |
static_assert(std::is_standard_layout<t_pod_type>(), "expected standard layout type"); |
| 100 |
static_assert(alignof(t_pod_type) == 1, "type may have padding"); |
| 101 |
static_assert(std::is_trivially_copyable<t_pod_type>(), "type must be trivially copyable"); |
| 102 |
return from_hex::to_buffer(as_mut_byte_span(s), hex_str); |
| 103 |
} |
| 104 |
|
| 105 |
template<class t_pod_type> |
| 106 |
bool hex_to_pod(const boost::string_ref hex_str, tools::scrubbed<t_pod_type>& s) |
| 107 |
{ |
| 108 |
return hex_to_pod(hex_str, unwrap(s)); |
| 109 |
} |
| 110 |
|
| 111 |
template<class t_pod_type> |
| 112 |
bool hex_to_pod(const boost::string_ref hex_str, epee::mlocked<t_pod_type>& s) |
| 113 |
{ |
| 114 |
return hex_to_pod(hex_str, unwrap(s)); |
| 115 |
} |
| 116 |
|
| 117 |
template<typename T> |
| 118 |
inline std::string to_string_hex(const T &val) |
| 119 |
{ |
| 120 |
static_assert(std::is_arithmetic<T>::value, "only arithmetic types"); |
| 121 |
std::stringstream ss; |
| 122 |
ss << std::hex << val; |
| 123 |
std::string s; |
| 124 |
ss >> s; |
| 125 |
return s; |
| 126 |
} |
| 127 |
|
| 128 |
bool validate_hex(uint64_t length, const std::string& str); |
| 129 |
std::string get_extension(const std::string& str); |
| 130 |
std::string cut_off_extension(const std::string& str); |
| 131 |
|
| 132 |
#ifdef _WIN32 |
| 133 |
std::wstring utf8_to_utf16(const std::string& str); |
| 134 |
std::string utf16_to_utf8(const std::wstring& wstr); |
| 135 |
#endif |
| 136 |
} |
| 137 |
} |
| 138 |
#endif |