Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/Grep.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
* Emulator of perl's grep function.
31
* This class uses java.util.regexp classes which appear in
32
* JDK1.4 API. This implies the restriction for this class
33
* to not be used with the tests against JDKs prior to 1.4.
34
*
35
* @see java.util.regex.Pattern
36
* @see java.util.regex.Matcher
37
*/
38
39
public class Grep {
40
41
String[] stringArray;
42
/**
43
* Takes String array as character sequence for matching the pattern.
44
*/
45
public Grep (String[] stringArray) {
46
this.stringArray = stringArray;
47
}
48
49
/**
50
* Returns number of non-interleaved occurences of groups which match the pattern.
51
*/
52
public int find (String regExpPattern) {
53
if (regExpPattern.length() == 0) {
54
throw new Failure("Empty string as input parameter for Grep.find(regExpPattern) method");
55
}
56
Pattern pattern = Pattern.compile(regExpPattern);
57
int counter = 0;
58
for (int i = 0; i < stringArray.length; i++) {
59
60
String string = stringArray[i];
61
if (string != null) {
62
// Create matcher for this string
63
Matcher matcher = pattern.matcher(string);
64
65
// Find all non-interleaved occurences of pattern in this string
66
for (int ind = 0; ind < string.length(); ) {
67
if (matcher.find(ind)) {
68
counter++;
69
ind = matcher.end();
70
} else {
71
break;
72
}
73
}
74
}
75
}
76
return counter;
77
}
78
79
/**
80
* Returns first string of stringArray with group which matches
81
* the pattern or empty string othrewise.
82
*/
83
public String findFirst (String regExpPattern) {
84
if (regExpPattern.length() == 0) {
85
throw new Failure("Empty string as input parameter for Grep.findFirst(regExpPattern) method");
86
}
87
Pattern pattern = Pattern.compile(regExpPattern);
88
String result = "";
89
for (int i = 0; i < stringArray.length; i++) {
90
91
String string = stringArray[i];
92
if (string != null) {
93
// Create matcher for this string
94
Matcher matcher = pattern.matcher(string);
95
if (matcher.find()) {
96
result = string;
97
break;
98
}
99
}
100
}
101
return result;
102
}
103
}
104
105