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

32
LINES

< > BotCompany Repo | #1005595 // quote - Java-compatible string quoting with "". now also escapes \t

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

Libraryless. Click here for Pure Java version (2489L/16K).

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 == '\t')
25  
      out.append("\\t");
26  
    else if (c == '\0')
27  
      out.append("\\0");
28  
    else
29  
      out.append(c);
30  
  }
31  
  out.append('"');
32  
}

Author comment

Began life as a copy of #1001239

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1005595
Snippet name: quote - Java-compatible string quoting with "". now also escapes \t
Eternal ID of this version: #1005595/6
Text MD5: e251fab9c3975a14d85fa27c36dd7152
Transpilation MD5: 62fa5a2ca6587d0f03346339b3fa5f94
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-09 15:44:29
Source code size: 736 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 839 / 1426
Version history: 5 change(s)
Referenced in: [show references]