aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorGravatar jesopo2019-02-10 18:16:19 +0000
committerGravatar jesopo2019-02-10 18:16:19 +0000
commitbd9eaad0b2d05786dbe5b6c5d66bd199c9c712ea (patch)
tree01060ddd06525fa22d3599d20084bfaa2affb926 /src/utils
parentMake use of utils.truncate_encode, to avoid cutting utf8 sequences in half (diff)
signature
Return truncted data from encode_truncate and hold it in IRCLine.Line
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/__init__.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/utils/__init__.py b/src/utils/__init__.py
index 24b3d71d..738541d5 100644
--- a/src/utils/__init__.py
+++ b/src/utils/__init__.py
@@ -190,12 +190,15 @@ def is_ip(s: str) -> bool:
return False
return True
-def encode_truncate(s: str, encoding: str, byte_max: int) -> bytes:
+def encode_truncate(s: str, encoding: str, byte_max: int
+ ) -> typing.Tuple[bytes, str]:
encoded = b""
- for character in s:
+ truncated = ""
+ for i, character in enumerate(s):
encoded_character = character.encode(encoding)
if len(encoded + encoded_character) > byte_max:
+ truncated = s[i:]
break
else:
encoded += encoded_character
- return encoded
+ return encoded, truncated