Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/Grep.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* Emulator of perl's grep function.30* This class uses java.util.regexp classes which appear in31* JDK1.4 API. This implies the restriction for this class32* to not be used with the tests against JDKs prior to 1.4.33*34* @see java.util.regex.Pattern35* @see java.util.regex.Matcher36*/3738public class Grep {3940String[] stringArray;41/**42* Takes String array as character sequence for matching the pattern.43*/44public Grep (String[] stringArray) {45this.stringArray = stringArray;46}4748/**49* Returns number of non-interleaved occurences of groups which match the pattern.50*/51public int find (String regExpPattern) {52if (regExpPattern.length() == 0) {53throw new Failure("Empty string as input parameter for Grep.find(regExpPattern) method");54}55Pattern pattern = Pattern.compile(regExpPattern);56int counter = 0;57for (int i = 0; i < stringArray.length; i++) {5859String string = stringArray[i];60if (string != null) {61// Create matcher for this string62Matcher matcher = pattern.matcher(string);6364// Find all non-interleaved occurences of pattern in this string65for (int ind = 0; ind < string.length(); ) {66if (matcher.find(ind)) {67counter++;68ind = matcher.end();69} else {70break;71}72}73}74}75return counter;76}7778/**79* Returns first string of stringArray with group which matches80* the pattern or empty string othrewise.81*/82public String findFirst (String regExpPattern) {83if (regExpPattern.length() == 0) {84throw new Failure("Empty string as input parameter for Grep.findFirst(regExpPattern) method");85}86Pattern pattern = Pattern.compile(regExpPattern);87String result = "";88for (int i = 0; i < stringArray.length; i++) {8990String string = stringArray[i];91if (string != null) {92// Create matcher for this string93Matcher matcher = pattern.matcher(string);94if (matcher.find()) {95result = string;96break;97}98}99}100return result;101}102}103104105