The title says it all, this program will clear programs of comments, however it will not clear any multi line comments Aka –[[ Comment in Here ]] (EDIT HERE), Block Comments
Program: http://pastebin.com/2hHydSQt
Again for those who prefer spoiler tags
Spoiler
tArgs = {…}function stringSplit(sString)
local chars= {}
for i = 1, #sString do
chars = sString:sub(i, i)
end
return chars
end
function comment(line)
local letters = {}
if line == nil or line == "" then
return "empty", line
end
local letters = stringSplit(line)
if letters == nil or #letters == 0 then
return "empty", line
end
for i = 1, #letters do
if letters == "-" and letters[i+1] == "-" then
if letters[i+2] == "[" and letters[i+3] == "[" then
return "empty", line
elseif letters[i-1] == "]" and letters[i-2] == "]" then
return "empty", line
end
local clearLine = {}
for t = 1, i-1 do
table.insert(clearLine, letters[t])
end
return "clearLine", table.concat(clearLine)
end
end
return "empty", line
end
function lines(str)
local t = {}
local function helper(line) table.insert(t, line) return "" end
helper((str:gsub("(.-)\r?\n", helper)))
return t
end
function clearFile(Path, newPath)
local file = fs.open(Path, "r")
fileTotal = file.readAll()
file.close()
local fileTable = {}
local newFile = {}
local fileTable = lines(fileTotal)
for i = 1,#fileTable do
local result, Q = comment(fileTable)
if result == "empty" then
table.insert(newFile, Q)
elseif result == "clearLine" then
table.insert(newFile, Q)
end
end
local file = fs.open(newPath, "w")
for i = 1, #newFile do
file.writeLine(newFile)
end
file.close()
end
if tArgs[2] == nil then
clearFile(tArgs[1], tArgs[1])
else
clearFile(tArgs[1], tArgs[2])
end
Usage:
ClearC file1 file2 -- This will clear file1 of comments and place the cleared file in file2
OR
ClearC file1 -- This will clear file1 of comments and replace it in the same file
Also note this program does not remove cleared lines if they are emptied and there is no text before the comment, so;
function this()
print("Some text here") -- Comment Here
-- And Another Comment
end
Will turn into this:
function this()
print("Some text here")
end
Post any bugs you get when using this program and if you really want me to fix the empty line thing just say, if there's enough interest then I will try and get it to work.