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