make_idna_table.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright 2013-2014 Valentin Gosu.
  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_idna_table.py idna_table.txt > src/idna_table.rs
  9. # You can get the latest idna table from
  10. # http://www.unicode.org/Public/idna/latest/IdnaMappingTable.txt
  11. print('''\
  12. // Copyright 2013-2014 Valentin Gosu.
  13. //
  14. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  15. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  16. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  17. // option. This file may not be copied, modified, or distributed
  18. // except according to those terms.
  19. // Generated by make_idna_table.py
  20. use idna::Mapping::*;
  21. use idna::Range;
  22. pub static TABLE: &'static [Range] = &[
  23. ''')
  24. txt = open("IdnaMappingTable.txt")
  25. def char(s):
  26. return (unichr(int(s, 16))
  27. .encode('utf8')
  28. .replace('\\', '\\\\')
  29. .replace('"', '\\"')
  30. .replace('\0', '\\0'))
  31. for line in txt:
  32. # remove comments
  33. line, _, _ = line.partition('#')
  34. # skip empty lines
  35. if len(line.strip()) == 0:
  36. continue
  37. fields = line.split(';')
  38. if fields[0].strip() == 'D800..DFFF':
  39. continue # Surrogates don't occur in Rust strings.
  40. first, _, last = fields[0].strip().partition('..')
  41. if not last:
  42. last = first
  43. mapping = fields[1].strip().replace('_', ' ').title().replace(' ', '')
  44. if len(fields) > 2:
  45. if fields[2].strip():
  46. mapping += '("%s")' % ''.join(char(c) for c in fields[2].strip().split(' '))
  47. elif mapping == "Deviation":
  48. mapping += '("")'
  49. print(" Range { from: '%s', to: '%s', mapping: %s }," % (char(first), char(last), mapping))
  50. print("];")