make_idna_table.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. from sys import argv
  41. script, filename = argv
  42. txt = open(filename)
  43. line_no = 0
  44. for line in txt:
  45. # remove comments
  46. head, sep, tail = line.partition('#')
  47. # skip empty lines
  48. if len(head.strip()) == 0:
  49. continue
  50. line_no = line_no + 1
  51. txt = open(filename)
  52. print("pub static TABLE: [Mapping; "+str(line_no)+"] = [")
  53. mappings = []
  54. for line in txt:
  55. # remove comments
  56. head, sep, tail = line.partition('#')
  57. # skip empty lines
  58. if len(head.strip()) == 0:
  59. continue
  60. table_line = head.split(';')
  61. first, sep, last = table_line[0].strip().partition('..')
  62. if len(last)==0:
  63. last = first
  64. mapping = "NONE"
  65. if len(table_line)>2:
  66. if len(table_line[2].strip())>0:
  67. codes = table_line[2].strip().split(' ')
  68. newmap = ""
  69. for code in codes:
  70. newmap = newmap + "0x" + code + ", "
  71. newmap = "[" + newmap + "]"
  72. mapping = "MAPPING_%s_%s" % (first, last)
  73. static_array = "static %s : [u32; %d] = %s;" % (mapping, len(codes), newmap)
  74. mappings.append(static_array)
  75. print " Mapping{ from: 0x%s, to: 0x%s, status: MappingStatus::%s, mapping: &%s }," % (first, last, table_line[1].strip(), mapping)
  76. print("];")
  77. for mapping in mappings:
  78. print mapping