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

30
LINES

< > BotCompany Repo | #1005595 - quote - Java-compatible string quoting with ""

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (705L/5K/19K).

1  
static S quote(O o) {
2  
  if (o == null) return "null";
3  
  ret quote(str(o));
4  
}
5  
6  
static S quote(S s) {
7  
  if (s == null) return "null";
8  
  StringBuilder out = new StringBuilder((int) (l(s)*1.5+2));
9  
  quote_impl(s, out);
10  
  ret out.toString();
11  
}
12  
  
13  
static void quote_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 if (c == '\0')
25  
      out.append("\\0");
26  
    else
27  
      out.append(c);
28  
  }
29  
  out.append('"');
30  
}

Author comment

Began life as a copy of #1001239

download  show line numbers  debug dex   

Travelled to 12 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, podlckwnjdmb, tslmcundralx, tvejysmllsmz, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1005595
Snippet name: quote - Java-compatible string quoting with ""
Eternal ID of this version: #1005595/4
Text MD5: ee0f7e61fcaecc9c7bdc5ac492216526
Transpilation MD5: c93397376fa83091e25c1c6d009a9b72
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-18 04:28:14
Source code size: 685 bytes / 30 lines
Pitched / IR pitched: No / No
Views / Downloads: 461 / 838
Version history: 3 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1005595 & http://1005595.tinybrain.de