# Copyright 2013-2014 Valentin Gosu. # # Licensed under the Apache License, Version 2.0 or the MIT license # , at your # option. This file may not be copied, modified, or distributed # except according to those terms. # Run as: python make_idna_table.py idna_table.txt > src/idna_table.rs # You can get the latest idna table from # http://www.unicode.org/Public/idna/latest/IdnaMappingTable.txt print('''\ // Copyright 2013-2014 Valentin Gosu. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. // Generated by make_idna_table.py ''') print('''\ #[allow(non_camel_case_types)] pub enum MappingStatus { valid, ignored, mapped, deviation, disallowed, disallowed_STD3_valid, disallowed_STD3_mapped, } pub struct Mapping { pub from: char, pub to: char, pub status: MappingStatus, pub mapping: &'static [char], } ''') print("static NONE: [char;0] = [];") txt = open("IdnaMappingTable.txt") line_no = 0 for line in txt: # remove comments head, sep, tail = line.partition('#') # skip empty lines if len(head.strip()) == 0: continue line_no = line_no + 1 txt = open("IdnaMappingTable.txt") print("pub static TABLE: &'static [Mapping] = &[") def char(s): return "'%s'" % unichr(int(s, 16)).replace('\\', '\\\\').replace('\'', '\\\'').encode('utf8') mappings = [] for line in txt: # remove comments head, sep, tail = line.partition('#') # skip empty lines if len(head.strip()) == 0: continue table_line = head.split(';') if table_line[0].strip() == 'D800..DFFF': continue # Surrogates don't occur in Rust strings. first, sep, last = table_line[0].strip().partition('..') if len(last)==0: last = first mapping = "NONE" if len(table_line)>2: if len(table_line[2].strip())>0: codes = table_line[2].strip().split(' ') newmap = "" for code in codes: newmap = newmap + char(code) + ", " newmap = "[" + newmap + "]" mapping = "MAPPING_%s_%s" % (first, last) static_array = "static %s : [char; %d] = %s;" % (mapping, len(codes), newmap) mappings.append(static_array) print " Mapping{ from: %s, to: %s, status: MappingStatus::%s, mapping: &%s }," % (char(first), char(last), table_line[1].strip(), mapping) print("];") for mapping in mappings: print mapping