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

28
LINES

< > BotCompany Repo | #1009089 // singleQuote - enclose string in single quotes ('); doesn't escape "; not a Java construct

JavaX fragment (include)

1  
static S singleQuote(O o) {
2  
  if (o == null) return "null";
3  
  ret singleQuote(str(o));
4  
}
5  
6  
static S singleQuote(S s) {
7  
  if (s == null) return "null";
8  
  StringBuilder out = new StringBuilder((int) (l(s)*1.5+2));
9  
  singleQuote_impl(s, out);
10  
  ret out.toString();
11  
}
12  
  
13  
static void singleQuote_impl(S s, StringBuilder out) {
14  
  out.append('\'');
15  
  int l = s.length();
16  
  for (int i = 0; i < l; i++) {
17  
    char c = s.charAt(i);
18  
    if (c == '\\' || c == '\'')
19  
      out.append('\\').append(c);
20  
    else if (c == '\r')
21  
      out.append("\\r");
22  
    else if (c == '\n')
23  
      out.append("\\n");
24  
    else
25  
      out.append(c);
26  
  }
27  
  out.append('\'');
28  
}

Author comment

Began life as a copy of #1005595

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: #1009089
Snippet name: singleQuote - enclose string in single quotes ('); doesn't escape "; not a Java construct
Eternal ID of this version: #1009089/5
Text MD5: 3a817199794449dd9a4d2973f9c63edc
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-02 17:17:49
Source code size: 667 bytes / 28 lines
Pitched / IR pitched: No / No
Views / Downloads: 356 / 413
Version history: 4 change(s)
Referenced in: [show references]