1 | """
|
2 | j8_lite.py: Low dependency library for ASDL circular dep in prebuilt/
|
3 | """
|
4 |
|
5 | import fastfunc # Skip pyj8 and fastlex
|
6 |
|
7 |
|
8 | def EncodeString(s, unquoted_ok=False):
|
9 | # type: (str, bool) -> str
|
10 | """Convenience API that doesn't require instance of j8.Printer()
|
11 |
|
12 | This is the same logic as j8.Printer.EncodeString(), which reuses a
|
13 | BufWriter.
|
14 |
|
15 | If you have to create many strings, it may generate less garbage to use
|
16 | that method, then call BufWriter.clear() in between.
|
17 | """
|
18 | if unquoted_ok and fastfunc.CanOmitQuotes(s):
|
19 | return s
|
20 |
|
21 | return fastfunc.J8EncodeString(s, 1) # j8_fallback is true
|
22 |
|
23 |
|
24 | def EncodeStringYsh(s):
|
25 | # type: (str) -> str
|
26 |
|
27 | # TODO: r'' then b''
|
28 | return EncodeString(s)
|
29 |
|
30 |
|
31 | def MaybeShellEncode(s):
|
32 | # type: (str) -> str
|
33 | """
|
34 | This is like ShellEncode(s, unquoted_ok=True)
|
35 | But it's common, so we give it a shorter name.
|
36 | """
|
37 | if fastfunc.CanOmitQuotes(s):
|
38 | return s
|
39 |
|
40 | return fastfunc.ShellEncodeString(s, 0) # no ysh_fallback
|
41 |
|
42 |
|
43 | def ShellEncode(s):
|
44 | # type: (str) -> str
|
45 | return fastfunc.ShellEncodeString(s, 0) # no ysh_fallback
|
46 |
|
47 |
|
48 | def YshEncode(s, unquoted_ok=False):
|
49 | # type: (str, bool) -> str
|
50 | if unquoted_ok and fastfunc.CanOmitQuotes(s):
|
51 | return s
|
52 |
|
53 | return fastfunc.ShellEncodeString(s, 1) # ysh_fallback
|