@Test public void test4grep_ABC() { Object[][] samples = new Object[][] { { "ABC\n1\n2\n3\n4\nc", "ABC", 0, 4, "ABC\n1\n2\n3\n4" }, { "ABC\n1\n2\n3\n4\nABC\n5", "ABC", 2, 1, "ABC\n1\n3\n4\nABC\n5" }, { "ABC\n1\n2\n3\n4\na", "ABC", 2, 1, "ABC\n1" }, { "ABC\n1\n2\n3\n4\nb", "ABC", 0, 0, "ABC" }, { "ABC\n1\n2\n3\n4\nc", "ABC", 0, 5, "ABC\n1\n2\n3\n4\nc" }, { "ABC\n1\n2\n3\n4\nc", "ABC", 0, 10, "ABC\n1\n2\n3\n4\nc" }, { "ABC\n1\n2\n3\n4\nc", "ABC", 0, 2, "ABC\n1\n2" }, { "1\n2\n3\n4\nABC", "ABC", 5, 1, "1\n2\n3\n4\nABC" }, { "1\n2\n3\n4\nABC", "ABC", 4, 1, "1\n2\n3\n4\nABC" }, { "1\n2\n3\n4\nABC", "ABC", 2, 1, "3\n4\nABC" } }; for (Object[] args : samples) { String word = (String) args[1]; int beforeLines = (Integer) args[2]; int afterLines = (Integer) args[3]; GrepHandler handler = new GrepHandler(word, false, false, true, false, true, beforeLines, afterLines, 0); String input = (String) args[0]; final String ret = handler.apply(input); final String expected = (String) args[4]; Assert.assertEquals(expected, ret.substring(0, ret.length() - 1)); } }
@Override public String apply(String input) { StringBuilder output = new StringBuilder(); String[] lines = input.split("\n"); int continueCount = 0; int lastStartPos = 0; int lastContinueLineNum = -1; int matchCount = 0; for (int lineNum = 0; lineNum < lines.length;) { String line = null; if (this.trimEnd) { line = StringUtils.stripEnd(lines[lineNum], null); } else { line = lines[lineNum]; } lineNum++; final boolean match; if (pattern == null) { match = (ignoreCase ? line.toLowerCase() : line).contains(keyword); } else { match = pattern.matcher(line).find(); } if (invertMatch ? !match : match) { matchCount++; if (beforeLines > continueCount) { int n = lastContinueLineNum == -1 ? (beforeLines >= lineNum ? 1 : lineNum - beforeLines) : lineNum - beforeLines - continueCount; if (n >= lastContinueLineNum || lastContinueLineNum == -1) { StringBuilder beforeSb = new StringBuilder(); for (int i = n; i < lineNum; i++) { appendLine(beforeSb, i, lines[i - 1]); } output.insert(lastStartPos, beforeSb); } } lastStartPos = output.length(); appendLine(output, lineNum, line); if (afterLines > continueCount) { int last = lineNum + afterLines - continueCount; if (last > lines.length) { last = lines.length; } for (int i = lineNum; i < last; i++) { appendLine(output, i + 1, lines[i]); lineNum++; continueCount++; lastStartPos = output.length(); } } continueCount++; if (maxCount > 0 && matchCount >= maxCount) { break; } } else { if (continueCount > 0) { lastContinueLineNum = lineNum - 1; continueCount = 0; } } } final String str = output.toString(); return str; }
GrepHandler extends StdoutHandler { @Override public String apply(String input) { StringBuilder output = new StringBuilder(); String[] lines = input.split("\n"); int continueCount = 0; int lastStartPos = 0; int lastContinueLineNum = -1; int matchCount = 0; for (int lineNum = 0; lineNum < lines.length;) { String line = null; if (this.trimEnd) { line = StringUtils.stripEnd(lines[lineNum], null); } else { line = lines[lineNum]; } lineNum++; final boolean match; if (pattern == null) { match = (ignoreCase ? line.toLowerCase() : line).contains(keyword); } else { match = pattern.matcher(line).find(); } if (invertMatch ? !match : match) { matchCount++; if (beforeLines > continueCount) { int n = lastContinueLineNum == -1 ? (beforeLines >= lineNum ? 1 : lineNum - beforeLines) : lineNum - beforeLines - continueCount; if (n >= lastContinueLineNum || lastContinueLineNum == -1) { StringBuilder beforeSb = new StringBuilder(); for (int i = n; i < lineNum; i++) { appendLine(beforeSb, i, lines[i - 1]); } output.insert(lastStartPos, beforeSb); } } lastStartPos = output.length(); appendLine(output, lineNum, line); if (afterLines > continueCount) { int last = lineNum + afterLines - continueCount; if (last > lines.length) { last = lines.length; } for (int i = lineNum; i < last; i++) { appendLine(output, i + 1, lines[i]); lineNum++; continueCount++; lastStartPos = output.length(); } } continueCount++; if (maxCount > 0 && matchCount >= maxCount) { break; } } else { if (continueCount > 0) { lastContinueLineNum = lineNum - 1; continueCount = 0; } } } final String str = output.toString(); return str; } }
GrepHandler extends StdoutHandler { @Override public String apply(String input) { StringBuilder output = new StringBuilder(); String[] lines = input.split("\n"); int continueCount = 0; int lastStartPos = 0; int lastContinueLineNum = -1; int matchCount = 0; for (int lineNum = 0; lineNum < lines.length;) { String line = null; if (this.trimEnd) { line = StringUtils.stripEnd(lines[lineNum], null); } else { line = lines[lineNum]; } lineNum++; final boolean match; if (pattern == null) { match = (ignoreCase ? line.toLowerCase() : line).contains(keyword); } else { match = pattern.matcher(line).find(); } if (invertMatch ? !match : match) { matchCount++; if (beforeLines > continueCount) { int n = lastContinueLineNum == -1 ? (beforeLines >= lineNum ? 1 : lineNum - beforeLines) : lineNum - beforeLines - continueCount; if (n >= lastContinueLineNum || lastContinueLineNum == -1) { StringBuilder beforeSb = new StringBuilder(); for (int i = n; i < lineNum; i++) { appendLine(beforeSb, i, lines[i - 1]); } output.insert(lastStartPos, beforeSb); } } lastStartPos = output.length(); appendLine(output, lineNum, line); if (afterLines > continueCount) { int last = lineNum + afterLines - continueCount; if (last > lines.length) { last = lines.length; } for (int i = lineNum; i < last; i++) { appendLine(output, i + 1, lines[i]); lineNum++; continueCount++; lastStartPos = output.length(); } } continueCount++; if (maxCount > 0 && matchCount >= maxCount) { break; } } else { if (continueCount > 0) { lastContinueLineNum = lineNum - 1; continueCount = 0; } } } final String str = output.toString(); return str; } GrepHandler(String keyword, boolean ignoreCase, boolean invertMatch, boolean regexpMode,
                    boolean showLineNumber, boolean trimEnd, int beforeLines, int afterLines, int maxCount); }
