v.0.6.1 URLEncoder.encode_utf8

This commit is contained in:
tiijay
2025-11-13 13:59:31 +01:00
parent 794e296614
commit e21908cd44
3 changed files with 30 additions and 9 deletions

View File

@@ -12,8 +12,29 @@ class URLEncoder:
else:
# result.append('%' + format(ord(char), '02X'))
# Converted to % formatting:
result.append("%%%02X" % ord(char))
special_char = "%%%02X" % ord(char)
print( f"{char} --> {special_char}")
result.append(special_char)
return ''.join(result)
@staticmethod
def encode_utf8(string):
"""URL encode a string with proper UTF-8 handling"""
result = []
safe_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
for char in string:
if char in safe_chars:
result.append(char)
else:
# UTF-8 encoding for proper URL encoding
utf8_bytes = char.encode('utf-8')
for byte in utf8_bytes:
result.append(f"%{byte:02X}")
return ''.join(result)
@staticmethod