make_idna_table.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. static TABLE: &'static [Range] = &[
  21. ''')
  22. txt = open("IdnaMappingTable.txt")
  23. def char(s):
  24. return (unichr(int(s, 16))
  25. .encode('utf8')
  26. .replace('\\', '\\\\')
  27. .replace('"', '\\"')
  28. .replace('\0', '\\0'))
  29. for line in txt:
  30. # remove comments
  31. line, _, _ = line.partition('#')
  32. # skip empty lines
  33. if len(line.strip()) == 0:
  34. continue
  35. fields = line.split(';')
  36. if fields[0].strip() == 'D800..DFFF':
  37. continue # Surrogates don't occur in Rust strings.
  38. first, _, last = fields[0].strip().partition('..')
  39. if not last:
  40. last = first
  41. mapping = fields[1].strip().replace('_', ' ').title().replace(' ', '')
  42. if len(fields) > 2:
  43. if fields[2].strip():
  44. mapping += '("%s")' % ''.join(char(c) for c in fields[2].strip().split(' '))
  45. elif mapping == "Deviation":
  46. mapping += '("")'
  47. print(" Range { from: '%s', to: '%s', mapping: %s }," % (char(first), char(last), mapping))
  48. print("];")