@@ -13,6 +13,7 @@ local match,gmatch=string.match,string.gmatch
13
13
local rep ,rev = string.rep ,string.reverse
14
14
local upper ,lower = string.upper ,string.lower
15
15
local char ,byte = string.char ,string.byte
16
+ local buffer = require ' string.buffer'
16
17
17
18
--- @class Zenitha.StringExt
18
19
local STRING = {}
@@ -321,7 +322,7 @@ function STRING.editDist(s1,s2) -- By Copilot
321
322
for i = 1 ,len2 do t2 [i ]= s2 :sub (i ,i ) end
322
323
323
324
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
325
326
dp [0 ][0 ]= 0
326
327
for i = 1 ,len1 do dp [i ][0 ]= i end
327
328
for i = 1 ,len2 do dp [0 ][i ]= i end
@@ -563,16 +564,11 @@ end
563
564
--- @nodiscard
564
565
function STRING .vcsEncrypt (text ,key )
565
566
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 ))
574
570
end
575
- return result .. buffer
571
+ return buf : get ()
576
572
end
577
573
578
574
--- Simple vcs decryption
@@ -582,16 +578,11 @@ end
582
578
--- @nodiscard
583
579
function STRING .vcsDecrypt (text ,key )
584
580
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 ))
593
584
end
594
- return result .. buffer
585
+ return buf : get ()
595
586
end
596
587
597
588
--- Return 16 byte string. Not powerful hash, just simply protect the original text
@@ -617,9 +608,9 @@ function STRING.digezt(text,seedRange,seed)
617
608
pos = (pos + step )% 16
618
609
end
619
610
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 ()
623
614
end
624
615
625
616
--- Cut a line off a string
@@ -710,6 +701,19 @@ function STRING.unpackTable(str)
710
701
return JSON .decode (STRING .unpackText (str ))
711
702
end
712
703
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
+
713
717
do
714
718
local split = STRING .split
715
719
local function parseFile (fname )
0 commit comments