luaext.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. -- lua扩展
  2. -- table扩展
  3. function table.indexof(array, value, begin)
  4. for i = begin or 1, #array do
  5. if array[i] == value then
  6. return i
  7. end
  8. end
  9. return false
  10. end
  11. -- 返回table大小
  12. table.size = function(t)
  13. local count = 0
  14. for _ in pairs(t) do
  15. count = count + 1
  16. end
  17. return count
  18. end
  19. -- 判断table是否为空
  20. table.empty = function(t)
  21. return not next(t)
  22. end
  23. -- 判断数值是否在范围内
  24. table.between = function(t, v)
  25. return not t or (v >= t[1] and (t[2] == -1 or v <= t[2]))
  26. end
  27. -- 返回table索引列表
  28. table.indices = function(t)
  29. local result = {}
  30. for k, v in pairs(t) do
  31. table.insert(result, k)
  32. end
  33. return result
  34. end
  35. -- 返回table值列表
  36. table.values = function(t)
  37. local result = {}
  38. for k, v in pairs(t) do
  39. table.insert(result, v)
  40. end
  41. return result
  42. end
  43. -- 浅拷贝
  44. table.clone = function(t, nometa)
  45. local result = {}
  46. if not nometa then
  47. setmetatable(result, getmetatable(t))
  48. end
  49. for k, v in pairs(t) do
  50. result[k] = v
  51. end
  52. return result
  53. end
  54. --清空表
  55. table.clear = function(array)
  56. if type(array) ~= "table" then
  57. return
  58. end
  59. for k, _ in pairs(array) do
  60. array[k] = nil
  61. end
  62. end
  63. -- 深拷贝
  64. table.copy = function(t, nometa)
  65. local result = {}
  66. if not nometa then
  67. setmetatable(result, getmetatable(t))
  68. end
  69. for k, v in pairs(t) do
  70. if type(v) == "table" then
  71. result[k] = table.copy(v)
  72. else
  73. result[k] = v
  74. end
  75. end
  76. return result
  77. end
  78. table.merge = function(dest, src)
  79. for k, v in pairs(src) do
  80. dest[k] = v
  81. end
  82. end
  83. -- 数组全等
  84. table.congruent = function(arr1, arr2, sortFunc)
  85. if type(arr1) ~= "table" or type(arr2) ~= "table" then
  86. return false
  87. end
  88. if #arr1 ~= #arr2 then
  89. return false
  90. end
  91. arr1 = table.copy(arr1)
  92. arr2 = table.copy(arr2)
  93. local func = function(l, r)
  94. return l < r
  95. end
  96. func = sortFunc or func
  97. -- 排序来防止相同元素干扰
  98. table.sort(arr1, func)
  99. table.sort(arr2, func)
  100. for k, v in pairs(arr1) do
  101. if v ~= arr2[k] then
  102. return false
  103. end
  104. end
  105. return true
  106. end
  107. table.array_merge = function(dest, src, unique)
  108. local insert = unique and table.unique_insert or table.insert
  109. for k, v in pairs(src) do
  110. insert(dest, v)
  111. end
  112. return dest
  113. end
  114. table.sum = function(tb, key)
  115. local sum = 0
  116. for k, v in pairs(tb) do
  117. sum = sum + (key and v[key] or v)
  118. end
  119. return sum
  120. end
  121. table.unique_insert = function(t, val)
  122. for k, v in pairs(t) do
  123. if v == val then
  124. return
  125. end
  126. end
  127. table.insert(t, val)
  128. end
  129. table.delete = function(t, val)
  130. for k, v in pairs(t) do
  131. if v == val then
  132. table.remove(t, k)
  133. break
  134. end
  135. end
  136. end
  137. -- 按索引序列找到对应值
  138. -- 如:参数为 tb,a,b,c 返回tb[a][b][c]
  139. table.get_value = function(t, ...)
  140. if not t then
  141. return
  142. end
  143. local path = {...}
  144. local res = t
  145. for k, v in ipairs(path) do
  146. if res[v] then
  147. res = res[v]
  148. else
  149. return
  150. end
  151. end
  152. return res
  153. end
  154. table.include = function(array, val)
  155. if array and val then
  156. for _, v in pairs(array) do
  157. if v == val then
  158. return true
  159. end
  160. end
  161. end
  162. return false
  163. end
  164. -- 查找表中元素对应key
  165. table.find = function(tbl, val)
  166. if tbl and val then
  167. for k, v in pairs(tbl) do
  168. if v == val then
  169. return k
  170. end
  171. end
  172. end
  173. end
  174. --根据table某个字段获取值,array所有值为table结构
  175. table.key_find = function(array, key, val)
  176. for k, v in pairs(array) do
  177. if v[key] == val then
  178. return v, k
  179. end
  180. end
  181. return nil
  182. end
  183. -- 根据键找值最大的一个
  184. table.find_max = function(array, key)
  185. local tmp
  186. for k, v in pairs(array) do
  187. if not tmp then
  188. tmp = v
  189. elseif key and tmp[key] < v[key] then
  190. tmp = v
  191. elseif not key and tmp < v then
  192. tmp = v
  193. end
  194. end
  195. return tmp
  196. end
  197. -- 根据键找值最小的一个
  198. table.find_min = function(array, key)
  199. local tmp
  200. for k, v in pairs(array) do
  201. if not tmp then
  202. tmp = v
  203. elseif key and tmp[key] > v[key] then
  204. tmp = v
  205. elseif not key and tmp > v then
  206. tmp = v
  207. end
  208. end
  209. return tmp
  210. end
  211. table.key_delete = function(array, key, val)
  212. for k, v in ipairs(array) do
  213. if v[key] == val then
  214. table.remove(array, k)
  215. return v
  216. end
  217. end
  218. end
  219. --删除所有v[key] = val的值
  220. table.key_delete_all = function(array, key, val)
  221. local i = 1
  222. while i <= #array do
  223. if array[i][key] == val then
  224. table.remove(array, i)
  225. else
  226. i = i + 1
  227. end
  228. end
  229. end
  230. --删除多个数据
  231. table.array_delete = function(array, t_val)
  232. for k, v in pairs(t_val) do
  233. table.delete(array, v)
  234. end
  235. end
  236. --检测某个值是否在table里面
  237. table.member = function(array, val)
  238. for k, v in ipairs(array) do
  239. if v == val then
  240. return true
  241. end
  242. end
  243. return false
  244. end
  245. --连接
  246. table.link = function(dest, obj)
  247. for k, v in pairs(obj) do
  248. table.insert(dest, v)
  249. end
  250. end
  251. table.fori_each = function(t, f)
  252. for k, v in pairs(t) do
  253. if f(k, v) then
  254. return
  255. end
  256. end
  257. end
  258. table.to_key_value_array = function(source)
  259. local arr = {}
  260. for k, v in pairs(source) do
  261. table.insert(arr, tostring(k))
  262. table.insert(arr, v)
  263. end
  264. return arr
  265. end
  266. -- 数组倒序
  267. table.reverse = function(src)
  268. if not src or #src <= 1 then
  269. return src
  270. end
  271. local ret = {}
  272. for i = #src, 1, -1 do
  273. table.insert(ret, src[i])
  274. end
  275. return ret
  276. end
  277. table.random_array = function(arr)
  278. local tmp, index
  279. for i = 1, #arr - 1 do
  280. index = math.random(i, #arr)
  281. if i ~= index then
  282. tmp = arr[index]
  283. arr[index] = arr[i]
  284. arr[i] = tmp
  285. end
  286. end
  287. end
  288. table.arry_merge = function(dest, src)
  289. for _, v in ipairs(src) do
  290. table.insert(dest, v)
  291. end
  292. end
  293. -- string扩展
  294. -- 下标运算
  295. -- do
  296. -- local mt = getmetatable("")
  297. -- local _index = mt.__index
  298. -- mt.__index = function(s, ...)
  299. -- local k = ...
  300. -- if "number" == type(k) then
  301. -- return _index.sub(s, k, k)
  302. -- else
  303. -- return _index[k]
  304. -- end
  305. -- end
  306. -- end
  307. -- 字符串长度计算(一个汉字长度为1)
  308. string.char_num = function(str)
  309. local lenInByte = #str
  310. local count = 0
  311. local i = 1
  312. while true do
  313. local curByte = string.byte(str, i)
  314. if i > lenInByte then
  315. break
  316. end
  317. local byteCount = 1
  318. if curByte > 0 and curByte < 128 then
  319. byteCount = 1
  320. elseif curByte >= 128 and curByte < 224 then
  321. byteCount = 2
  322. elseif curByte >= 224 and curByte < 240 then
  323. byteCount = 3
  324. elseif curByte >= 240 and curByte <= 247 then
  325. byteCount = 4
  326. else
  327. break
  328. end
  329. i = i + byteCount
  330. count = count + 1
  331. end
  332. return count
  333. end
  334. function string.firstToUpper(str)
  335. return (str:gsub("^%l", string.upper))
  336. end
  337. function string.firstToLower(str)
  338. return (str:gsub("^%u", string.lower))
  339. end
  340. function string.split(s, p)
  341. local rt = {}
  342. string.gsub(
  343. s,
  344. "[^" .. p .. "]+",
  345. function(w)
  346. table.insert(rt, w)
  347. end
  348. )
  349. return rt
  350. end
  351. string.ltrim = function(s, c)
  352. local pattern = "^" .. (c or "%s") .. "+"
  353. return (string.gsub(s, pattern, ""))
  354. end
  355. string.rtrim = function(s, c)
  356. local pattern = (c or "%s") .. "+" .. "$"
  357. return (string.gsub(s, pattern, ""))
  358. end
  359. string.trim = function(s, c)
  360. return string.rtrim(string.ltrim(s, c), c)
  361. end
  362. -- 将字条串转换成字符数组
  363. -- needChar为true时,返回字符数组,否则返回ASCII码数组
  364. string.to_array = function(str, needChar)
  365. local list = {}
  366. for i = 1, math.huge do
  367. local ch = string.sub(str, i, i)
  368. if ch == "" then
  369. break
  370. end
  371. list[i] = needChar and ch or string.byte(ch)
  372. end
  373. return list
  374. end
  375. -- 最长公共子序列(longest common sequence)
  376. -- 返回lcs长度及s长度
  377. string.lcs = function(s, c)
  378. local arrS = string.to_array(s)
  379. local arrC = string.to_array(c)
  380. local dp = {}
  381. for i = 1, #arrS do
  382. dp[i] = dp[i] or {}
  383. for j = 1, #arrC do
  384. local left = dp[i - 1] and dp[i - 1][j] or 0
  385. local right = dp[i][j - 1] or 0
  386. local up = dp[i - 1] and dp[i - 1][j - 1] or 0
  387. local curCount = arrS[i] == arrC[j] and 1 or 0
  388. dp[i][j] = math.max(left, right, up + curCount)
  389. end
  390. end
  391. return dp[#arrS][#arrC], #arrS
  392. end
  393. _tostring = tostring
  394. local function dump(obj)
  395. local cache = {}
  396. local getIndent, quoteStr, wrapKey, wrapVal, dumpObj, isLoop
  397. local pointerList = {}
  398. -- table检查:如果出现相同table指针,只打印指针地址,不打印内容,防止死循环
  399. isLoop = function(val)
  400. if type(val) ~= "table" then
  401. return
  402. end
  403. local key = string.format("%s", val)
  404. if pointerList[key] then
  405. return true, key .. "(loop)"
  406. end
  407. pointerList[key] = true
  408. end
  409. getIndent = function(level)
  410. return string.rep("\t", level)
  411. end
  412. quoteStr = function(str)
  413. return '"' .. string.gsub(str, '"', '\\"') .. '"'
  414. end
  415. wrapKey = function(val, level)
  416. if type(val) == "number" then
  417. return "[" .. val .. "]"
  418. elseif type(val) == "string" then
  419. return "[" .. quoteStr(val) .. "]"
  420. elseif type(val) == "table" then
  421. if cache[val] then
  422. return "[" .. cache[val] .. "]"
  423. else
  424. return "[" .. dumpObj(val, level, ".") .. "]"
  425. end
  426. else
  427. return "[" .. tostring(val) .. "]"
  428. end
  429. end
  430. wrapVal = function(val, level, path)
  431. if type(val) == "table" then
  432. return dumpObj(val, level, path)
  433. elseif type(val) == "number" then
  434. return val
  435. elseif type(val) == "string" then
  436. return quoteStr(val)
  437. else
  438. return tostring(val)
  439. end
  440. end
  441. dumpObj = function(obj, level, path)
  442. if type(obj) ~= "table" then
  443. return wrapVal(obj)
  444. end
  445. local loop, val = isLoop(obj)
  446. if loop then
  447. return val
  448. end
  449. level = level + 1
  450. if cache[obj] then
  451. return cache[obj]
  452. end
  453. cache[obj] = string.format('"%s"', path)
  454. local tokens = {}
  455. tokens[#tokens + 1] = "{"
  456. for k, v in pairs(obj) do
  457. if type(k) == "table" then
  458. tokens[#tokens + 1] =
  459. getIndent(level) .. wrapKey(k, level) .. " = " .. wrapVal(v, level, path .. cache[k] .. ".") .. ","
  460. else
  461. tokens[#tokens + 1] =
  462. getIndent(level) .. wrapKey(k, level) .. " = " .. wrapVal(v, level, path .. k .. ".") .. ","
  463. end
  464. end
  465. tokens[#tokens + 1] = getIndent(level - 1) .. "}"
  466. return table.concat(tokens, "\n")
  467. end
  468. return dumpObj(obj, 0, ".")
  469. end
  470. _tostring = tostring
  471. do
  472. local _tostring = tostring
  473. tostring = function(v)
  474. if type(v) == "table" then
  475. return dump(v)
  476. else
  477. return _tostring(v)
  478. end
  479. end
  480. end
  481. -- math扩展
  482. do
  483. local _floor = math.floor
  484. math.floor = function(n, p)
  485. if p and p ~= 0 then
  486. local e = 10 ^ p
  487. return _floor(n * e) / e
  488. else
  489. return _floor(n)
  490. end
  491. end
  492. end
  493. math.round = function(n, p)
  494. local e = 10 ^ (p or 0)
  495. return math.floor(n * e + 0.5) / e
  496. end
  497. local rand = rand or math.random
  498. math.random = function(...)
  499. local args = {...}
  500. if type(args[1]) == "table" then
  501. return rand(args[1][1], args[1][2])
  502. end
  503. return rand(...)
  504. end
  505. math.INT_MAX = math.floor(2 ^ 31 - 1)
  506. math.INT_MIN = math.floor(-2 ^ 31)
  507. -- lua面向对象扩展
  508. function class(classname, super)
  509. local superType = type(super)
  510. local cls
  511. if superType ~= "function" and superType ~= "table" then
  512. superType = nil
  513. super = nil
  514. end
  515. if superType == "function" or (super and super.__ctype == 1) then
  516. -- inherited from native C++ Object
  517. cls = {}
  518. if superType == "table" then
  519. -- copy fields from super
  520. for k, v in pairs(super) do
  521. cls[k] = v
  522. end
  523. cls.__create = super.__create
  524. cls.super = super
  525. else
  526. cls.__create = super
  527. cls.ctor = function()
  528. end
  529. end
  530. cls.__cname = classname
  531. cls.__ctype = 1
  532. function cls.new(...)
  533. local instance = cls.__create(...)
  534. -- copy fields from class to native object
  535. for k, v in pairs(cls) do
  536. instance[k] = v
  537. end
  538. instance.class = cls
  539. instance:ctor(...)
  540. return instance
  541. end
  542. else
  543. -- inherited from Lua Object
  544. if super then
  545. cls = {}
  546. setmetatable(cls, {__index = super})
  547. cls.super = super
  548. else
  549. cls = {
  550. ctor = function()
  551. end
  552. }
  553. end
  554. cls.__cname = classname
  555. cls.__ctype = 2 -- lua
  556. function cls.new(...)
  557. local instance = setmetatable({}, {__index = cls})
  558. instance.class = cls
  559. instance:ctor(...)
  560. return instance
  561. end
  562. end
  563. return cls
  564. end
  565. function iskindof(obj, classname)
  566. local t = type(obj)
  567. local mt
  568. if t == "table" then
  569. mt = getmetatable(obj)
  570. elseif t == "userdata" then
  571. mt = tolua.getpeer(obj)
  572. end
  573. while mt do
  574. if mt.__cname == classname then
  575. return true
  576. end
  577. mt = mt.super
  578. end
  579. return false
  580. end