标签:milang python 域名 互联网 unicode
本库主要提供访问RFC3454定义的UNICODE字符集。当我们需要比较互联网的域名是否相同时就需要比较互联网上主机名称是否相同,更确切地说就是比较应用程序的域名称,比如是否大小写有区分。又或者限制在可打印的字符集合的字符组成域名。
本库封装对RFC 3454的字符表访问,如果全部使用字典或者列表来表示会比较大,目前使用像UNICODE数据库的形式来保存,这样就可以很方便访问,因此需要使用工具mkstringprep.py来生成相应的文件。
本库主要提供一些操作函数,而没有提供结构来访问数据的。所有这些操作的函数如下:
stringprep.in_table_a1(code)
检查code是否在表tableA.1(Unassigned code points in Unicode 3.2).
stringprep.in_table_b1(code)
检查code是否在表tableB.1 (Commonly mapped to nothing).
stringprep.map_table_b2(code)
返回code在表tableB.2的值 (Mapping for case-folding used with NFKC).
stringprep.map_table_b3(code)
返回code在表tableB.3的值 (Mapping for case-folding used with no normalization).
stringprep.in_table_c11(code)
检查code是否在表tableC.1.1 (ASCII space characters).
stringprep.in_table_c12(code)
检查code是否在表tableC.1.2 (Non-ASCII space characters).
stringprep.in_table_c11_c12(code)
检查code是否在表tableC.1 (Space characters, union of C.1.1 and C.1.2).
stringprep.in_table_c21(code)
检查code是否在表tableC.2.1 (ASCII control characters).
stringprep.in_table_c22(code)
检查code是否在表tableC.2.2 (Non-ASCII control characters).
stringprep.in_table_c21_c22(code)
检查code是否在表tableC.2 (Control characters, union of C.2.1 and C.2.2).
stringprep.in_table_c3(code)
检查code是否在表tableC.3 (Private use).
stringprep.in_table_c4(code)
检查code是否在表tableC.4 (Non-character code points).
stringprep.in_table_c5(code)
检查code是否在表tableC.5 (Surrogate codes).
stringprep.in_table_c6(code)
检查code是否在表tableC.6 (Inappropriate for plain text).
stringprep.in_table_c7(code)
检查code是否在表tableC.7 (Inappropriate for canonical representation).
stringprep.in_table_c8(code)
检查code是否在表tableC.8 (Change display properties or are deprecated).
stringprep.in_table_c9(code)
检查code是否在表tableC.9 (Tagging characters).
stringprep.in_table_d1(code)
检查code是否在表tableD.1 (Characters with bidirectional property “R” or “AL”).
stringprep.in_table_d2(code)
检查code是否在表tableD.2 (Characters with bidirectional property “L”).
例子:
def nameprep(label): # Map newlabel = [] for c in label: if stringprep.in_table_b1(c): # Map to nothing continue newlabel.append(stringprep.map_table_b2(c)) label = u"".join(newlabel) # Normalize label = unicodedata.normalize("NFKC", label) # Prohibit for c in label: if stringprep.in_table_c12(c) or stringprep.in_table_c22(c) or stringprep.in_table_c3(c) or stringprep.in_table_c4(c) or stringprep.in_table_c5(c) or stringprep.in_table_c6(c) or stringprep.in_table_c7(c) or stringprep.in_table_c8(c) or stringprep.in_table_c9(c): raise UnicodeError("Invalid character %r" % c) # Check bidi RandAL = map(stringprep.in_table_d1, label) for c in RandAL: if c: # There is a RandAL char in the string. Must perform further # tests: # 1) The characters in section 5.8 MUST be prohibited. # This is table C.8, which was already checked # 2) If a string contains any RandALCat character, the string # MUST NOT contain any LCat character. if filter(stringprep.in_table_d2, label): raise UnicodeError("Violation of BIDI requirement 2") # 3) If a string contains any RandALCat character, a # RandALCat character MUST be the first character of the # string, and a RandALCat character MUST be the last # character of the string. if not RandAL[0] or not RandAL[-1]: raise UnicodeError("Violation of BIDI requirement 3") return label
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:milang python 域名 互联网 unicode
原文地址:http://blog.csdn.net/caimouse/article/details/49401461