local String = string.gsub( String, '^(.*)%s$', Function )
This will return if the string has a space at the end of it. Not sure how to make it execute the function, but probably involves loadstring
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
local lastTime = false
local timewhenstarted = love.timer.getTime()
local time = 0 --count the time it takes, resets after reaching sleepAfter
local checkTime = 5 --when it should stop the function
local CPUspeed = 20 --MHz, the virtual-computer
local realCPUspeed = 3 * 3200 --MHz, my computer
local sleepAfter = CPUspeed / realCPUspeed
function wait()
local microtime = love.timer.getTime()
if lastTime then
time = time + ( microtime - lastTime )
if time > sleepAfter then
love.timer.sleep( sleepAfter * ( realCPUspeed / CPUspeed - 1 ) )
microtime = microtime + sleepAfter * ( realCPUspeed / CPUspeed - 1 )
time = time - sleepAfter
end
end
lastTime = microtime
if microtime - timewhenstarted > checkTime then
if counter then
print( "loops per second: " .. math.floor( counter / checkTime ) .. "\n" )
else
print("No counter added...\n")
end
os.exit()
end
end
function transformCode(code)
local time = love.timer.getTime()
local functname = "wait()"
local commandsToCheck = {"end", "else", "elseif"}
local string = code
local parts = { }
local lastLetter = 1
local i = 1
while true do
if i > #string then
parts[#parts+1] = {text = string:sub(lastLetter, #string), text_type = "code"}
break
end
if string:sub(i, i) == '"' then
parts[#parts+1] = {text = string:sub(lastLetter, i-1), text_type = "code"}
parts[#parts+1] = {text = string:sub(i, string:find('"', i+1)), text_type = "string"}
lastLetter = string:find('"', i+1) + 1
i = lastLetter
elseif string:sub(i, i) == "'" then
parts[#parts+1] = {text = string:sub(lastLetter, i-1), text_type = "code"}
parts[#parts+1] = {text = string:sub(i, string:find("'", i+1)), text_type = "string"}
lastLetter = string:find("'", i+1) + 1
i = lastLetter
else
i = i + 1
end
end
for d,s in ipairs(parts) do
if s.text_type == "code" then
--remove all \n and tabs
s.text = s.text:gsub("\n", " ")
s.text = s.text:gsub("\t", " ")
--remove all double spaces
while true do
if not s.text:find(" ") then
break
end
s.text = s.text:gsub(" ", " ")
end
--removes all spaces between . and a letter
s.text = s.text:gsub("%. ", ".")
--finally: add wait()
for i,v in ipairs(commandsToCheck) do
s.text = s.text:gsub("([^_%w])(" .. v .. "[^_%w])", "%1 wait() %2")
end
end
end
string = ""
for d,s in ipairs(parts) do
string = string .. s.text
end
return string, math.floor( ( love.timer.getTime() - time ) * 1000 * 1000 ) / 1000
end
local original_code = "b = 'Dont take this elseif!'\na = 0\nc = { }\nc.end = 0\nc. else = 7\ntestwithendword = '56'\nwhile true do\n a = a + 1\nend"
--local original_code = "counter = 0\nwhile true do\n counter = counter + 1\nend"
local code, time = transformCode( original_code .. " " )
print("")
print("#ORIGINALCODE:")
print(original_code)
print("")
print("#CODE:")
print(code)
print("")
print("########################")
print("Need " .. time .. " ms to transform code")
print("Virtual computerspeed: " .. CPUspeed .. "MHz (" .. math.floor(CPUspeed/realCPUspeed*100) .. "% of real speed")
print("Running code for " .. checkTime .. " seconds")
print("########################")
print("")
--old printing methode
--print("\nORIGINALCODE:\n" .. original_code .. "\n\nCODE:\n" .. code .. "\n\nNeed " .. time .. " ms to transform code\n")
local func, msg = loadstring( code )
if func then
ok, msg = pcall(func)
if msg then
print("ERROR: " .. msg)
end
else
print("ERROR: " .. msg)
end
print("")
os.exit()