diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-11-20 13:38:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-20 13:38:24 +0100 |
commit | f440d2e1b8c25e46e2df9e8c70742d04725d5358 (patch) | |
tree | 0bdfd64914dc340bbcb24ed9c6c92cfa5edceca9 | |
parent | 5490cc04d10487038eadf66012ce96b11779185b (diff) | |
parent | 44833817233272a3e128979cbebb5cc74ba23b6d (diff) |
Merge pull request #29380 from vespa-engine/balder/avoid-atoi
atoi => std::from_chars
-rw-r--r-- | searchlib/src/tests/fef/properties/properties_test.cpp | 41 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/fef/indexproperties.cpp | 10 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/fef/properties.cpp | 16 |
3 files changed, 54 insertions, 13 deletions
diff --git a/searchlib/src/tests/fef/properties/properties_test.cpp b/searchlib/src/tests/fef/properties/properties_test.cpp index 5e18c41b40a..c8073739b3e 100644 --- a/searchlib/src/tests/fef/properties/properties_test.cpp +++ b/searchlib/src/tests/fef/properties/properties_test.cpp @@ -10,9 +10,8 @@ using namespace search::fef::indexproperties; struct CopyVisitor : public IPropertiesVisitor { Properties &dst; - CopyVisitor(Properties &p) : dst(p) {} - virtual void visitProperty(const Property::Value &key, - const Property &values) override + explicit CopyVisitor(Properties &p) noexcept : dst(p) {} + void visitProperty(const Property::Value &key, const Property &values) override { for (uint32_t i = 0; i < values.size(); ++i) { dst.add(key, values.getAt(i)); @@ -590,5 +589,41 @@ TEST("test query feature type properties") EXPECT_EQUAL("", type::QueryFeature::lookup(p, "bar")); } +TEST("test integer lookup") +{ + EXPECT_EQUAL(matching::NumThreadsPerSearch::NAME, vespalib::string("vespa.matching.numthreadspersearch")); + EXPECT_EQUAL(matching::NumThreadsPerSearch::DEFAULT_VALUE, std::numeric_limits<uint32_t>::max()); + { + Properties p; + p.add("vespa.matching.numthreadspersearch", "50"); + EXPECT_EQUAL(matching::NumThreadsPerSearch::lookup(p), 50u); + } + { + Properties p; + p.add("vespa.matching.numthreadspersearch", "50 "); + EXPECT_EQUAL(matching::NumThreadsPerSearch::lookup(p), 50u); + } + { + Properties p; + p.add("vespa.matching.numthreadspersearch", " 50"); + EXPECT_EQUAL(matching::NumThreadsPerSearch::lookup(p), 50u); + } + { + Properties p; + p.add("vespa.matching.numthreadspersearch", " "); + EXPECT_EQUAL(matching::NumThreadsPerSearch::lookup(p), matching::NumThreadsPerSearch::DEFAULT_VALUE); + } + { + Properties p; + p.add("vespa.matching.numthreadspersearch", "50x"); + EXPECT_EQUAL(matching::NumThreadsPerSearch::lookup(p), 50u); + } + { + Properties p; + p.add("vespa.matching.numthreadspersearch", "x"); + EXPECT_EQUAL(matching::NumThreadsPerSearch::lookup(p), matching::NumThreadsPerSearch::DEFAULT_VALUE); + } +} + TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/searchlib/src/vespa/searchlib/fef/indexproperties.cpp b/searchlib/src/vespa/searchlib/fef/indexproperties.cpp index 5d3dbe69e77..9c986d0bc63 100644 --- a/searchlib/src/vespa/searchlib/fef/indexproperties.cpp +++ b/searchlib/src/vespa/searchlib/fef/indexproperties.cpp @@ -4,6 +4,7 @@ #include "properties.h" #include <vespa/vespalib/locale/c.h> #include <limits> +#include <charconv> namespace search::fef::indexproperties { @@ -49,10 +50,15 @@ uint32_t lookupUint32(const Properties &props, const vespalib::string &name, uint32_t defaultValue) { Property p = props.lookup(name); + uint32_t value(defaultValue); if (p.found()) { - return atoi(p.get().c_str()); + const auto & valS = p.get(); + const char * start = valS.c_str(); + const char * end = start + valS.size(); + while ((start != end) && isspace(start[0])) { start++; } + std::from_chars(start, end, value); } - return defaultValue; + return value; } bool diff --git a/searchlib/src/vespa/searchlib/fef/properties.cpp b/searchlib/src/vespa/searchlib/fef/properties.cpp index bd4795fcc5a..6134807fa60 100644 --- a/searchlib/src/vespa/searchlib/fef/properties.cpp +++ b/searchlib/src/vespa/searchlib/fef/properties.cpp @@ -25,7 +25,7 @@ uint32_t Properties::rawHash(const void *buf, uint32_t len) noexcept { uint32_t res = 0; - unsigned const char *pt = (unsigned const char *) buf; + auto *pt = (unsigned const char *) buf; unsigned const char *end = pt + len; while (pt < end) { res = (res << 7) + (res >> 25) + *pt++; @@ -52,7 +52,7 @@ Properties::add(vespalib::stringref key, vespalib::stringref value) { if (!key.empty()) { Value & v = _data[key]; - v.push_back(value); + v.emplace_back(value); ++_numValues; } return *this; @@ -162,20 +162,20 @@ Property Properties::lookup(vespalib::stringref key) const noexcept { if (key.empty()) { - return Property(); + return {}; } auto node = _data.find(key); if (node == _data.end()) { - return Property(); + return {}; } - return Property(node->second); + return {node->second}; } Property Properties::lookup(vespalib::stringref namespace1, vespalib::stringref key) const noexcept { if (namespace1.empty() || key.empty()) { - return Property(); + return {}; } vespalib::string fullKey(namespace1); fullKey.append('.').append(key); @@ -187,7 +187,7 @@ Property Properties::lookup(vespalib::stringref namespace1, vespalib::stringref key) const noexcept { if (namespace1.empty() || namespace2.empty() || key.empty()) { - return Property(); + return {}; } vespalib::string fullKey(namespace1); fullKey.append('.').append(namespace2).append('.').append(key); @@ -200,7 +200,7 @@ Property Properties::lookup(vespalib::stringref namespace1, vespalib::stringref key) const noexcept { if (namespace1.empty() || namespace2.empty() || namespace3.empty() || key.empty()) { - return Property(); + return {}; } vespalib::string fullKey(namespace1); fullKey.append('.').append(namespace2).append('.').append(namespace3).append('.').append(key); |