summaryrefslogtreecommitdiffstats
path: root/searchlib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-06-14 17:46:06 +0200
committerGitHub <noreply@github.com>2019-06-14 17:46:06 +0200
commitd4c9a66d62ad56b23aafdcae8dad28c5efebc259 (patch)
treea87859011a2d7d984d8e47ca01e8ee4054b4f157 /searchlib
parent066304231cea6f9517ee0c532cce0e23fdc1ddd1 (diff)
parent44f5b006837d5905c8140813e2ca48e2998cc09d (diff)
Merge pull request #9811 from vespa-engine/balder/use-from_chars
Use from_chars
Diffstat (limited to 'searchlib')
-rw-r--r--searchlib/src/tests/attribute/enumstore/enumstore_test.cpp6
-rw-r--r--searchlib/src/tests/features/util/util_test.cpp20
-rw-r--r--searchlib/src/vespa/searchlib/features/utils.cpp31
3 files changed, 46 insertions, 11 deletions
diff --git a/searchlib/src/tests/attribute/enumstore/enumstore_test.cpp b/searchlib/src/tests/attribute/enumstore/enumstore_test.cpp
index 6b6abd956fc..5476f0f8e66 100644
--- a/searchlib/src/tests/attribute/enumstore/enumstore_test.cpp
+++ b/searchlib/src/tests/attribute/enumstore/enumstore_test.cpp
@@ -1,13 +1,13 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-#include <vespa/log/log.h>
-LOG_SETUP("enumstore_test");
#include <vespa/vespalib/testkit/testapp.h>
-//#define LOG_ENUM_STORE
#include <vespa/searchlib/attribute/enumstore.hpp>
#include <limits>
#include <string>
#include <iostream>
+#include <vespa/log/log.h>
+LOG_SETUP("enumstore_test");
+
namespace search {
size_t enumStoreAlign(size_t size)
diff --git a/searchlib/src/tests/features/util/util_test.cpp b/searchlib/src/tests/features/util/util_test.cpp
index 6b166682346..208c290cff3 100644
--- a/searchlib/src/tests/features/util/util_test.cpp
+++ b/searchlib/src/tests/features/util/util_test.cpp
@@ -8,6 +8,7 @@ using namespace search;
using namespace search::fef;
using namespace search::fef::test;
using namespace search::features;
+using namespace search::features::util;
SimpleTermData make_term(uint32_t uid) {
SimpleTermData term;
@@ -37,4 +38,23 @@ TEST_F("require that label can be mapped to term", TermLabelFixture) {
EXPECT_EQUAL((ITermData*)0, util::getTermByLabel(f1.queryEnv, "unknown"));
}
+template <typename T>
+void verifyStrToNum() {
+ EXPECT_EQUAL(-17, static_cast<long>(strToNum<T>("-17")));
+ EXPECT_EQUAL(-1, static_cast<long>(strToNum<T>("-1")));
+ EXPECT_EQUAL(0, static_cast<long>(strToNum<T>("0")));
+ EXPECT_EQUAL(1, static_cast<long>(strToNum<T>("1")));
+ EXPECT_EQUAL(17, static_cast<long>(strToNum<T>("17")));
+ EXPECT_EQUAL(0, static_cast<long>(strToNum<T>("0x0")));
+ EXPECT_EQUAL(1, static_cast<long>(strToNum<T>("0x1")));
+ EXPECT_EQUAL(27, static_cast<long>(strToNum<T>("0x1b")));
+}
+
+TEST("verify str2Num") {
+ verifyStrToNum<int8_t>();
+ verifyStrToNum<int16_t>();
+ verifyStrToNum<int32_t>();
+ verifyStrToNum<int64_t>();
+}
+
TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/searchlib/src/vespa/searchlib/features/utils.cpp b/searchlib/src/vespa/searchlib/features/utils.cpp
index 3f68e69ff25..7bfea67b7f6 100644
--- a/searchlib/src/vespa/searchlib/features/utils.cpp
+++ b/searchlib/src/vespa/searchlib/features/utils.cpp
@@ -1,28 +1,43 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "utils.hpp"
+#include <charconv>
namespace search::features::util {
+template <typename T>
+T strToInt(vespalib::stringref str)
+{
+ T retval = 0;
+ if ((str.size() > 2) && (str[0] == '0') && ((str[1] | 0x20) == 'x')) {
+ std::from_chars(str.data()+2, str.data()+str.size(), retval, 16);
+ } else {
+ std::from_chars(str.data(), str.data()+str.size(), retval, 10);
+ }
+
+ return retval;
+}
+
template <>
uint8_t
strToNum<uint8_t>(vespalib::stringref str) {
- return strToNum<uint16_t>(str);
+ return strToInt<uint16_t>(str);
}
template <>
int8_t
strToNum<int8_t>(vespalib::stringref str) {
- return strToNum<int16_t>(str);
+ return strToInt<int16_t>(str);
}
template double strToNum<double>(vespalib::stringref str);
template float strToNum<float>(vespalib::stringref str);
-template uint16_t strToNum<uint16_t>(vespalib::stringref str);
-template uint32_t strToNum<uint32_t>(vespalib::stringref str);
-template uint64_t strToNum<uint64_t>(vespalib::stringref str);
-template int16_t strToNum<int16_t>(vespalib::stringref str);
-template int32_t strToNum<int32_t>(vespalib::stringref str);
-template int64_t strToNum<int64_t>(vespalib::stringref str);
+
+template <> uint16_t strToNum<uint16_t>(vespalib::stringref str) { return strToInt<uint16_t>(str); }
+template <> uint32_t strToNum<uint32_t>(vespalib::stringref str) { return strToInt<uint32_t>(str); }
+template <> uint64_t strToNum<uint64_t>(vespalib::stringref str) { return strToInt<uint64_t>(str); }
+template <> int16_t strToNum<int16_t>(vespalib::stringref str) { return strToInt<int16_t>(str); }
+template <> int32_t strToNum<int32_t>(vespalib::stringref str) { return strToInt<int32_t>(str); }
+template <> int64_t strToNum<int64_t>(vespalib::stringref str) { return strToInt<int64_t>(str); }
}