Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/org/apache/commons/codec/language/Nysiis.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ public String nysiis(String str) {

// First character of key = first character of name.
final StringBuilder key = new StringBuilder(str.length());
key.append(str.charAt(0));
final char firstChar = str.charAt(0);
key.append(firstChar);

// Transcode remaining characters, incrementing by one character each time
final char[] chars = str.toCharArray();
Expand Down Expand Up @@ -306,6 +307,12 @@ public String nysiis(String str) {
if (lastChar == 'A') {
key.deleteCharAt(key.length() - 1);
}

if (key.length() == 0) {
// We've removed the first character of the string. Likely because it was an S or A
// We should return at least the first character
key.append(firstChar);
}
Comment on lines 307 to +315

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot is wrong on this. The check to remove a solitary 's' is already happening before this final length check, at lines 300-303

}

final String string = key.toString();
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/org/apache/commons/codec/language/NysiisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package org.apache.commons.codec.language;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.commons.codec.AbstractStringEncoderTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests {@link Nysiis}
*/
Expand Down Expand Up @@ -136,7 +136,9 @@ void testDropBy() {
new String[] { "JILES", "JAL" },
// violates 6: if the last two characters are AY, remove A
new String[] { "CARRAWAY", "CARY" }, // Original: CARAY
new String[] { "YAMADA", "YANAD" });
new String[] { "YAMADA", "YANAD" },
new String[] { "ASH", "A"},
new String[] { "SSH", "S"});
}

@Test
Expand Down