Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 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> |
| 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 | /** Convert string values to float. |
| 135 | * |
| 136 | * @note This function implements the same behaviour as std::stof. The latter |
| 137 | * is missing in some Android toolchains. |
| 138 | * |
| 139 | * @param[in] str String to be converted to float. |
| 140 | * |
| 141 | * @return Float representation of @p str. |
| 142 | */ |
| 143 | inline float stof(const std::string &str) |
| 144 | { |
| 145 | std::stringstream stream(str); |
| 146 | float value = 0.f; |
| 147 | stream >> value; |
| 148 | return value; |
| 149 | } |
| 150 | |
| 151 | #else /* (__ANDROID__ || BARE_METAL) */ |
| 152 | /** Convert integer and float values to string. |
| 153 | * |
| 154 | * @note This function acts as a convenience wrapper around std::to_string. The |
| 155 | * latter is missing in some Android toolchains. |
| 156 | * |
| 157 | * @param[in] value Value to be converted to string. |
| 158 | * |
| 159 | * @return String representation of @p value. |
| 160 | */ |
| 161 | template <typename T> |
| 162 | inline std::string to_string(T &&value) |
| 163 | { |
| 164 | return ::std::to_string(std::forward<T>(value)); |
| 165 | } |
| 166 | |
| 167 | /** Convert string values to float. |
| 168 | * |
| 169 | * @note This function acts as a convenience wrapper around std::stof. The |
| 170 | * latter is missing in some Android toolchains. |
| 171 | * |
| 172 | * @param[in] args Arguments forwarded to std::stof. |
| 173 | * |
| 174 | * @return Float representation of input string. |
| 175 | */ |
| 176 | template <typename... Ts> |
| 177 | int stof(Ts &&... args) |
| 178 | { |
| 179 | return ::std::stof(std::forward<Ts>(args)...); |
| 180 | } |
| 181 | |
| 182 | #endif /* (__ANDROID__ || BARE_METAL) */ |
| 183 | |
| 184 | inline std::string to_string(bool value) |
| 185 | { |
| 186 | std::stringstream str; |
| 187 | str << std::boolalpha << value; |
| 188 | return str.str(); |
| 189 | } |
| 190 | |
| 191 | } // namespace cpp11 |
| 192 | } // namespace support |
| 193 | } // namespace arm_compute |
| 194 | #endif /* ARM_COMPUTE_TEST_STRINGSUPPORT */ |