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

75
LINES

< > BotCompany Repo | #2000514 // function unquote, fixed

New Tinybrain snippet

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

Author comment

Began life as a copy of #2000471

download  show line numbers   

Snippet is not live.

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

Comments [hide]

ID Author/Program Comment Date
659 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
public static String unquote(String s) {
if (s.startsWith("[[") && s.endsWith("]]"))
return s.substring(2, s.length()-2);
else if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) {
String st = s.substring(1, s.length()-1);
StringBuilder sb = new StringBuilder(st.length());

for (int i = 0; i < st.length(); i++) {
char ch = st.charAt(i);
if (ch == '\\') {
char nextChar = (i == st.length() - 1) ? '\\' : st
.charAt(i + 1);
// Octal escape?
if (nextChar >= '0' && nextChar <= '7') {
String code = "" + nextChar;
i++;
if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
&& st.charAt(i + 1) <= '7') {
code += st.charAt(i + 1);
i++;
if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
&& st.charAt(i + 1) <= '7') {
code += st.charAt(i + 1);
i++;
}
}
sb.append((char) Integer.parseInt(code, 8));
continue;
}
switch (nextChar) {
case '\\':
ch = '\\';
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case '\"':
ch = '\"';
break;
case '\'':
ch = '\'';
break;
// Hex Unicode: u????
case 'u':
if (i >= st.length() - 5) {
ch = 'u';
break;
}
int code = Integer.parseInt(
"" + st.charAt(i + 2) + st.charAt(i + 3)
+ st.charAt(i + 4) + st.charAt(i + 5), 16);
sb.append(Character.toChars(code));
i += 5;
continue;
}
i++;
}
sb.append(ch);
}
return sb.toString();
} else
return s; // return original
}
}}
2015-08-19 05:32:59  delete 
657 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #2000514
Snippet name: function unquote, fixed
Eternal ID of this version: #2000514/1
Text MD5: 1062f13566cd143aa5ee704911b216c6
Author: stefan
Category:
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-10 19:26:05
Source code size: 2399 bytes / 75 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 892 / 137
Referenced in: [show references]