patternMinor
Lua string parsing
Viewed 0 times
parsingstringlua
Problem
The following code takes a string and converts it to it's number equivalent.
A=1 ... Z=26, while keeping all other characters intact.
It formats the string using two delimiters. One for between characters and one for between words. I want to optimize it, and have fixed a lot of redundancy, and slow parts. However, I can't seem to make it any faster. The longest amount of time is taken for decrypting an encrypted string. I would like to optimize both parts.
Suggestions on techniques or code with explanation would be most helpful. This is mostly for me to understand the bottlenecks in Lua logic.
```
function parseString(str,del1,del2)
local delim1 = del1 or ' '
local delim2 = del2 or '-'
local tab = {}
local sub = ''
local index = 1
local c = ''
local i = 1
while (i > #str and sub == '') == false do
c = str:sub(i,i)
if c == delim2 or c == delim1 or c == '' then
if string.match(sub,'%d') == nil then
tab[index] = sub
index = index + 1
else
tab[index] = string.char(tonumber(sub) + 96)
index = index + 1
end
if c == delim1 then
tab[index] = ' '
index = index + 1
end
sub = ''
else
sub = sub .. c
end
i = i + 1
end
return table.concat(tab)
end
function strToNums(str)
local t = {string.byte(str,1,#str)}
for i,v in ipairs(t) do
if v = 65then
t[i] = v - 64
else
if v = 97then
t[i] = v - 96
else
t[i] = v
end
end
end
return t
end
function formater(del1,del2,arg)
local out = {}
local index = 1
local nDel1 = string.byte(del1)
local nDel2 = string.byte(del2)
for i,v in ipairs(arg) do
if v = 1 then
out[index] = tostring(v)
index = index + 1
if i <
A=1 ... Z=26, while keeping all other characters intact.
It formats the string using two delimiters. One for between characters and one for between words. I want to optimize it, and have fixed a lot of redundancy, and slow parts. However, I can't seem to make it any faster. The longest amount of time is taken for decrypting an encrypted string. I would like to optimize both parts.
Suggestions on techniques or code with explanation would be most helpful. This is mostly for me to understand the bottlenecks in Lua logic.
```
function parseString(str,del1,del2)
local delim1 = del1 or ' '
local delim2 = del2 or '-'
local tab = {}
local sub = ''
local index = 1
local c = ''
local i = 1
while (i > #str and sub == '') == false do
c = str:sub(i,i)
if c == delim2 or c == delim1 or c == '' then
if string.match(sub,'%d') == nil then
tab[index] = sub
index = index + 1
else
tab[index] = string.char(tonumber(sub) + 96)
index = index + 1
end
if c == delim1 then
tab[index] = ' '
index = index + 1
end
sub = ''
else
sub = sub .. c
end
i = i + 1
end
return table.concat(tab)
end
function strToNums(str)
local t = {string.byte(str,1,#str)}
for i,v in ipairs(t) do
if v = 65then
t[i] = v - 64
else
if v = 97then
t[i] = v - 96
else
t[i] = v
end
end
end
return t
end
function formater(del1,del2,arg)
local out = {}
local index = 1
local nDel1 = string.byte(del1)
local nDel2 = string.byte(del2)
for i,v in ipairs(arg) do
if v = 1 then
out[index] = tostring(v)
index = index + 1
if i <
Solution
You can use a translation table with
gsub, as in the code below:local t={}
for c in ("abcdefghijklmnopqrstuvwxyz"):gmatch(".") do
local k=c:upper()
local v=(k:byte()-("A"):byte()+1).."-"
t[c]=v
t[k]=v
end
s="This string'll test my system!"
s=s:gsub("%A+","/")
print(s:gsub("%a",t))Code Snippets
local t={}
for c in ("abcdefghijklmnopqrstuvwxyz"):gmatch(".") do
local k=c:upper()
local v=(k:byte()-("A"):byte()+1).."-"
t[c]=v
t[k]=v
end
s="This string'll test my system!"
s=s:gsub("%A+","/")
print(s:gsub("%a",t))Context
StackExchange Code Review Q#29471, answer score: 6
Revisions (0)
No revisions yet.