簡體   English   中英

在 unicode 代碼點列表中查找連續范圍

[英]Find continuous range in a list of unicode code points

我有一個 unicode 代碼點列表,大致如下(不是實際集合,僅問題說明):

uni050B
uni050C
uni050D
uni050E
uni050F
uni0510
uni0511
uni0512
uni0513
uni1E00
uni1E01
uni1E3E
uni1E3F
uni1E80
uni1E81
uni1E82
uni1E83
uni1E84
uni1E85
uni1EA0
and so forth…

我需要找到這些的unicode-range 該集合的某些部分是連續的,缺少一些點-因此范圍不是U+050B-1EA0 有沒有一種合理的方法來提取那些連續的“子范圍”?

我不知道任何“現成的”但很容易計算的東西。 下面查找連續數字並使用 Python 構建一個unicode-range

import re

def build_range(uni):
    '''Pass a list of sorted positive integers to include in the unicode-range.
    '''
    uni.append(-1) # sentinel prevents having to special case the last element
    start,uni = uni[0],uni[1:]
    current = start

    strings = []
    for u in uni:
        if u == current: # in case of duplicates
            continue
        if u == current + 1: # in a consecutive range...
            current = u
        elif start == current: # single element
            strings.append(f'U+{current:X}')
            start = current = u
        else: # range
            strings.append(f'U+{start:X}-{current:X}')
            start = current = u
        
    return 'unicode-range: ' + ', '.join(strings) + ';'

data = '''\
uni050B
uni050C
uni050D
uni050E
uni050F
uni0510
uni0511
uni0512
uni0513
uni1E00
uni1E01
uni1E3E
uni1E3F
uni1E80
uni1E81
uni1E82
uni1E83
uni1E84
uni1E85
uni1EA0'''

# parse out the hexadecimal values into an integer list
uni = sorted([int(x,16) for x in re.findall(r'uni([0-9A-F]{4})',data)])

print(build_range(uni))

Output:

unicode-range: U+50B-513, U+1E00-1E01, U+1E3E-1E3F, U+1E80-1E85, U+1EA0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM