package me.ykrank.s1next.util;

import android.support.annotation.Nullable;

import org.apache.commons.lang3.StringUtils;

public final class StringUtil {

    static final String TWO_SPACES = "  ";

    private static final String NON_BREAKING_SPACE_ENTITY_NAME = "&nbsp;";

    private StringUtil() {
    }

    /**
     * Concatenates {@code first} and {@code second} with {@link #TWO_SPACES}.
     * <p>
     * <pre>
     * StringUtil.concatWithTwoSpaces("a", 1) = "a  1"
     * </pre>
     *
     * @return A new string which is the concatenation of this string, two spaces
     * and the specified string.
     */
    public static String concatWithTwoSpaces(CharSequence first, int last) {
        return concatWithTwoSpaces(first, String.valueOf(last));
    }

    /**
     * <pre>
     * StringUtil.concatWithTwoSpaces(1, "a") = "1  a"
     * </pre>
     *
     * @see #concatWithTwoSpaces(CharSequence, int)
     */
    public static String concatWithTwoSpaces(int first, CharSequence last) {
        return concatWithTwoSpaces(String.valueOf(first), last);
    }

    /**
     * <pre>
     * StringUtil.concatWithTwoSpaces("a", "b") = "a  b"
     * </pre>
     *
     * @see #concatWithTwoSpaces(CharSequence, int)
     */
    public static String concatWithTwoSpaces(@Nullable CharSequence first, CharSequence last) {
        if (first == null) {
            return last.toString();
        }
        return first + TWO_SPACES + last;
    }

    /**
     * Replaces all occurrences of the {@link #NON_BREAKING_SPACE_ENTITY_NAME}
     * within the space.
     *
     * @param text The text to search and replace in.
     * @return The text with any replacements processed.
     */
    public static String unescapeNonBreakingSpace(String text) {
        return StringUtils.replace(text, NON_BREAKING_SPACE_ENTITY_NAME,
                StringUtils.SPACE);
    }

    /**
     * decode like \u8652
     */
    public static String uniDecode(String s) {
        StringBuilder sb = new StringBuilder(s.length());
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (c == '\\' && chars[i + 1] == 'u') {
                char cc = 0;
                for (int j = 0; j < 4; j++) {
                    char ch = Character.toLowerCase(chars[i + 2 + j]);
                    if ('0' <= ch && ch <= '9' || 'a' <= ch && ch <= 'f') {
                        cc |= (Character.digit(ch, 16) << (3 - j) * 4);
                    } else {
                        cc = 0;
                        break;
                    }
                }
                if (cc > 0) {
                    i += 5;
                    sb.append(cc);
                    continue;
                }
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

@@ -2158,20 +2158,185 @@
  == '%5C%5C'
- && 
+) %7B%0A                if (chars%5Bi + 1%5D == '%5C%5C') %7B%0A                    i++;%0A                    sb.append(%22%5C%5C%5C%5C%22);%0A                    continue;%0A                %7D else if (
 chars%5Bi 
@@ -2362,24 +2362,28 @@
             
+  
+  
 char cc = 0;
@@ -2375,32 +2375,36 @@
    char cc = 0;%0A
+    
                 
@@ -2449,24 +2449,28 @@
             
+  
+  
 char ch = Ch
@@ -2508,16 +2508,20 @@
  + j%5D);%0A
+    
         
@@ -2612,16 +2612,20 @@
         
+ 
+   
 cc %7C= (C
@@ -2680,24 +2680,28 @@
             
+    
 %7D else %7B%0A   
@@ -2721,16 +2721,20 @@
         
+ 
+   
 cc = 0;%0A
@@ -2757,16 +2757,20 @@
         
+    
 break;%0A 
@@ -2780,34 +2780,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
                 
@@ -2828,24 +2828,28 @@
             
+    
 if (cc %3E 0) 
@@ -2846,24 +2846,28 @@
  (cc %3E 0) %7B%0A
+    
             
@@ -2894,32 +2894,36 @@
                 
+    
 sb.append(cc);%0A 
@@ -2933,32 +2933,36 @@
                 
+    
 continue;%0A      
@@ -2947,32 +2947,54 @@
       continue;%0A
+                    %7D%0A
                 

Fix unicode parse error