Debian Codegraph @

contrib/epee/include/string_tools.h

raw symbols SCIP
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
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 //_STRING_TOOLS_H_