Compare commits

...

4 Commits

Author SHA1 Message Date
2ed51d670c EmmyLua -> LuaLS 2026-06-17 21:53:43 +03:00
00382fbe88 Fix lower function 2026-05-14 22:31:43 +03:00
08edd23035 Fix warning EmmyLua. Close #1 2026-01-17 22:24:37 +03:00
4478c13bec Rename api -> system 2026-01-14 15:27:52 +03:00
6 changed files with 87 additions and 95 deletions

View File

@@ -1,27 +0,0 @@
{
"completion": {
"enable": true,
"autoRequire": false,
"callSnippet": false,
"postfix": "@"
},
"diagnostics": {
"globals": [
"core",
"table",
"Ensure",
"string",
"string_utf8"
],
"disable": [
"param-type-not-match"
]
},
"runtime": {
"version": "Lua5.1",
"requireLikeFunction": [],
"frameworkVersions": [],
"extensions": [],
"requirePattern": []
}
}

View File

@@ -1,9 +1,10 @@
BSD 3-Clause License BSD 3-Clause License
Copyright (c) 2006-2007, Kyle Smith Copyright (c) 2006-2007, Kyle Smith
Copyright (c) 2025-2026, Van (varttist), Apancore.Studio
Contributors: Contributors:
Alimov Stepan, Van (Apancore.Studio) Alimov Stepan, Van (varttist)
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:

View File

