Remove unnecessary endianness stuff

This commit is contained in:
richardpaulhudson 2022-10-14 20:24:32 +02:00
parent 342433f09d
commit fa5724e927
2 changed files with 43 additions and 47 deletions

View File

@ -2,16 +2,6 @@ import sys
import spacy
def _correct_endianness(littleendian: bytes) -> bytes:
if sys.byteorder == "little":
return littleendian
output = bytearray()
for idx in range(0, len(littleendian), 2):
output.append(littleendian[idx + 1])
output.append(littleendian[idx])
return bytes(output)
def test_get_byte_arrays_for_search_chars_width_1_not_case_sensitive():
(
w1_search,
@ -22,9 +12,10 @@ def test_get_byte_arrays_for_search_chars_width_1_not_case_sensitive():
w4_finding,
) = spacy.util.get_byte_arrays_for_search_chars("bfEWfwe", False)
assert w1_search == b"BEFWbefw"
assert w2_search == _correct_endianness(b"B\x00E\x00F\x00W\x00b\x00e\x00f\x00w\x00")
assert w4_search == _correct_endianness(
b"B\x00\x00\x00E\x00\x00\x00F\x00\x00\x00W\x00\x00\x00b\x00\x00\x00e\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
assert w2_search == b"B\x00E\x00F\x00W\x00b\x00e\x00f\x00w\x00"
assert (
w4_search
== b"B\x00\x00\x00E\x00\x00\x00F\x00\x00\x00W\x00\x00\x00b\x00\x00\x00e\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
)
assert w1_finding == w2_finding == w4_finding == w4_search.lower()
@ -54,21 +45,19 @@ def test_get_byte_arrays_for_search_chars_width_2_not_case_sensitive():
w4_finding,
) = spacy.util.get_byte_arrays_for_search_chars("bféwfw", False)
assert w1_search == b"BFWbfw"
assert w1_finding == _correct_endianness(
b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
)
assert w2_search == _correct_endianness(
b"B\x00F\x00W\x00b\x00f\x00w\x00\xc9\x00\xe9\x00"
assert (
w1_finding
== b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
)
assert w2_search == b"B\x00F\x00W\x00b\x00f\x00w\x00\xc9\x00\xe9\x00"
assert (
w2_finding
== w4_finding
== _correct_endianness(
b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00\xe9\x00\x00\x00"
== b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00\xe9\x00\x00\x00"
)
)
assert w4_search == _correct_endianness(
b"B\x00\x00\x00F\x00\x00\x00W\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
assert (
w4_search
== b"B\x00\x00\x00F\x00\x00\x00W\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
)
@ -82,15 +71,13 @@ def test_get_byte_arrays_for_search_chars_width_2_case_sensitive():
w4_finding,
) = spacy.util.get_byte_arrays_for_search_chars("bféwfw", True)
assert w1_search == b"bfw"
assert w1_finding == _correct_endianness(b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00")
assert w2_search == _correct_endianness(b"b\x00f\x00w\x00\xe9\x00")
assert w1_finding == b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
assert w2_search == b"b\x00f\x00w\x00\xe9\x00"
assert (
w2_finding
== w4_finding
== w4_search
== _correct_endianness(
b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00"
)
== b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00"
)
@ -104,20 +91,26 @@ def test_get_byte_arrays_for_search_chars_width_4_not_case_sensitive():
w4_finding,
) = spacy.util.get_byte_arrays_for_search_chars("bfé𐌞wf𐌞wÉ", False)
assert w1_search == b"BFWbfw"
assert w1_finding == _correct_endianness(
b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
assert (
w1_finding
== b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
)
assert w2_search == _correct_endianness(
b"B\x00F\x00W\x00b\x00f\x00w\x00\xc9\x00\xe9\x00"
assert w2_search == b"B\x00F\x00W\x00b\x00f\x00w\x00\xc9\x00\xe9\x00"
assert (
w2_finding
== b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00\xe9\x00\x00\x00"
)
assert w2_finding == _correct_endianness(
b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00\xe9\x00\x00\x00"
assert (
w4_search
== b"\x1e\x03\x01\x00B\x00\x00\x00F\x00\x00\x00W\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
)
assert w4_search == _correct_endianness(
b"\x1e\x03\x01\x00B\x00\x00\x00F\x00\x00\x00W\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
)
assert w4_finding == _correct_endianness(
b"\x1e\x03\x01\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00\xe9\x00\x00\x00"
assert (
w4_finding
== b"\x1e\x03\x01\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xe9\x00\x00\x00\xe9\x00\x00\x00"
)
@ -131,12 +124,15 @@ def test_get_byte_arrays_for_search_chars_width_4_case_sensitive():
w4_finding,
) = spacy.util.get_byte_arrays_for_search_chars("bfé𐌞wf𐌞wÉ", True)
assert w1_search == b"bfw"
assert w1_finding == _correct_endianness(b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00")
assert w2_search == _correct_endianness(b"b\x00f\x00w\x00\xc9\x00\xe9\x00")
assert w2_finding == _correct_endianness(
b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
assert w1_finding == b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00"
assert w2_search == b"b\x00f\x00w\x00\xc9\x00\xe9\x00"
assert (
w2_finding
== b"b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
)
assert w4_search == w4_finding
assert w4_finding == _correct_endianness(
b"\x1e\x03\x01\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
assert (
w4_finding
== b"\x1e\x03\x01\x00b\x00\x00\x00f\x00\x00\x00w\x00\x00\x00\xc9\x00\x00\x00\xe9\x00\x00\x00"
)

View File

@ -1772,9 +1772,9 @@ def get_byte_arrays_for_search_chars(
int: the width of the character encoding to use
"""
if width == 4:
return ch.encode("UTF-32")[width:] # remove byte order mark
return ch.encode("UTF-32LE")
elif width == 2:
return ch.encode("UTF-16")[width:] # remove byte order mark
return ch.encode("UTF-16LE")
else:
return ch.encode("UTF-8")