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

77
LINES

< > BotCompany Repo | #1008848 // unquoteCtx

JavaX fragment (include)

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

Author comment

Began life as a copy of #1001735

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: #1008848
Snippet name: unquoteCtx
Eternal ID of this version: #1008848/1
Text MD5: eeecf00511801222284cbde1813ac5fa
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-06-12 18:41:54
Source code size: 2215 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 535 / 582
Referenced in: [show references]