GrepHandler extends StdoutHandler { @Override public String apply(String input) { StringBuilder output = new StringBuilder(); String[] lines = input.split("\n"); int continueCount = 0; int lastStartPos = 0; int lastContinueLineNum = -1; int matchCount = 0; for (int lineNum = 0; lineNum < lines.length;) { String line = null; if (this.trimEnd) { line = StringUtils.stripEnd(lines[lineNum], null); } else { line = lines[lineNum]; } lineNum++; final boolean match; if (pattern == null) { match = (ignoreCase ? line.toLowerCase() : line).contains(keyword); } else { match = pattern.matcher(line).find(); } if (invertMatch ? !match : match) { matchCount++; if (beforeLines > continueCount) { int n = lastContinueLineNum == -1 ? (beforeLines >= lineNum ? 1 : lineNum - beforeLines) : lineNum - beforeLines - continueCount; if (n >= lastContinueLineNum || lastContinueLineNum == -1) { StringBuilder beforeSb = new StringBuilder(); for (int i = n; i < lineNum; i++) { appendLine(beforeSb, i, lines[i - 1]); } output.insert(lastStartPos, beforeSb); } } lastStartPos = output.length(); appendLine(output, lineNum, line); if (afterLines > continueCount) { int last = lineNum + afterLines - continueCount; if (last > lines.length) { last = lines.length; } for (int i = lineNum; i < last; i++) { appendLine(output, i + 1, lines[i]); lineNum++; continueCount++; lastStartPos = output.length(); } } continueCount++; if (maxCount > 0 && matchCount >= maxCount) { break; } } else { if (continueCount > 0) { lastContinueLineNum = lineNum - 1; continueCount = 0; } } } final String str = output.toString(); return str; } GrepHandler(String keyword, boolean ignoreCase, boolean invertMatch, boolean regexpMode,
                    boolean showLineNumber, boolean trimEnd, int beforeLines, int afterLines, int maxCount); static StdoutHandler inject(List<CliToken> tokens); @Override String apply(String input); }
GrepHandler extends StdoutHandler { @Override public String apply(String input) { StringBuilder output = new StringBuilder(); String[] lines = input.split("\n"); int continueCount = 0; int lastStartPos = 0; int lastContinueLineNum = -1; int matchCount = 0; for (int lineNum = 0; lineNum < lines.length;) { String line = null; if (this.trimEnd) { line = StringUtils.stripEnd(lines[lineNum], null); } else { line = lines[lineNum]; } lineNum++; final boolean match; if (pattern == null) { match = (ignoreCase ? line.toLowerCase() : line).contains(keyword); } else { match = pattern.matcher(line).find(); } if (invertMatch ? !match : match) { matchCount++; if (beforeLines > continueCount) { int n = lastContinueLineNum == -1 ? (beforeLines >= lineNum ? 1 : lineNum - beforeLines) : lineNum - beforeLines - continueCount; if (n >= lastContinueLineNum || lastContinueLineNum == -1) { StringBuilder beforeSb = new StringBuilder(); for (int i = n; i < lineNum; i++) { appendLine(beforeSb, i, lines[i - 1]); } output.insert(lastStartPos, beforeSb); } } lastStartPos = output.length(); appendLine(output, lineNum, line); if (afterLines > continueCount) { int last = lineNum + afterLines - continueCount; if (last > lines.length) { last = lines.length; } for (int i = lineNum; i < last; i++) { appendLine(output, i + 1, lines[i]); lineNum++; continueCount++; lastStartPos = output.length(); } } continueCount++; if (maxCount > 0 && matchCount >= maxCount) { break; } } else { if (continueCount > 0) { lastContinueLineNum = lineNum - 1; continueCount = 0; } } } final String str = output.toString(); return str; } GrepHandler(String keyword, boolean ignoreCase, boolean invertMatch, boolean regexpMode,
                    boolean showLineNumber, boolean trimEnd, int beforeLines, int afterLines, int maxCount); static StdoutHandler inject(List<CliToken> tokens); @Override String apply(String input); static final String NAME; }