aboutsummaryrefslogtreecommitdiff
path: root/include/armnn/utility/StringUtils.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/armnn/utility/StringUtils.hpp')
-rw-r--r--include/armnn/utility/StringUtils.hpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/include/armnn/utility/StringUtils.hpp b/include/armnn/utility/StringUtils.hpp
index cc5e8e7349..172b1798c5 100644
--- a/include/armnn/utility/StringUtils.hpp
+++ b/include/armnn/utility/StringUtils.hpp
@@ -7,6 +7,9 @@
#include <iostream>
#include <sstream>
+#include <algorithm>
+#include <vector>
+#include <armnn/Exceptions.hpp>
namespace armnn
{
@@ -115,6 +118,47 @@ inline void StringReplaceAll(std::string& str,
}
}
+///
+/// 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
+///
+inline bool StringToBool(const std::string& s, bool throw_on_error = true)
+{
+ // in case of failure to convert returns false
+ auto result = false;
+
+ // isstringstream fails if parsing didn't work
+ std::istringstream is(s);
+
+ // try integer conversion first. For the case s is a number
+ is >> result;
+
+ if (is.fail())
+ {
+ // transform to lower case to make case-insensitive
+ std::string s_lower = s;
+ std::transform(s_lower.begin(),
+ s_lower.end(),
+ s_lower.begin(),
+ [](unsigned char c){ return std::tolower(c); });
+ is.str(s_lower);
+ // try boolean -> s="false" or "true"
+ is.clear();
+ is >> std::boolalpha >> result;
+ }
+
+ if (is.fail() && throw_on_error)
+ {
+ throw armnn::InvalidArgumentException(s + " is not convertable to bool");
+ }
+
+ return result;
+}
+
} // namespace stringUtils
} // namespace armnn \ No newline at end of file