Skip to content

Commit 3ae8399

Browse files
committed
CODEC-308: change NYSIIS encoding to not remove the first character if its an A or S
1 parent 8d7887a commit 3ae8399

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ public String nysiis(String str) {
275275

276276
// First character of key = first character of name.
277277
final StringBuilder key = new StringBuilder(str.length());
278-
key.append(str.charAt(0));
278+
final char firstChar = str.charAt(0);
279+
key.append(firstChar);
279280

280281
// Transcode remaining characters, incrementing by one character each time
281282
final char[] chars = str.toCharArray();
@@ -314,6 +315,12 @@ public String nysiis(String str) {
314315
if (lastChar == 'A') {
315316
key.deleteCharAt(key.length() - 1);
316317
}
318+
319+
if(key.length()==0){
320+
// We've removed the first character of the string. Likely because it was na S or A
321+
// We should return at least the first character
322+
key.append(firstChar);
323+
}
317324
}
318325

319326
final String string = key.toString();

src/test/java/org/apache/commons/codec/language/NysiisTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public void testDropBy() throws EncoderException {
140140
new String[] { "JILES", "JAL" },
141141
// violates 6: if the last two characters are AY, remove A
142142
new String[] { "CARRAWAY", "CARY" }, // Original: CARAY
143-
new String[] { "YAMADA", "YANAD" });
143+
new String[] { "YAMADA", "YANAD" },
144+
new String[] { "ASH", "A"});
144145
}
145146

146147
@Test

0 commit comments

Comments
 (0)