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