Change a Chars method
This commit is contained in:
6 files changed
+65
-28
No files matched your search
@@ -346,21 +346,20 @@ public final class Chars implements CharSequence {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the substring starting at the given offset and with the
|
||||
* given length. This differs from {@link String#substring(int, int)}
|
||||
* in that it uses offset + length instead of start + end.
|
||||
* Returns the substring between the given offsets.
|
||||
* given length.
|
||||
*
|
||||
* @param off Start offset ({@code 0 <= off < this.length()})
|
||||
* @param len Length of the substring ({@code 0 <= len <= this.length() - off})
|
||||
* @param start Start offset ({@code 0 <= start < this.length()})
|
||||
* @param end End offset ({@code start <= end <= this.length()})
|
||||
*
|
||||
* @return A substring
|
||||
*
|
||||
* @throws IndexOutOfBoundsException If the parameters are not a valid range
|
||||
* @see String#substring(int, int)
|
||||
*/
|
||||
public String substring(int off, int len) {
|
||||
validateRange(off, len, this.len);
|
||||
int start = idx(off);
|
||||
return str.substring(start, start + len);
|
||||
public String substring(int start, int end) {
|
||||
validateRange(start, end - start, this.len);
|
||||
return str.substring(idx(start), idx(end));
|
||||
}
|
||||
|
||||
private static void validateRangeWithAssert(int off, int len, int bound) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.function.BiConsumer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.DummyNode.DummyRootNode;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
import net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil;
|
||||
@@ -49,7 +50,10 @@ public class RuleContextTest {
|
||||
}
|
||||
|
||||
private RuleViolation makeViolation(String unescapedMessage, Object... args) throws Exception {
|
||||
Report report = getReport(new FooRule(), (r, ctx) -> ctx.addViolationWithMessage(DummyTreeUtil.tree(DummyTreeUtil::root), unescapedMessage, args));
|
||||
Report report = getReport(new FooRule(), (r, ctx) -> {
|
||||
DummyRootNode node = DummyTreeUtil.tree(DummyTreeUtil::root);
|
||||
ctx.addViolationWithMessage(node, unescapedMessage, args);
|
||||
});
|
||||
return report.getViolations().get(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -110,12 +110,6 @@ public class PMDTaskTest {
|
||||
expected = expected.replaceFirst("timestamp=\"[^\"]+\"", "timestamp=\"\"");
|
||||
expected = expected.replaceFirst("\\.xsd\" version=\"[^\"]+\"", ".xsd\" version=\"\"");
|
||||
|
||||
// under windows, the file source sample.dummy has different line endings
|
||||
// and therefore the endcolumn of the nodes also change
|
||||
if (System.lineSeparator().equals("\r\n")) {
|
||||
expected = expected.replaceFirst("endcolumn=\"109\"", "endcolumn=\"110\"");
|
||||
}
|
||||
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
package net.sourceforge.pmd.lang.ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -154,28 +153,39 @@ public class DummyNode extends AbstractNode<DummyNode, DummyNode> implements Gen
|
||||
|
||||
public static class DummyRootNode extends DummyNode implements RootNode {
|
||||
|
||||
private Map<Integer, String> suppressMap = Collections.emptyMap();
|
||||
private TextDocument sourceText = TextDocument.readOnlyString(
|
||||
"dummy text",
|
||||
TextFile.UNKNOWN_FILENAME,
|
||||
DummyLanguageModule.getInstance().getDefaultVersion()
|
||||
);
|
||||
|
||||
private AstInfo<DummyRootNode> astInfo;
|
||||
|
||||
public DummyRootNode() {
|
||||
TextDocument document = TextDocument.readOnlyString(
|
||||
"dummy text",
|
||||
TextFile.UNKNOWN_FILENAME,
|
||||
DummyLanguageModule.getInstance().getDefaultVersion()
|
||||
);
|
||||
astInfo = new AstInfo<>(
|
||||
new ParserTask(
|
||||
document,
|
||||
SemanticErrorReporter.noop()
|
||||
),
|
||||
this);
|
||||
}
|
||||
|
||||
public DummyRootNode withTaskInfo(ParserTask task) {
|
||||
this.astInfo = new AstInfo<>(task, this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DummyRootNode withNoPmdComments(Map<Integer, String> suppressMap) {
|
||||
this.suppressMap = suppressMap;
|
||||
this.astInfo = new AstInfo<>(
|
||||
astInfo.getTextDocument(),
|
||||
this,
|
||||
suppressMap
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AstInfo<DummyRootNode> getAstInfo() {
|
||||
return Objects.requireNonNull(astInfo, "no ast info, don't use DummyRootNode's ctor directly");
|
||||
return Objects.requireNonNull(astInfo, "no ast info");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -77,7 +77,7 @@ public class CharsTest {
|
||||
Chars bc = Chars.wrap("abcd").slice(1, 2);
|
||||
|
||||
bc.getChars(0, arr, 1, 2);
|
||||
assertArrayEquals(arr, new char[] {0, 'b', 'c', 0});
|
||||
assertArrayEquals(arr, new char[] { 0, 'b', 'c', 0 });
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> bc.getChars(2, arr, 0, 1));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> bc.getChars(-1, arr, 0, 1));
|
||||
@@ -232,4 +232,34 @@ public class CharsTest {
|
||||
assertTrue(chars.contentEquals(Chars.wrap("A_B_C"), true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSlice() {
|
||||
// slice is offset + length
|
||||
Chars chars = Chars.wrap("a_a_b_c_s").slice(2, 5);
|
||||
// -----
|
||||
assertEquals(Chars.wrap("_b_"), chars.slice(1, 3));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, -1));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, 6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubsequence() {
|
||||
// subsequence is start + end
|
||||
Chars chars = Chars.wrap("a_a_b_c_s").slice(2, 5);
|
||||
// -----
|
||||
assertEquals(Chars.wrap("_b"), chars.subSequence(1, 3));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, -1));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, 6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubstring() {
|
||||
// substring is start + end
|
||||
Chars chars = Chars.wrap("a_a_b_c_s").slice(2, 5);
|
||||
// -----
|
||||
assertEquals("_b", chars.substring(1, 3));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> chars.substring(0, -1));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> chars.substring(0, 6));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd" version="6.45.0-SNAPSHOT" timestamp="2022-03-31T17:55:43.046">
|
||||
<file name="sample.dummy">
|
||||
<violation beginline="1" endline="1" begincolumn="1" endcolumn="109" rule="SampleXPathRule" ruleset="Test Ruleset" package="foo" externalInfoUrl="${pmd.website.baseurl}/rules/dummy/basic.xml#SampleXPathRule" priority="3">
|
||||
<violation beginline="1" endline="2" begincolumn="1" endcolumn="1" rule="SampleXPathRule" ruleset="Test Ruleset" package="foo" externalInfoUrl="${pmd.website.baseurl}/rules/dummy/basic.xml#SampleXPathRule" priority="3">
|
||||
Test Rule 2
|
||||
</violation>
|
||||
</file>
|
||||
|
||||
Reference in new issue
Block a user