Skip to content

Commit 29a7e67

Browse files
committed
CODEC-199 Bug in HW rule in Soundex
Revert to a fix which does not entail change to public API git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1789764 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8bb1151 commit 29a7e67

2 files changed

Lines changed: 17 additions & 25 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
5959
<action issue="CODEC-221" dev="sebb" type="update">HmacUtils.updateHmac calls reset() unnecessarily</action>
6060
<action issue="CODEC-200" dev="sebb" type="fix" due-to="Luciano Vernaschi">Base32.HEX_DECODE_TABLE contains the wrong value 32</action>
6161
<action issue="CODEC-207" dev="ggregory" type="fix" due-to="Gary Gregory">Charsets Javadoc breaks build when using Java 8</action>
62-
<action issue="CODEC-199" dev="ggregory" type="fix" due-to="Yossi Tamari">Bug in HW rule in Soundex</action>
62+
<action issue="CODEC-199" dev="ggregory/sebb" type="fix" due-to="Yossi Tamari">Bug in HW rule in Soundex</action>
6363
<action issue="CODEC-209" dev="ggregory" type="fix" due-to="Gary Gregory">Javadoc for SHA-224 DigestUtils methods should mention Java 1.8.0 restriction instead of 1.4.0.</action>
6464
<action issue="CODEC-219" dev="ggregory" type="fix" due-to="Gary Gregory, Sebb">Don't deprecate Charsets Charset constants in favor of Java 7's java.nio.charset.StandardCharsets</action>
6565
<action issue="CODEC-217" dev="ggregory" type="add" due-to="Gary Gregory">Add HmacAlgorithms.HMAC_SHA_224 (Java 8 only)</action>

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Soundex implements StringEncoder {
4141
*
4242
* @see #US_ENGLISH_MAPPING
4343
*/
44-
public static final String US_ENGLISH_MAPPING_STRING = "0123012#02245501262301#202";
44+
public static final String US_ENGLISH_MAPPING_STRING = "01230120022455012623010202";
4545

4646
/**
4747
* This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
@@ -178,15 +178,6 @@ public int getMaxLength() {
178178
return this.maxLength;
179179
}
180180

181-
/**
182-
* Returns the soundex mapping.
183-
*
184-
* @return soundexMapping.
185-
*/
186-
private char[] getSoundexMapping() {
187-
return this.soundexMapping;
188-
}
189-
190181
/**
191182
* Maps the given upper-case character to its Soundex code.
192183
*
@@ -198,10 +189,10 @@ private char[] getSoundexMapping() {
198189
*/
199190
private char map(final char ch) {
200191
final int index = ch - 'A';
201-
if (index < 0 || index >= this.getSoundexMapping().length) {
192+
if (index < 0 || index >= this.soundexMapping.length) {
202193
throw new IllegalArgumentException("The character is not mapped: " + ch);
203194
}
204-
return this.getSoundexMapping()[index];
195+
return this.soundexMapping[index];
205196
}
206197

207198
/**
@@ -234,19 +225,20 @@ public String soundex(String str) {
234225
return str;
235226
}
236227
final char out[] = {'0', '0', '0', '0'};
237-
char last, mapped;
238-
int incount = 1, count = 1;
239-
out[0] = str.charAt(0);
240-
// map() throws IllegalArgumentException
241-
last = this.map(str.charAt(0));
242-
while (incount < str.length() && count < out.length) {
243-
mapped = this.map(str.charAt(incount++));
244-
if (mapped == '0') {
245-
last = mapped;
246-
} else if (mapped != '#' && mapped != last) {
247-
out[count++] = mapped;
248-
last = mapped;
228+
int count = 0;
229+
final char first = str.charAt(0);
230+
out[count++] = first;
231+
char lastDigit = map(first); // previous digit
232+
for(int i = 1; i < str.length() && count < out.length ; i++) {
233+
char ch = str.charAt(i);
234+
if (ch == 'H' || ch == 'W') { // these are ignored completely
235+
continue;
236+
}
237+
char digit = map(ch);
238+
if (digit != '0' && digit != lastDigit) { // don't store vowels or repeats
239+
out[count++] = digit;
249240
}
241+
lastDigit = digit;
250242
}
251243
return new String(out);
252244
}

0 commit comments

Comments
 (0)