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