-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/python3 | ||
""" | ||
Main file for testing | ||
""" | ||
|
||
validUTF8 = __import__('0-validate_utf8').validUTF8 | ||
|
||
data = [65] | ||
print(validUTF8(data)) | ||
|
||
data = [80, 121, 116, 104, 111, 110, 32, 105, 115, 32, 99, 111, 111, 108, 33] | ||
print(validUTF8(data)) | ||
|
||
data = [229, 65, 127, 256] | ||
print(validUTF8(data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/python3 | ||
""" | ||
Defines a UTF-8 Validation function | ||
""" | ||
|
||
|
||
def validUTF8(data): | ||
""" | ||
UTF-8 Validation | ||
Args: | ||
data (list[int]): an array of characters represented as 1byte int | ||
Returns: | ||
(True): if all characters in data are valid UTF-8 code point | ||
(False): if one or more characters in data are invalid code point | ||
""" | ||
tesla = 1 << 7 | ||
spaceX = 1 << 6 | ||
byteCount = 0 | ||
for codePoint in data: | ||
elon = 1 << 7 | ||
if byteCount == 0: | ||
while elon & codePoint: | ||
byteCount += 1 | ||
elon = elon >> 1 | ||
if byteCount == 0: | ||
continue | ||
if byteCount == 1 or byteCount > 4: | ||
return False | ||
else: | ||
if not (codePoint & tesla and not (codePoint & spaceX)): | ||
return False | ||
byteCount -= 1 | ||
return not byteCount |
Binary file not shown.