@Test public void testEmojiTransliteration() throws UnsupportedEncodingException { String replacements = "U+263A :-)\n" + "U+1F641 :-(\n" + "U+1F632 o_O\n" + "U+1F918 \\m/\n" + "U+1F3FD\n"; TranslitUtil tu = new TranslitUtil(new ByteArrayInputStream(replacements.getBytes("UTF-8"))); assertEquals(":-) o_O \\m/ \uD83D\uDC4D", tu.transliterate("☺ \uD83D\uDE32 \uD83E\uDD18 \uD83D\uDC4D\uD83C\uDFFD")); }
public String transliterate(CharSequence text) { if (text == null) { return null; } try { StringBuffer sb; if (symbols != null) { sb = new StringBuffer(text.length() * 2); for (int i = 0; i < text.length(); i++) { int c = text.charAt(i); if (0xD800 <= c && c <= 0xDBFF) { if (i < text.length() - 1) { int low = text.charAt(i + 1); if (0xDC00 <= low && low <= 0xDFFF) { c = (c - 0xD800) * 0x400 + low - 0xDC00 + 0x10000; i++; } } } int pos = Arrays.binarySearch(symbols, c); if (pos >= 0) { sb.append(replacements[pos]); } else { sb.appendCodePoint(c); } } } else { sb = new StringBuffer(text); } ReplaceableString result = new ReplaceableString(sb); ANY_TO_LATIN.transliterate(result); return result.toString(); } catch (Exception e) { return text.toString(); } }
TranslitUtil { public String transliterate(CharSequence text) { if (text == null) { return null; } try { StringBuffer sb; if (symbols != null) { sb = new StringBuffer(text.length() * 2); for (int i = 0; i < text.length(); i++) { int c = text.charAt(i); if (0xD800 <= c && c <= 0xDBFF) { if (i < text.length() - 1) { int low = text.charAt(i + 1); if (0xDC00 <= low && low <= 0xDFFF) { c = (c - 0xD800) * 0x400 + low - 0xDC00 + 0x10000; i++; } } } int pos = Arrays.binarySearch(symbols, c); if (pos >= 0) { sb.append(replacements[pos]); } else { sb.appendCodePoint(c); } } } else { sb = new StringBuffer(text); } ReplaceableString result = new ReplaceableString(sb); ANY_TO_LATIN.transliterate(result); return result.toString(); } catch (Exception e) { return text.toString(); } } }
TranslitUtil { public String transliterate(CharSequence text) { if (text == null) { return null; } try { StringBuffer sb; if (symbols != null) { sb = new StringBuffer(text.length() * 2); for (int i = 0; i < text.length(); i++) { int c = text.charAt(i); if (0xD800 <= c && c <= 0xDBFF) { if (i < text.length() - 1) { int low = text.charAt(i + 1); if (0xDC00 <= low && low <= 0xDFFF) { c = (c - 0xD800) * 0x400 + low - 0xDC00 + 0x10000; i++; } } } int pos = Arrays.binarySearch(symbols, c); if (pos >= 0) { sb.append(replacements[pos]); } else { sb.appendCodePoint(c); } } } else { sb = new StringBuffer(text); } ReplaceableString result = new ReplaceableString(sb); ANY_TO_LATIN.transliterate(result); return result.toString(); } catch (Exception e) { return text.toString(); } } TranslitUtil(Resources res);  TranslitUtil(InputStream is); }
TranslitUtil { public String transliterate(CharSequence text) { if (text == null) { return null; } try { StringBuffer sb; if (symbols != null) { sb = new StringBuffer(text.length() * 2); for (int i = 0; i < text.length(); i++) { int c = text.charAt(i); if (0xD800 <= c && c <= 0xDBFF) { if (i < text.length() - 1) { int low = text.charAt(i + 1); if (0xDC00 <= low && low <= 0xDFFF) { c = (c - 0xD800) * 0x400 + low - 0xDC00 + 0x10000; i++; } } } int pos = Arrays.binarySearch(symbols, c); if (pos >= 0) { sb.append(replacements[pos]); } else { sb.appendCodePoint(c); } } } else { sb = new StringBuffer(text); } ReplaceableString result = new ReplaceableString(sb); ANY_TO_LATIN.transliterate(result); return result.toString(); } catch (Exception e) { return text.toString(); } } TranslitUtil(Resources res);  TranslitUtil(InputStream is); String transliterate(CharSequence text); }
TranslitUtil { public String transliterate(CharSequence text) { if (text == null) { return null; } try { StringBuffer sb; if (symbols != null) { sb = new StringBuffer(text.length() * 2); for (int i = 0; i < text.length(); i++) { int c = text.charAt(i); if (0xD800 <= c && c <= 0xDBFF) { if (i < text.length() - 1) { int low = text.charAt(i + 1); if (0xDC00 <= low && low <= 0xDFFF) { c = (c - 0xD800) * 0x400 + low - 0xDC00 + 0x10000; i++; } } } int pos = Arrays.binarySearch(symbols, c); if (pos >= 0) { sb.append(replacements[pos]); } else { sb.appendCodePoint(c); } } } else { sb = new StringBuffer(text); } ReplaceableString result = new ReplaceableString(sb); ANY_TO_LATIN.transliterate(result); return result.toString(); } catch (Exception e) { return text.toString(); } } TranslitUtil(Resources res);  TranslitUtil(InputStream is); String transliterate(CharSequence text);  }