From ae4c6e98f2845a818bb743b54001b6d9a573c05c Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 25 Apr 2014 20:58:14 -0700 Subject: [PATCH] replace escaped carraige returns and unicode from resources to get closer to what getString returns. If I can't find an Android API to do this it should be done on the server, but this is an improvement until I do. --- .../org/eehouse/android/xw4/loc/LocUtils.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java index 485fa0659..8ca2aa420 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java @@ -45,6 +45,8 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.json.JSONArray; import org.json.JSONObject; @@ -480,11 +482,33 @@ public class LocUtils { return result; } + private static Pattern s_patUnicode = Pattern.compile("(\\\\[Uu][0-9a-fA-F]{4})"); + private static Pattern s_patCr = Pattern.compile("\\\\n"); + private static String replaceEscaped( String txt ) { - String result = txt.replaceAll("\\n", "\n"); - // DbgUtils.logf( "replaceEscaped(<<%s>>) -> <<%s>>", txt, result ); - return result; + // String orig = txt; + + // Swap unicode escapes for real chars + Matcher matcher = s_patUnicode.matcher( txt ); + StringBuffer sb = new StringBuffer(); + while ( matcher.find() ) { + int start = matcher.start(); + int end = matcher.end(); + String match = txt.substring( start, end ); + char ch = (char)Integer.parseInt( match.substring(2), 16 ); + matcher.appendReplacement( sb, String.valueOf(ch) ); + } + matcher.appendTail(sb); + txt = sb.toString(); + + // Swap in real carriage returns + txt = s_patCr.matcher( txt ).replaceAll( "\n" ); + + // if ( ! orig.equals( txt ) ) { + // DbgUtils.logf( "replaceEscaped: <<%s>> -> <<%s>>", orig, txt ); + // } + return txt; } public static AlertDialog.Builder makeAlertBuilder( Context context )