summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2024-01-02 19:34:49 +0100
committerGitHub <noreply@github.com>2024-01-02 19:34:49 +0100
commitf031666ee60ab878782bc4490aa697635c43026d (patch)
treef1f02def2b05a230637263ee1430c16deb496031
parent124240e25bc8521222de04652b4af72879dbb4bb (diff)
parent93cc4a3144fe833eab4677a2eddc622506d646d6 (diff)
Merge pull request #29762 from vespa-engine/balder/strtoll-2-from_chars
Use std::from_chars instead of strtoll.
-rw-r--r--searchlib/src/vespa/searchlib/query/query_term_simple.cpp33
-rw-r--r--vespalib/src/vespa/vespalib/locale/locale.h2
2 files changed, 24 insertions, 11 deletions
diff --git a/searchlib/src/vespa/searchlib/query/query_term_simple.cpp b/searchlib/src/vespa/searchlib/query/query_term_simple.cpp
index 8df7764c183..fb2be58c536 100644
--- a/searchlib/src/vespa/searchlib/query/query_term_simple.cpp
+++ b/searchlib/src/vespa/searchlib/query/query_term_simple.cpp
@@ -7,6 +7,7 @@
#include <vespa/vespalib/locale/c.h>
#include <cmath>
#include <limits>
+#include <charconv>
namespace {
@@ -161,24 +162,35 @@ QueryTermSimple::getRange() const
return getIntegerRange<int64_t>();
}
-template <int B>
struct IntDecoder {
- static int64_t fromstr(const char * v, char ** end) { return strtoll(v, end, B); }
- static int64_t nearestDownwd(int64_t n, int64_t min) { return (n > min ? n - 1 : n); }
- static int64_t nearestUpward(int64_t n, int64_t max) { return (n < max ? n + 1 : n); }
+ static int64_t fromstr(const char * q, const char * qend, char ** end) noexcept {
+ int64_t v(0);
+ for (;q < qend && isspace(*q); q++);
+ std::from_chars_result err = std::from_chars(q, qend, v, 10);
+ if (err.ec == std::errc::result_out_of_range) {
+ v = (*q == '-') ? std::numeric_limits<int64_t>::min() : std::numeric_limits<int64_t>::max();
+ }
+ *end = const_cast<char *>(err.ptr);
+ return v;
+ }
+ static int64_t nearestDownwd(int64_t n, int64_t min) noexcept { return (n > min ? n - 1 : n); }
+ static int64_t nearestUpward(int64_t n, int64_t max) noexcept { return (n < max ? n + 1 : n); }
};
struct DoubleDecoder {
- static double fromstr(const char * v, char ** end) { return vespalib::locale::c::strtod(v, end); }
- static double nearestDownwd(double n, double min) { return std::nextafterf(n, min); }
- static double nearestUpward(double n, double max) { return std::nextafterf(n, max); }
+ static double fromstr(const char * q, const char * qend, char ** end) {
+ (void) qend;
+ return vespalib::locale::c::strtod(q, end);
+ }
+ static double nearestDownwd(double n, double min) noexcept { return std::nextafterf(n, min); }
+ static double nearestUpward(double n, double max) noexcept { return std::nextafterf(n, max); }
};
bool QueryTermSimple::getAsIntegerTerm(int64_t & lower, int64_t & upper) const
{
lower = std::numeric_limits<int64_t>::min();
upper = std::numeric_limits<int64_t>::max();
- return getAsNumericTerm(lower, upper, IntDecoder<10>());
+ return getAsNumericTerm(lower, upper, IntDecoder());
}
bool QueryTermSimple::getAsDoubleTerm(double & lower, double & upper) const
@@ -266,11 +278,12 @@ QueryTermSimple::getAsNumericTerm(T & lower, T & upper, D d) const
T low(lower);
T high(upper);
const char * q = _term.c_str();
+ const char * qend = q + sz;
const char first(q[0]);
const char last(q[sz-1]);
bool isRange = (first == '<') || (first == '>') || (first == '[');
q += isRange ? 1 : 0;
- T ll = d.fromstr(q, &err);
+ T ll = d.fromstr(q, qend, &err);
bool valid = isValid() && ((*err == 0) || (*err == ';'));
if (!valid) return false;
@@ -289,7 +302,7 @@ QueryTermSimple::getAsNumericTerm(T & lower, T & upper, D d) const
low = (first == '[') ? ll : d.nearestUpward(ll, upper);
}
q = err + 1;
- T hh = d.fromstr(q, &err);
+ T hh = d.fromstr(q, qend, &err);
bool hasUpperLimit(q != err);
if (*err == ';') {
err = const_cast<char *>(_term.end() - 1);
diff --git a/vespalib/src/vespa/vespalib/locale/locale.h b/vespalib/src/vespa/vespalib/locale/locale.h
index 10ac6144fe7..16aa7a77d98 100644
--- a/vespalib/src/vespa/vespalib/locale/locale.h
+++ b/vespalib/src/vespa/vespalib/locale/locale.h
@@ -14,7 +14,7 @@ public:
Locale(); // Standard C locale, NOT default locale.
Locale(int category, const char *locale);
~Locale();
- locale_t get() const { return _locale; }
+ locale_t get() const noexcept { return _locale; }
private:
locale_t _locale;
};