1 /*
2 * Copyright (c) 2017-2022 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #ifndef ARM_COMPUTE_TEST_STRINGSUPPORT
25 #define ARM_COMPUTE_TEST_STRINGSUPPORT
26
27 #include <cassert>
28 #include <memory>
29 #include <sstream>
30 #include <string>
31
32 namespace arm_compute
33 {
34 namespace support
35 {
36 namespace cpp11
37 {
38 enum class NumericBase
39 {
40 BASE_10,
41 BASE_16
42 };
43
44 /** Convert string values to integer.
45 *
46 * @note This function implements the same behaviour as std::stoi. The latter
47 * is missing in some Android toolchains.
48 *
49 * @param[in] str String to be converted to int.
50 * @param[in] pos If idx is not a null pointer, the function sets the value of pos to the position of the first character in str after the number.
51 * @param[in] base Numeric base used to interpret the string.
52 *
53 * @return Integer representation of @p str.
54 */
55 inline int stoi(const std::string &str, std::size_t *pos = 0, NumericBase base = NumericBase::BASE_10)
56 {
57 assert(base == NumericBase::BASE_10 || base == NumericBase::BASE_16);
58 unsigned int x;
59 std::stringstream ss;
60 if(base == NumericBase::BASE_16)
61 {
62 ss << std::hex;
63 }
64 ss << str;
65 ss >> x;
66
67 if(pos)
68 {
69 std::string s;
70 std::stringstream ss_p;
71
72 ss_p << x;
73 ss_p >> s;
74 *pos = s.length();
75 }
76
77 return x;
78 }
79
80 /** Convert string values to unsigned long.
81 *
82 * @note This function implements the same behaviour as std::stoul. The latter
83 * is missing in some Android toolchains.
84 *
85 * @param[in] str String to be converted to unsigned long.
86 * @param[in] pos If idx is not a null pointer, the function sets the value of pos to the position of the first character in str after the number.
87 * @param[in] base Numeric base used to interpret the string.
88 *
89 * @return Unsigned long representation of @p str.
90 */
91 inline unsigned long stoul(const std::string &str, std::size_t *pos = 0, NumericBase base = NumericBase::BASE_10)
92 {
93 assert(base == NumericBase::BASE_10 || base == NumericBase::BASE_16);
94 std::stringstream stream;
95 unsigned long value = 0;
96 if(base == NumericBase::BASE_16)
97 {
98 stream << std::hex;
99 }
100 stream << str;
101 stream >> value;
102
103 if(pos)
104 {
105 std::string s;
106 std::stringstream ss_p;
107
108 ss_p << value;
109 ss_p >> s;
110 *pos = s.length();
111 }
112
113 return value;
114 }
115
116 #if(__ANDROID__ || BARE_METAL)
117 /** Convert integer and float values to string.
118 *
119 * @note This function implements the same behaviour as std::to_string. The
120 * latter is missing in some Android toolchains.
121 *
122 * @param[in] value Value to be converted to string.
123 *
124 * @return String representation of @p value.
125 */
126 template <typename T, typename std::enable_if<std::is_arithmetic<typename std::decay<T>::type>::value, int>::type = 0>
to_string(T && value)127 inline std::string to_string(T && value)
128 {
129 std::stringstream stream;
130 stream << std::forward<T>(value);
131 return stream.str();
132 }
133
134 // Specialization for const std::string&
to_string(const std::string & value)135 inline std::string to_string(const std::string &value)
136 {
137 return value;
138 }
139
140 /** Convert string values to float.
141 *
142 * @note This function implements the same behaviour as std::stof. The latter
143 * is missing in some Android toolchains.
144 *
145 * @param[in] str String to be converted to float.
146 *
147 * @return Float representation of @p str.
148 */
stof(const std::string & str)149 inline float stof(const std::string &str)
150 {
151 std::stringstream stream(str);
152 float value = 0.f;
153 stream >> value;
154 return value;
155 }
156
157 #else /* (__ANDROID__ || BARE_METAL) */
158 /** Convert integer and float values to string.
159 *
160 * @note This function acts as a convenience wrapper around std::to_string. The
161 * latter is missing in some Android toolchains.
162 *
163 * @param[in] value Value to be converted to string.
164 *
165 * @return String representation of @p value.
166 */
167 template <typename T>
to_string(T && value)168 inline std::string to_string(T &&value)
169 {
170 return ::std::to_string(std::forward<T>(value));
171 }
172
173 // Specialization for const std::string&
to_string(const std::string & value)174 inline std::string to_string(const std::string &value)
175 {
176 return value;
177 }
178
179 /** Convert string values to float.
180 *
181 * @note This function acts as a convenience wrapper around std::stof. The
182 * latter is missing in some Android toolchains.
183 *
184 * @param[in] args Arguments forwarded to std::stof.
185 *
186 * @return Float representation of input string.
187 */
188 template <typename... Ts>
stof(Ts &&...args)189 int stof(Ts &&... args)
190 {
191 return ::std::stof(std::forward<Ts>(args)...);
192 }
193
194 #endif /* (__ANDROID__ || BARE_METAL) */
195
to_string(bool value)196 inline std::string to_string(bool value)
197 {
198 std::stringstream str;
199 str << std::boolalpha << value;
200 return str.str();
201 }
202
203 } // namespace cpp11
204 } // namespace support
205 } // namespace arm_compute
206 #endif /* ARM_COMPUTE_TEST_STRINGSUPPORT */
207