NDOJ/judge/utils/unicode.py

38 lines
732 B
Python
Raw Normal View History

2022-11-01 01:43:06 +00:00
from typing import AnyStr, Optional, overload
@overload
def utf8bytes(maybe_text: AnyStr) -> bytes:
pass
@overload
def utf8bytes(maybe_text: None) -> None:
pass
2020-01-21 06:35:58 +00:00
def utf8bytes(maybe_text):
if maybe_text is None:
2022-11-01 01:43:06 +00:00
return None
if isinstance(maybe_text, bytes):
2020-01-21 06:35:58 +00:00
return maybe_text
2022-05-14 17:57:27 +00:00
return maybe_text.encode("utf-8")
2020-01-21 06:35:58 +00:00
2022-11-01 01:43:06 +00:00
@overload
def utf8text(maybe_bytes: AnyStr, errors="strict") -> str:
pass
@overload
def utf8text(maybe_bytes: None, errors="strict") -> None:
pass
def utf8text(maybe_bytes, errors="strict") -> Optional[str]:
2020-01-21 06:35:58 +00:00
if maybe_bytes is None:
2022-11-01 01:43:06 +00:00
return None
if isinstance(maybe_bytes, str):
2020-01-21 06:35:58 +00:00
return maybe_bytes
2022-05-14 17:57:27 +00:00
return maybe_bytes.decode("utf-8", errors)