@@ -1,3 +1,5 @@
[![Maintained By: Apancore.Studio](https://img.shields.io/badge/Maintained%20By-Apancore.Studio-543c93?style=for-the-badge)](https://github.com/lord-server/lord?tab=readme-ov-file#contributors--%D1%83%D1%87%D0%B0%D1%81%D1%82%D0%BD%D0%B8%D0%BA%D0%B8)
# UTF8 API # UTF8 API
Mod for luanti to support Unicode strings. Mod for luanti to support Unicode strings.

140
init.lua
View File

@@ -18,26 +18,28 @@ end
local utf8charpattern = '[%z\1-\127\194-\244][\128-\191]*' local utf8charpattern = '[%z\1-\127\194-\244][\128-\191]*'
--- Determines the length of a character in bytes encoded in UTF-8, based on the first byte of the character. ---Determines the length of a character in bytes encoded in UTF-8, based on the first byte of the character.
--- @param byte number First byte of a character. ---@param byte number First byte of a character.
--- @return number ---@return integer
local function utf8symbollen(byte) local function utf8symbollen(byte)
ensureArgType(byte, "number", 1, "utf8symbollen") ensureArgType(byte, "number", 1, "utf8symbollen")
return not byte and 0 or (byte < 0x80 and 1) or (byte >= 0xF0 and 4) or (byte >= 0xE0 and 3) or (byte >= 0xC0 and 2) or 1 return not byte and 0 or (byte < 0x80 and 1) or (byte >= 0xF0 and 4) or (byte >= 0xE0 and 3) or (byte >= 0xC0 and 2) or 1
end end
---@type table<integer, integer>
local head_table = {} local head_table = {}
for i = 0, 255 do for i = 0, 255 do
head_table[i] = utf8symbollen(i) head_table[i] = utf8symbollen(i)
end end
head_table[256] = 0 head_table[256] = 0
--- Returns the length of the character in bytes starting at position `pos` in the string `str` using the table `head_table`. ---Returns the length of the character in bytes starting at position `pos` in the string `str` using the table `head_table`.
--- @param str string Input string. ---@param str string Input string.
--- @param bs number Position of character in line. ---@param bs integer Position of character in line.
--- @return number ---@return integer
local function utf8charbytes(str, bs) local function utf8charbytes(str, bs)
ensureArgType(str, "string", 1, "utf8charbytes") ensureArgType(str, "string", 1, "utf8charbytes")
ensureArgType(bs, "number", 2, "utf8charbytes") ensureArgType(bs, "number", 2, "utf8charbytes")
@@ -47,10 +49,10 @@ local function utf8charbytes(str, bs)
return head_table[byte(str, bs) or 256] return head_table[byte(str, bs) or 256]
end end
--- Returns the new position in the string `str`, skipping the current character. ---Returns the new position in the string `str`, skipping the current character.
--- @param str string Input string. ---@param str string Input string.
--- @param bs number Position of character in line. ---@param bs integer Position of character in line.
--- @return number ---@return integer
local function utf8next(str, bs) local function utf8next(str, bs)
ensureArgType(str, "string", 1, "utf8next") ensureArgType(str, "string", 1, "utf8next")
ensureArgType(bs, "number", 2, "utf8next") ensureArgType(bs, "number", 2, "utf8next")
@@ -60,9 +62,9 @@ local function utf8next(str, bs)
return bs + utf8charbytes(str, bs) return bs + utf8charbytes(str, bs)
end end
--- Returns the number of characters in a UTF-8 string ---Returns the number of characters in a UTF-8 string
--- @param str string Input string. ---@param str string Input string.
--- @return number ---@return integer
function string_utf8.len(str) function string_utf8.len(str)
ensureArgType(str, "string", 1, "string_utf8.len") ensureArgType(str, "string", 1, "string_utf8.len")
@@ -81,12 +83,12 @@ function string_utf8.len(str)
end end
--- Functions identically to `string.sub` except that `i` and `j` are UTF-8 characters instead of bytes. ---Functions identically to `string.sub` except that `i` and `j` are UTF-8 characters instead of bytes.
--- @param str string Input string. ---@param str string Input string.
--- @param i number The starting character index (default: 1). Negative values count from the end. ---@param i integer The starting character index (default: 1). Negative values count from the end.
--- @param j number The ending character index (default: i). Negative values count from the end. ---@param j integer The ending character index (default: i). Negative values count from the end.
--- @overload fun(s: string, i: number) ---@overload fun(s: string, i: integer)
--- @return string ---@return string
function string_utf8.sub(str, i, j) function string_utf8.sub(str, i, j)
j = j or -1 j = j or -1
@@ -129,15 +131,14 @@ function string_utf8.sub(str, i, j)
return sub(str, start, finish) return sub(str, start, finish)
end end
--- Returns a UTF-8 string from the given Unicode codes. ---Returns a UTF-8 string from the given Unicode codes.
--- @param ... number List of Unicode codes. ---@param ... integer List of Unicode codes.
--- @return string ---@return string
function string_utf8.char(...) function string_utf8.char(...)
local codes = {...} local codes = {...}
assert(codes ~= {}, 'bad argument #1 to \'string_utf8.char\' (key cannot be nil)') assert(codes ~= {}, 'bad argument #1 to \'string_utf8.char\' (key cannot be nil)')
--- @type int[]
local result = {} local result = {}
for _, unicode in ipairs(codes) do for _, unicode in ipairs(codes) do
@@ -183,11 +184,11 @@ local shift_6 = 16 -- 2^6
local shift_12 = 4096 -- 2^12 local shift_12 = 4096 -- 2^12
local shift_18 = 262144 -- 2^18 local shift_18 = 262144 -- 2^18
--- Converts UTF-8 encoded characters in a string to their corresponding Unicode code points. ---Converts UTF-8 encoded characters in a string to their corresponding Unicode code points.
--- @param str string The input string containing UTF-8 encoded characters. ---@param str string The input string containing UTF-8 encoded characters.
--- @param ibs number The starting byte position in the string (inclusive). ---@param ibs integer The starting byte position in the string (inclusive).
--- @param jbs number The ending byte position in the string (inclusive). ---@param jbs integer The ending byte position in the string (inclusive).
--- @return number | number... ---@return integer?, integer?
function string_utf8.unicode(str, ibs, jbs) function string_utf8.unicode(str, ibs, jbs)
ensureArgType(str, "string", 1, "string_utf8.unicode") ensureArgType(str, "string", 1, "string_utf8.unicode")
ensureArgType(ibs, "number", 2, "string_utf8.unicode") ensureArgType(ibs, "number", 2, "string_utf8.unicode")
@@ -201,12 +202,15 @@ function string_utf8.unicode(str, ibs, jbs)
bytes = utf8charbytes(str, ibs) bytes = utf8charbytes(str, ibs)
if bytes == 0 then return end if bytes == 0 then return end
---@type integer
local unicode local unicode
if bytes == 1 then unicode = byte(str, ibs, ibs) end if bytes == 1 then unicode = byte(str, ibs, ibs) end
if bytes == 2 then if bytes == 2 then
---@type integer, integer
local byte0,byte1 = byte(str, ibs, ibs + 1) local byte0,byte1 = byte(str, ibs, ibs + 1)
if byte0 and byte1 then if byte0 and byte1 then
---@type integer, integer
local code0,code1 = byte0-0xC0,byte1-0x80 local code0,code1 = byte0-0xC0,byte1-0x80
unicode = code0*shift_6 + code1 unicode = code0*shift_6 + code1
else else
@@ -214,8 +218,10 @@ function string_utf8.unicode(str, ibs, jbs)
end end
end end
if bytes == 3 then if bytes == 3 then
---@type integer, integer, integer
local byte0,byte1,byte2 = byte(str, ibs, ibs + 2) local byte0,byte1,byte2 = byte(str, ibs, ibs + 2)
if byte0 and byte1 and byte2 then if byte0 and byte1 and byte2 then
---@type integer, integer, integer
local code0,code1,code2 = byte0-0xE0,byte1-0x80,byte2-0x80 local code0,code1,code2 = byte0-0xE0,byte1-0x80,byte2-0x80
unicode = code0*shift_12 + code1*shift_6 + code2 unicode = code0*shift_12 + code1*shift_6 + code2
else else
@@ -223,8 +229,10 @@ function string_utf8.unicode(str, ibs, jbs)
end end
end end
if bytes == 4 then if bytes == 4 then
---@type integer, integer, integer, integer
local byte0,byte1,byte2,byte3 = byte(str, ibs, ibs + 3) local byte0,byte1,byte2,byte3 = byte(str, ibs, ibs + 3)
if byte0 and byte1 and byte2 and byte3 then if byte0 and byte1 and byte2 and byte3 then
---@type integer, integer, integer, integer
local code0,code1,code2,code3 = byte0-0xF0,byte1-0x80,byte2-0x80,byte3-0x80 local code0,code1,code2,code3 = byte0-0xF0,byte1-0x80,byte2-0x80,byte3-0x80
unicode = code0*shift_18 + code1*shift_12 + code2*shift_6 + code3 unicode = code0*shift_18 + code1*shift_12 + code2*shift_6 + code3
else else
@@ -239,13 +247,13 @@ function string_utf8.unicode(str, ibs, jbs)
end end
end end
--- Returns Unicode code points for characters in a UTF-8 string within the specified range. ---Returns Unicode code points for characters in a UTF-8 string within the specified range.
--- @param str string The input UTF-8 encoded string. ---@param str string The input UTF-8 encoded string.
--- @param i number The starting character index (default: 1). Negative values count from the end. ---@param i integer The starting character index (default: 1). Negative values count from the end.
--- @param j number The ending character index (default: i). Negative values count from the end. ---@param j integer The ending character index (default: i). Negative values count from the end.
--- @overload fun(str: string) ---@overload fun(str: string)
--- @overload fun(str: string, i: number) ---@overload fun(str: string, i: number)
--- @return number | number... ---@return integer?, integer?
function string_utf8.byte(str, i, j) function string_utf8.byte(str, i, j)
ensureArgType(str, "string", 1, "string_utf8.byte") ensureArgType(str, "string", 1, "string_utf8.byte")
ensureArgType(i, "number", 2, "string_utf8.byte") ensureArgType(i, "number", 2, "string_utf8.byte")
@@ -279,7 +287,7 @@ function string_utf8.byte(str, i, j)
end end
if not ibs or not jbs then if not ibs or not jbs then
return nil return
end end
else else
ibs, jbs = 1, 1 ibs, jbs = 1, 1
@@ -288,10 +296,10 @@ function string_utf8.byte(str, i, j)
return string_utf8.unicode(str, ibs, jbs) return string_utf8.unicode(str, ibs, jbs)
end end
--- Creates an iterator to generate UTF-8 substrings of a specified length. ---Creates an iterator to generate UTF-8 substrings of a specified length.
--- @param str string The input UTF-8 encoded string. ---@param str string The input UTF-8 encoded string.
--- @param sub_len? number The length of each substring in characters (default: 1). ---@param sub_len integer? The length of each substring in characters (default: 1).
--- @return fun(skip_ptr: table | nil, bs: number | nil) ---@return fun(skip_ptr: table | nil, bs: integer | nil):(integer?, string?, integer?)
function string_utf8.gensub(str, sub_len) function string_utf8.gensub(str, sub_len)
sub_len = sub_len or 1 sub_len = sub_len or 1
@@ -302,6 +310,9 @@ function string_utf8.gensub(str, sub_len)
local max_len = #str local max_len = #str
---@param skip_ptr table
---@param bs integer
---@return integer?, string?, integer?
return function(skip_ptr, bs) return function(skip_ptr, bs)
bs = (bs and bs or 1) + (skip_ptr and (skip_ptr[1] or 0) or 0) bs = (bs and bs or 1) + (skip_ptr and (skip_ptr[1] or 0) or 0)
@@ -315,9 +326,9 @@ function string_utf8.gensub(str, sub_len)
end end
end end
--- Reverses a UTF-8 encoded string while preserving multi-byte characters. ---Reverses a UTF-8 encoded string while preserving multi-byte characters.
--- @param str string The input UTF-8 encoded string. ---@param str string The input UTF-8 encoded string.
--- @return string ---@return string
function string_utf8.reverse(str) function string_utf8.reverse(str)
ensureArgType(str, "string", 1, "string_utf8.reverse") ensureArgType(str, "string", 1, "string_utf8.reverse")
@@ -330,11 +341,11 @@ function string_utf8.reverse(str)
return result return result
end end
--- Validates UTF-8 encoded characters in a string starting from a given byte position. ---Validates UTF-8 encoded characters in a string starting from a given byte position.
--- @param str string The input UTF-8 encoded string to validate. ---@param str string The input UTF-8 encoded string to validate.
--- @param bs number The starting byte position for validation (default: 1). ---@param bs integer The starting byte position for validation (default: 1).
--- @overload fun(str: string) ---@overload fun(str: string)
--- @return number? ---@return integer?, integer?, integer?, integer?
local function utf8validator(str, bs) local function utf8validator(str, bs)
bs = bs or 1 bs = bs or 1
@@ -354,6 +365,7 @@ local function utf8validator(str, bs)
return bs + 1 return bs + 1
elseif c >= 128 and c <= 193 then elseif c >= 128 and c <= 193 then
return bs + 1, bs, 1, c return bs + 1, bs, 1, c
-- UTF8-2 -- UTF8-2
elseif c >= 194 and c <= 223 then elseif c >= 194 and c <= 223 then
local c2 = byte(str, bs + 1) local c2 = byte(str, bs + 1)
@@ -362,8 +374,10 @@ local function utf8validator(str, bs)
end end
return bs + 2 return bs + 2
-- UTF8-3 -- UTF8-3
elseif c >= 224 and c <= 239 then elseif c >= 224 and c <= 239 then
---@type integer?
local c2 = byte(str, bs + 1) local c2 = byte(str, bs + 1)
if not c2 then if not c2 then
@@ -387,6 +401,7 @@ local function utf8validator(str, bs)
return bs + 3 return bs + 3
-- UTF8-4 -- UTF8-4
elseif c >= 240 and c <= 244 then elseif c >= 240 and c <= 244 then
---@type integer?
local c2 = byte(str, bs + 1) local c2 = byte(str, bs + 1)
if not c2 then if not c2 then
@@ -418,10 +433,10 @@ local function utf8validator(str, bs)
end end
end end
--- Validates a UTF-8 encoded string for correctness. ---Validates a UTF-8 encoded string for correctness.
--- @param str string The input UTF-8 encoded string to validate. ---@param str string The input UTF-8 encoded string to validate.
--- @param byte_pos? number The starting byte position for validation (default: 1). ---@param byte_pos? integer The starting byte position for validation (default: 1).
--- @return boolean | table ---@return boolean, table
function string_utf8.validate(str, byte_pos) function string_utf8.validate(str, byte_pos)
byte_pos = byte_pos or 1 byte_pos = byte_pos or 1
@@ -437,12 +452,13 @@ function string_utf8.validate(str, byte_pos)
result[#result + 1] = { pos = bs, part = part, code = code } result[#result + 1] = { pos = bs, part = part, code = code }
end end
end end
return #result == 0, result return #result == 0, result
end end
--- Creates an iterator to traverse UTF-8 encoded characters in a string. ---Creates an iterator to traverse UTF-8 encoded characters in a string.
--- @param str string The input UTF-8 encoded string. ---@param str string The input UTF-8 encoded string.
--- @return fun(skip_ptr: table|nil) ---@return fun(skip_ptr: table|nil):integer?, integer?, integer?
function string_utf8.codes(str) function string_utf8.codes(str)
ensureArgType(str, "string", 1, "string_utf8.codes") ensureArgType(str, "string", 1, "string_utf8.codes")
@@ -476,12 +492,12 @@ n > 0: n jumps forward
n < 0: n more scans backwards n < 0: n more scans backwards
--]] --]]
--- Finds the byte position of the `n`-th UTF-8 character in a string, starting from a given byte position. ---Finds the byte position of the `n`-th UTF-8 character in a string, starting from a given byte position.
--- @param str string The input UTF-8 encoded string. ---@param str string The input UTF-8 encoded string.
--- @param n number The character index to find. If `n` is negative, counts from the end. ---@param n integer The character index to find. If `n` is negative, counts from the end.
--- @param bs number The starting byte position (default: 1 for positive `n`, end of string for negative `n`). ---@param bs integer The starting byte position (default: 1 for positive `n`, end of string for negative `n`).
--- @overload fun(str: string, n: number) ---@overload fun(str: string, n: number)
--- @return number | nil ---@return number | nil
function string_utf8.offset(str, n, bs) function string_utf8.offset(str, n, bs)
local l = #str local l = #str
if not bs then if not bs then

View File

@@ -1,2 +1,2 @@
name = smc__api__utf8 name = smc__system__utf8
depends = smc__api__ensure depends = smc__system__ensure

View File

@@ -933,7 +933,7 @@ string_utf8.lc_uc = {
} }
string_utf8.utf8_uc_lc = { string_utf8.uc_lc = {
["A"] = "a", ["A"] = "a",
["B"] = "b", ["B"] = "b",
["C"] = "c", ["C"] = "c",