* @return the translated character string
*/
public static CharSequence translateUsingMap(CharSequence in, IntToIntMap map) {
int len = in.length();
FastStringBuffer sb = new FastStringBuffer(len);
for (int i=0; i<len; i++) {
int charval;
int c = in.charAt(i);
if (c >= 55296 && c <= 56319) {
// we'll trust the data to be sound
charval = ((c - 55296) * 1024) + ((int) in.charAt(i + 1) - 56320) + 65536;
i++;
} else {
charval = c;
}
int newchar = map.get(charval);
if (newchar == Integer.MAX_VALUE) {
// character not in map, so is not to be translated
newchar = charval;
}
if (newchar == -1) {
// no action, delete the character
} else if (newchar < 65536) {
sb.append((char)newchar);
} else { // output a surrogate pair
//To compute the numeric value of the character corresponding to a surrogate
//pair, use this formula (all numbers are hex):
//(FirstChar - D800) * 400 + (SecondChar - DC00) + 10000
newchar -= 65536;
sb.append((char)((newchar / 1024) + 55296));
sb.append((char)((newchar % 1024) + 56320));
}
}
return sb;
}