diff options
| author | 2019-02-10 16:41:56 +0000 | |
|---|---|---|
| committer | 2019-02-10 16:41:56 +0000 | |
| commit | f2c762a2d0a03f12d5b95b6177058a9714d51081 (patch) | |
| tree | 77d4de3153935308039e1cf14e14556a4d563f1e /src/utils | |
| parent | 'jesopo pushed commit to master' -> 'jesopo pushed to master' (github) (diff) | |
| signature | ||
add `utils.truncate_encode`, to encode and truncate a string while respecting utf8 multi-byte encoding
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/__init__.py | 10 |
1 files changed, 10 insertions, 0 deletions
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 |
