From 17fd42f3dcb34180bdf677e5f1c75629424fd64f Mon Sep 17 00:00:00 2001 From: Ambesawi Date: Thu, 26 Oct 2023 22:46:40 +0000 Subject: [PATCH] 0-validate_utf8.py --- 0x04-utf8_validation/0-main.py | 15 ++++++++ 0x04-utf8_validation/0-validate_utf8.py | 33 ++++++++++++++++++ .../0-validate_utf8.cpython-36.pyc | Bin 0 -> 812 bytes 3 files changed, 48 insertions(+) create mode 100755 0x04-utf8_validation/0-main.py create mode 100644 0x04-utf8_validation/0-validate_utf8.py create mode 100644 0x04-utf8_validation/__pycache__/0-validate_utf8.cpython-36.pyc diff --git a/0x04-utf8_validation/0-main.py b/0x04-utf8_validation/0-main.py new file mode 100755 index 0000000..c0d4219 --- /dev/null +++ b/0x04-utf8_validation/0-main.py @@ -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)) diff --git a/0x04-utf8_validation/0-validate_utf8.py b/0x04-utf8_validation/0-validate_utf8.py new file mode 100644 index 0000000..deb3fae --- /dev/null +++ b/0x04-utf8_validation/0-validate_utf8.py @@ -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 diff --git a/0x04-utf8_validation/__pycache__/0-validate_utf8.cpython-36.pyc b/0x04-utf8_validation/__pycache__/0-validate_utf8.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4638571781ea49e6db1f7a24d55b92178e6ca8e7 GIT binary patch literal 812 zcmZ8gO^?$s5VaGhAE7I)df>#tLLjw=(jt(k3R;SG)dL5F*yTe(t6*XmqGs*Zc1nv9 zi311z3V*3rPL=otoEWD?RO@J-Co|*cH+J&i=qMz=M`G0I!#r zH%gC_(qGvEouSLDB)nM$h0n>%(;rs1il884q)#A%AEDAO0*;@eGFaSsi}qE|7j2iF zc_xB6m=V3?wR0qI7No#qq~;-JVPfx^ySJSD7d-=v@6{blN%-mYF?^l7cEtuM{jhP<6?_|hM&1`m6=oetwlT=hBi2fh7^h+XR5 ze7$K_5xB6SQ)+GlpeDiY3BekG4cr3r@G-7=ZHTq7z5!J)y56Q9$6O_GTm}A1+Ib-q TZ5mIm6wfnp;yO3(XCVGxJ~_;p literal 0 HcmV?d00001