Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local utf8substr = function(str, pos1, pos2)
- if type(str)=="number" then str = tostring(str) end
- if type(str)~="string" then
- error("bad argument #1 to 'utf8substr' (string expected, got "..type(str)..")", 2)
- return str
- end
- if pos1 and type(pos1)~="number" then
- error("bad argument #2 to 'utf8substr' (number expected, got "..type(pos1)..")", 2)
- return str
- end
- if pos2 and type(pos2)~="number" then
- error("bad argument #3 to 'utf8substr' (number expected, got "..type(pos2)..")", 2)
- return str
- end
- pos1 = pos1 or 1
- pos2 = pos2 or -1
- if pos1<0 or pos2<0 then
- local len = utf8strlen(str)
- if pos1<0 then pos1 = len+pos1+1 end
- if pos2<0 then pos2 = len+pos2+1 end
- end
- local fn = rawget(GLOBAL, "utf8substr")
- if fn then pos1 = pos1-1 end --c-like numbering behaviour
- fn = fn or str.sub
- return fn(str, pos1, pos2)
- end
Advertisement
Add Comment
Please, Sign In to add comment