3.2 Insertion sort coding
(base, extended, errors)
| 156 | |
| 157 | |
| 158 | def insertion_sort(base, extended, errors): |
| 159 | """3.2 Insertion sort coding""" |
| 160 | # This function raises UnicodeDecodeError with position in the extended. |
| 161 | # Caller should add the offset. |
| 162 | char = 0x80 |
| 163 | pos = -1 |
| 164 | bias = 72 |
| 165 | extpos = 0 |
| 166 | |
| 167 | while extpos < len(extended): |
| 168 | newpos, delta = decode_generalized_number(extended, extpos, |
| 169 | bias, errors) |
| 170 | if delta is None: |
| 171 | # There was an error in decoding. We can't continue because |
| 172 | # synchronization is lost. |
| 173 | return base |
| 174 | pos += delta+1 |
| 175 | char += pos // (len(base) + 1) |
| 176 | if char > 0x10FFFF: |
| 177 | if errors == "strict": |
| 178 | raise UnicodeDecodeError( |
| 179 | "punycode", extended, pos-1, pos, |
| 180 | f"Invalid character U+{char:x}") |
| 181 | char = ord('?') |
| 182 | pos = pos % (len(base) + 1) |
| 183 | base = base[:pos] + chr(char) + base[pos:] |
| 184 | bias = adapt(delta, (extpos == 0), len(base)) |
| 185 | extpos = newpos |
| 186 | return base |
| 187 | |
| 188 | def punycode_decode(text, errors): |
| 189 | if isinstance(text, str): |
no test coverage detected
searching dependent graphs…