diff options
author | jade <lix@jade.fyi> | 2024-05-30 14:57:37 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@lix-systems> | 2024-05-30 14:57:37 +0000 |
commit | 18aa3e1d570b4ecbb9962376e5fba5757dad8da9 (patch) | |
tree | ad9edb3d85c7f3e9b6a33f65efeebbdbc9bd243b /src/libutil/strings.cc | |
parent | 53d40888ffe8238ab5baf6a9d1a7481c29e9c65d (diff) | |
parent | 7575db522e9008685c4009423398f6900a16bcce (diff) |
Merge "Remove 100s of CPU time (10%) from build times (1465s -> 1302s)" into main
Diffstat (limited to 'src/libutil/strings.cc')
-rw-r--r-- | src/libutil/strings.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/libutil/strings.cc b/src/libutil/strings.cc index 9cb319cce..947478481 100644 --- a/src/libutil/strings.cc +++ b/src/libutil/strings.cc @@ -1,4 +1,6 @@ #include "strings.hh" +#include <boost/lexical_cast.hpp> +#include <stdint.h> namespace nix { @@ -89,6 +91,43 @@ std::string Rewriter::operator()(std::string s) return s; } +template<class N> +std::optional<N> string2Int(const std::string_view s) +{ + if (s.substr(0, 1) == "-" && !std::numeric_limits<N>::is_signed) + return std::nullopt; + try { + return boost::lexical_cast<N>(s.data(), s.size()); + } catch (const boost::bad_lexical_cast &) { + return std::nullopt; + } +} + +// Explicitly instantiated in one place for faster compilation +template std::optional<unsigned char> string2Int<unsigned char>(const std::string_view s); +template std::optional<unsigned short> string2Int<unsigned short>(const std::string_view s); +template std::optional<unsigned int> string2Int<unsigned int>(const std::string_view s); +template std::optional<unsigned long> string2Int<unsigned long>(const std::string_view s); +template std::optional<unsigned long long> string2Int<unsigned long long>(const std::string_view s); +template std::optional<signed char> string2Int<signed char>(const std::string_view s); +template std::optional<signed short> string2Int<signed short>(const std::string_view s); +template std::optional<signed int> string2Int<signed int>(const std::string_view s); +template std::optional<signed long> string2Int<signed long>(const std::string_view s); +template std::optional<signed long long> string2Int<signed long long>(const std::string_view s); + +template<class N> +std::optional<N> string2Float(const std::string_view s) +{ + try { + return boost::lexical_cast<N>(s.data(), s.size()); + } catch (const boost::bad_lexical_cast &) { + return std::nullopt; + } +} + +template std::optional<double> string2Float<double>(const std::string_view s); +template std::optional<float> string2Float<float>(const std::string_view s); + std::string toLower(const std::string & s) { |