Skip to content

Commit d6d8c7f

Browse files
authored
Merge pull request #106 from evllabs/fix-smash-i
Remove Regressive Smash I Cases.
2 parents 1b982d5 + 8bf9666 commit d6d8c7f

File tree

2 files changed

+35
-48
lines changed

2 files changed

+35
-48
lines changed

src/com/jgaap/canonicizers/SmashI.java

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@
1717
*/
1818
package com.jgaap.canonicizers;
1919

20-
import com.jgaap.generics.CanonicizationException;
2120
import com.jgaap.generics.Canonicizer;
2221

22+
import java.util.regex.Matcher;
23+
import java.util.regex.Pattern;
24+
2325
/**
2426
* Canonicizer for smashing all instances in which "I" is used as a
2527
* word.
26-
*
28+
*
2729
* @author David
2830
* @since 8.0.0
2931
*/
3032
public class SmashI extends Canonicizer {
3133

34+
private static Pattern iWord = Pattern.compile("(^|\\W+)I(\\W+|$)");
35+
3236
@Override
3337
public String displayName() {
3438
return "Smash I";
@@ -38,7 +42,7 @@ public String displayName() {
3842
public String tooltipText() {
3943
return "Converts all uses of \"I\" to lowercase.";
4044
}
41-
45+
4246
@Override
4347
public String longDescription() {
4448
return "Converts all uses of \"I\" as a word to lowercase.";
@@ -50,40 +54,9 @@ public boolean showInGUI() {
5054
}
5155

5256
@Override
53-
public char[] process(char[] procText) throws CanonicizationException {
54-
for (int x = 0; x < procText.length; x++) {
55-
// Character is not even a potential candidate for smashing if it is
56-
// not a capital I.
57-
if (procText[x] == 'I' ) {
58-
// Left and right flag are for indicating if whitespace was found
59-
// on either side of "I."
60-
boolean leftFlag = false;
61-
boolean rightFlag = false;
62-
63-
try {
64-
// Check for whitespace on left side.
65-
leftFlag = Character.isWhitespace(procText[x - 1]);
66-
}
67-
catch (ArrayIndexOutOfBoundsException e) {
68-
// If the I is at the beginning of the string, set left flag to true.
69-
leftFlag = true;
70-
}
71-
72-
try {
73-
// Check for whitespace on the right side.
74-
rightFlag = Character.isWhitespace(procText[x + 1]);
75-
}
76-
catch (ArrayIndexOutOfBoundsException e) {
77-
// If the I is at the end of the string, set right flag to true.
78-
rightFlag = true;
79-
}
80-
81-
// Smash character if both flags are set to true.
82-
if (leftFlag && rightFlag)
83-
procText[x] = Character.toLowerCase(procText[x]);
84-
}
85-
}
86-
return procText;
57+
public char[] process(char[] procText) {
58+
Matcher m = iWord.matcher(new String(procText));
59+
return m.replaceAll("$1i$2").toCharArray();
8760
}
88-
8961
}
62+

unittests/com/jgaap/canonicizers/SmashITest.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717
*/
1818
package com.jgaap.canonicizers;
1919

20-
import static org.junit.Assert.assertTrue;
21-
22-
import java.util.Arrays;
23-
2420
import org.junit.Test;
2521

26-
import com.jgaap.generics.CanonicizationException;
22+
import static org.junit.Assert.assertArrayEquals;
2723

2824
/**
2925
* Unit test for the Smash I canonicizer.
@@ -33,32 +29,50 @@
3329
*/
3430
public class SmashITest {
3531
@Test
36-
public void testProcess() throws CanonicizationException {
32+
public void testProcess() {
3733
SmashI smashI = new SmashI();
3834

3935
// Test 1 - I on ends
4036
String in = "I don't care if IPad is supposed to be spelled with a lowercase I";
4137
char[] correct = "i don't care if IPad is supposed to be spelled with a lowercase i".toCharArray();
4238
char[] actual = smashI.process(in.toCharArray());
43-
assertTrue(Arrays.equals(correct, actual));
39+
assertArrayEquals(correct, actual);
4440

4541
// Test 2 - I in middle surrounded by spaces
4642
in = "Sometimes I cannot think of creative things to write for unit tests.";
4743
correct = "Sometimes i cannot think of creative things to write for unit tests.".toCharArray();
4844
actual = smashI.process(in.toCharArray());
49-
assertTrue(Arrays.equals(correct, actual));
45+
assertArrayEquals(correct, actual);
5046

5147
// Test 3 - I in middle surrounded by tabs
5248
in = "Sometimes I cannot think of creative things to write for unit tests.";
5349
correct = in.toCharArray();
5450
correct[11] = 'i';
5551
actual = smashI.process(in.toCharArray());
56-
assertTrue(Arrays.equals(correct, actual));
52+
assertArrayEquals(correct, actual);
5753

5854
// Test 4 - Bunch of I's next to each other with varying case
5955
in = "iIiIiiIIiiiiiiIIiiIiIiIIiiIIiiIiiIiiIIiiiIiiiIiI";
6056
correct = in.toCharArray();
6157
actual = smashI.process(in.toCharArray());
62-
assertTrue(Arrays.equals(correct, actual));
58+
assertArrayEquals(correct, actual);
59+
60+
// Test 5 - Non-whitespaced I
61+
in = "\"I am here\", she said.";
62+
correct = "\"i am here\", she said.".toCharArray();
63+
actual = smashI.process(in.toCharArray());
64+
assertArrayEquals(correct, actual);
65+
66+
// Test 6 - Non-whitespaced I
67+
in = "I'm here.";
68+
correct = "i'm here.".toCharArray();
69+
actual = smashI.process(in.toCharArray());
70+
assertArrayEquals(correct, actual);
71+
72+
// Test 7 - Non-whitespaced I
73+
in = "It is I.";
74+
correct = "It is i.".toCharArray();
75+
actual = smashI.process(in.toCharArray());
76+
assertArrayEquals(correct, actual);
6377
}
6478
}

0 commit comments

Comments
 (0)