Skip to content

Commit 0455cc0

Browse files
committed
Create own internal StringUtil
1 parent e22d11e commit 0455cc0

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
package net.sourceforge.pmd.eclipse.util.internal;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class StringUtilTest {
11+
12+
@Test
13+
public void testMaxCommonLeadingWhitespaceForAll() {
14+
Assert.assertEquals(4, StringUtil.maxCommonLeadingWhitespaceForAll(new String[] { " a", " b" }));
15+
Assert.assertEquals(0, StringUtil.maxCommonLeadingWhitespaceForAll(new String[] { " a", "b" }));
16+
Assert.assertEquals(4, StringUtil.maxCommonLeadingWhitespaceForAll(new String[] { " a", " b" }));
17+
}
18+
19+
@Test
20+
public void testTrimStartOn() {
21+
Assert.assertArrayEquals(new String[] { "a", "b" },
22+
StringUtil.trimStartOn(new String[] { " a", " b" }, 4));
23+
Assert.assertArrayEquals(new String[] { "a", " b" },
24+
StringUtil.trimStartOn(new String[] { " a", " b" }, 2));
25+
Assert.assertArrayEquals(new String[] { " a", " b" },
26+
StringUtil.trimStartOn(new String[] { " a", " b" }, 0));
27+
}
28+
}

net.sourceforge.pmd.eclipse.plugin/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Export-Package: ch.qos.logback.classic;x-friends:="net.sourceforge.pmd.eclipse.p
158158
net.sourceforge.pmd.eclipse.ui.model,
159159
org.eclipse.jface.viewers,
160160
org.eclipse.swt.widgets",
161+
net.sourceforge.pmd.eclipse.util.internal;x-friends:="net.sourceforge.pmd.eclipse.plugin.test",
161162
net.sourceforge.pmd.lang,
162163
net.sourceforge.pmd.lang.java.rule,
163164
net.sourceforge.pmd.lang.rule,

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/panelmanagers/ExamplePanelManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
import org.eclipse.swt.widgets.Event;
1919
import org.eclipse.swt.widgets.Listener;
2020

21-
import net.sourceforge.pmd.PMD;
2221
import net.sourceforge.pmd.Rule;
2322
import net.sourceforge.pmd.eclipse.ui.editors.SyntaxManager;
2423
import net.sourceforge.pmd.eclipse.ui.preferences.br.ValueChangeListener;
24+
import net.sourceforge.pmd.eclipse.util.internal.StringUtil;
2525
import net.sourceforge.pmd.lang.rule.RuleReference;
26-
import net.sourceforge.pmd.util.StringUtil;
2726

2827
/**
2928
*
@@ -116,7 +115,7 @@ private void formatExampleOn(StringBuilder sb, String example) {
116115
lines = StringUtil.trimStartOn(lines, trimDepth);
117116
}
118117
for (String line : lines) {
119-
sb.append(line).append(PMD.EOL);
118+
sb.append(line).append(System.lineSeparator());
120119
}
121120
}
122121

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/cpd2/CPDView2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
4040
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
4141
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
42-
import net.sourceforge.pmd.util.StringUtil;
42+
import net.sourceforge.pmd.eclipse.util.internal.StringUtil;
4343

4444
/**
4545
* An updated view for Cut & Paste Detector that shows the results in a tree
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
package net.sourceforge.pmd.eclipse.util.internal;
6+
7+
public final class StringUtil {
8+
9+
private StringUtil() {
10+
// utility
11+
}
12+
13+
public static int maxCommonLeadingWhitespaceForAll(String[] lines) {
14+
int max = Integer.MAX_VALUE;
15+
16+
for (String line : lines) {
17+
int current = 0;
18+
for (int i = 0; i < line.length() && Character.isWhitespace(line.charAt(i)); i++) {
19+
current++;
20+
}
21+
if (current < max) {
22+
max = current;
23+
}
24+
}
25+
26+
return max;
27+
}
28+
29+
public static String[] trimStartOn(String[] lines, int count) {
30+
if (count == 0) {
31+
return lines;
32+
}
33+
34+
String[] result = new String[lines.length];
35+
for (int i = 0; i < lines.length; i++) {
36+
result[i] = lines[i].substring(count);
37+
}
38+
return result;
39+
}
40+
}

0 commit comments

Comments
 (0)