make_idna_table.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. pub enum Mapping {
  21. Valid,
  22. Ignored,
  23. Mapped(&'static str),
  24. Deviation(&'static str),
  25. Disallowed,
  26. DisallowedStd3Valid,
  27. DisallowedStd3Mapped(&'static str),
  28. }
  29. pub struct Range {
  30. pub from: char,
  31. pub to: char,
  32. pub mapping: Mapping,
  33. }
  34. use self::Mapping::*;
  35. pub static TABLE: &'static [Range] = &[
  36. ''')
  37. txt = open("IdnaMappingTable.txt")
  38. def char(s):
  39. return (unichr(int(s, 16))
  40. .encode('utf8')
  41. .replace('\\', '\\\\')
  42. .replace('"', '\\"')
  43. .replace('\0', '\\0'))
  44. for line in txt:
  45. # remove comments
  46. line, _, _ = line.partition('#')
  47. # skip empty lines
  48. if len(line.strip()) == 0:
  49. continue
  50. fields = line.split(';')
  51. if fields[0].strip() == 'D800..DFFF':
  52. continue # Surrogates don't occur in Rust strings.
  53. first, _, last = fields[0].strip().partition('..')
  54. if not last:
  55. last = first
  56. mapping = fields[1].strip().replace('_', ' ').title().replace(' ', '')
  57. if len(fields) > 2:
  58. if fields[2].strip():
  59. mapping += '("%s")' % ''.join(char(c) for c in fields[2].strip().split(' '))
  60. elif mapping == "Deviation":
  61. mapping += '("")'
  62. print(" Range { from: '%s', to: '%s', mapping: %s }," % (char(first), char(last), mapping))
  63. print("];")