From f2c762a2d0a03f12d5b95b6177058a9714d51081 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 10 Feb 2019 16:41:56 +0000 Subject: add `utils.truncate_encode`, to encode and truncate a string while respecting utf8 multi-byte encoding --- src/utils/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/utils') diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 188b8e9c..24b3d71d 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -189,3 +189,13 @@ def is_ip(s: str) -> bool: except ValueError: return False return True + +def encode_truncate(s: str, encoding: str, byte_max: int) -> bytes: + encoded = b"" + for character in s: + encoded_character = character.encode(encoding) + if len(encoded + encoded_character) > byte_max: + break + else: + encoded += encoded_character + return encoded -- cgit v1.3.1-10-gc9f91