diff --git a/common/utils/UtilsTest.cpp b/common/utils/UtilsTest.cpp index 32edab397b81652ecd587ff2cabe996dbea1419e..8a9d00faa72e2b684da6b68f440d1cbbf7a6cc72 100644 --- a/common/utils/UtilsTest.cpp +++ b/common/utils/UtilsTest.cpp @@ -17,6 +17,7 @@ */ #include "common/utils/utils.hpp" +#include "common/exception/Exception.hpp" #include <gtest/gtest.h> @@ -452,6 +453,37 @@ TEST_F(cta_UtilsTest, isValidUInt_not_a_number) { ASSERT_FALSE(utils::isValidUInt("one")); } +TEST_F(cta_UtilsTest, toUint64_unsigned_int) { + using namespace cta; + + ASSERT_EQ((uint64_t)12345, utils::toUint64("12345")); + ASSERT_EQ((uint64_t)18446744073709551615ULL, utils::toUint64("18446744073709551615")); +} + +TEST_F(cta_UtilsTest, toUint64_too_big) { + using namespace cta; + + ASSERT_THROW(utils::toUint64("18446744073709551616"), exception::Exception); +} + +TEST_F(cta_UtilsTest, toUint64_empty_string) { + using namespace cta; + + ASSERT_THROW(utils::toUint64(""), exception::Exception); +} + +TEST_F(cta_UtilsTest, toUint64_minus_one) { + using namespace cta; + + ASSERT_EQ((uint64_t)18446744073709551615UL, utils::toUint64("18446744073709551615")); +} + +TEST_F(cta_UtilsTest, toUint64_not_a_number) { + using namespace cta; + + ASSERT_THROW(utils::toUint64("one"), exception::Exception); +} + TEST_F(cta_UtilsTest, adler32_empty_buf) { using namespace cta; diff --git a/common/utils/utils.cpp b/common/utils/utils.cpp index 23c0fe502db62462d81d77be523d227eb4b191c0..a01e74148972f96bab87e4154e9173c1c57228f5 100644 --- a/common/utils/utils.cpp +++ b/common/utils/utils.cpp @@ -525,8 +525,7 @@ gid_t toGid(const std::string &str) { //------------------------------------------------------------------------------ // isValidUInt //------------------------------------------------------------------------------ -bool isValidUInt(const std::string &str) - throw() { +bool isValidUInt(const std::string &str) throw() { // An empty string is not a valid unsigned integer if(str.empty()) { return false; @@ -545,6 +544,26 @@ bool isValidUInt(const std::string &str) return true; } +//------------------------------------------------------------------------------ +// toUint64 +//------------------------------------------------------------------------------ +uint64_t toUint64(const std::string &str) { + try { + try { + return std::stoul(str); + } catch(std::invalid_argument &) { + throw exception::Exception("Invalid string"); + } catch(std::out_of_range &) { + throw exception::Exception("Out of range"); + } catch(std::exception &se) { + throw exception::Exception(se.what()); + } + } catch(exception::Exception &ex) { + throw exception::Exception(std::string("Failed to parse ") + str + " as an unsigned 64-bit integer: " + + ex.getMessage().str()); + } +} + //------------------------------------------------------------------------------ // toUpper //------------------------------------------------------------------------------ diff --git a/common/utils/utils.hpp b/common/utils/utils.hpp index e368d660e89272360674e39b23132cdbce21c2f1..1f14744460bce42f5c14bd8c09aaf08926db6ab9 100644 --- a/common/utils/utils.hpp +++ b/common/utils/utils.hpp @@ -202,6 +202,15 @@ namespace utils { */ bool isValidUInt(const std::string &str) throw(); + /** + * Parses the specified string representation of an unsigned 64-bit integer. + * + * Please note that "-1" is a valid string and will parse successfully. + * + * @return The parsed unsigned 64-bit integer. + */ + uint64_t toUint64(const std::string &str); + /** * Converts the specified string to uppercase. *