3.6 stringprep--互联网域名的字符标准库

本库主要提供访问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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值