function string.isNilOrEmpty(s) return s == nil or s == '' end -- 用指定字符或字符串分割输入字符串,返回包含分割结果的数组-- function string.split(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if (delimiter == '') then return false end local pos, arr = 0, { } for st, sp in function() return string.find(input, delimiter, pos, true) end do table.insert(arr, string.sub(input, pos, st - 1)) pos = sp + 1 end table.insert(arr, string.sub(input, pos)) return arr end function string.findutf8(s, pattern, index, plain) local luaStartIndex, luaEndIndex = string.find(s, pattern,index,plain) local utf8StartIndex, utf8EndIndex = luaStartIndex, luaEndIndex if luaStartIndex > 0 then utf8StartIndex = utf8.char_index(s, luaStartIndex) utf8EndIndex = utf8StartIndex + utf8.len(pattern) return utf8StartIndex, utf8EndIndex, luaStartIndex, luaEndIndex end end --idsKey:string function string.FormatByIdsKey(idsKey, ...) return string.FormatByIdsId(Const_IDS[idsKey], ...) end local FormatDataArr = {} local FormatFunParamArr = {} --idsKey:int function string.FormatByIdsId(idsId, param1, ...) local srcFormatStr = UIUtils.GetLocalizedTxt(idsId) if not param1 then return srcFormatStr end local paramCount = LuaExt.ResetTable(FormatFunParamArr, param1, ...) --1个参数,不需要调参数顺序 if paramCount<2 then return string.format(srcFormatStr, FormatFunParamArr[1]) end if Localization then if not Localization.HasParamOrderCfg(idsId) then return string.format(srcFormatStr, param1, ...) end end if paramCount>=10 then logging.error(LTAG.ERROR, "string.FormatByIdsId的参数超过了10个") end table.clearArray(FormatDataArr) if Localization then for index, order in Localization.GetParamOrderEnumerator(idsId) do FormatDataArr[index] = FormatFunParamArr[order] end end return string.format(srcFormatStr, table.unpack(FormatDataArr)) end -- 从表格中删除指定值,返回删除的值的个数-- function table.removebyvalue(array, value, removeall) local c, i, max = 0, 1, #array while i <= max do if array[i] == value then table.remove(array, i) c = c + 1 i = i - 1 max = max - 1 if not removeall then break end end i = i + 1 end return c end -- 判断表格中是否包含指定的value-- function table.containValue(t, value) for k, v in pairs(t) do if v == value then return true end end return false end function table.pack(...) local c = select("#", ...) return {n = c, ...} end table.unpack = unpack -- 清空一个表格-- function table.clear(t) for k in pairs(t) do t[k] = nil end end -- 清空一个数组-- function table.clearArray(t) for i = #t, 1, -1 do t[i] = nil end end function table.reverseTable(tab) local len = #tab local midIndex = math.floor(#tab/2)-1 for i=0,midIndex do local tmp = tab[i+1] tab[i+1] = tab[len-i] tab[len-i] = tmp end end --获取一个无序table中的元素个数 function table.getDictCount(dict) local count = 0 for i, v in pairs(dict) do count = count + 1 end return count end -- 浮点数比较的精度 math.epsilon = 0.000001 local math_sqrt = math.sqrt function math.V2_Magnitude(x, z) return math_sqrt(x * x + z * z) end function math.clamp(val, min, max) if val < min then val = min elseif val > max then val = max end return val end function math.getPreciseDecimal(nNum, n) if type(nNum) ~= "number" then return nNum; end n = n or 0; n = math.floor(n) if n < 0 then n = 0; end local nDecimal = 10 ^ n local nTemp = math.floor(nNum * nDecimal); local nRet = nTemp / nDecimal; return nRet; end function table.ResetTableByIter(t, iterFunc, iterSelf, iterVar) local len = #t local index = 1 for var, val in iterFunc, iterSelf, iterVar do t[index] = val index = index + 1 end for i = len, index, -1 do t[i] = nil end end