Created
April 9, 2022 19:29
-
-
Save EngineerSmith/16a2164768915ba9841bc45d56cf2ba5 to your computer and use it in GitHub Desktop.
utf8 substring by index five
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local utf8 = require("utf8") | |
local utf8pos, utf8len = utf8.offset, utf8.len | |
local sub = string.sub | |
local max, min = math.max, math.min | |
local posrelat = function(pos, len) | |
if pos >= 0 then return pos end | |
if -pos > len then return 0 end | |
return pos + len + 1 | |
end | |
utf8.sub = function(str, i, j) | |
local len = utf8len(str) | |
i, j = max(posrelat(i, len), 1), j and min(posrelat(j, len), len) or len | |
if i <= j then | |
return sub(str, utf8pos(str, i), utf8pos(str, j + 1) - 1) | |
end | |
return "" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment