summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2020-06-02 18:19:23 -0400
committerAshod Nakashian <ashnakash@gmail.com>2020-07-02 02:38:35 +0200
commit65c245eab0b9c43866cfa7069cdd6f3889a0a23e (patch)
treecebf8e4f4da284a263b3e5f0e66ca693cfcdd90e /common
parent4082a462dad52151907fe71f5780cb82b0d272a7 (diff)
wsd: move string-to-integer helper to Util
Improved implementation. Change-Id: I0b426f8742c8b718f8c939d271f6645a8ed466d4 Reviewed-on: https://gerrit.libreoffice.org/c/online/+/96374 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Tested-by: Jenkins Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Diffstat (limited to 'common')
-rw-r--r--common/Protocol.cpp42
-rw-r--r--common/Protocol.hpp23
-rw-r--r--common/Util.hpp86
3 files changed, 102 insertions, 49 deletions
diff --git a/common/Protocol.cpp b/common/Protocol.cpp
index 771b87b49..d02f2d340 100644
--- a/common/Protocol.cpp
+++ b/common/Protocol.cpp
@@ -48,48 +48,6 @@ namespace LOOLProtocol
return std::make_tuple(major, minor, patch);
}
- bool stringToInteger(const std::string& input, int& value)
- {
- try
- {
- value = std::stoi(input);
- }
- catch (std::invalid_argument&)
- {
- return false;
- }
-
- return true;
- }
-
- bool stringToUInt32(const std::string& input, uint32_t& value)
- {
- try
- {
- value = std::stoul(input);
- }
- catch (std::invalid_argument&)
- {
- return false;
- }
-
- return true;
- }
-
- bool stringToUInt64(const std::string& input, uint64_t& value)
- {
- try
- {
- value = std::stoull(input);
- }
- catch (std::invalid_argument&)
- {
- return false;
- }
-
- return true;
- }
-
bool getTokenInteger(const std::string& token, const std::string& name, int& value)
{
if (token.size() > (name.size() + 1) &&
diff --git a/common/Protocol.hpp b/common/Protocol.hpp
index 5eb90941b..367d9ff27 100644
--- a/common/Protocol.hpp
+++ b/common/Protocol.hpp
@@ -41,9 +41,26 @@ namespace LOOLProtocol
// Negative numbers for error.
std::tuple<int, int, std::string> ParseVersion(const std::string& version);
- bool stringToInteger(const std::string& input, int& value);
- bool stringToUInt32(const std::string& input, uint32_t& value);
- bool stringToUInt64(const std::string& input, uint64_t& value);
+ inline bool stringToInteger(const std::string& input, int& value)
+ {
+ bool res;
+ std::tie(value, res) = Util::i32FromString(input);
+ return res;
+ }
+
+ inline bool stringToUInt32(const std::string& input, uint32_t& value)
+ {
+ bool res;
+ std::tie(value, res) = Util::i32FromString(input);
+ return res;
+ }
+
+ inline bool stringToUInt64(const std::string& input, uint64_t& value)
+ {
+ bool res;
+ std::tie(value, res) = Util::u64FromString(input);
+ return res;
+ }
inline
bool parseNameValuePair(const std::string& token, std::string& name, std::string& value, const char delim = '=')
diff --git a/common/Util.hpp b/common/Util.hpp
index ba70cba47..6dcfa3d48 100644
--- a/common/Util.hpp
+++ b/common/Util.hpp
@@ -13,6 +13,7 @@
#include <cerrno>
#include <cinttypes>
#include <cstddef>
+#include <cstdint>
#include <cstring>
#include <atomic>
#include <functional>
@@ -22,6 +23,7 @@
#include <sstream>
#include <string>
#include <map>
+#include <utility>
#include <inttypes.h>
#include <memory.h>
@@ -1129,10 +1131,86 @@ int main(int argc, char**argv)
*/
std::map<std::string, std::string> stringVectorToMap(std::vector<std::string> sVector, const char delimiter);
- #if !MOBILEAPP
- // If OS is not mobile, it must be Linux.
- std::string getLinuxVersion();
- #endif
+#if !MOBILEAPP
+ // If OS is not mobile, it must be Linux.
+ std::string getLinuxVersion();
+#endif
+
+ /// Convert a string to 32-bit signed int.
+ /// Returs the parsed value and a boolean indiciating success or failure.
+ inline std::pair<std::int32_t, bool> i32FromString(const std::string& input)
+ {
+ const char* str = input.data();
+ char* endptr = nullptr;
+ const auto value = std::strtol(str, &endptr, 10);
+ return std::make_pair(value, endptr > str && errno != ERANGE);
+ }
+
+ /// Convert a string to 32-bit signed int. On failure, returns the default
+ /// value, and sets the bool to false (to signify that parsing had failed).
+ inline std::pair<std::int32_t, bool> i32FromString(const std::string& input,
+ const std::int32_t def)
+ {
+ const auto pair = i32FromString(input);
+ return pair.second ? pair : std::make_pair(def, false);
+ }
+
+ /// Convert a string to 32-bit unsigned int.
+ /// Returs the parsed value and a boolean indiciating success or failure.
+ inline std::pair<std::uint32_t, bool> u32FromString(const std::string& input)
+ {
+ const char* str = input.data();
+ char* endptr = nullptr;
+ const auto value = std::strtoul(str, &endptr, 10);
+ return std::make_pair(value, endptr > str && errno != ERANGE);
+ }
+
+ /// Convert a string to 32-bit usigned int. On failure, returns the default
+ /// value, and sets the bool to false (to signify that parsing had failed).
+ inline std::pair<std::uint32_t, bool> u32FromString(const std::string& input,
+ const std::uint32_t def)
+ {
+ const auto pair = u32FromString(input);
+ return pair.second ? pair : std::make_pair(def, false);
+ }
+
+ /// Convert a string to 64-bit signed int.
+ /// Returs the parsed value and a boolean indiciating success or failure.
+ inline std::pair<std::int64_t, bool> i64FromString(const std::string& input)
+ {
+ const char* str = input.data();
+ char* endptr = nullptr;
+ const auto value = std::strtol(str, &endptr, 10);
+ return std::make_pair(value, endptr > str && errno != ERANGE);
+ }
+
+ /// Convert a string to 64-bit signed int. On failure, returns the default
+ /// value, and sets the bool to false (to signify that parsing had failed).
+ inline std::pair<std::int64_t, bool> i64FromString(const std::string& input,
+ const std::int64_t def)
+ {
+ const auto pair = i64FromString(input);
+ return pair.second ? pair : std::make_pair(def, false);
+ }
+
+ /// Convert a string to 64-bit unsigned int.
+ /// Returs the parsed value and a boolean indiciating success or failure.
+ inline std::pair<std::uint64_t, bool> u64FromString(const std::string& input)
+ {
+ const char* str = input.data();
+ char* endptr = nullptr;
+ const auto value = std::strtoul(str, &endptr, 10);
+ return std::make_pair(value, endptr > str && errno != ERANGE);
+ }
+
+ /// Convert a string to 64-bit usigned int. On failure, returns the default
+ /// value, and sets the bool to false (to signify that parsing had failed).
+ inline std::pair<std::uint64_t, bool> u64FromString(const std::string& input,
+ const std::uint64_t def)
+ {
+ const auto pair = u64FromString(input);
+ return pair.second ? pair : std::make_pair(def, false);
+ }
} // end namespace Util