Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

86
LINES

< > BotCompany Repo | #1005609 // unquote_prev (previous, known to work)

JavaX fragment (include)

1  
public static String unquote_prev(String s) {
2  
  if (s == null) return null;
3  
  if (s.startsWith("[")) {
4  
    int i = 1;
5  
    while (i < s.length() && s.charAt(i) == '=') ++i;
6  
    if (i < s.length() && s.charAt(i) == '[') {
7  
      String m = s.substring(1, i);
8  
      if (s.endsWith("]" + m + "]"))
9  
        return s.substring(i+1, s.length()-i-1);
10  
    }
11  
  }
12  
  
13  
  if (s.startsWith("\"") /*&& s.endsWith("\"")*/ && s.length() > 1) {
14  
    String st = s.substring(1, s.endsWith("\"") ? s.length()-1 : s.length());
15  
    StringBuilder sb = new StringBuilder(st.length());
16  
17  
    for (int i = 0; i < st.length(); i++) {
18  
      char ch = st.charAt(i);
19  
      if (ch == '\\') {
20  
        char nextChar = (i == st.length() - 1) ? '\\' : st
21  
                .charAt(i + 1);
22  
        // Octal escape?
23  
        if (nextChar >= '0' && nextChar <= '7') {
24  
            String code = "" + nextChar;
25  
            i++;
26  
            if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
27  
                    && st.charAt(i + 1) <= '7') {
28  
                code += st.charAt(i + 1);
29  
                i++;
30  
                if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
31  
                        && st.charAt(i + 1) <= '7') {
32  
                    code += st.charAt(i + 1);
33  
                    i++;
34  
                }
35  
            }
36  
            sb.append((char) Integer.parseInt(code, 8));
37  
            continue;
38  
        }
39  
        switch (nextChar) {
40  
        case '\\':
41  
            ch = '\\';
42  
            break;
43  
        case 'b':
44  
            ch = '\b';
45  
            break;
46  
        case 'f':
47  
            ch = '\f';
48  
            break;
49  
        case 'n':
50  
            ch = '\n';
51  
            break;
52  
        case 'r':
53  
            ch = '\r';
54  
            break;
55  
        case 't':
56  
            ch = '\t';
57  
            break;
58  
        case '\"':
59  
            ch = '\"';
60  
            break;
61  
        case '\'':
62  
            ch = '\'';
63  
            break;
64  
        // Hex Unicode: u????
65  
        case 'u':
66  
            if (i >= st.length() - 5) {
67  
                ch = 'u';
68  
                break;
69  
            }
70  
            int code = Integer.parseInt(
71  
                    "" + st.charAt(i + 2) + st.charAt(i + 3)
72  
                            + st.charAt(i + 4) + st.charAt(i + 5), 16);
73  
            sb.append(Character.toChars(code));
74  
            i += 5;
75  
            continue;
76  
        default:
77  
          ch = nextChar; // added by Stefan
78  
        }
79  
        i++;
80  
      }
81  
      sb.append(ch);
82  
    }
83  
    return sb.toString();      
84  
  } else
85  
    return s; // return original
86  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005609
Snippet name: unquote_prev (previous, known to work)
Eternal ID of this version: #1005609/1
Text MD5: 32679aea839c0b19ab601e9c037e1147
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-29 16:31:35
Source code size: 2579 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 553 / 867
Referenced in: [show references]