Skip to content

Commit f2a6b4b

Browse files
committed
Use String.replace() instead of replaceAll() when we don't need a Regex.
1 parent 3638aba commit f2a6b4b

6 files changed

Lines changed: 80 additions & 80 deletions

File tree

src/main/java/org/apache/commons/codec/language/Caverphone1.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,56 +65,56 @@ public String encode(final String source) {
6565
txt = txt.replaceAll("mb$", "m2");
6666

6767
// 4. Handle replacements
68-
txt = txt.replaceAll("cq", "2q");
69-
txt = txt.replaceAll("ci", "si");
70-
txt = txt.replaceAll("ce", "se");
71-
txt = txt.replaceAll("cy", "sy");
72-
txt = txt.replaceAll("tch", "2ch");
73-
txt = txt.replaceAll("c", "k");
74-
txt = txt.replaceAll("q", "k");
75-
txt = txt.replaceAll("x", "k");
76-
txt = txt.replaceAll("v", "f");
77-
txt = txt.replaceAll("dg", "2g");
78-
txt = txt.replaceAll("tio", "sio");
79-
txt = txt.replaceAll("tia", "sia");
80-
txt = txt.replaceAll("d", "t");
81-
txt = txt.replaceAll("ph", "fh");
82-
txt = txt.replaceAll("b", "p");
83-
txt = txt.replaceAll("sh", "s2");
84-
txt = txt.replaceAll("z", "s");
68+
txt = txt.replace("cq", "2q");
69+
txt = txt.replace("ci", "si");
70+
txt = txt.replace("ce", "se");
71+
txt = txt.replace("cy", "sy");
72+
txt = txt.replace("tch", "2ch");
73+
txt = txt.replace("c", "k");
74+
txt = txt.replace("q", "k");
75+
txt = txt.replace("x", "k");
76+
txt = txt.replace("v", "f");
77+
txt = txt.replace("dg", "2g");
78+
txt = txt.replace("tio", "sio");
79+
txt = txt.replace("tia", "sia");
80+
txt = txt.replace("d", "t");
81+
txt = txt.replace("ph", "fh");
82+
txt = txt.replace("b", "p");
83+
txt = txt.replace("sh", "s2");
84+
txt = txt.replace("z", "s");
8585
txt = txt.replaceAll("^[aeiou]", "A");
8686
// 3 is a temporary placeholder marking a vowel
8787
txt = txt.replaceAll("[aeiou]", "3");
88-
txt = txt.replaceAll("3gh3", "3kh3");
89-
txt = txt.replaceAll("gh", "22");
90-
txt = txt.replaceAll("g", "k");
88+
txt = txt.replace("3gh3", "3kh3");
89+
txt = txt.replace("gh", "22");
90+
txt = txt.replace("g", "k");
9191
txt = txt.replaceAll("s+", "S");
9292
txt = txt.replaceAll("t+", "T");
9393
txt = txt.replaceAll("p+", "P");
9494
txt = txt.replaceAll("k+", "K");
9595
txt = txt.replaceAll("f+", "F");
9696
txt = txt.replaceAll("m+", "M");
9797
txt = txt.replaceAll("n+", "N");
98-
txt = txt.replaceAll("w3", "W3");
99-
txt = txt.replaceAll("wy", "Wy"); // 1.0 only
100-
txt = txt.replaceAll("wh3", "Wh3");
101-
txt = txt.replaceAll("why", "Why"); // 1.0 only
102-
txt = txt.replaceAll("w", "2");
98+
txt = txt.replace("w3", "W3");
99+
txt = txt.replace("wy", "Wy"); // 1.0 only
100+
txt = txt.replace("wh3", "Wh3");
101+
txt = txt.replace("why", "Why"); // 1.0 only
102+
txt = txt.replace("w", "2");
103103
txt = txt.replaceAll("^h", "A");
104-
txt = txt.replaceAll("h", "2");
105-
txt = txt.replaceAll("r3", "R3");
106-
txt = txt.replaceAll("ry", "Ry"); // 1.0 only
107-
txt = txt.replaceAll("r", "2");
108-
txt = txt.replaceAll("l3", "L3");
109-
txt = txt.replaceAll("ly", "Ly"); // 1.0 only
110-
txt = txt.replaceAll("l", "2");
111-
txt = txt.replaceAll("j", "y"); // 1.0 only
112-
txt = txt.replaceAll("y3", "Y3"); // 1.0 only
113-
txt = txt.replaceAll("y", "2"); // 1.0 only
104+
txt = txt.replace("h", "2");
105+
txt = txt.replace("r3", "R3");
106+
txt = txt.replace("ry", "Ry"); // 1.0 only
107+
txt = txt.replace("r", "2");
108+
txt = txt.replace("l3", "L3");
109+
txt = txt.replace("ly", "Ly"); // 1.0 only
110+
txt = txt.replace("l", "2");
111+
txt = txt.replace("j", "y"); // 1.0 only
112+
txt = txt.replace("y3", "Y3"); // 1.0 only
113+
txt = txt.replace("y", "2"); // 1.0 only
114114

115115
// 5. Handle removals
116-
txt = txt.replaceAll("2", "");
117-
txt = txt.replaceAll("3", "");
116+
txt = txt.replace("2", "");
117+
txt = txt.replace("3", "");
118118

119119
// 6. put six 1s on the end
120120
txt = txt + SIX_1;

src/main/java/org/apache/commons/codec/language/Caverphone2.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,56 +69,56 @@ public String encode(final String source) {
6969
txt = txt.replaceAll("mb$", "m2");
7070

7171
// 4. Handle replacements
72-
txt = txt.replaceAll("cq", "2q");
73-
txt = txt.replaceAll("ci", "si");
74-
txt = txt.replaceAll("ce", "se");
75-
txt = txt.replaceAll("cy", "sy");
76-
txt = txt.replaceAll("tch", "2ch");
77-
txt = txt.replaceAll("c", "k");
78-
txt = txt.replaceAll("q", "k");
79-
txt = txt.replaceAll("x", "k");
80-
txt = txt.replaceAll("v", "f");
81-
txt = txt.replaceAll("dg", "2g");
82-
txt = txt.replaceAll("tio", "sio");
83-
txt = txt.replaceAll("tia", "sia");
84-
txt = txt.replaceAll("d", "t");
85-
txt = txt.replaceAll("ph", "fh");
86-
txt = txt.replaceAll("b", "p");
87-
txt = txt.replaceAll("sh", "s2");
88-
txt = txt.replaceAll("z", "s");
72+
txt = txt.replace("cq", "2q");
73+
txt = txt.replace("ci", "si");
74+
txt = txt.replace("ce", "se");
75+
txt = txt.replace("cy", "sy");
76+
txt = txt.replace("tch", "2ch");
77+
txt = txt.replace("c", "k");
78+
txt = txt.replace("q", "k");
79+
txt = txt.replace("x", "k");
80+
txt = txt.replace("v", "f");
81+
txt = txt.replace("dg", "2g");
82+
txt = txt.replace("tio", "sio");
83+
txt = txt.replace("tia", "sia");
84+
txt = txt.replace("d", "t");
85+
txt = txt.replace("ph", "fh");
86+
txt = txt.replace("b", "p");
87+
txt = txt.replace("sh", "s2");
88+
txt = txt.replace("z", "s");
8989
txt = txt.replaceAll("^[aeiou]", "A");
9090
txt = txt.replaceAll("[aeiou]", "3");
91-
txt = txt.replaceAll("j", "y"); // 2.0 only
91+
txt = txt.replace("j", "y"); // 2.0 only
9292
txt = txt.replaceAll("^y3", "Y3"); // 2.0 only
9393
txt = txt.replaceAll("^y", "A"); // 2.0 only
94-
txt = txt.replaceAll("y", "3"); // 2.0 only
95-
txt = txt.replaceAll("3gh3", "3kh3");
96-
txt = txt.replaceAll("gh", "22");
97-
txt = txt.replaceAll("g", "k");
94+
txt = txt.replace("y", "3"); // 2.0 only
95+
txt = txt.replace("3gh3", "3kh3");
96+
txt = txt.replace("gh", "22");
97+
txt = txt.replace("g", "k");
9898
txt = txt.replaceAll("s+", "S");
9999
txt = txt.replaceAll("t+", "T");
100100
txt = txt.replaceAll("p+", "P");
101101
txt = txt.replaceAll("k+", "K");
102102
txt = txt.replaceAll("f+", "F");
103103
txt = txt.replaceAll("m+", "M");
104104
txt = txt.replaceAll("n+", "N");
105-
txt = txt.replaceAll("w3", "W3");
106-
txt = txt.replaceAll("wh3", "Wh3");
105+
txt = txt.replace("w3", "W3");
106+
txt = txt.replace("wh3", "Wh3");
107107
txt = txt.replaceAll("w$", "3"); // 2.0 only
108-
txt = txt.replaceAll("w", "2");
108+
txt = txt.replace("w", "2");
109109
txt = txt.replaceAll("^h", "A");
110-
txt = txt.replaceAll("h", "2");
111-
txt = txt.replaceAll("r3", "R3");
110+
txt = txt.replace("h", "2");
111+
txt = txt.replace("r3", "R3");
112112
txt = txt.replaceAll("r$", "3"); // 2.0 only
113-
txt = txt.replaceAll("r", "2");
114-
txt = txt.replaceAll("l3", "L3");
113+
txt = txt.replace("r", "2");
114+
txt = txt.replace("l3", "L3");
115115
txt = txt.replaceAll("l$", "3"); // 2.0 only
116-
txt = txt.replaceAll("l", "2");
116+
txt = txt.replace("l", "2");
117117

118118
// 5. Handle removals
119-
txt = txt.replaceAll("2", "");
119+
txt = txt.replace("2", "");
120120
txt = txt.replaceAll("3$", "A"); // 2.0 only
121-
txt = txt.replaceAll("3", "");
121+
txt = txt.replace("3", "");
122122

123123
// 6. put ten 1s on the end
124124
txt = txt + TEN_1;

src/main/java/org/apache/commons/codec/language/MatchRatingApproachEncoder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,11 @@ String removeVowels(String name) {
405405
// Extract first letter
406406
final String firstLetter = name.substring(0, 1);
407407

408-
name = name.replaceAll("A", EMPTY);
409-
name = name.replaceAll("E", EMPTY);
410-
name = name.replaceAll("I", EMPTY);
411-
name = name.replaceAll("O", EMPTY);
412-
name = name.replaceAll("U", EMPTY);
408+
name = name.replace("A", EMPTY);
409+
name = name.replace("E", EMPTY);
410+
name = name.replace("I", EMPTY);
411+
name = name.replace("O", EMPTY);
412+
name = name.replace("U", EMPTY);
413413

414414
name = name.replaceAll("\\s{2,}\\b", SPACE);
415415

src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public void testBase64InputStreamByChunk() throws Exception {
222222
testByChunk(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF);
223223

224224
// Single Line test.
225-
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
225+
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replace("\n", "");
226226
encoded = StringUtils.getBytesUtf8(singleLine);
227227
decoded = BaseNTestData.DECODED;
228228
testByChunk(encoded, decoded, 0, LF);
@@ -261,7 +261,7 @@ public void testBase64InputStreamByteByByte() throws Exception {
261261
testByteByByte(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF);
262262

263263
// Single Line test.
264-
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
264+
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replace("\n", "");
265265
encoded = StringUtils.getBytesUtf8(singleLine);
266266
decoded = BaseNTestData.DECODED;
267267
testByteByByte(encoded, decoded, 0, LF);

src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void testBase64OutputStreamByChunk() throws Exception {
115115
testByChunk(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF);
116116

117117
// Single Line test.
118-
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
118+
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replace("\n", "");
119119
encoded = StringUtils.getBytesUtf8(singleLine);
120120
decoded = BaseNTestData.DECODED;
121121
testByChunk(encoded, decoded, 0, LF);
@@ -154,7 +154,7 @@ public void testBase64OutputStreamByteByByte() throws Exception {
154154
testByteByByte(encoded, decoded, 64, LF);
155155

156156
// Single Line test.
157-
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
157+
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replace("\n", "");
158158
encoded = StringUtils.getBytesUtf8(singleLine);
159159
decoded = BaseNTestData.DECODED;
160160
testByteByByte(encoded, decoded, 0, LF);

src/test/java/org/apache/commons/codec/binary/Base64Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void testChunkedEncodeMultipleOf76() {
190190
// convert to "\r\n" so we're equal to the old openssl encoding test
191191
// stored
192192
// in Base64TestData.ENCODED_76_CHARS_PER_LINE:
193-
final String actualResult = Base64TestData.ENCODED_76_CHARS_PER_LINE.replaceAll("\n", "\r\n");
193+
final String actualResult = Base64TestData.ENCODED_76_CHARS_PER_LINE.replace("\n", "\r\n");
194194
final byte[] actualEncode = StringUtils.getBytesUtf8(actualResult);
195195
assertTrue("chunkedEncodeMultipleOf76", Arrays.equals(expectedEncode, actualEncode));
196196
}
@@ -324,7 +324,7 @@ public void testConstructor_Int_ByteArray_Boolean_UrlSafe() {
324324
final Base64 base64 = new Base64(64, new byte[] { '\t' }, true);
325325
final byte[] encoded = base64.encode(BaseNTestData.DECODED);
326326
String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE;
327-
expectedResult = expectedResult.replaceAll("=", ""); // url-safe has no
327+
expectedResult = expectedResult.replace("=", ""); // url-safe has no
328328
// == padding.
329329
expectedResult = expectedResult.replace('\n', '\t');
330330
expectedResult = expectedResult.replace('+', '-');

0 commit comments

Comments
 (0)