Speed up IDNA for the common case
This commit is contained in:
parent
de20b0b50e
commit
9768676f6f
|
@ -153,6 +153,20 @@ class Codec(codecs.Codec):
|
||||||
if not input:
|
if not input:
|
||||||
return b'', 0
|
return b'', 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = input.encode('ascii')
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# ASCII name: fast path
|
||||||
|
labels = result.split(b'.')
|
||||||
|
for label in labels[:-1]:
|
||||||
|
if not (0 < len(label) < 64):
|
||||||
|
raise UnicodeError("label empty or too long")
|
||||||
|
if len(labels[-1]) >= 64:
|
||||||
|
raise UnicodeError("label too long")
|
||||||
|
return result, len(input)
|
||||||
|
|
||||||
result = bytearray()
|
result = bytearray()
|
||||||
labels = dots.split(input)
|
labels = dots.split(input)
|
||||||
if labels and not labels[-1]:
|
if labels and not labels[-1]:
|
||||||
|
@ -179,6 +193,14 @@ class Codec(codecs.Codec):
|
||||||
if not isinstance(input, bytes):
|
if not isinstance(input, bytes):
|
||||||
# XXX obviously wrong, see #3232
|
# XXX obviously wrong, see #3232
|
||||||
input = bytes(input)
|
input = bytes(input)
|
||||||
|
|
||||||
|
if ace_prefix not in input:
|
||||||
|
# Fast path
|
||||||
|
try:
|
||||||
|
return input.decode('ascii'), len(input)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
pass
|
||||||
|
|
||||||
labels = input.split(b".")
|
labels = input.split(b".")
|
||||||
|
|
||||||
if labels and len(labels[-1]) == 0:
|
if labels and len(labels[-1]) == 0:
|
||||||
|
|
Loading…
Reference in New Issue