diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index d7e3a2cdb0b..c1dadeee087 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -28,13 +28,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include #include diff --git a/src/libexpr/lexer.l b/src/libexpr/lexer.l index ee2b6b80790..8c0f9d1f2f7 100644 --- a/src/libexpr/lexer.l +++ b/src/libexpr/lexer.l @@ -20,8 +20,6 @@ #pragma clang diagnostic ignored "-Wunneeded-internal-declaration" #endif -#include - #include "nixexpr.hh" #include "parser-tab.hh" @@ -129,9 +127,10 @@ or { return OR_KW; } {ID} { yylval->id = {yytext, (size_t) yyleng}; return ID; } {INT} { errno = 0; - try { - yylval->n = boost::lexical_cast(yytext); - } catch (const boost::bad_lexical_cast &) { + std::optional numMay = string2Int(yytext); + if (numMay.has_value()) { + yylval->n = *numMay; + } else { throw ParseError(ErrorInfo{ .msg = HintFmt("invalid integer '%1%'", yytext), .pos = state->positions[CUR_POS], diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index c1e2b044882..44198a25238 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -6,6 +6,7 @@ #include "print.hh" #include +#include namespace nix { diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 1b1a9be3d02..7371bd488f0 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -26,6 +26,7 @@ #include #include +#include #include #ifndef _WIN32 diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc index d53fadac75a..920490cfad4 100644 --- a/src/libexpr/print.cc +++ b/src/libexpr/print.cc @@ -1,5 +1,6 @@ #include #include +#include #include "print.hh" #include "ansicolor.hh" diff --git a/src/libmain/progress-bar.cc b/src/libmain/progress-bar.cc index ce45eae2ba1..bb4c52ef7ea 100644 --- a/src/libmain/progress-bar.cc +++ b/src/libmain/progress-bar.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index 5153ca64fb1..56e47697c96 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index eb6a4e69039..4e231ca99eb 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -19,6 +19,8 @@ # include "monitor-fd.hh" #endif +#include + namespace nix::daemon { Sink & operator << (Sink & sink, const Logger::Fields & fields) diff --git a/src/libstore/machines.cc b/src/libstore/machines.cc index 20f24e8833b..256cf918892 100644 --- a/src/libstore/machines.cc +++ b/src/libstore/machines.cc @@ -148,7 +148,7 @@ static Machine parseBuilderLine(const std::set & defaultSystems, co }; auto parseFloatField = [&](size_t fieldIndex) { - const auto result = string2Int(tokens[fieldIndex]); + const auto result = string2Float(tokens[fieldIndex]); if (!result) { throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'float'", fieldIndex, line); } diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index 9efb68d47a2..6ca48220d21 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -7,6 +7,7 @@ #include "file-system.hh" #include "processes.hh" #include "signals.hh" +#include #ifdef __APPLE__ # include diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 933e88441e7..c6b6ecedbb7 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -20,8 +20,6 @@ #endif #include -#include - #include #include #include diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 2511c88499f..5fa01f0d969 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -8,6 +8,7 @@ #include "position.hh" #include +#include #include #include diff --git a/src/libutil/processes.hh b/src/libutil/processes.hh index 9d5367b024c..168fcaa5559 100644 --- a/src/libutil/processes.hh +++ b/src/libutil/processes.hh @@ -12,8 +12,6 @@ #include #include -#include - #include #include #include diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 103ce4232b3..99fbcaf9daa 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -7,6 +7,8 @@ #include #include +#include +#include #ifdef NDEBUG #error "Nix may not be built with assertions disabled (i.e. with -DNDEBUG)." @@ -111,6 +113,43 @@ std::string rewriteStrings(std::string s, const StringMap & rewrites) return s; } +template +std::optional string2Int(const std::string_view s) +{ + if (s.substr(0, 1) == "-" && !std::numeric_limits::is_signed) + return std::nullopt; + try { + return boost::lexical_cast(s.data(), s.size()); + } catch (const boost::bad_lexical_cast &) { + return std::nullopt; + } +} + +// Explicitly instantiated in one place for faster compilation +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); +template std::optional string2Int(const std::string_view s); + +template +std::optional string2Float(const std::string_view s) +{ + try { + return boost::lexical_cast(s.data(), s.size()); + } catch (const boost::bad_lexical_cast &) { + return std::nullopt; + } +} + +template std::optional string2Float(const std::string_view s); +template std::optional string2Float(const std::string_view s); + bool hasPrefix(std::string_view s, std::string_view prefix) { diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 8b049875a89..0919f43c3a3 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -5,7 +5,6 @@ #include "error.hh" #include "logging.hh" -#include #include #include @@ -102,16 +101,7 @@ std::string rewriteStrings(std::string s, const StringMap & rewrites); * Parse a string into an integer. */ template -std::optional string2Int(const std::string_view s) -{ - if (s.substr(0, 1) == "-" && !std::numeric_limits::is_signed) - return std::nullopt; - try { - return boost::lexical_cast(s.data(), s.size()); - } catch (const boost::bad_lexical_cast &) { - return std::nullopt; - } -} +std::optional string2Int(const std::string_view s); /** * Like string2Int(), but support an optional suffix 'K', 'M', 'G' or @@ -141,14 +131,7 @@ N string2IntWithUnitPrefix(std::string_view s) * Parse a string into a float. */ template -std::optional string2Float(const std::string_view s) -{ - try { - return boost::lexical_cast(s.data(), s.size()); - } catch (const boost::bad_lexical_cast &) { - return std::nullopt; - } -} +std::optional string2Float(const std::string_view s); /** diff --git a/src/nix-env/user-env.cc b/src/nix-env/user-env.cc index f7b091f8f56..5246b03e4cd 100644 --- a/src/nix-env/user-env.cc +++ b/src/nix-env/user-env.cc @@ -9,8 +9,9 @@ #include "eval-inline.hh" #include "profiles.hh" #include "print-ambiguous.hh" -#include +#include +#include namespace nix { diff --git a/src/nix/develop.cc b/src/nix/develop.cc index 0363ca8294a..27287a1a87a 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -14,6 +14,7 @@ #include #include +#include #include #include