libs/core: optimize strip_bytecode() memory usage by avoiding string concatenation
[project/luci.git] / libs / core / luasrc / util.lua
index 10606e8..adeb99f 100644 (file)
@@ -193,16 +193,25 @@ end
 --- Create valid XML PCDATA from given string.
 -- @param value        String value containing the data to escape
 -- @return             String value containing the escaped data
-local _pcdata_repl = {
-                ["&"] = "&",
-                ['"'] = """,
-                ["'"] = "'",
-                ["<"] = "&#60;",
-                [">"] = "&#62;"
-}
+local function _pcdata_repl(c)
+       local i = string.byte(c)
+
+       if ( i >= 0x00 and i <= 0x08 ) or ( i >= 0x0B and i <= 0x0C ) or
+          ( i >= 0x0E and i <= 0x1F ) or ( i == 0x7F )
+       then
+               return ""
+               
+       elseif ( i == 0x26 ) or ( i == 0x27 ) or ( i == 0x22 ) or
+              ( i == 0x3C ) or ( i == 0x3E )
+       then
+               return string.format("&#%i;", i)
+       end
+
+       return c
+end
 
 function pcdata(value)
-       return value and tostring(value):gsub("[&\"'<>]", _pcdata_repl)
+       return value and tostring(value):gsub("[&\"'<>%c]", _pcdata_repl)
 end
 
 --- Strip HTML tags from given string.
@@ -545,10 +554,10 @@ function strip_bytecode(code)
                end
        end
 
-       local strip_function
-       strip_function = function(code)
+       local function strip_function(code)
                local count, offset = subint(code, 1, size)
-               local stripped, dirty = string.rep("\0", size), offset + count
+               local stripped = { string.rep("\0", size) }
+               local dirty = offset + count
                offset = offset + count + int * 2 + 4
                offset = offset + int + subint(code, offset, int) * ins
                count, offset = subint(code, offset, int)
@@ -566,10 +575,11 @@ function strip_bytecode(code)
                        end
                end
                count, offset = subint(code, offset, int)
-               stripped = stripped .. code:sub(dirty, offset - 1)
+               stripped[#stripped+1] = code:sub(dirty, offset - 1)
                for n = 1, count do
                        local proto, off = strip_function(code:sub(offset, -1))
-                       stripped, offset = stripped .. proto, offset + off - 1
+                       stripped[#stripped+1] = proto
+                       offset = offset + off - 1
                end
                offset = offset + subint(code, offset, int) * int + int
                count, offset = subint(code, offset, int)
@@ -580,8 +590,8 @@ function strip_bytecode(code)
                for n = 1, count do
                        offset = offset + subint(code, offset, size) + size
                end
-               stripped = stripped .. string.rep("\0", int * 3)
-               return stripped, offset
+               stripped[#stripped+1] = string.rep("\0", int * 3)
+               return table.concat(stripped), offset
        end
 
        return code:sub(1,12) .. strip_function(code:sub(13,-1))