Move trimBlankLines

This commit is contained in:
Clément Fournier
2022-04-24 19:38:01 +02:00
parent 89545272af
commit e4627fb841
5 changed files with 51 additions and 52 deletions

View File

@ -295,6 +295,43 @@ public final class Chars implements CharSequence {
return trimStart().trimEnd();
}
/**
* Remove trailing and leading blank lines. The resulting string
* does not end with a line terminator.
*/
public Chars trimBlankLines() {
int offsetOfFirstNonBlankChar = length();
for (int i = 0; i < length(); i++) {
if (!Character.isWhitespace(charAt(i))) {
offsetOfFirstNonBlankChar = i;
break;
}
}
int offsetOfLastNonBlankChar = 0;
for (int i = length() - 1; i > offsetOfFirstNonBlankChar; i--) {
if (!Character.isWhitespace(charAt(i))) {
offsetOfLastNonBlankChar = i;
break;
}
}
// look backwards before the first non-blank char
int cutFromInclusive = lastIndexOf('\n', offsetOfFirstNonBlankChar);
// If firstNonBlankLineStart == -1, ie we're on the first line,
// we want to start at zero: then we add 1 to get 0
// If firstNonBlankLineStart >= 0, then it's the index of the
// \n, we want to cut right after that, so we add 1.
cutFromInclusive += 1;
// look forwards after the last non-blank char
int cutUntilExclusive = indexOf('\n', offsetOfLastNonBlankChar);
if (cutUntilExclusive == StringUtils.INDEX_NOT_FOUND) {
cutUntilExclusive = length();
}
return subSequence(cutFromInclusive, cutUntilExclusive);
}
/**
* Remove the suffix if it is present, otherwise returns this.
*/

View File

@ -381,42 +381,6 @@ public final class StringUtil {
}
}
/**
* Remove trailing and leading blank lines.
*/
public static Chars trimBlankLines(Chars string) {
int offsetOfFirstNonBlankChar = string.length();
for (int i = 0; i < string.length(); i++) {
if (!Character.isWhitespace(string.charAt(i))) {
offsetOfFirstNonBlankChar = i;
break;
}
}
int offsetOfLastNonBlankChar = 0;
for (int i = string.length() - 1; i > offsetOfFirstNonBlankChar; i--) {
if (!Character.isWhitespace(string.charAt(i))) {
offsetOfLastNonBlankChar = i;
break;
}
}
// look backwards before the first non-blank char
int cutFromInclusive = string.lastIndexOf('\n', offsetOfFirstNonBlankChar);
// If firstNonBlankLineStart == -1, ie we're on the first line,
// we want to start at zero: then we add 1 to get 0
// If firstNonBlankLineStart >= 0, then it's the index of the
// \n, we want to cut right after that, so we add 1.
cutFromInclusive += 1;
// look forwards after the last non-blank char
int cutUntilExclusive = string.indexOf('\n', offsetOfLastNonBlankChar);
if (cutUntilExclusive == StringUtils.INDEX_NOT_FOUND) {
cutUntilExclusive = string.length();
}
return string.subSequence(cutFromInclusive, cutUntilExclusive);
}
private static int countLeadingWhitespace(CharSequence s) {
int count = 0;

View File

@ -310,4 +310,17 @@ public class CharsTest {
assertThrows(IndexOutOfBoundsException.class, () -> chars.substring(0, 6));
}
@Test
public void testTrimBlankLines() {
assertTrimBlankLinesEquals(" \n \n abc \n \n de \n \n ",
" abc \n \n de ");
assertTrimBlankLinesEquals("", "");
}
private void assertTrimBlankLinesEquals(String input, String expected) {
Chars actual = Chars.wrap(input).trimBlankLines();
assertEquals(Chars.wrap(expected), actual);
}
}

View File

@ -115,20 +115,6 @@ public class StringUtilTest {
assertEquals("abc", StringUtil.substringAfterLast("abc", '.'));
}
@Test
public void trimBlankLines() {
assertTrimBlankLinesEquals(" \n \n abc \n \n de \n \n ",
" abc \n \n de ");
assertTrimBlankLinesEquals("", "");
}
private void assertTrimBlankLinesEquals(String input, String output) {
assertEquals(
Chars.wrap(output),
StringUtil.trimBlankLines(Chars.wrap(input))
);
}
@Test
public void linesWithTrimIndent() {
}

View File

@ -50,7 +50,6 @@ import net.sourceforge.pmd.processor.AbstractPMDProcessor;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.renderers.TextRenderer;
import net.sourceforge.pmd.reporting.GlobalAnalysisListener;
import net.sourceforge.pmd.util.StringUtil;
/**
* Advanced methods for test cases
@ -486,7 +485,7 @@ public abstract class RuleTst {
throw new RuntimeException("No matching code fragment found for coderef");
}
}
code = StringUtil.trimBlankLines(Chars.wrap(code)).toString();
code = Chars.wrap(code).trimBlankLines().toString();
String description = getNodeValue(testCode, "description", true);
int expectedProblems = Integer.parseInt(getNodeValue(testCode, "expected-problems", true).trim());