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 <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
46namespace epee
47{
48namespace string_tools
49{
50 //----------------------------------------------------------------------------
51 inline std::string buff_to_hex_nodelimer(const std::string& src)
52 {
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);
63 bool parse_peer_from_string(uint32_t& ip, uint16_t& port, const std::string& addres);
64 std::string num_to_string_fast(int64_t val);
65
66 bool compare_no_case(const std::string& str1, const std::string& str2);
67 std::string& get_current_module_name();
68 std::string& get_current_module_folder();
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 //----------------------------------------------------------------------------
76 inline std::string& trim(std::string& str)
77 {
80 return str;
81 }
82 //----------------------------------------------------------------------------
83 inline std::string trim(const std::string& str_)
84 {
85 std::string str = 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");
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);
131 std::string get_extension(const std::string& str);
132 std::string cut_off_extension(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_
#define s(x, c)
Definition: aesb.c:47
const char * res
Definition: hmac_keccak.cpp:42
bool set_module_name_and_folder(const std::string &path_to_process_)
Definition: string_tools.cpp:161
std::string cut_off_extension(const std::string &str)
Definition: string_tools.cpp:223
std::string pod_to_hex(const t_pod_type &s)
Definition: string_tools.h:94
std::string get_extension(const std::string &str)
Definition: string_tools.cpp:212
std::string & get_current_module_name()
Definition: string_tools.cpp:139
std::string get_ip_string_from_int32(uint32_t ip)
Definition: string_tools.cpp:65
bool parse_hexstr_to_binbuff(const boost::string_ref s, std::string &res)
Definition: string_tools.h:56
std::string to_string_hex(const T &val)
Definition: string_tools.h:120
bool trim_left(std::string &str)
Definition: string_tools.cpp:183
bool hex_to_pod(const boost::string_ref hex_str, t_pod_type &s)
Definition: string_tools.h:101
bool get_ip_int32_from_string(uint32_t &ip, const std::string &ip_str)
Definition: string_tools.cpp:76
std::string pad_string(std::string s, size_t n, char c=' ', bool prepend=false)
Definition: string_tools.cpp:200
std::string & get_current_module_folder()
Definition: string_tools.cpp:145
bool validate_hex(uint64_t length, const std::string &str)
Definition: string_tools.cpp:85
bool compare_no_case(const std::string &str1, const std::string &str2)
Definition: string_tools.cpp:133
std::string buff_to_hex_nodelimer(const std::string &src)
Definition: string_tools.h:51
std::string num_to_string_fast(int64_t val)
Definition: string_tools.cpp:123
bool parse_peer_from_string(uint32_t &ip, uint16_t &port, const std::string &addres)
Definition: string_tools.cpp:95
std::string & trim(std::string &str)
Definition: string_tools.h:76
bool trim_right(std::string &str)
Definition: string_tools.cpp:191
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:161
span< const std::uint8_t > to_byte_span(const span< const T > src) noexcept
Definition: span.h:144
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:152
const char *const str
Definition: portlistingparse.c:23
boost::endian::big_uint16_t port
Definition: socks.cpp:59
boost::endian::big_uint32_t ip
Definition: socks.cpp:60
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)