| 1 | #!/usr/bin/env python2
 | 
| 2 | """
 | 
| 3 | escape.py: Test string and list operations.
 | 
| 4 | """
 | 
| 5 | from __future__ import print_function
 | 
| 6 | 
 | 
| 7 | import os
 | 
| 8 | from mycpp import mylib
 | 
| 9 | from mycpp.mylib import log
 | 
| 10 | 
 | 
| 11 | from typing import List
 | 
| 12 | 
 | 
| 13 | 
 | 
| 14 | def BackslashEscape(s, meta_chars):
 | 
| 15 |     # type: (str, str) -> str
 | 
| 16 |     """Escaped certain characters with backslashes.
 | 
| 17 | 
 | 
| 18 |   Used for shell syntax (i.e. quoting completed filenames), globs, and EREs.
 | 
| 19 |   """
 | 
| 20 |     escaped = []  # type: List[str]
 | 
| 21 |     for c in s:
 | 
| 22 |         if c in meta_chars:
 | 
| 23 |             escaped.append('\\')
 | 
| 24 |         escaped.append(c)
 | 
| 25 |     return ''.join(escaped)
 | 
| 26 | 
 | 
| 27 | 
 | 
| 28 | GLOB_META_CHARS = r'\*?[]-:!'
 | 
| 29 | 
 | 
| 30 | 
 | 
| 31 | def TestNotIn():
 | 
| 32 |     # type: () -> None
 | 
| 33 |     if '.' not in GLOB_META_CHARS:
 | 
| 34 |         print('NOT IN .')
 | 
| 35 |     if '*' not in GLOB_META_CHARS:
 | 
| 36 |         print('NOT IN *')
 | 
| 37 | 
 | 
| 38 | 
 | 
| 39 | def run_tests():
 | 
| 40 |     # type: () -> None
 | 
| 41 | 
 | 
| 42 |     log("result: %s", BackslashEscape('echo *.[ch] *.?', GLOB_META_CHARS))
 | 
| 43 | 
 | 
| 44 |     # 200K iterations takes ~433 ms in Python, and ~311 ms in C with -O2 (~600 ms
 | 
| 45 |     # with -O0)  The algorithm is very inefficient.  There are many tiny objects
 | 
| 46 |     # being allocated.
 | 
| 47 | 
 | 
| 48 |     TestNotIn()
 | 
| 49 | 
 | 
| 50 | 
 | 
| 51 | def run_benchmarks():
 | 
| 52 |     # type: () -> None
 | 
| 53 | 
 | 
| 54 |     i = 0
 | 
| 55 |     n = 200000
 | 
| 56 |     while i < n:
 | 
| 57 |         s = 'echo *.[ch] *.?'
 | 
| 58 |         #s = '*'
 | 
| 59 |         BackslashEscape(s, GLOB_META_CHARS)
 | 
| 60 |         i = i + 1
 | 
| 61 | 
 | 
| 62 |         mylib.MaybeCollect()
 | 
| 63 | 
 | 
| 64 | 
 | 
| 65 | if __name__ == '__main__':
 | 
| 66 |     if os.getenv('BENCHMARK'):
 | 
| 67 |         log('Benchmarking...')
 | 
| 68 |         run_benchmarks()
 | 
| 69 |     else:
 | 
| 70 |         run_tests()
 |