Skip to content

Commit

Permalink
0-validate_utf8.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambesawi committed Oct 26, 2023
1 parent 22738f3 commit 17fd42f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 0x04-utf8_validation/0-main.py
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))
33 changes: 33 additions & 0 deletions 0x04-utf8_validation/0-validate_utf8.py
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.

0 comments on commit 17fd42f

Please sign in to comment.