Changeset 6419

Show
Ignore:
Timestamp:
11/13/10 14:16:51 (3 years ago)
Author:
jow
Message:

libs/core: switch to C pcdata() implementation, its up to 7 times faster while also ensuring safe UTF-8

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/core/luasrc/util.lua

    r6410 r6419  
    3232local string = require "string" 
    3333local coroutine = require "coroutine" 
     34local tparser = require "luci.template.parser" 
    3435 
    3536local getmetatable, setmetatable = getmetatable, setmetatable 
     
    194195-- @param value String value containing the data to escape 
    195196-- @return      String value containing the escaped data 
    196 local function _pcdata_repl(c) 
    197     local i = string.byte(c) 
    198  
    199     if ( i >= 0x00 and i <= 0x08 ) or ( i >= 0x0B and i <= 0x0C ) or 
    200        ( i >= 0x0E and i <= 0x1F ) or ( i == 0x7F ) 
    201     then 
    202         return "" 
    203  
    204     elseif ( i == 0x26 ) or ( i == 0x27 ) or ( i == 0x22 ) or 
    205            ( i == 0x3C ) or ( i == 0x3E ) 
    206     then 
    207         return string.format("&#%i;", i) 
    208     end 
    209  
    210     return c 
    211 end 
    212  
    213197function pcdata(value) 
    214     return value and tostring(value):gsub("[&\"'<>%c]", _pcdata_repl) 
     198    return value and tparser.sanitize_pcdata(tostring(value)) 
    215199end 
    216200 
     
    294278    elseif type(v) == "table" then 
    295279        v = table.concat(v, " ") 
     280    elseif type(v) ~= "string" then 
     281        v = tostring(v) 
    296282    end 
    297283