Can you try to improve this function that removes comments from the lua file? Here is…
- User
Can you try to improve this function that removes comments from the lua file? Here is the function: ---@param code string ---@return string local function removeComments(code) ---@type "code"|"comment"|"string"|"mstring"|"mcomment" local state = "code" local new_code = "" for i = 1, #code do local char = code:sub(i, i) if state == "code" then if (char == "-" and code:sub(i+1, i+1) == "-") then if char == """ then state = "string" end if char == "-" and code:sub(i+1, i+1) == "-" then state = "comment" end if char == "[" and code:sub(i+1, i+1) == "[" then state = "mstring" end else new_code = new_code..char end elseif state == "comment" then if char == "\n" then state = "code" elseif char == "[" and code:sub(i+1, i+1) == "[" then state = "mcomment" end elseif state == "mcomment" then if char == "]" and code:sub(i+1, i+1) == "]" then state = "code" end elseif state == "string" or state == "mstring" then new_code = new_code..char if (char == """ and state == "string") or (char == "]" and code:sub(i-1, i-1) == "]" and state == "mstring") then state = "code" end end end return new_code end
- Assistant
Below is an improved function for
removeCommentswith more readability and fixes the mentioned issue:---@param code string ---@return string local function removeComments(code) ---@type "code"|"comment"|"string"|"mstring"|"mcomment" local state = "code" local new_code = "" for i = 1, #code do local char = code:sub(i, i) local next_char = code:sub(i + 1, i + 1) if state == "code" then if char == "\"" then state = "string" new_code = new_code..char elseif char == "-" and next_char == "-" then state = "comment" elseif char == "[" and next_char == "[" then state = "mstring" new_code = new_code..char else new_code = new_code..char end elseif state == "comment" then if char == "\n" then state = "code" elseif char == "[" and next_char == "[" then state = "mcomment" end elseif state == "mcomment" then if char == "]" and next_char == "]" then state = "code" i = i + 1 end elseif state == "string" then new_code = new_code..char if char == "\"" then state = "code" end elseif state == "mstring" then new_code = new_code..char if char == "]" and code:sub(i-1, i-1) == "]" then state = "code" end end end return new_code end - User
can you try to optimize the function by instead of appending each character to the
new_codeyou will append the substring that goes from the last switch state to the one that will happen? - Assistant
Sure, I've further optimized the
removeCommentsfunction below by appending a substring to thenew_codewhenever a state change occurs. This should increase the performance since string concatenation is an expensive operation.---@param code string ---@return string local function removeComments(code) ---@type "code"|"comment"|"string"|"mstring"|"mcomment" local state = "code" local new_code = "" local sidx = 1 for i = 1, #code do local char = code:sub(i, i) local next_char = code:sub(i + 1, i + 1) if state == "code" then if char == "\"" then new_code = new_code..code:sub(sidx, i) state = "string" sidx = i elseif char == "-" and next_char == "-" then new_code = new_code..code:sub(sidx, i - 1) state = "comment" elseif char == "[" and next_char == "[" then state = "mstring" end elseif state == "comment" then if char == "\n" then state = "code" sidx = i + 1 elseif char == "[" and next_char == "[" then state = "mcomment" end elseif state == "mcomment" then if char == "]" and next_char == "]" then state = "code" i = i + 1 sidx = i + 1 end elseif state == "string" then if char == "\"" then state = "code" sidx = i + 1 end elseif state == "mstring" then if char == "]" and code:sub(i-1, i-1) == "]" then state = "code" sidx = i + 1 end end end if sidx <= #code then new_code = new_code..code:sub(sidx) end return new_code end
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.