From 2d8577655b5e5cbcc414330029b4c7585d6a9fa2 Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Mon, 20 Apr 2020 08:14:28 +0000 Subject: add utility for replacing a variable in a string --- vespalib/src/tests/stllike/CMakeLists.txt | 8 ++++ .../src/tests/stllike/replace_variable_test.cpp | 24 ++++++++++ vespalib/src/vespa/vespalib/stllike/CMakeLists.txt | 1 + .../vespa/vespalib/stllike/replace_variable.cpp | 51 ++++++++++++++++++++++ .../src/vespa/vespalib/stllike/replace_variable.h | 9 ++++ 5 files changed, 93 insertions(+) create mode 100644 vespalib/src/tests/stllike/replace_variable_test.cpp create mode 100644 vespalib/src/vespa/vespalib/stllike/replace_variable.cpp create mode 100644 vespalib/src/vespa/vespalib/stllike/replace_variable.h (limited to 'vespalib') diff --git a/vespalib/src/tests/stllike/CMakeLists.txt b/vespalib/src/tests/stllike/CMakeLists.txt index 04c1a420f5e..ebf7de9c747 100644 --- a/vespalib/src/tests/stllike/CMakeLists.txt +++ b/vespalib/src/tests/stllike/CMakeLists.txt @@ -47,3 +47,11 @@ vespa_add_executable(vespalib_lookup_benchmark_app vespalib ) vespa_add_test(NAME vespalib_lookup_benchmark_app COMMAND vespalib_lookup_benchmark_app BENCHMARK) +vespa_add_executable(vespalib_replace_variable_test_app TEST + SOURCES + replace_variable_test.cpp + DEPENDS + vespalib + gtest +) +vespa_add_test(NAME vespalib_replace_variable_test_app COMMAND vespalib_replace_variable_test_app) diff --git a/vespalib/src/tests/stllike/replace_variable_test.cpp b/vespalib/src/tests/stllike/replace_variable_test.cpp new file mode 100644 index 00000000000..d32bf67a23d --- /dev/null +++ b/vespalib/src/tests/stllike/replace_variable_test.cpp @@ -0,0 +1,24 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include +#include + +using namespace vespalib; + +TEST(ReplaceVariableTest, simple_usage) +{ + EXPECT_EQ("vv", replace_variable("x", "x", "vv")); + EXPECT_EQ("f(vv)", replace_variable("f(x)", "x", "vv")); + EXPECT_EQ("vv(f)", replace_variable("x(f)", "x", "vv")); + EXPECT_EQ("3*vv", replace_variable("3*x", "x", "vv")); + EXPECT_EQ("f(vv,vv,y)", replace_variable("f(x,x,y)", "x", "vv")); + + EXPECT_EQ("f(xx)", replace_variable("f(xx)", "x", "vv")); + EXPECT_EQ("f(ax)", replace_variable("f(ax)", "x", "vv")); + EXPECT_EQ("f(xa)", replace_variable("f(xa)", "x", "vv")); + EXPECT_EQ("f(axa)", replace_variable("f(axa)", "x", "vv")); + + EXPECT_EQ("f(vv)", replace_variable("f(x_y)", "x_y", "vv")); +} + +GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/vespa/vespalib/stllike/CMakeLists.txt b/vespalib/src/vespa/vespalib/stllike/CMakeLists.txt index aaada6fdcc4..f8b9e90dc62 100644 --- a/vespalib/src/vespa/vespalib/stllike/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/stllike/CMakeLists.txt @@ -7,6 +7,7 @@ vespa_add_library(vespalib_vespalib_stllike OBJECT hash_fun.cpp hash_set.cpp hash_map.cpp + replace_variable.cpp string.cpp DEPENDS ) diff --git a/vespalib/src/vespa/vespalib/stllike/replace_variable.cpp b/vespalib/src/vespa/vespalib/stllike/replace_variable.cpp new file mode 100644 index 00000000000..fbbcff48963 --- /dev/null +++ b/vespalib/src/vespa/vespalib/stllike/replace_variable.cpp @@ -0,0 +1,51 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "replace_variable.h" +#include "asciistream.h" +#include + +namespace vespalib { + +vespalib::string +replace_variable(const vespalib::string &input, + const vespalib::string &variable, + const vespalib::string &replacement) +{ + vespalib::asciistream result; + bool is_in_word = false; + size_t last_word_start = 0; + size_t last_word_size = 0; + for (size_t i = 0; i < input.size(); ++i) { + char c = input[i]; + if (isalnum(c) || (c == '_')) { + if (! is_in_word) { + last_word_start = i; + last_word_size = 0; + is_in_word = true; + } + ++last_word_size; + } else { + if (is_in_word) { + vespalib::string last_word = input.substr(last_word_start, last_word_size); + if (last_word == variable) { + result << replacement; + } else { + result << last_word; + } + is_in_word = false; + } + result << c; + } + } + if (is_in_word) { + vespalib::string last_word = input.substr(last_word_start, last_word_size); + if (last_word == variable) { + result << replacement; + } else { + result << last_word; + } + } + return result.str(); +} + +} // namespace diff --git a/vespalib/src/vespa/vespalib/stllike/replace_variable.h b/vespalib/src/vespa/vespalib/stllike/replace_variable.h new file mode 100644 index 00000000000..8caf1c54bfd --- /dev/null +++ b/vespalib/src/vespa/vespalib/stllike/replace_variable.h @@ -0,0 +1,9 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "string.h" + +namespace vespalib { +vespalib::string replace_variable(const vespalib::string &input, + const vespalib::string &variable, + const vespalib::string &replacement); +} // namespace vespalib -- cgit v1.2.3