Monero
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 <sstream>
35 #include <string>
36 #include <cstdint>
37 
38 #ifndef OUT
39  #define OUT
40 #endif
41 
42 #ifdef WINDOWS_PLATFORM
43 #pragma comment (lib, "Rpcrt4.lib")
44 #endif
45 
46 namespace epee
47 {
48 namespace string_tools
49 {
50  //----------------------------------------------------------------------------
52  {
53  return to_hex::string(to_byte_span(to_span(src)));
54  }
55  //----------------------------------------------------------------------------
56  inline bool parse_hexstr_to_binbuff(const boost::string_ref s, std::string& res)
57  {
58  return from_hex::to_string(res, s);
59  }
60 
62  bool get_ip_int32_from_string(uint32_t& ip, const std::string& ip_str);
65 
66  bool compare_no_case(const std::string& str1, const std::string& str2);
69 #ifdef _WIN32
70  std::string get_current_module_path();
71 #endif
72  bool set_module_name_and_folder(const std::string& path_to_process_);
73  bool trim_left(std::string& str);
74  bool trim_right(std::string& str);
75  //----------------------------------------------------------------------------
77  {
78  trim_left(str);
79  trim_right(str);
80  return str;
81  }
82  //----------------------------------------------------------------------------
83  inline std::string trim(const std::string& str_)
84  {
85  std::string str = str_;
86  trim_left(str);
87  trim_right(str);
88  return str;
89  }
90  std::string pad_string(std::string s, size_t n, char c = ' ', bool prepend = false);
91 
92  //----------------------------------------------------------------------------
93  template<class t_pod_type>
94  std::string pod_to_hex(const t_pod_type& s)
95  {
96  static_assert(std::is_standard_layout<t_pod_type>(), "expected standard layout type");
97  return to_hex::string(as_byte_span(s));
98  }
99  //----------------------------------------------------------------------------
100  template<class t_pod_type>
101  bool hex_to_pod(const boost::string_ref hex_str, t_pod_type& s)
102  {
103  static_assert(std::is_standard_layout<t_pod_type>(), "expected standard layout type");
104  return from_hex::to_buffer(as_mut_byte_span(s), hex_str);
105  }
106  //----------------------------------------------------------------------------
107  template<class t_pod_type>
108  bool hex_to_pod(const boost::string_ref hex_str, tools::scrubbed<t_pod_type>& s)
109  {
110  return hex_to_pod(hex_str, unwrap(s));
111  }
112  //----------------------------------------------------------------------------
113  template<class t_pod_type>
114  bool hex_to_pod(const boost::string_ref hex_str, epee::mlocked<t_pod_type>& s)
115  {
116  return hex_to_pod(hex_str, unwrap(s));
117  }
118  //----------------------------------------------------------------------------
119  template<typename T>
120  inline std::string to_string_hex(const T &val)
121  {
122  static_assert(std::is_arithmetic<T>::value, "only arithmetic types");
123  std::stringstream ss;
124  ss << std::hex << val;
125  std::string s;
126  ss >> s;
127  return s;
128  }
129 
130  bool validate_hex(uint64_t length, const std::string& str);
133 
134 #ifdef _WIN32
135  std::wstring utf8_to_utf16(const std::string& str);
136  std::string utf16_to_utf8(const std::wstring& wstr);
137 #endif
138 }
139 }
140 #endif //_STRING_TOOLS_H_
const char * res
Definition: hmac_keccak.cpp:42
std::string get_extension(const std::string &str)
Definition: string_tools.cpp:212
const uint32_t T[512]
Definition: groestl_tables.h:36
bool trim_left(std::string &str)
Definition: string_tools.cpp:183
span< const std::uint8_t > as_byte_span(const T &src) noexcept
Definition: span.h:162
std::string cut_off_extension(const std::string &str)
Definition: string_tools.cpp:223
std::string num_to_string_fast(int64_t val)
Definition: string_tools.cpp:123
bool set_module_name_and_folder(const std::string &path_to_process_)
Definition: string_tools.cpp:161
::std::string string
Definition: gtest-port.h:1097
const char * s
Definition: minissdp.c:596
unsigned short uint16_t
Definition: stdint.h:125
span< std::uint8_t > as_mut_byte_span(T &src) noexcept
Definition: span.h:171
bool trim_right(std::string &str)
Definition: string_tools.cpp:191
const char * str2
Definition: testupnpdescgen.c:132
std::string & trim(std::string &str)
Definition: string_tools.h:76
std::string pad_string(std::string s, size_t n, char c=' ', bool prepend=false)
Definition: string_tools.cpp:200
bool validate_hex(uint64_t length, const std::string &str)
Definition: string_tools.cpp:85
const char * str1
Definition: testupnpdescgen.c:131
std::string get_ip_string_from_int32(uint32_t ip)
Definition: string_tools.cpp:65
std::string pod_to_hex(const t_pod_type &s)
Definition: string_tools.h:94
unsigned int uint32_t
Definition: stdint.h:126
::std::wstring wstring
Definition: gtest-port.h:1103
unsigned __int64 uint64_t
Definition: stdint.h:136
bool compare_no_case(const std::string &str1, const std::string &str2)
Definition: string_tools.cpp:133
static bool to_buffer(span< std::uint8_t > out, boost::string_ref src) noexcept
Definition: hex.cpp:96
std::string & get_current_module_folder()
Definition: string_tools.cpp:145
bool hex_to_pod(const boost::string_ref hex_str, t_pod_type &s)
Definition: string_tools.h:101
static bool to_string(std::string &out, boost::string_ref src)
Definition: hex.cpp:90
boost::endian::big_uint32_t ip
Definition: socks.cpp:60
boost::endian::big_uint16_t port
Definition: socks.cpp:59
const char *const str
Definition: portlistingparse.c:23
std::string to_string_hex(const T &val)
Definition: string_tools.h:120
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
constexpr span< const typename T::value_type > to_span(const T &src)
Definition: span.h:122
std::string buff_to_hex_nodelimer(const std::string &src)
Definition: string_tools.h:51
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
signed __int64 int64_t
Definition: stdint.h:135
Definition: mlocker.h:68
T & unwrap(mlocked< T > &src)
Definition: mlocker.h:81
static std::string string(const span< const std::uint8_t > src)
Definition: hex.cpp:69
bool parse_hexstr_to_binbuff(const boost::string_ref s, std::string &res)
Definition: string_tools.h:56
static constexpr const char hex[]
Definition: wipeable_string.cpp:36
bool get_ip_int32_from_string(uint32_t &ip, const std::string &ip_str)
Definition: string_tools.cpp:76
c
Definition: pymoduletest.py:79
std::string & get_current_module_name()
Definition: string_tools.cpp:139
span< const std::uint8_t > to_byte_span(const span< const T > src) noexcept
Definition: span.h:144
bool parse_peer_from_string(uint32_t &ip, uint16_t &port, const std::string &addres)
Definition: string_tools.cpp:95