From 3ff4f1e4cd0d9a4ef65476b2b95c686d00aeb257 Mon Sep 17 00:00:00 2001 From: Gaxeer <44334376+Gaxeer@users.noreply.github.com> Date: Wed, 31 Jul 2024 23:26:12 +0300 Subject: [PATCH] fix: add support for multibyte chars for `truncate`, `stripped_input`, `tgui_input_text` (#85313) ## About The Pull Request Add support for multibyte chars for `truncate`, `stripped_input`, `tgui_input_text`. Right now, on UI of `tgui_input_text` chars are counted, but on back-end - bytes. So I made it consistent. Also fixed that for `stripped_input`, which is used in case of disabled `tgui input`. While I was here, made `truncate` proc consistent too ## Why It's Good For The Game Tgui text input won't lie about the size of input ## Changelog N/A --- code/__HELPERS/text.dm | 6 +++--- code/modules/tgui_input/text.dm | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/code/__HELPERS/text.dm b/code/__HELPERS/text.dm index d1d41c8e145c..697570439657 100644 --- a/code/__HELPERS/text.dm +++ b/code/__HELPERS/text.dm @@ -123,7 +123,7 @@ if(isnull(user_input)) // User pressed cancel return if(no_trim) - return copytext(html_encode(user_input), 1, max_length) + return copytext_char(html_encode(user_input), 1, max_length) else return trim(html_encode(user_input), max_length) //trim is "outside" because html_encode can expand single symbols into multiple symbols (such as turning < into <) @@ -142,7 +142,7 @@ if(isnull(user_input)) // User pressed cancel return if(no_trim) - return copytext(html_encode(user_input), 1, max_length) + return copytext_char(html_encode(user_input), 1, max_length) else return trim(html_encode(user_input), max_length) @@ -324,7 +324,7 @@ */ /proc/truncate(text, max_length) if(length(text) > max_length) - return copytext(text, 1, max_length) + return copytext_char(text, 1, max_length) return text //Returns a string with reserved characters and spaces before the first word and after the last word removed. diff --git a/code/modules/tgui_input/text.dm b/code/modules/tgui_input/text.dm index 29c59ba20183..b0474e4b14e9 100644 --- a/code/modules/tgui_input/text.dm +++ b/code/modules/tgui_input/text.dm @@ -132,9 +132,9 @@ switch(action) if("submit") if(max_length) - if(length(params["entry"]) > max_length) + if(length_char(params["entry"]) > max_length) CRASH("[usr] typed a text string longer than the max length") - if(encode && (length(html_encode(params["entry"])) > max_length)) + if(encode && (length_char(html_encode(params["entry"])) > max_length)) to_chat(usr, span_notice("Your message was clipped due to special character usage.")) set_entry(params["entry"]) closed = TRUE