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

63
LINES

< > BotCompany Repo | #1035508 // unquoteSingleOrDoubleQuotes

JavaX fragment (include)

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

Author comment

Began life as a copy of #1001735

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035508
Snippet name: unquoteSingleOrDoubleQuotes
Eternal ID of this version: #1035508/1
Text MD5: 23793f3b8717119a71e55b13f48a5242
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-31 04:01:40
Source code size: 2081 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 141
Referenced in: [show references]