functions.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. --[[
  2. Copyright (c) 2011-2014 chukong-inc.com
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. ]]
  19. function printLog(tag, fmt, ...)
  20. local t = {
  21. "[",
  22. string.upper(tostring(tag)),
  23. "] ",
  24. string.format(tostring(fmt), ...)
  25. }
  26. print(table.concat(t))
  27. end
  28. function printError(fmt, ...)
  29. printLog("ERR", fmt, ...)
  30. print(debug.traceback("", 2))
  31. end
  32. function printInfo(fmt, ...)
  33. if type(DEBUG) ~= "number" or DEBUG < 2 then return end
  34. printLog("INFO", fmt, ...)
  35. end
  36. local function dump_value_(v)
  37. if type(v) == "string" then
  38. v = "\"" .. v .. "\""
  39. end
  40. return tostring(v)
  41. end
  42. function dump(value, desciption, nesting)
  43. if not _DEBUG then
  44. return nil
  45. end
  46. if type(nesting) ~= "number" then nesting = 3 end
  47. local lookupTable = {}
  48. local result = {}
  49. local traceback = string.split(debug.traceback("", 2), "\n")
  50. print("dump from: " .. string.trim(traceback[3]))
  51. local function dump_(value, desciption, indent, nest, keylen)
  52. desciption = desciption or "<var>"
  53. local spc = ""
  54. if type(keylen) == "number" then
  55. spc = string.rep(" ", keylen - string.len(dump_value_(desciption)))
  56. end
  57. if type(value) ~= "table" then
  58. result[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(desciption), spc, dump_value_(value))
  59. elseif lookupTable[tostring(value)] then
  60. result[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(desciption), spc)
  61. else
  62. lookupTable[tostring(value)] = true
  63. if nest > nesting then
  64. result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(desciption))
  65. else
  66. result[#result +1 ] = string.format("%s%s = {", indent, dump_value_(desciption))
  67. local indent2 = indent.." "
  68. local keys = {}
  69. local keylen = 0
  70. local values = {}
  71. for k, v in pairs(value) do
  72. keys[#keys + 1] = k
  73. local vk = dump_value_(k)
  74. local vkl = string.len(vk)
  75. if vkl > keylen then keylen = vkl end
  76. values[k] = v
  77. end
  78. table.sort(keys, function(a, b)
  79. if type(a) == "number" and type(b) == "number" then
  80. return a < b
  81. else
  82. return tostring(a) < tostring(b)
  83. end
  84. end)
  85. for i, k in ipairs(keys) do
  86. dump_(values[k], k, indent2, nest + 1, keylen)
  87. end
  88. result[#result +1] = string.format("%s}", indent)
  89. end
  90. end
  91. end
  92. dump_(value, desciption, "- ", 1)
  93. for i, line in ipairs(result) do
  94. print(line)
  95. end
  96. end
  97. function printf(fmt, ...)
  98. print(string.format(tostring(fmt), ...))
  99. end
  100. function checknumber(value, base)
  101. return tonumber(value, base) or 0
  102. end
  103. function checkint(value)
  104. return math.round(checknumber(value))
  105. end
  106. function checkbool(value)
  107. return (value ~= nil and value ~= false)
  108. end
  109. function checktable(value)
  110. if type(value) ~= "table" then value = {} end
  111. return value
  112. end
  113. function isset(hashtable, key)
  114. local t = type(hashtable)
  115. return (t == "table" or t == "userdata") and hashtable[key] ~= nil
  116. end
  117. local setmetatableindex_
  118. setmetatableindex_ = function(t, index)
  119. if type(t) == "userdata" then
  120. local peer = tolua.getpeer(t)
  121. if not peer then
  122. peer = {}
  123. tolua.setpeer(t, peer)
  124. end
  125. setmetatableindex_(peer, index)
  126. else
  127. local mt = getmetatable(t)
  128. if not mt then mt = {} end
  129. if not mt.__index then
  130. mt.__index = index
  131. setmetatable(t, mt)
  132. elseif mt.__index ~= index then
  133. setmetatableindex_(mt, index)
  134. end
  135. end
  136. end
  137. setmetatableindex = setmetatableindex_
  138. function clone(object)
  139. local lookup_table = {}
  140. local function _copy(object)
  141. if type(object) ~= "table" then
  142. return object
  143. elseif lookup_table[object] then
  144. return lookup_table[object]
  145. end
  146. local newObject = {}
  147. lookup_table[object] = newObject
  148. for key, value in pairs(object) do
  149. newObject[_copy(key)] = _copy(value)
  150. end
  151. return setmetatable(newObject, getmetatable(object))
  152. end
  153. return _copy(object)
  154. end
  155. function class(classname, ...)
  156. local cls = {__cname = classname}
  157. local supers = {...}
  158. for _, super in ipairs(supers) do
  159. local superType = type(super)
  160. assert(superType == "nil" or superType == "table" or superType == "function",
  161. string.format("class() - create class \"%s\" with invalid super class type \"%s\"",
  162. classname, superType))
  163. if superType == "function" then
  164. assert(cls.__create == nil,
  165. string.format("class() - create class \"%s\" with more than one creating function",
  166. classname));
  167. -- if super is function, set it to __create
  168. cls.__create = super
  169. elseif superType == "table" then
  170. if super[".isclass"] then
  171. -- super is native class
  172. assert(cls.__create == nil,
  173. string.format("class() - create class \"%s\" with more than one creating function or native class",
  174. classname));
  175. cls.__create = function() return super:create() end
  176. else
  177. -- super is pure lua class
  178. cls.__supers = cls.__supers or {}
  179. cls.__supers[#cls.__supers + 1] = super
  180. if not cls.super then
  181. -- set first super pure lua class as class.super
  182. cls.super = super
  183. end
  184. end
  185. else
  186. error(string.format("class() - create class \"%s\" with invalid super type",
  187. classname), 0)
  188. end
  189. end
  190. cls.__index = cls
  191. if not cls.__supers or #cls.__supers == 1 then
  192. setmetatable(cls, {__index = cls.super})
  193. else
  194. setmetatable(cls, {__index = function(_, key)
  195. local supers = cls.__supers
  196. for i = 1, #supers do
  197. local super = supers[i]
  198. if super[key] then return super[key] end
  199. end
  200. end})
  201. end
  202. if not cls.ctor then
  203. -- add default constructor
  204. cls.ctor = function() end
  205. end
  206. cls.new = function(...)
  207. local instance
  208. if cls.__create then
  209. instance = cls.__create(...)
  210. else
  211. instance = {}
  212. end
  213. setmetatableindex(instance, cls)
  214. instance.class = cls
  215. instance:ctor(...)
  216. return instance
  217. end
  218. cls.create = function(_, ...)
  219. return cls.new(...)
  220. end
  221. return cls
  222. end
  223. local iskindof_
  224. iskindof_ = function(cls, name)
  225. local __index = rawget(cls, "__index")
  226. if type(__index) == "table" and rawget(__index, "__cname") == name then return true end
  227. if rawget(cls, "__cname") == name then return true end
  228. local __supers = rawget(cls, "__supers")
  229. if not __supers then return false end
  230. for _, super in ipairs(__supers) do
  231. if iskindof_(super, name) then return true end
  232. end
  233. return false
  234. end
  235. function iskindof(obj, classname)
  236. local t = type(obj)
  237. if t ~= "table" and t ~= "userdata" then return false end
  238. local mt
  239. if t == "userdata" then
  240. if tolua.iskindof(obj, classname) then return true end
  241. mt = tolua.getpeer(obj)
  242. else
  243. mt = getmetatable(obj)
  244. end
  245. if mt then
  246. return iskindof_(mt, classname)
  247. end
  248. return false
  249. end
  250. function import(moduleName, currentModuleName)
  251. local currentModuleNameParts
  252. local moduleFullName = moduleName
  253. local offset = 1
  254. while true do
  255. if string.byte(moduleName, offset) ~= 46 then -- .
  256. moduleFullName = string.sub(moduleName, offset)
  257. if currentModuleNameParts and #currentModuleNameParts > 0 then
  258. moduleFullName = table.concat(currentModuleNameParts, ".") .. "." .. moduleFullName
  259. end
  260. break
  261. end
  262. offset = offset + 1
  263. if not currentModuleNameParts then
  264. if not currentModuleName then
  265. local n,v = debug.getlocal(3, 1)
  266. currentModuleName = v
  267. end
  268. currentModuleNameParts = string.split(currentModuleName, ".")
  269. end
  270. table.remove(currentModuleNameParts, #currentModuleNameParts)
  271. end
  272. return require(moduleFullName)
  273. end
  274. function handler(obj, method)
  275. return function(...)
  276. return method(obj, ...)
  277. end
  278. end
  279. function math.newrandomseed()
  280. local ok, socket = pcall(function()
  281. return require("socket")
  282. end)
  283. if ok then
  284. math.randomseed(socket.gettime() * 1000)
  285. else
  286. math.randomseed(os.time())
  287. end
  288. math.random()
  289. math.random()
  290. math.random()
  291. math.random()
  292. end
  293. function math.round(value)
  294. value = checknumber(value)
  295. return math.floor(value + 0.5)
  296. end
  297. local pi_div_180 = math.pi / 180
  298. function math.angle2radian(angle)
  299. return angle * pi_div_180
  300. end
  301. local pi_mul_180 = math.pi * 180
  302. function math.radian2angle(radian)
  303. return radian / pi_mul_180
  304. end
  305. function io.exists(path)
  306. local file = io.open(path, "r")
  307. if file then
  308. io.close(file)
  309. return true
  310. end
  311. return false
  312. end
  313. function io.readfile(path)
  314. local file = io.open(path, "r")
  315. if file then
  316. local content = file:read("*a")
  317. io.close(file)
  318. return content
  319. end
  320. return nil
  321. end
  322. function io.writefile(path, content, mode)
  323. mode = mode or "w+b"
  324. local file = io.open(path, mode)
  325. if file then
  326. if file:write(content) == nil then return false end
  327. io.close(file)
  328. return true
  329. else
  330. return false
  331. end
  332. end
  333. function io.pathinfo(path)
  334. local pos = string.len(path)
  335. local extpos = pos + 1
  336. while pos > 0 do
  337. local b = string.byte(path, pos)
  338. if b == 46 then -- 46 = char "."
  339. extpos = pos
  340. elseif b == 47 then -- 47 = char "/"
  341. break
  342. end
  343. pos = pos - 1
  344. end
  345. local dirname = string.sub(path, 1, pos)
  346. local filename = string.sub(path, pos + 1)
  347. extpos = extpos - pos
  348. local basename = string.sub(filename, 1, extpos - 1)
  349. local extname = string.sub(filename, extpos)
  350. return {
  351. dirname = dirname,
  352. filename = filename,
  353. basename = basename,
  354. extname = extname
  355. }
  356. end
  357. function io.filesize(path)
  358. local size = false
  359. local file = io.open(path, "r")
  360. if file then
  361. local current = file:seek()
  362. size = file:seek("end")
  363. file:seek("set", current)
  364. io.close(file)
  365. end
  366. return size
  367. end
  368. function table.nums(t)
  369. local count = 0
  370. for k, v in pairs(t) do
  371. count = count + 1
  372. end
  373. return count
  374. end
  375. function table.keys(hashtable)
  376. local keys = {}
  377. for k, v in pairs(hashtable) do
  378. keys[#keys + 1] = k
  379. end
  380. return keys
  381. end
  382. function table.values(hashtable)
  383. local values = {}
  384. for k, v in pairs(hashtable) do
  385. values[#values + 1] = v
  386. end
  387. return values
  388. end
  389. function table.merge(dest, src)
  390. for k, v in pairs(src) do
  391. dest[k] = v
  392. end
  393. end
  394. function table.insertto(dest, src, begin)
  395. begin = checkint(begin)
  396. if begin <= 0 then
  397. begin = #dest + 1
  398. end
  399. local len = #src
  400. for i = 0, len - 1 do
  401. dest[i + begin] = src[i + 1]
  402. end
  403. end
  404. function table.indexof(array, value, begin)
  405. for i = begin or 1, #array do
  406. if array[i] == value then return i end
  407. end
  408. return false
  409. end
  410. function table.keyof(hashtable, value)
  411. for k, v in pairs(hashtable) do
  412. if v == value then return k end
  413. end
  414. return nil
  415. end
  416. function table.removebyvalue(array, value, removeall)
  417. local c, i, max = 0, 1, #array
  418. while i <= max do
  419. if array[i] == value then
  420. table.remove(array, i)
  421. c = c + 1
  422. i = i - 1
  423. max = max - 1
  424. if not removeall then break end
  425. end
  426. i = i + 1
  427. end
  428. return c
  429. end
  430. function table.map(t, fn)
  431. for k, v in pairs(t) do
  432. t[k] = fn(v, k)
  433. end
  434. end
  435. function table.walk(t, fn)
  436. for k,v in pairs(t) do
  437. fn(v, k)
  438. end
  439. end
  440. function table.filter(t, fn)
  441. for k, v in pairs(t) do
  442. if not fn(v, k) then t[k] = nil end
  443. end
  444. end
  445. function table.unique(t, bArray)
  446. local check = {}
  447. local n = {}
  448. local idx = 1
  449. for k, v in pairs(t) do
  450. if not check[v] then
  451. if bArray then
  452. n[idx] = v
  453. idx = idx + 1
  454. else
  455. n[k] = v
  456. end
  457. check[v] = true
  458. end
  459. end
  460. return n
  461. end
  462. function table.find(arr, val)
  463. for k, v in pairs(arr) do
  464. if v == val then
  465. return k
  466. end
  467. end
  468. return nil
  469. end
  470. function table.split(arr, len)
  471. local data = {}
  472. local td = {}
  473. local idx = 1
  474. for i = 1, #arr do
  475. if idx > len then
  476. idx = 1
  477. table.insert(data, td)
  478. td = {}
  479. end
  480. table.insert(td, arr[i])
  481. idx = idx + 1
  482. end
  483. if #td > 0 then
  484. table.insert(data, td)
  485. end
  486. return data
  487. end
  488. function table.addition(dataA, dataB)
  489. for k, v in pairs(dataB) do
  490. table.insert(dataA, v)
  491. end
  492. end
  493. function table.IsChild(parent, child)
  494. if type(child) == "table" and type(parent) == "table" then
  495. for k, v in pairs(child) do
  496. if not table.IsChild(parent[k], v) then
  497. return false
  498. end
  499. end
  500. else
  501. if child ~= parent then
  502. return false
  503. end
  504. end
  505. return true
  506. end
  507. -- 顺序表合并
  508. function table.order_merge(a, b)
  509. local concat = {}
  510. local out = 1
  511. local i = 1
  512. local byte = a[i]
  513. while byte ~= nil do
  514. concat[out] = byte
  515. i = i + 1
  516. out = out + 1
  517. byte = a[i]
  518. end
  519. i = 1
  520. byte = b[i]
  521. while byte ~= nil do
  522. concat[out] = byte
  523. i = i + 1
  524. out = out + 1
  525. byte = b[i]
  526. end
  527. return concat
  528. end
  529. string._htmlspecialchars_set = {}
  530. string._htmlspecialchars_set["&"] = "&amp;"
  531. string._htmlspecialchars_set["\""] = "&quot;"
  532. string._htmlspecialchars_set["'"] = "&#039;"
  533. string._htmlspecialchars_set["<"] = "&lt;"
  534. string._htmlspecialchars_set[">"] = "&gt;"
  535. function string.htmlspecialchars(input)
  536. for k, v in pairs(string._htmlspecialchars_set) do
  537. input = string.gsub(input, k, v)
  538. end
  539. return input
  540. end
  541. function string.restorehtmlspecialchars(input)
  542. for k, v in pairs(string._htmlspecialchars_set) do
  543. input = string.gsub(input, v, k)
  544. end
  545. return input
  546. end
  547. function string.nl2br(input)
  548. return string.gsub(input, "\n", "<br />")
  549. end
  550. function string.text2html(input)
  551. input = string.gsub(input, "\t", " ")
  552. input = string.htmlspecialchars(input)
  553. input = string.gsub(input, " ", "&nbsp;")
  554. input = string.nl2br(input)
  555. return input
  556. end
  557. function string.split(input, delimiter)
  558. input = tostring(input)
  559. delimiter = tostring(delimiter)
  560. if (delimiter=='') then return false end
  561. local pos,arr = 0, {}
  562. -- for each divider found
  563. for st,sp in function() return string.find(input, delimiter, pos, true) end do
  564. table.insert(arr, string.sub(input, pos, st - 1))
  565. pos = sp + 1
  566. end
  567. table.insert(arr, string.sub(input, pos))
  568. return arr
  569. end
  570. function string.ltrim(input)
  571. return string.gsub(input, "^[ \t\n\r]+", "")
  572. end
  573. function string.rtrim(input)
  574. return string.gsub(input, "[ \t\n\r]+$", "")
  575. end
  576. function string.trim(input)
  577. input = string.gsub(input, "^[ \t\n\r]+", "")
  578. return string.gsub(input, "[ \t\n\r]+$", "")
  579. end
  580. function string.ucfirst(input)
  581. return string.upper(string.sub(input, 1, 1)) .. string.sub(input, 2)
  582. end
  583. local function urlencodechar(char)
  584. return "%" .. string.format("%02X", string.byte(char))
  585. end
  586. function string.urlencode(input)
  587. -- convert line endings
  588. input = string.gsub(tostring(input), "\n", "\r\n")
  589. -- escape all characters but alphanumeric, '.' and '-'
  590. input = string.gsub(input, "([^%w%.%- ])", urlencodechar)
  591. -- convert spaces to "+" symbols
  592. return string.gsub(input, " ", "+")
  593. end
  594. function string.urldecode(input)
  595. input = string.gsub (input, "+", " ")
  596. input = string.gsub (input, "%%(%x%x)", function(h) return string.char(checknumber(h,16)) end)
  597. input = string.gsub (input, "\r\n", "\n")
  598. return input
  599. end
  600. function string.utf8len(input)
  601. local len = string.len(input)
  602. local left = len
  603. local cnt = 0
  604. local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}
  605. while left ~= 0 do
  606. local tmp = string.byte(input, -left)
  607. local i = #arr
  608. while arr[i] do
  609. if tmp >= arr[i] then
  610. left = left - i
  611. break
  612. end
  613. i = i - 1
  614. end
  615. cnt = cnt + 1
  616. end
  617. return cnt
  618. end
  619. function string.formatnumberthousands(num)
  620. local formatted = tostring(checknumber(num))
  621. local k
  622. while true do
  623. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  624. if k == 0 then break end
  625. end
  626. return formatted
  627. end
  628. function string.unicode2utf8(str)
  629. if type(str) ~= "string" then
  630. return str
  631. end
  632. local items = {}
  633. local i = 1
  634. while true do
  635. local num1 = string.byte(str, i)
  636. local unicode = nil
  637. if num1 ~= nil and string.sub(str, i, i + 1)=="\\u" then
  638. unicode = tonumber("0x" .. string.sub(str, i + 2, i + 5))
  639. i = i + 6
  640. elseif num1 ~= nil then
  641. unicode = num1
  642. i = i + 1
  643. else
  644. break
  645. end
  646. if unicode <= 0x007f then
  647. table.insert(items, string.char(bit.band(unicode,0x7f)))
  648. elseif unicode >= 0x0080 and unicode <= 0x07ff then
  649. table.insert(items, string.char(bit.bor(0xc0,bit.band(bit.rshift(unicode,6),0x1f))))
  650. table.insert(items, string.char(bit.bor(0x80,bit.band(unicode,0x3f))))
  651. elseif unicode >= 0x0800 and unicode <= 0xffff then
  652. table.insert(items, string.char(bit.bor(0xe0,bit.band(bit.rshift(unicode,12),0x0f))))
  653. table.insert(items, string.char(bit.bor(0x80,bit.band(bit.rshift(unicode,6),0x3f))))
  654. table.insert(items, string.char(bit.bor(0x80,bit.band(unicode,0x3f))))
  655. end
  656. end
  657. table.insert(items, '\0')
  658. return table.concat(items, "")
  659. end
  660. function string.endsWith(str, subStr)
  661. local len1 = #str
  662. local len2 = #subStr
  663. if len1 < len2 then
  664. return false
  665. end
  666. return string.find(str, subStr, len1 - len2 + 1) ~= nil
  667. end
  668. function string.startsWith(str, subStr)
  669. return string.find(str, subStr) == 1
  670. end
  671. function string.contains(str, subStr)
  672. return string.find(str, subStr) ~= nil
  673. end
  674. function string.cchar(n)
  675. local t = {
  676. [174] = "®",
  677. [175] = "¯",
  678. [176] = "°",
  679. [177] = "±",
  680. [178] = "²",
  681. [179] = "³",
  682. [180] = "´",
  683. [181] = "µ",
  684. [182] = "¶",
  685. [183] = "·",
  686. [184] = "¸",
  687. [185] = "¹",
  688. [186] = "º",
  689. [187] = "»",
  690. [188] = "¼",
  691. [189] = "½",
  692. [190] = "¾",
  693. [191] = "¿",
  694. [192] = "À",
  695. [193] = "Á",
  696. [194] = "Â",
  697. [195] = "Ã",
  698. [196] = "Ä",
  699. [197] = "Å",
  700. [198] = "Æ",
  701. [199] = "Ç",
  702. [200] = "È",
  703. [201] = "É",
  704. [202] = "Ê",
  705. [203] = "Ë",
  706. [204] = "Ì",
  707. [205] = "Í",
  708. [206] = "Î",
  709. [207] = "Ï",
  710. [208] = "Ð",
  711. [209] = "Ñ",
  712. [210] = "Ò",
  713. [211] = "Ó",
  714. [212] = "Ô",
  715. [213] = "Õ",
  716. [214] = "Ö",
  717. [215] = "×",
  718. [216] = "Ø",
  719. [217] = "Ù",
  720. [218] = "Ú",
  721. [219] = "Û",
  722. [220] = "Ü",
  723. [221] = "Ý",
  724. [222] = "Þ",
  725. [223] = "ß",
  726. [224] = "à",
  727. [225] = "á",
  728. [226] = "â",
  729. [227] = "ã",
  730. [228] = "ä",
  731. [229] = "å",
  732. [230] = "æ",
  733. [231] = "ç",
  734. [232] = "è",
  735. [233] = "é",
  736. [234] = "ê",
  737. [235] = "ë",
  738. [236] = "ì",
  739. [237] = "í",
  740. [238] = "î",
  741. [239] = "ï",
  742. [240] = "ð",
  743. [241] = "ñ",
  744. [242] = "ò",
  745. [243] = "ó",
  746. [244] = "ô",
  747. [245] = "õ",
  748. [246] = "ö",
  749. [247] = "÷",
  750. [248] = "ø",
  751. [249] = "ù",
  752. [250] = "ú",
  753. [251] = "û",
  754. [252] = "ü",
  755. [253] = "ý",
  756. [254] = "þ",
  757. [255] = "ÿ"
  758. }
  759. return t[n]
  760. end
  761. function string.cbyte(c)
  762. local t = {
  763. ["®"] = 174,
  764. ["¯"] = 175,
  765. ["°"] = 176,
  766. ["±"] = 177,
  767. ["²"] = 178,
  768. ["³"] = 179,
  769. ["´"] = 180,
  770. ["µ"] = 181,
  771. ["¶"] = 182,
  772. ["·"] = 183,
  773. ["¸"] = 184,
  774. ["¹"] = 185,
  775. ["º"] = 186,
  776. ["»"] = 187,
  777. ["¼"] = 188,
  778. ["½"] = 189,
  779. ["¾"] = 190,
  780. ["¿"] = 191,
  781. ["À"] = 192,
  782. ["Á"] = 193,
  783. ["Â"] = 194,
  784. ["Ã"] = 195,
  785. ["Ä"] = 196,
  786. ["Å"] = 197,
  787. ["Æ"] = 198,
  788. ["Ç"] = 199,
  789. ["È"] = 200,
  790. ["É"] = 201,
  791. ["Ê"] = 202,
  792. ["Ë"] = 203,
  793. ["Ì"] = 204,
  794. ["Í"] = 205,
  795. ["Î"] = 206,
  796. ["Ï"] = 207,
  797. ["Ð"] = 208,
  798. ["Ñ"] = 209,
  799. ["Ò"] = 210,
  800. ["Ó"] = 211,
  801. ["Ô"] = 212,
  802. ["Õ"] = 213,
  803. ["Ö"] = 214,
  804. ["×"] = 215,
  805. ["Ø"] = 216,
  806. ["Ù"] = 217,
  807. ["Ú"] = 218,
  808. ["Û"] = 219,
  809. ["Ü"] = 220,
  810. ["Ý"] = 221,
  811. ["Þ"] = 222,
  812. ["ß"] = 223,
  813. ["à"] = 224,
  814. ["á"] = 225,
  815. ["â"] = 226,
  816. ["ã"] = 227,
  817. ["ä"] = 228,
  818. ["å"] = 229,
  819. ["æ"] = 230,
  820. ["ç"] = 231,
  821. ["è"] = 232,
  822. ["é"] = 233,
  823. ["ê"] = 234,
  824. ["ë"] = 235,
  825. ["ì"] = 236,
  826. ["í"] = 237,
  827. ["î"] = 238,
  828. ["ï"] = 239,
  829. ["ð"] = 240,
  830. ["ñ"] = 241,
  831. ["ò"] = 242,
  832. ["ó"] = 243,
  833. ["ô"] = 244,
  834. ["õ"] = 245,
  835. ["ö"] = 246,
  836. ["÷"] = 247,
  837. ["ø"] = 248,
  838. ["ù"] = 249,
  839. ["ú"] = 250,
  840. ["û"] = 251,
  841. ["ü"] = 252,
  842. ["ý"] = 253,
  843. ["þ"] = 254,
  844. ["ÿ"] = 255
  845. }
  846. return t[c]
  847. end