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