From 6fdd906d2c1d049ef2ba090456b20d217907a3ac Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 27 May 2016 18:49:58 +0300 Subject: [PATCH] Unicode support for length checking in /nick --- plugins/nick.lua | 2 +- utilities.lua | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/plugins/nick.lua b/plugins/nick.lua index 37f3c2f..cc38275 100755 --- a/plugins/nick.lua +++ b/plugins/nick.lua @@ -34,7 +34,7 @@ function nick:action(msg) else output = target.name .. ' currently has no nickname.' end - elseif string.len(input) > 32 then + elseif utilities.utf8_len(input) > 32 then output = 'The character limit for nicknames is 32.' elseif input == '--' or input == utilities.char.em_dash then self.database.users[target.id_str].nickname = nil diff --git a/utilities.lua b/utilities.lua index 9d3fb85..1b13939 100755 --- a/utilities.lua +++ b/utilities.lua @@ -39,6 +39,18 @@ function utilities.input(s) return s:sub(s:find(' ')+1) end +-- Calculates the length of the given string as UTF-8 characters +function utilities.utf8_len(s) + local chars = 0 + for i = 1, string.len(s) do + local b = string.byte(s, i) + if b < 128 or b >= 192 then + chars = chars + 1 + end + end + return chars +end + -- I swear, I copied this from PIL, not yago! :) function utilities.trim(str) -- Trims whitespace from a string. local s = str:gsub('^%s*(.-)%s*$', '%1')