diff options
author | HÃ¥vard Pettersen <3535158+havardpe@users.noreply.github.com> | 2023-02-10 11:26:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-10 11:26:48 +0100 |
commit | 3d5e5a56b7aed8315726b203117a2b6bc2dd0d31 (patch) | |
tree | 684d2a8634f589b1c3439e6a179d59ce1e68acb9 /vespalib | |
parent | 6cafa7d885b5e0fe2ca7e33a786d0fa79d0e48ff (diff) | |
parent | 37391e9cb5b375a5a8c1c4acc21a10805cb2f5e0 (diff) |
Merge pull request #25967 from vespa-engine/havardpe/trace-global-filter-iterator-tree
trace global filter iterator tree
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespalib/src/tests/util/static_string/CMakeLists.txt | 9 | ||||
-rw-r--r-- | vespalib/src/tests/util/static_string/static_string_test.cpp | 34 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/static_string.h | 37 |
4 files changed, 81 insertions, 0 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt index 76308260578..2720d8786cb 100644 --- a/vespalib/CMakeLists.txt +++ b/vespalib/CMakeLists.txt @@ -200,6 +200,7 @@ vespa_define_module( src/tests/util/mmap_file_allocator_factory src/tests/util/rcuvector src/tests/util/size_literals + src/tests/util/static_string src/tests/util/string_escape src/tests/valgrind src/tests/visit_ranges diff --git a/vespalib/src/tests/util/static_string/CMakeLists.txt b/vespalib/src/tests/util/static_string/CMakeLists.txt new file mode 100644 index 00000000000..14360470b6b --- /dev/null +++ b/vespalib/src/tests/util/static_string/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_static_string_test_app TEST + SOURCES + static_string_test.cpp + DEPENDS + vespalib + GTest::GTest +) +vespa_add_test(NAME vespalib_static_string_test_app COMMAND vespalib_static_string_test_app) diff --git a/vespalib/src/tests/util/static_string/static_string_test.cpp b/vespalib/src/tests/util/static_string/static_string_test.cpp new file mode 100644 index 00000000000..d91fb613f0a --- /dev/null +++ b/vespalib/src/tests/util/static_string/static_string_test.cpp @@ -0,0 +1,34 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/util/static_string.h> +#include <vespa/vespalib/gtest/gtest.h> + +using vespalib::StaticStringView; +using namespace vespalib::literals; + +TEST(StaticStringViewTest, simple_usage) { + auto value = "foo bar"_ssv; + vespalib::string expect("foo bar"); + std::string expect_std("foo bar"); + static_assert(std::same_as<decltype(value),StaticStringView>); + auto a_ref = value.ref(); + auto a_view = value.view(); + static_assert(std::same_as<decltype(a_ref),vespalib::stringref>); + static_assert(std::same_as<decltype(a_view),std::string_view>); + vespalib::stringref ref = value; + std::string_view view = value; + EXPECT_EQ(a_ref, expect); + EXPECT_EQ(a_view, expect_std); + EXPECT_EQ(ref, expect); + EXPECT_EQ(view, expect_std); + EXPECT_EQ(value.ref(), expect); + EXPECT_EQ(value.view(), expect_std); +} + +TEST(StaticStringViewTest, with_null_byte) { + auto value = "foo\0bar"_ssv; + std::string expect("foo\0bar", 7); + EXPECT_EQ(value.view(), expect); +} + +GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/vespa/vespalib/util/static_string.h b/vespalib/src/vespa/vespalib/util/static_string.h new file mode 100644 index 00000000000..570c49a127b --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/static_string.h @@ -0,0 +1,37 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <string_view> +#include <vespa/vespalib/stllike/string.h> + +namespace vespalib { + +class StaticStringView; +namespace literals { +constexpr StaticStringView operator "" _ssv(const char *literal, size_t size); +} // literals + +/** + * Contains the view of a literal string + **/ +class StaticStringView { +private: + std::string_view _view; + friend constexpr StaticStringView literals::operator "" _ssv(const char *, size_t); + constexpr StaticStringView(const char *literal, size_t size) noexcept + : _view(literal, size) {} +public: + constexpr std::string_view view() const noexcept { return _view; } + constexpr operator std::string_view() const noexcept { return _view; } + vespalib::stringref ref() const noexcept { return {_view.data(), _view.size()}; } + operator vespalib::stringref() const noexcept { return ref(); } +}; + +namespace literals { +constexpr StaticStringView operator "" _ssv(const char *literal, size_t size) { + return vespalib::StaticStringView(literal, size); +} +} // literals + +} // vespalib |