Epix-Incorporated / Adonis

Roblox Server Administration System
https://adonis.dev
MIT License
308 stars 178 forks source link

Fix string clamping for unicode characters #1724

Closed ccuser44 closed 4 days ago

ccuser44 commented 6 days ago

PoF: image

PoF script:

MaxLen = function(message, length)
    local rawLength = string.len(message)
    local utf8Length = rawLength <= length * 6 and utf8.len(message)
    if rawLength > length and not (utf8Length and utf8Length <= length) then
        local clamped = string.sub(message, 1, utf8Length and utf8.offset(message, length + 1) - 1 or length)
        return `{not utf8.len(clamped) and string.sub(clamped, 1, select(2, utf8.len(clamped)) - 1) or clamped}...`
    else
        return message
    end
end;

local str = "Hêllö thëre ﷽ I äm â mürder!"

for i = utf8.len(str), 0, -1 do
    print(MaxLen(str, i))
end