From 263b259b276edd075b0abcd6aad0e923230c2d15 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Fri, 7 Dec 2018 02:22:20 +0100 Subject: Various speed optimizations --- src/internal.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/internal.cpp') diff --git a/src/internal.cpp b/src/internal.cpp index 4bec1bf9..ecb724c2 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -32,6 +32,7 @@ #include "proj/internal/internal.hpp" +#include #include #ifdef _MSC_VER #include @@ -237,6 +238,38 @@ bool ends_with(const std::string &str, const std::string &suffix) noexcept { // --------------------------------------------------------------------------- double c_locale_stod(const std::string &s) { + + const auto s_size = s.size(); + // Fast path + if (s_size > 0 && s_size < 15) { + std::int64_t acc = 0; + std::int64_t div = 1; + bool afterDot = false; + size_t i = 0; + if (s[0] == '-') { + ++i; + div = -1; + } else if (s[0] == '+') { + ++i; + } + for (; i < s_size; ++i) { + const auto ch = s[i]; + if (ch >= '0' && ch <= '9') { + acc = acc * 10 + ch - '0'; + if (afterDot) { + div *= 10; + } + } else if (ch == '.') { + afterDot = true; + } else { + div = 0; + } + } + if (div) { + return static_cast(acc) / div; + } + } + std::istringstream iss(s); iss.imbue(std::locale::classic()); double d; -- cgit v1.2.3