Converts a string to bool. Accepts "true", "false" (case-insensitive) and numbers, 1 (true) or 0 (false). \param s String to convert to bool \param throw_on_error Bool variable to suppress error if conversion failed (Will return false in that case) \return bool value
| 127 | /// \return bool value |
| 128 | /// |
| 129 | inline bool StringToBool(const std::string& s, bool throw_on_error = true) |
| 130 | { |
| 131 | // in case of failure to convert returns false |
| 132 | auto result = false; |
| 133 | |
| 134 | // isstringstream fails if parsing didn't work |
| 135 | std::istringstream is(s); |
| 136 | |
| 137 | // try integer conversion first. For the case s is a number |
| 138 | is >> result; |
| 139 | |
| 140 | if (is.fail()) |
| 141 | { |
| 142 | // transform to lower case to make case-insensitive |
| 143 | std::string s_lower = s; |
| 144 | std::transform(s_lower.begin(), |
| 145 | s_lower.end(), |
| 146 | s_lower.begin(), |
| 147 | [](unsigned char c){ return std::tolower(c); }); |
| 148 | is.str(s_lower); |
| 149 | // try boolean -> s="false" or "true" |
| 150 | is.clear(); |
| 151 | is >> std::boolalpha >> result; |
| 152 | } |
| 153 | |
| 154 | if (is.fail() && throw_on_error) |
| 155 | { |
| 156 | throw armnn::InvalidArgumentException(s + " is not convertable to bool"); |
| 157 | } |
| 158 | |
| 159 | return result; |
| 160 | } |
| 161 | |
| 162 | } // namespace stringUtils |
| 163 |
no test coverage detected