Monero
Loading...
Searching...
No Matches
string_tools.h
Go to the documentation of this file.
1// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above copyright
9// notice, this list of conditions and the following disclaimer in the
10// documentation and/or other materials provided with the distribution.
11// * Neither the name of the Andrey N. Sabelnikov nor the
12// names of its contributors may be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
47namespace epee
48{
49namespace string_tools
50{
51 //----------------------------------------------------------------------------
52 inline std::string buff_to_hex_nodelimer(const std::string& src)
53 {
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");
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 //_STRING_TOOLS_H_
#define s(x, c)
Definition aesb.c:47
const char * res
Definition hmac_keccak.cpp:42
Definition string_tools.h:50
std::string cut_off_extension(const std::string &str)
Definition string_tools.cpp:204
std::string pod_to_hex(const t_pod_type &s)
Definition string_tools.h:89
std::string get_extension(const std::string &str)
Definition string_tools.cpp:193
std::string & get_current_module_name()
Definition string_tools.cpp:142
std::string get_ip_string_from_int32(uint32_t ip)
Definition string_tools.cpp:68
bool parse_hexstr_to_binbuff(const boost::string_ref s, std::string &res)
Definition string_tools.h:57
std::string to_string_hex(const T &val)
Definition string_tools.h:118
void set_module_name_and_folder(const std::string &path_to_process_)
Definition string_tools.cpp:164
bool hex_to_pod(const boost::string_ref hex_str, t_pod_type &s)
Definition string_tools.h:97
bool get_ip_int32_from_string(uint32_t &ip, const std::string &ip_str)
Definition string_tools.cpp:79
std::string pad_string(std::string s, size_t n, char c=' ', bool prepend=false)
Definition string_tools.cpp:181
std::string & get_current_module_folder()
Definition string_tools.cpp:148
bool validate_hex(uint64_t length, const std::string &str)
Definition string_tools.cpp:88
bool compare_no_case(const std::string &str1, const std::string &str2)
Definition string_tools.cpp:136
std::string buff_to_hex_nodelimer(const std::string &src)
Definition string_tools.h:52
std::string num_to_string_fast(int64_t val)
Definition string_tools.cpp:126
bool parse_peer_from_string(uint32_t &ip, uint16_t &port, const std::string &addres)
Definition string_tools.cpp:98
std::string & trim(std::string &str)
Definition string_tools.h:75
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
span< std::uint8_t > as_mut_byte_span(T &src) noexcept
Definition span.h:172
span< const std::uint8_t > to_byte_span(const span< const T > src) noexcept
Definition span.h:138
T & unwrap(mlocked< T > &src)
Definition mlocker.h:81
constexpr span< const typename T::value_type > to_span(const T &src)
Definition span.h:122
span< const std::uint8_t > as_byte_span(const T &src) noexcept
Definition span.h:161
const char *const str
Definition portlistingparse.c:23
unsigned short uint16_t
Definition stdint.h:125
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
static bool to_string(std::string &out, boost::string_ref src)
Definition hex.cpp:90
static bool to_buffer(span< std::uint8_t > out, boost::string_ref src) noexcept
Definition hex.cpp:96
Definition mlocker.h:68
static std::string string(const span< const std::uint8_t > src)
Definition hex.cpp:69
const char * str1
Definition testupnpdescgen.c:131
const char * str2
Definition testupnpdescgen.c:132
#define T(x)