|
| 1 | +import regex as re |
| 2 | + |
| 3 | +aliases = { |
| 4 | + "any_character": '.', |
| 5 | + "none": "", |
| 6 | + "space": " ", |
| 7 | + "whitespace": "[\\b \\t\\0\\r\\n]", |
| 8 | + "boundary": "\\b", |
| 9 | + "non_word_boundary": "\\B", |
| 10 | + "word": "\\w", |
| 11 | + "not_word": "\\W", |
| 12 | + "alpha": "[a-zA-Z]", |
| 13 | + "not_alpha": "[^a-zA-Z]", |
| 14 | + "digit": "\\d", |
| 15 | + "not_digit": "\\D", |
| 16 | + "line_start": "^", |
| 17 | + "line_end": "$", |
| 18 | + "string_start": "\\A", |
| 19 | + "string_end": "\\z" |
| 20 | +} |
| 21 | + |
| 22 | +available_functions = [ |
| 23 | + "title_case", |
| 24 | + "downcase", |
| 25 | + "compose", |
| 26 | + "decompose", |
| 27 | + "separate", |
| 28 | +] |
| 29 | + |
| 30 | +maps = {} |
| 31 | + |
| 32 | +def define_map(map): |
| 33 | + maps[map] = { |
| 34 | + "name": map, |
| 35 | + "aliases": {}, |
| 36 | + "aliases_re": {}, |
| 37 | + "cache": {}, |
| 38 | + "stages": {}, |
| 39 | + } |
| 40 | + |
| 41 | +def get_alias(map, alias): |
| 42 | + return maps[map]["aliases"][alias]; |
| 43 | + |
| 44 | +def get_alias_re(map, alias): |
| 45 | + return maps[map]["aliases_re"][alias]; |
| 46 | + |
| 47 | +def add_map_stage(map, stage, fun): |
| 48 | + maps[map]["stages"][stage] = fun |
| 49 | + |
| 50 | +def add_map_alias(map, alias, aliased): |
| 51 | + maps[map]["aliases"][alias] = aliased |
| 52 | + |
| 53 | +def add_map_alias_re(map, alias, aliased): |
| 54 | + maps[map]["aliases_re"][alias] = aliased |
| 55 | + |
| 56 | + |
| 57 | +def parallel_replace_tree(str, tree): |
| 58 | + newstr = "" |
| 59 | + len_str = len(str) |
| 60 | + i = 0 |
| 61 | + while i < len_str: |
| 62 | + c = str[i] |
| 63 | + |
| 64 | + sub = "" |
| 65 | + branch = tree |
| 66 | + match, repl = None, None |
| 67 | + |
| 68 | + j = 0 |
| 69 | + while j < len_str - i: |
| 70 | + cc = str[i + j] |
| 71 | + if ord(cc) in branch: |
| 72 | + branch = branch[ord(cc)] |
| 73 | + sub += cc |
| 74 | + if None in branch: # Check for None to find the terminal node |
| 75 | + match = sub |
| 76 | + repl = branch[None] |
| 77 | + j += 1 |
| 78 | + else: |
| 79 | + break |
| 80 | + |
| 81 | + if match: |
| 82 | + i += len(match) |
| 83 | + newstr += repl |
| 84 | + else: |
| 85 | + newstr += c |
| 86 | + i += 1 |
| 87 | + |
| 88 | + return newstr |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +def parallel_regexp_gsub(s, subs_regexp, subs_hash): |
| 93 | + # Compile the regular expression from the data[0] pattern |
| 94 | + subs_regexp = re.compile(subs_regexp, re.MULTILINE) |
| 95 | + |
| 96 | + # Define the replacement function |
| 97 | + def replacement(match): |
| 98 | + # Iterate through the named groups to find the matched one |
| 99 | + for name, value in match.groupdict().items(): |
| 100 | + if value is not None: |
| 101 | + # Extract the numeric part of the name and convert it to an integer |
| 102 | + idx = int(name[1:]) # Assuming names are like "_1", "_2", etc. |
| 103 | + # Return the corresponding replacement from data[1] |
| 104 | + return subs_hash[idx] |
| 105 | + # If no named group was matched (which shouldn't happen), return the whole match |
| 106 | + return match.group(0) |
| 107 | + |
| 108 | + # Perform the substitution and return the result |
| 109 | + return subs_regexp.sub(replacement, s) |
| 110 | + |
| 111 | +def upper(match): |
| 112 | + return match.group().upper() |
| 113 | + |
| 114 | +def lower(match): |
| 115 | + return match.group().lower() |
0 commit comments