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 YshEncodeString(s):
|
25 | # type: (str) -> str
|
26 |
|
27 | # Possibilities:
|
28 | # - '' then b'' - simplest logic
|
29 | return fastfunc.ShellEncodeString(s, 1) # ysh_fallback
|
30 |
|
31 |
|
32 | def MaybeShellEncode(s):
|
33 | # type: (str) -> str
|
34 | """
|
35 | This is like ShellEncode(s, unquoted_ok=True)
|
36 | But it's common, so we give it a shorter name.
|
37 | """
|
38 | if fastfunc.CanOmitQuotes(s):
|
39 | return s
|
40 |
|
41 | return fastfunc.ShellEncodeString(s, 0) # no ysh_fallback
|
42 |
|
43 |
|
44 | def ShellEncode(s):
|
45 | # type: (str) -> str
|
46 | return fastfunc.ShellEncodeString(s, 0) # no ysh_fallback
|
47 |
|
48 |
|
49 | def YshEncode(s, unquoted_ok=False):
|
50 | # type: (str, bool) -> str
|
51 | if unquoted_ok and fastfunc.CanOmitQuotes(s):
|
52 | return s
|
53 |
|
54 | return fastfunc.ShellEncodeString(s, 1) # ysh_fallback
|