Java的中文問題由來已久,前不久筆者需要做內存中的中文比較排序,對字符串進行GBK或者GB2312編碼以后,使用String.compareTo方法仍然不能得到正確結果。因此,懷著懷疑的態度,對JDK中String類的源代碼做了一翻探究。(作者使用JDK為1.3.1版本)
以下是String.java中compareTo的源代碼,請注意其中的注釋:
public class String
{
…
public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
//n為兩個字符串長度的最小者
int n = Math.min(len1, len2);
//獲取字符數組
char v1[] = value;
char v2[] = anotherString.value;
//取偏依位置
/** The offset is the first index of the storage that is used. */
//offset 是第一個存儲索引
int i = offset;
int j = anotherString.offset;
//如果i == j
//這里可能是判斷取同一內存中兩個字符串的情景。。。
// A <-- <----
// B s1 |
// C <-- |
// D s2
// E |
// F |
// G <----------
// 可能這種情況 i = j
if (i == j) {
int k = i;
int lim = n + i;
while (k < lim)
{
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) file://直到找到一個不相等的字符,返回c1 - c2
return c1 - c2;
k++;
}
} else {
while (n-- != 0)
file://直到兩個字符串長度記數為0
{
char c1 = v1[i++]; file://分別取字符
char c2 = v2[j++];
if (c1 != c2) {
//發現不相等,立即返回c1 - c2;
return c1 - c2;
}
}
}
return len1 - len2;
//最后這里可能出現的情況是: 兩個字符串比較完之后還沒有得到結果。相等的情況
}
…
}//end of class String c2) file://直到找到一個不相等的字符,返回c1 - c2 return c1 - c2; k++; } } else { while (n-- != 0) file://直到兩個字符串長度記數為0 { char c1 = v1[i++]; file://分別取字符 char c2 = v2[j++]; if (c1 != c2) { //發現不相等,立即返回c1 - c2; return c1 - c2; } } } return len1 - len2; //最后這里可能出現的情況是: 兩個字符串比較完之后還沒有得到結果。相等的情況 }…}//end of class String
為什么Java在做漢字的CompareTo時比較會有問題呢?通過對compareTo源代碼的分析發現,關鍵在于JDK的compareTo實現是直接使用Char來進行比較的:
char c1 = v1[k];
char c2 = v2[k];
可是當Java使用GB2312編碼時,一個對漢字所獲取到的Char值卻是不規則的,即一個漢字在Java中作為一個char來處理(雙字節字符)時,將這樣的雙字節字符進行強制轉換成int類型時,所得到的不是包含了漢字編碼順序的中文內碼??梢钥匆幌乱唤M測試數據可以看到其中奧妙:
字符
Char值
Byte[]值
按Byte[]合成的值
我
25105
[50:46]
[-5046]
愛
29233
[80:82]
[-8082]
北
21271
[79:79]
[-7979]
京
20140
[66:87]
[-6687]
天
22825
[52:20]
[-5220]
安
23433
[80:78]
[-8078]
門
38376
[61:59]
[-6159]
A
65
[-65]
[65]
B
66
[-66]
[66]
C
67
[-67]
[67]
D
68
[-68]
[68]
按照中文順序:“我”字應該在“愛”字后面,因此理論上來講"我"字的Char值應該比“愛"字的char值要大??墒遣恢罏槭裁碕ava的漢字char(兩個byte)->int類型的轉換會發生很大偏差。而失去了漢字原本在GBK規范當中,按內碼排列好的順序。但從一個漢字拆分成2個字節的byte[]時,所得到的值并沒有打亂GBK編碼規定的順序,因此得到解決問題的思路:將String進行GB2312編碼后取得某個漢字獲取其Char值時,將漢字拆分成2個字節byte[]再進行計算,從而得到正確的內碼。
因此我自己寫了下面這樣幾個函數,基本上解決了漢字比較的問題:
函數包括三個,你可以隨意放置到任何類當中作為輔助函數使用(Private Helper)。
n public int compare(String s1, String s2) :主要工作是為比較做一些前期的編碼工作可以說是系統的一個外殼。
n public int chineseCompareTo(String s1, String s2):該函數則是中文字符串比較主體,其內部實現了比較的最基本邏輯,和JDK的compareTo所使用的邏輯是一樣的。調用接口也一樣。
n public static int getCharCode(String s):該函數則負責將一個以字符串形式存在的字符轉換成為int編碼,兒不損失其位置信息。注意輸入通常是:“我”或者“A”,如果輸入更長的字符串,則改函數獲得的是第一個字符的值。
private static String __ENCODE__ = "GBK"; file://一定要是GBKprivate static String __SERVER_ENCODE__ = "GB2312"; file://服務器上的缺省編碼/*比較兩字符串*/ public int compare(String s1, String s2) { String m_s1 = null, m_s2 = null; try { //先將兩字符串編碼成GBK m_s1 = new String ( s1.getBytes(__SERVER_ENCODE__), __ENCODE__); m_s2 = new String ( s2.getBytes(__SERVER_ENCODE__), __ENCODE__); } catch( Exception ex) { return s1.compareTo(s2); } int res = chineseCompareTo(m_s1, m_s2); System.out.println("比較:" + s1 + " | " + s2 + "==== Result: " + res); return res; } //獲取一個漢字/字母的Char值 public static int getCharCode(String s) { if (s==null && s.equals(“”)) return -1; file://保護代碼byte [] b = s.getBytes(); int value = 0; //保證取第一個字符(漢字或者英文) for (int i = 0; i < b.length && i <= 2; i ++) { value = value * 100 + b[i]; } return value; } //比較兩個字符串 public int chineseCompareTo(String s1, String s2) { int len1 = s1.length(); int len2 = s2.length(); int n = Math.min(len1, len2); for (int i = 0; i < n; i ++) { int s1_code = getCharCode(s1.charAt(i) + ""); int s2_code = getCharCode(s2.charAt(i) + ""); if (s1_code != s2_code) return s1_code - s2_code; } return len1 - len2; }
可見,對系統源代碼的解剖,能讓我們在迷惑之余同樣有機會窺探系統內部運作的奧妙。不過讓人非常費解的是,Java內部的某些類書寫風格非常不好,同時存在一些Bug。不過這也許是筆者個人感受。偶有所獲,愿與大家共同分享,其中疏漏之處望不吝賜教。
原文轉自:http://www.anti-gravitydesign.com