From 79117b61590a2c37f968a54c468877a7f3d2f544 Mon Sep 17 00:00:00 2001 From: Robin Friedmann Date: Wed, 7 Feb 2024 09:26:26 +0100 Subject: [PATCH] Fix failing test(#12065) Signed-off-by: Robin Friedmann Refactored Glob.globMatch by using iterative approach; added unit tests (#12065) Signed-off-by: Robin Friedmann --- .../main/java/org/opensearch/common/Glob.java | 54 ++++++++++--------- .../java/org/opensearch/common/GlobTests.java | 31 ++++++++--- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/libs/common/src/main/java/org/opensearch/common/Glob.java b/libs/common/src/main/java/org/opensearch/common/Glob.java index daf045dd49e3a..12b90a915a2b5 100644 --- a/libs/common/src/main/java/org/opensearch/common/Glob.java +++ b/libs/common/src/main/java/org/opensearch/common/Glob.java @@ -49,37 +49,39 @@ public class Glob { * @return whether the String matches the given pattern */ public static boolean globMatch(String pattern, String str) { + if (pattern == null || str == null) { return false; } - int firstIndex = pattern.indexOf('*'); - if (firstIndex == -1) { - return pattern.equals(str); - } - if (firstIndex == 0) { - if (pattern.length() == 1) { - return true; - } - int nextIndex = pattern.indexOf('*', firstIndex + 1); - if (nextIndex == -1) { - return str.endsWith(pattern.substring(1)); - } else if (nextIndex == 1) { - // Double wildcard "**" - skipping the first "*" - return globMatch(pattern.substring(1), str); - } - String part = pattern.substring(1, nextIndex); - int partIndex = str.indexOf(part); - while (partIndex != -1) { - if (globMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) { - return true; - } - partIndex = str.indexOf(part, partIndex + 1); + + int stringIndex = 0; + int patternIndex = 0; + int wildcardIndex = -1; + + while (stringIndex < str.length()) { + // pattern and string match + if (patternIndex < pattern.length() && str.charAt(stringIndex) == pattern.charAt(patternIndex)) { + stringIndex++; + patternIndex++; + } else if (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') { + // wildcard found + wildcardIndex = patternIndex; + patternIndex++; + } else if (wildcardIndex != -1) { + // last pattern pointer was a wildcard + patternIndex = wildcardIndex + 1; + stringIndex++; + } else { + // characters do not match + return false; } - return false; } - return (str.length() >= firstIndex - && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) - && globMatch(pattern.substring(firstIndex), str.substring(firstIndex))); + + while (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') { + patternIndex++; + } + + return patternIndex == pattern.length(); } } diff --git a/libs/common/src/test/java/org/opensearch/common/GlobTests.java b/libs/common/src/test/java/org/opensearch/common/GlobTests.java index d7359df4292ae..a108bf81000b3 100644 --- a/libs/common/src/test/java/org/opensearch/common/GlobTests.java +++ b/libs/common/src/test/java/org/opensearch/common/GlobTests.java @@ -12,49 +12,64 @@ public class GlobTests extends OpenSearchTestCase { - public void testGlobMatchNoWildcard() { assertTrue(Glob.globMatch("test", "test")); assertFalse(Glob.globMatch("test", "testing")); } - public void testGlobMatchWildcardAtBeginning() { assertTrue(Glob.globMatch("*test", "thisisatest")); assertFalse(Glob.globMatch("*test", "thisisatesting")); } - public void testGlobMatchWildcardAtEnd() { assertTrue(Glob.globMatch("test*", "testthisisa")); assertFalse(Glob.globMatch("test*", "atestthisis")); } - public void testGlobMatchWildcardAtMiddle() { assertTrue(Glob.globMatch("test*ing", "testthisisaing")); assertFalse(Glob.globMatch("test*ing", "testthisisa")); } - public void testGlobMatchMultipleWildcards() { assertTrue(Glob.globMatch("*test*", "thisisatesting")); assertFalse(Glob.globMatch("*test*", "thisisatesing")); + assertTrue(Glob.globMatch("*test*test", "thisisatestingtest")); + assertFalse(Glob.globMatch("*test*test", "thisisatesting")); } + public void testGlobMatchMultipleCharactersWithSingleWildcard() { + assertTrue(Glob.globMatch("a*b", "acb")); + assertTrue(Glob.globMatch("a*b", "aab")); + assertTrue(Glob.globMatch("a*b", "aaab")); + assertFalse(Glob.globMatch("a*b", "ac")); + } + + public void testGlobMatchWildcardWithEmptyString() { + assertTrue(Glob.globMatch("*", "")); + assertTrue(Glob.globMatch("a*", "a")); + assertFalse(Glob.globMatch("a*", "")); + } + + public void testGlobMatchMultipleWildcardsWithMultipleCharacters() { + assertTrue(Glob.globMatch("a*b*c", "abc")); + assertTrue(Glob.globMatch("a*b*c", "axxxbxbc")); + assertTrue(Glob.globMatch("a*b*c", "aabc")); + assertTrue(Glob.globMatch("a*b*c", "abac")); + assertFalse(Glob.globMatch("a*b*c", "abca")); + assertFalse(Glob.globMatch("a*b*c", "ac")); + } public void testGlobMatchNullPattern() { assertFalse(Glob.globMatch(null, "test")); } - public void testGlobMatchNullString() { assertFalse(Glob.globMatch("test", null)); } - public void testGlobMatchNullPatternAndString() { assertFalse(Glob.globMatch(null, null)); } } -