Skip to content

Commit 7498984

Browse files
committed
Simplify by using switch on char value; also don't convert to String unnecessarily
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1842176 13f79535-47bb-0310-9956-ffa450edef68
1 parent d89a872 commit 7498984

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,6 @@ public char removeNext() {
286286
* <li>small sharp s, German</li>
287287
* </ul>
288288
*/
289-
private static final char[][] PREPROCESS_MAP = new char[][]{
290-
{'\u00C4', 'A'}, // capital a, umlaut mark
291-
{'\u00DC', 'U'}, // capital u, umlaut mark
292-
{'\u00D6', 'O'}, // capital o, umlaut mark
293-
{'\u00DF', 'S'} // small sharp s, German
294-
};
295289

296290
/*
297291
* Returns whether the array contains the key, or not.
@@ -321,10 +315,8 @@ public String colognePhonetic(String text) {
321315
return null;
322316
}
323317

324-
text = preprocess(text);
325-
326-
final CologneOutputBuffer output = new CologneOutputBuffer(text.length() * 2);
327-
final CologneInputBuffer input = new CologneInputBuffer(text.toCharArray());
318+
final CologneInputBuffer input = new CologneInputBuffer(preprocess(text));
319+
final CologneOutputBuffer output = new CologneOutputBuffer(input.length() * 2);
328320

329321
char nextChar;
330322

@@ -423,23 +415,27 @@ public boolean isEncodeEqual(final String text1, final String text2) {
423415
}
424416

425417
/**
426-
* Converts the string to upper case and replaces germanic characters as defined in {@link #PREPROCESS_MAP}.
418+
* Converts the string to upper case and replaces Germanic umlaut characters
427419
*/
428-
private String preprocess(String text) {
429-
text = text.toUpperCase(Locale.GERMAN);
430-
431-
final char[] chrs = text.toCharArray();
420+
private char[] preprocess(String text) {
421+
// This converts German small sharp s (Eszett) to SS
422+
final char[] chrs = text.toUpperCase(Locale.GERMAN).toCharArray();
432423

433424
for (int index = 0; index < chrs.length; index++) {
434-
if (chrs[index] > 'Z') {
435-
for (final char[] element : PREPROCESS_MAP) {
436-
if (chrs[index] == element[0]) {
437-
chrs[index] = element[1];
438-
break;
439-
}
440-
}
425+
switch (chrs[index]) {
426+
case '\u00C4': // capital A, umlaut mark
427+
chrs[index] = 'A';
428+
break;
429+
case '\u00DC': // capital U, umlaut mark
430+
chrs[index] = 'U';
431+
break;
432+
case '\u00D6': // capital O, umlaut mark
433+
chrs[index] = 'O';
434+
break;
435+
default:
436+
break;
441437
}
442438
}
443-
return new String(chrs);
439+
return chrs;
444440
}
445441
}

0 commit comments

Comments
 (0)