Skip to content

Commit b2c77f2

Browse files
committed
table扩展模块的清除方法改为三个:clear/clearAll/clearList,第一个是luajit的扩展,fallback到clearAll
string扩展模块添加三个luajit的string.buffer方法 优化和上述相关的代码
1 parent 2be958e commit b2c77f2

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

http.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function HTTP.reset()
167167
threads[i]:release()
168168
threads[i]=false
169169
end
170-
TABLE.clearAll(msgPool)
170+
TABLE.clear(msgPool)
171171
sendCHN:clear()
172172
recvCHN:clear()
173173
addThread(threadCount)

stringExtend.lua

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local match,gmatch=string.match,string.gmatch
1313
local rep,rev=string.rep,string.reverse
1414
local upper,lower=string.upper,string.lower
1515
local char,byte=string.char,string.byte
16+
local buffer=require'string.buffer'
1617

1718
---@class Zenitha.StringExt
1819
local STRING={}
@@ -321,7 +322,7 @@ function STRING.editDist(s1,s2) -- By Copilot
321322
for i=1,len2 do t2[i]=s2:sub(i,i) end
322323

323324
local dp={}
324-
for i=0,len1 do dp[i]=TABLE.new(0,len2) end
325+
for i=0,len1 do dp[i]={} for j=0,len2 do dp[i][j]=0 end end
325326
dp[0][0]=0
326327
for i=1,len1 do dp[i][0]=i end
327328
for i=1,len2 do dp[0][i]=i end
@@ -563,16 +564,11 @@ end
563564
---@nodiscard
564565
function STRING.vcsEncrypt(text,key)
565566
local keyLen=#key
566-
local result=''
567-
local buffer=''
568-
for i=0,#text-1 do
569-
buffer=buffer..char((byte(text,i+1)-32+byte(key,i%keyLen+1))%95+32)
570-
if #buffer==26 then
571-
result=result..buffer
572-
buffer=''
573-
end
567+
local buf=buffer.new(#text)
568+
for i=1,#text do
569+
buf:put(char((byte(text,i)-32+byte(key,(i-1)%keyLen+1))%95+32))
574570
end
575-
return result..buffer
571+
return buf:get()
576572
end
577573

578574
---Simple vcs decryption
@@ -582,16 +578,11 @@ end
582578
---@nodiscard
583579
function STRING.vcsDecrypt(text,key)
584580
local keyLen=#key
585-
local result=''
586-
local buffer=''
587-
for i=0,#text-1 do
588-
buffer=buffer..char((byte(text,i+1)-32-byte(key,i%keyLen+1))%95+32)
589-
if #buffer==26 then
590-
result=result..buffer
591-
buffer=''
592-
end
581+
local buf=buffer.new(#text)
582+
for i=1,#text do
583+
buf:put(char((byte(text,i)-32-byte(key,(i-1)%keyLen+1))%95+32))
593584
end
594-
return result..buffer
585+
return buf:get()
595586
end
596587

597588
---Return 16 byte string. Not powerful hash, just simply protect the original text
@@ -617,9 +608,9 @@ function STRING.digezt(text,seedRange,seed)
617608
pos=(pos+step)%16
618609
end
619610
end
620-
local result=''
621-
for i=1,16 do result=result..char(out[i]) end
622-
return result
611+
local result=buffer.new(16)
612+
for i=1,16 do result:put(char(out[i])) end
613+
return result:get()
623614
end
624615

625616
---Cut a line off a string
@@ -710,6 +701,19 @@ function STRING.unpackTable(str)
710701
return JSON.decode(STRING.unpackText(str))
711702
end
712703

704+
--------------------------------------------------------------
705+
-- LuaJIT Extension
706+
707+
xpcall(function()
708+
STRING.newBuf=buffer.new
709+
STRING.encBuf=buffer.encode
710+
STRING.decBuf=buffer.decode
711+
end,function()
712+
STRING[('newBuf')]=function() error("string.buffer.new is not available") end
713+
STRING[('encBuf')]=function() error("string.buffer.encode is not available") end
714+
STRING[('decBuf')]=function() error("string.buffer.decode is not available") end
715+
end)
716+
713717
do
714718
local split=STRING.split
715719
local function parseFile(fname)

tableExtend.lua

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,26 +347,37 @@ end
347347
--------------------------------------------------------------
348348
-- Editing
349349

350-
---Clear [1~#] of a table
350+
---`table.clear` from luajit extension, clear the whole table but preserve the allocated array/hash sizes
351+
---
352+
---Fallback to `TABLE.clearAll` if failed to require `table.clear`
353+
---
351354
---@param t table
352355
function TABLE.clear(t)
353-
for i=1,#t do
354-
t[i]=nil
356+
for k in next,t do
357+
t[k]=nil
355358
end
356359
end
360+
pcall(function() TABLE[('clear')]=require'table.clear' end)
357361

358-
---Clear all table
362+
---Clear whole table (pure lua implementation)
359363
---
360-
---You can also use `table.clear` from luajit:
361-
---https://luajit.org/extensions.html
364+
---Recommend to use `TABLE.clear` instead
362365
---@param t table
363366
function TABLE.clearAll(t)
364367
for k in next,t do
365368
t[k]=nil
366369
end
367370
end
368371

369-
---Remove value of [1~#]
372+
---Clear [1~#] of a table (pure lua implementation)
373+
---@param t table
374+
function TABLE.clearList(t)
375+
for i=1,#t do
376+
t[i]=nil
377+
end
378+
end
379+
380+
---Remove a specific value of [1~#]
370381
---@param org any[]
371382
function TABLE.delete(org,value)
372383
for i=#org,1,-1 do
@@ -376,7 +387,7 @@ function TABLE.delete(org,value)
376387
end
377388
end
378389

379-
---Remove value in whole table
390+
---Remove a specific value in whole table
380391
---@param org table
381392
function TABLE.deleteAll(org,value)
382393
for k,v in next,org do

websocket_thread.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local connTimeout,pongTimeout=16,2.6
66
local sleepInterval,pingInterval=6,0.26
77
local lastRecvTime,lastSendTime
88

9+
local buffer=require'string.buffer'
910
local timer=require'love.timer'.getTime
1011
local sleep=require'love.timer'.sleep
1112
local CHN_demand,CHN_getCount=confCHN.demand,confCHN.getCount
@@ -129,20 +130,20 @@ end)
129130

130131
local function _receive(sock,len)
131132
lastRecvTime=timer()
132-
local buffer=""
133+
local buf=buffer.new(len)
133134
while true do
134135
local r,e,p=sock:receive(len)
135136
if r then
136-
buffer=buffer..r
137+
buf:put(r)
137138
len=len-#r
138139
elseif p then
139-
buffer=buffer..p
140+
buf:put(p)
140141
len=len-#p
141142
elseif e then
142143
return nil,e
143144
end
144145
if len==0 then
145-
return buffer
146+
return buf:get()
146147
end
147148
yield()
148149
end

widget.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ function Widgets.textBox:replaceTexts(newList)
17001700
end
17011701
function Widgets.textBox:setTexts(t)
17021702
assert(type(t)=='table',"Arg need table")
1703-
TABLE.clearAll(self._texts)
1703+
TABLE.clear(self._texts)
17041704
TABLE.connect(self._texts,t)
17051705
self._scrollPos=0
17061706
end

0 commit comments

Comments
 (0)