diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-05-13 12:55:39 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-05-13 12:55:39 +0000 |
commit | 3c44e7d4f38a4de679840b249baa20f950e61c58 (patch) | |
tree | 9a50d7f430ce30f408013e0f40dd3da773f78764 /juniper | |
parent | fc458207c3d957a79c909fe5daceaf0efcbbadad (diff) |
GC fastlib_util library and move wildcard_match.h to juniper.
Diffstat (limited to 'juniper')
-rw-r--r-- | juniper/src/test/matchobjectTestApp.cpp | 29 | ||||
-rw-r--r-- | juniper/src/vespa/juniper/matchobject.cpp | 2 | ||||
-rw-r--r-- | juniper/src/vespa/juniper/wildcard_match.h | 54 |
3 files changed, 84 insertions, 1 deletions
diff --git a/juniper/src/test/matchobjectTestApp.cpp b/juniper/src/test/matchobjectTestApp.cpp index 7fae5b4c0de..525f7b9446a 100644 --- a/juniper/src/test/matchobjectTestApp.cpp +++ b/juniper/src/test/matchobjectTestApp.cpp @@ -3,8 +3,37 @@ #include "matchobjectTest.h" #include "testenv.h" #include <vespa/vespalib/testkit/testapp.h> +#include <vespa/juniper/wildcard_match.h> +#include <iostream> + +namespace { +void test(const char * word, const char * pattern, bool expect) { + EXPECT_EQUAL(expect, fast::util::wildcard_match(word, pattern)); +} +} + +void +test_wildcard() +{ + test("a", "b", false); + test("b", "b", true); + test("abc", "def", false); + test("def", "def", true); + test("def", "d?f", true); + test("def", "d?d", false); + test("def", "??d", false); + test("def", "d??", true); + test("abcdef", "a*e", false); + test("abcdef", "a*f", true); + test("abcdef", "a?c*f", true); + test("abcdef", "a?b*f", false); + test("abcdef", "a*b*f", true); + test("abcdef", "abc*", true); + test("abcdef", "*def", true); +} int main(int argc, char **argv) { + test_wildcard(); juniper::TestEnv te(argc, argv, TEST_PATH("../rpclient/testclient.rc").c_str()); MatchObjectTest test; test.SetStream(&std::cout); diff --git a/juniper/src/vespa/juniper/matchobject.cpp b/juniper/src/vespa/juniper/matchobject.cpp index d6657250f80..376f970d73b 100644 --- a/juniper/src/vespa/juniper/matchobject.cpp +++ b/juniper/src/vespa/juniper/matchobject.cpp @@ -5,7 +5,7 @@ #include "juniperdebug.h" #include "result.h" #include "charutil.h" -#include <vespa/fastlib/util/wildcard_match.h> +#include "wildcard_match.h" #include <stack> #include <vespa/log/log.h> LOG_SETUP(".juniper.matchobject"); diff --git a/juniper/src/vespa/juniper/wildcard_match.h b/juniper/src/vespa/juniper/wildcard_match.h new file mode 100644 index 00000000000..2cde4364693 --- /dev/null +++ b/juniper/src/vespa/juniper/wildcard_match.h @@ -0,0 +1,54 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +namespace fast +{ +namespace util +{ +template<typename T> +bool wildcard_match(const T* word, const T* pattern, T multiple = '*', + T single = '?') +{ + while (*word != 0) + if (*pattern == 0) + return false; + else if (*pattern == multiple) + { + // advance past occurrences of multiple + while (*pattern == multiple) + ++pattern; + + // if pattern ended with multiple, we're done + if (*pattern == 0) + return true; + + while (*word != 0) + { + // does this position in the word match + if (*pattern == single || *pattern == *word) + { + // test the rest of the word + if (wildcard_match(word, pattern, multiple, single) == true) + { + // it matched + return true; + } + } + + // try next character + ++word; + } + } + else if (*pattern != single && *pattern != *word) + return false; + else + { + ++word; + ++pattern; + } + + // should be at end of pattern too if the word matched + return *pattern == 0; +} +} +} |