make_idna_table.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. ''')
  21. print('''\
  22. #[allow(non_camel_case_types)]
  23. pub enum MappingStatus {
  24. valid,
  25. ignored,
  26. mapped,
  27. deviation,
  28. disallowed,
  29. disallowed_STD3_valid,
  30. disallowed_STD3_mapped,
  31. }
  32. pub struct Mapping {
  33. pub from: u32,
  34. pub to: u32,
  35. pub status: MappingStatus,
  36. pub mapping: &'static [u32],
  37. }
  38. ''')
  39. print("static NONE: [u32;0] = [];")
  40. txt = open("IdnaMappingTable.txt")
  41. line_no = 0
  42. for line in txt:
  43. # remove comments
  44. head, sep, tail = line.partition('#')
  45. # skip empty lines
  46. if len(head.strip()) == 0:
  47. continue
  48. line_no = line_no + 1
  49. txt = open("IdnaMappingTable.txt")
  50. print("pub static TABLE: [Mapping; "+str(line_no)+"] = [")
  51. mappings = []
  52. for line in txt:
  53. # remove comments
  54. head, sep, tail = line.partition('#')
  55. # skip empty lines
  56. if len(head.strip()) == 0:
  57. continue
  58. table_line = head.split(';')
  59. first, sep, last = table_line[0].strip().partition('..')
  60. if len(last)==0:
  61. last = first
  62. mapping = "NONE"
  63. if len(table_line)>2:
  64. if len(table_line[2].strip())>0:
  65. codes = table_line[2].strip().split(' ')
  66. newmap = ""
  67. for code in codes:
  68. newmap = newmap + "0x" + code + ", "
  69. newmap = "[" + newmap + "]"
  70. mapping = "MAPPING_%s_%s" % (first, last)
  71. static_array = "static %s : [u32; %d] = %s;" % (mapping, len(codes), newmap)
  72. mappings.append(static_array)
  73. print " Mapping{ from: 0x%s, to: 0x%s, status: MappingStatus::%s, mapping: &%s }," % (first, last, table_line[1].strip(), mapping)
  74. print("];")
  75. for mapping in mappings:
  76. print mapping