1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.utils; | |
8 | ||
9 | import java.util.regex.Pattern; | |
10 | import lombok.experimental.UtilityClass; | |
11 | import org.opensearch.sql.data.model.ExprBooleanValue; | |
12 | import org.opensearch.sql.data.model.ExprIntegerValue; | |
13 | import org.opensearch.sql.data.model.ExprValue; | |
14 | ||
15 | @UtilityClass | |
16 | public class OperatorUtils { | |
17 | /** | |
18 | * Wildcard pattern matcher util. | |
19 | * Percent (%) character for wildcard, | |
20 | * Underscore (_) character for a single character match. | |
21 | * @param pattern string pattern to match. | |
22 | * @return if text matches pattern returns true; else return false. | |
23 | */ | |
24 | public static ExprBooleanValue matches(ExprValue text, ExprValue pattern) { | |
25 |
1
1. matches : replaced return value with null for org/opensearch/sql/utils/OperatorUtils::matches → KILLED |
return ExprBooleanValue |
26 | .of(Pattern.compile(patternToRegex(pattern.stringValue())).matcher(text.stringValue()) | |
27 | .matches()); | |
28 | } | |
29 | ||
30 | /** | |
31 | * Checks if text matches regular expression pattern. | |
32 | * @param pattern string pattern to match. | |
33 | * @return if text matches pattern returns true; else return false. | |
34 | */ | |
35 | public static ExprIntegerValue matchesRegexp(ExprValue text, ExprValue pattern) { | |
36 |
1
1. matchesRegexp : replaced return value with null for org/opensearch/sql/utils/OperatorUtils::matchesRegexp → KILLED |
return new ExprIntegerValue(Pattern.compile(pattern.stringValue()).matcher(text.stringValue()) |
37 |
1
1. matchesRegexp : negated conditional → KILLED |
.matches() ? 1 : 0); |
38 | } | |
39 | ||
40 | private static final char DEFAULT_ESCAPE = '\\'; | |
41 | ||
42 | private static String patternToRegex(String patternString) { | |
43 |
1
1. patternToRegex : Replaced integer multiplication with division → SURVIVED |
StringBuilder regex = new StringBuilder(patternString.length() * 2); |
44 | regex.append('^'); | |
45 | boolean escaped = false; | |
46 | for (char currentChar : patternString.toCharArray()) { | |
47 |
2
1. patternToRegex : negated conditional → SURVIVED 2. patternToRegex : negated conditional → KILLED |
if (!escaped && currentChar == DEFAULT_ESCAPE) { |
48 | escaped = true; | |
49 | } else { | |
50 | switch (currentChar) { | |
51 | case '%': | |
52 |
1
1. patternToRegex : negated conditional → KILLED |
if (escaped) { |
53 | regex.append("%"); | |
54 | } else { | |
55 | regex.append(".*"); | |
56 | } | |
57 | escaped = false; | |
58 | break; | |
59 | case '_': | |
60 |
1
1. patternToRegex : negated conditional → SURVIVED |
if (escaped) { |
61 | regex.append("_"); | |
62 | } else { | |
63 | regex.append('.'); | |
64 | } | |
65 | escaped = false; | |
66 | break; | |
67 | default: | |
68 | switch (currentChar) { | |
69 | case '\\': | |
70 | case '^': | |
71 | case '$': | |
72 | case '.': | |
73 | case '*': | |
74 | case '[': | |
75 | case ']': | |
76 | case '(': | |
77 | case ')': | |
78 | case '|': | |
79 | case '+': | |
80 | regex.append('\\'); | |
81 | break; | |
82 | default: | |
83 | } | |
84 | ||
85 | regex.append(currentChar); | |
86 | escaped = false; | |
87 | } | |
88 | } | |
89 | } | |
90 | regex.append('$'); | |
91 |
1
1. patternToRegex : replaced return value with "" for org/opensearch/sql/utils/OperatorUtils::patternToRegex → KILLED |
return regex.toString(); |
92 | } | |
93 | } | |
Mutations | ||
25 |
1.1 |
|
36 |
1.1 |
|
37 |
1.1 |
|
43 |
1.1 |
|
47 |
1.1 2.2 |
|
52 |
1.1 |
|
60 |
1.1 |
|
91 |
1.1 |