Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/Paragrep.java
40948 views
1
/*
2
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package nsk.share;
25
26
import java.util.*;
27
import java.util.regex.*;
28
29
/**
30
* Weak emulator of perl's grep function with very small functionality.
31
* This class does not use java.util.regexp classes which appear in
32
* JDK1.4 API.
33
*
34
* @see Grep
35
*/
36
37
public class Paragrep {
38
39
String[] stringArray;
40
/**
41
* Takes String array as character sequence for matching the pattern.
42
*/
43
public Paragrep (String[] stringArray) {
44
this.stringArray = stringArray;
45
}
46
47
/**
48
* Returns number of non-interleaved occurences of the pattern string.
49
*/
50
public int find (String pattern) {
51
if (pattern.length() == 0) {
52
throw new Failure("Empty string as input parameter for Grep.find(pattern) method");
53
}
54
int counter = 0;
55
for (int i = 0; i < stringArray.length; i++) {
56
57
String string = stringArray[i];
58
if (string != null) {
59
// Find all non-interleaved occurences of pattern in this string
60
for (int ind = 0; ind < string.length(); ) {
61
int k = 0;
62
if ((k = string.indexOf(pattern, ind)) >= 0) {
63
counter++;
64
ind = k + pattern.length();
65
} else {
66
break;
67
}
68
}
69
}
70
}
71
return counter;
72
}
73
74
/**
75
* Returns all string in <code>stringArray</code> which have
76
* occurences of the pattern string.
77
*/
78
public String[] findStrings (String pattern) {
79
if (pattern.length() == 0) {
80
throw new Failure("Empty string as input parameter for Grep.find(pattern) method");
81
}
82
Vector<String> v = new Vector<String>();
83
for (int i = 0; i < stringArray.length; i++) {
84
String string = stringArray[i];
85
if (string != null && string.indexOf(pattern) >= 0) {
86
v.add(string);
87
}
88
}
89
String[] result = new String[v.size()];
90
v.toArray(result);
91
return result;
92
}
93
94
/**
95
* Returns first string of stringArray which contains
96
* the pattern string or empty string othrewise.
97
*/
98
public String findFirst (String pattern) {
99
if (pattern.length() == 0) {
100
throw new Failure("Empty string as input parameter for Paragrep.findFirst(pattern) method");
101
}
102
String result = "";
103
for (int i = 0; i < stringArray.length; i++) {
104
String string = stringArray[i];
105
if (string != null) {
106
if (string.indexOf(pattern) >= 0) {
107
result = string;
108
break;
109
}
110
}
111
}
112
return result;
113
}
114
115
/**
116
* Returns first string of stringArray which contains
117
* all of the pattern strings or empty string otherwise.
118
*/
119
public String findFirst (Vector<String> patternVector) {
120
if (patternVector.isEmpty()) {
121
throw new Failure("Empty vector as input parameter for Paragrep.findFirst(patternVector) method");
122
}
123
String[] patterns = new String[patternVector.size()];
124
patternVector.toArray(patterns);
125
String result = "";
126
for (int i = 0; i < stringArray.length; i++) {
127
String string = stringArray[i];
128
if (string != null && string.length() > 0) {
129
for (int j = 0; j < patterns.length; j++) {
130
String pattern = patterns[j];
131
if (string.indexOf(pattern) >= 0) {
132
if (j + 1 == patterns.length) {
133
// found all patterns in the current string
134
result = string;
135
i = stringArray.length;
136
}
137
} else {
138
break;
139
}
140
}
141
}
142
}
143
return result;
144
}
145
146
/**
147
* Returns count of strings in stringArray which contain
148
* all of the pattern strings.
149
*/
150
public int find (Vector<String> patternVector) {
151
if (patternVector.isEmpty()) {
152
throw new Failure("Empty vector as input parameter for Paragrep.find(patternVector) method");
153
}
154
String[] patterns = new String[patternVector.size()];
155
patternVector.toArray(patterns);
156
int counter = 0;
157
158
for (int i = 0; i < stringArray.length; i++) {
159
String string = stringArray[i];
160
if (string != null && string.length() > 0) {
161
for (int j = 0; j < patterns.length; j++) {
162
String pattern = patterns[j];
163
if (string.indexOf(pattern) >= 0) {
164
if (j + 1 == patterns.length) {
165
// found all patterns in the current string
166
counter++;
167
}
168
} else {
169
break;
170
}
171
}
172
}
173
}
174
return counter;
175
}
176
}
177
178