make_encode_sets.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright 2013-2014 Simon Sapin.
  2. #
  3. # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. # option. This file may not be copied, modified, or distributed
  7. # except according to those terms.
  8. # Run as: python make_encode_sets.py > src/encode_sets.rs
  9. print('''\
  10. // Copyright 2013-2014 Simon Sapin.
  11. //
  12. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  13. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  14. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  15. // option. This file may not be copied, modified, or distributed
  16. // except according to those terms.
  17. // Generated by make_encode_sets.py
  18. ''')
  19. for name, encoded in [
  20. ('SIMPLE', ''),
  21. ('QUERY', r''' "#<>`'''),
  22. ('DEFAULT', r''' "#<>`?'''),
  23. ('USERINFO', r''' "#<>`?@'''),
  24. ('PASSWORD', r''' "#<>`?@\/'''),
  25. ('USERNAME', r''' "#<>`?@\/:'''),
  26. ('FORM_URLENCODED', r''' !"#$%&\'()+,/:;<=>?@[\]^`{|}~'''),
  27. ]:
  28. print(
  29. "pub static %s: [&'static str; 256] = [\n%s\n];\n\n"
  30. % (name, '\n'.join(
  31. ' ' + ' '.join(
  32. '"%s%s",' % ("\\" if chr(b) in '\\"' else "", chr(b))
  33. if 0x20 <= b <= 0x7E and chr(b) not in encoded
  34. else '"%%%02X",' % b
  35. for b in range(s, s + 8)
  36. ) for s in range(0, 256, 8))))