public static int compareToIgnoreSpaces(char[] charArray, char[] anotherCharArray) {
if (charArray == anotherCharArray) {
return 0;
}
if (charArray == null && anotherCharArray == null) {
return 0;
} else if (charArray == null) {
return -1;
} else if (anotherCharArray == null) {
return 1;
}
if (charArray.length == 0 && anotherCharArray.length == 0) {
return 0;
} else if (charArray.length == 0) {
return -1;
} else if (anotherCharArray.length == 0) {
return 1;
}
// we only ignore end white spaces
int startIndex0 = 0;
int endIndex0 = charArray.length - 1;
if (startIndex0 < charArray.length) {
// charArray has non whitespace char
while (endIndex0 >= 0) {
char c = charArray[endIndex0];
if (c == ' ') {
endIndex0--;
} else {
break;
}
}
}
int startIndex1 = 0;
int endIndex1 = anotherCharArray.length - 1;
if (startIndex1 < anotherCharArray.length) {
// anotherCharArray has non whitespace char
while (endIndex1 >= 0) {
char c = anotherCharArray[endIndex1];
if (c == ' ') {
endIndex1--;
} else {
break;
}
}
}
int len1 = endIndex0 - startIndex0 + 1;
int len2 = endIndex1 - startIndex1 + 1;
int n = ((len1 <= len2) ? len1 : len2);
for (int i = 0; i < n; i++) {
char c1 = charArray[startIndex0 + i];
char c2 = anotherCharArray[startIndex1 + i];
if (c1 >= 0x4E00 && c1 <= 0x9FA5 &&
c2 >= 0x4E00 && c2 <= 0x9FA5)
{
String py1 = Unicode2Pinyin.getPinyin(c1);
String py2 = Unicode2Pinyin.getPinyin(c2);
int code = py1.compareTo(py2);
if (code != 0) {
return code;
}
}
if (c1 != c2) {
return c1 - c2;
}
}
return len1 - len2;
}
public class Unicode2Pinyin {
private static final char firstChar = '\u3007';
private static final int size = 28575;
private static String[] u2y = new String[size];
public static final Unicode2Pinyin singleton = new Unicode2Pinyin();
public Unicode2Pinyin() {
try {
String confDirPath = System.getProperty("cloudwave.home", "..") + "/conf";
File file = new File(confDirPath + "/Uni2Pinyin");
if (!file.exists()) {
throw new RuntimeException(file.getAbsolutePath() + " does not exists!");
}
FileReader reader = new FileReader(file);
BufferedReader bReader = new BufferedReader(reader);
String line = bReader.readLine();
while (line != null) {
if (!line.startsWith("#")) {
StringTokenizer tokenizer = new StringTokenizer(line);
String unicode = null;
String pinyin = null;
if (tokenizer.hasMoreTokens()) {
unicode = tokenizer.nextToken();
if (tokenizer.hasMoreTokens()) {
pinyin = tokenizer.nextToken();
}
if (unicode != null) {
char c = (char) Integer.parseInt(unicode, 16);
if (pinyin == null) {
pinyin = Character.toString(c);
}
u2y[c - firstChar] = pinyin;
}
}
}
line = bReader.readLine();
}
bReader.close();
reader.close();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public static String getPinyin(char c) {
int index = c - firstChar;
if (index >= size || index < 0) {
return "zz";
}
String pinyin = u2y[index];
if (pinyin == null) {
return "zz";
}
return pinyin;
}
public static Unicode2Pinyin get() {
return singleton;
}
public static void main(String[] args) {
get();
}
}