[Libreoffice-commits] online.git: common/Protocol.cpp common/Protocol.hpp common/Util.hpp
Ashod Nakashian (via logerrit)
logerrit at kemper.freedesktop.org
Thu Jul 2 00:38:54 UTC 2020
common/Protocol.cpp | 42 -------------------------
common/Protocol.hpp | 23 ++++++++++++-
common/Util.hpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++---
3 files changed, 102 insertions(+), 49 deletions(-)
New commits:
commit 65c245eab0b9c43866cfa7069cdd6f3889a0a23e
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Tue Jun 2 18:19:23 2020 -0400
Commit: Ashod Nakashian <ashnakash at gmail.com>
CommitDate: Thu Jul 2 02:38:35 2020 +0200
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 at gmail.com>
Tested-by: Jenkins
Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
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
More information about the Libreoffice-commits
mailing list