COMPMID-1038: stoi and stoul cleanup.
Change-Id: I0d981a06655cdd86c71fddbd07303d781577d0fd
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/132620
Reviewed-by: Giorgio Arena <giorgio.arena@arm.com>
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/src/runtime/CPUUtils.cpp b/src/runtime/CPUUtils.cpp
index 7e8bf2b..cf29ce0 100644
--- a/src/runtime/CPUUtils.cpp
+++ b/src/runtime/CPUUtils.cpp
@@ -126,7 +126,7 @@
std::string line;
if(bool(getline(file, line)))
{
- const unsigned long midr = support::cpp11::stoul(line, nullptr, 16);
+ const unsigned long midr = support::cpp11::stoul(line, nullptr, support::cpp11::NumericBase::BASE_16);
c.midr = (midr & 0xffffffff);
c.model = midr_to_model(c.midr);
c.model_set = true;
@@ -160,7 +160,7 @@
if(std::regex_match(line, match, proc_regex))
{
std::string id = match[1];
- int newcpu = support::cpp11::stoi(id, nullptr, 0);
+ int newcpu = support::cpp11::stoi(id, nullptr);
if(curcpu >= 0 && midr == 0)
{
@@ -183,28 +183,28 @@
if(std::regex_match(line, match, imp_regex))
{
- int impv = support::cpp11::stoi(match[1], nullptr, 16);
+ int impv = support::cpp11::stoi(match[1], nullptr, support::cpp11::NumericBase::BASE_16);
midr |= (impv << 24);
continue;
}
if(std::regex_match(line, match, var_regex))
{
- int varv = support::cpp11::stoi(match[1], nullptr, 16);
+ int varv = support::cpp11::stoi(match[1], nullptr, support::cpp11::NumericBase::BASE_16);
midr |= (varv << 16);
continue;
}
if(std::regex_match(line, match, part_regex))
{
- int partv = support::cpp11::stoi(match[1], nullptr, 16);
+ int partv = support::cpp11::stoi(match[1], nullptr, support::cpp11::NumericBase::BASE_16);
midr |= (partv << 4);
continue;
}
if(std::regex_match(line, match, rev_regex))
{
- int regv = support::cpp11::stoi(match[1], nullptr, 10);
+ int regv = support::cpp11::stoi(match[1], nullptr);
midr |= (regv);
midr |= (0xf << 16);
continue;
@@ -251,7 +251,7 @@
line.erase(line.begin(), startfrom);
- max_cpus = support::cpp11::stoi(line, nullptr, 0) + 1;
+ max_cpus = support::cpp11::stoi(line, nullptr) + 1;
success = true;
}
}
@@ -262,7 +262,6 @@
max_cpus = std::thread::hardware_concurrency();
}
#endif /* BARE_METAL */
-
return max_cpus;
}
#endif /* !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) */
diff --git a/support/ToolchainSupport.h b/support/ToolchainSupport.h
index 88c1700..ece9667 100644
--- a/support/ToolchainSupport.h
+++ b/support/ToolchainSupport.h
@@ -25,6 +25,7 @@
#define ARM_COMPUTE_TEST_TOOLCHAINSUPPORT
#include <algorithm>
+#include <cassert>
#include <cmath>
#include <cstddef>
#include <limits>
@@ -42,6 +43,62 @@
{
namespace cpp11
{
+enum class NumericBase
+{
+ BASE_10,
+ BASE_16
+};
+
+/** Convert string values to integer.
+ *
+ * @note This function implements the same behaviour as std::stoi. The latter
+ * is missing in some Android toolchains.
+ *
+ * @param[in] str String to be converted to int.
+ * @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.
+ * @param[in] base Numeric base used to interpret the string.
+ *
+ * @return Integer representation of @p str.
+ */
+inline int stoi(const std::string &str, std::size_t *pos = 0, NumericBase base = NumericBase::BASE_10)
+{
+ assert(base == NumericBase::BASE_10 || base == NumericBase::BASE_16);
+ unsigned int x;
+ std::stringstream ss;
+ if(base == NumericBase::BASE_16)
+ {
+ ss << std::hex;
+ }
+ ss << str;
+ ss >> x;
+ return x;
+}
+
+/** Convert string values to unsigned long.
+ *
+ * @note This function implements the same behaviour as std::stoul. The latter
+ * is missing in some Android toolchains.
+ *
+ * @param[in] str String to be converted to unsigned long.
+ * @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.
+ * @param[in] base Numeric base used to interpret the string.
+ *
+ * @return Unsigned long representation of @p str.
+ */
+inline unsigned long stoul(const std::string &str, std::size_t *pos = 0, NumericBase base = NumericBase::BASE_10)
+{
+ assert(base == NumericBase::BASE_10 || base == NumericBase::BASE_16);
+ std::stringstream stream;
+ unsigned long value = 0;
+ if(base == NumericBase::BASE_16)
+ {
+ stream << std::hex;
+ }
+ stream << str;
+ stream >> value;
+ return value;
+}
+
#if(__ANDROID__ || BARE_METAL)
/** Convert integer and float values to string.
*
@@ -60,50 +117,6 @@
return stream.str();
}
-/** Convert string values to integer.
- *
- * @note This function implements the same behaviour as std::stoi. The latter
- * is missing in some Android toolchains.
- *
- * @param[in] str String to be converted to int.
- *
- * @return Integer representation of @p str.
- */
-inline int stoi(const std::string &str, std::size_t *pos = 0, int base = 10)
-{
- unsigned int x;
- std::stringstream ss;
- if(base == 16)
- {
- ss << std::hex;
- }
- ss << str;
- ss >> x;
- return x;
-}
-
-/** Convert string values to unsigned long.
- *
- * @note This function implements the same behaviour as std::stoul. The latter
- * is missing in some Android toolchains.
- *
- * @param[in] str String to be converted to unsigned long.
- *
- * @return Unsigned long representation of @p str.
- */
-inline unsigned long stoul(const std::string &str, std::size_t *pos = 0, int base = 10)
-{
- std::stringstream stream;
- unsigned long value = 0;
- if(base == 16)
- {
- stream << std::hex;
- }
- stream << str;
- stream >> value;
- return value;
-}
-
/** Convert string values to float.
*
* @note This function implements the same behaviour as std::stof. The latter
@@ -199,36 +212,6 @@
return ::std::to_string(std::forward<T>(value));
}
-/** Convert string values to integer.
- *
- * @note This function acts as a convenience wrapper around std::stoi. The
- * latter is missing in some Android toolchains.
- *
- * @param[in] args Arguments forwarded to std::stoi.
- *
- * @return Integer representation of input string.
- */
-template <typename... Ts>
-int stoi(Ts &&... args)
-{
- return ::std::stoi(std::forward<Ts>(args)...);
-}
-
-/** Convert string values to unsigned long.
- *
- * @note This function acts as a convenience wrapper around std::stoul. The
- * latter is missing in some Android toolchains.
- *
- * @param[in] args Arguments forwarded to std::stoul.
- *
- * @return Unsigned long representation of input string.
- */
-template <typename... Ts>
-int stoul(Ts &&... args)
-{
- return ::std::stoul(std::forward<Ts>(args)...);
-}
-
/** Convert string values to float.
*
* @note This function acts as a convenience wrapper around std::stof. The