Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4787

Show
Ignore:
Timestamp:
08/18/06 09:16:29 (2 years ago)
Author:
madrobby
Message:

Greatly increased performance of String.to_json, which speeds up RJS considerably on large pages, fixes #3473 [Shugo Maeda]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r4780 r4787  
    11*SVN* 
     2 
     3* Greatly increased performance of String.to_json, which speeds up RJS considerably on large pages, fixes #3473 [Shugo Maeda] 
    24 
    35* Detect missing_constants calls from removed modules and fail accordingly. [Nicholas Seckar] 
  • trunk/activesupport/lib/active_support/json/encoders/core.rb

    r4081 r4787  
    1717        'null' 
    1818      end 
     19 
     20      ESCAPED_CHARS = { 
     21        "\010" =>  '\b', 
     22        "\f" =>    '\f', 
     23        "\n" =>    '\n', 
     24        "\r" =>    '\r', 
     25        "\t" =>    '\t', 
     26        '"' =>     '\"', 
     27        '\\' =>    '\\\\' 
     28      } 
    1929       
    2030      define_encoder String do |string| 
    21         returning value = '"' do 
    22           string.each_char do |char| 
    23             value << case 
    24             when char == "\010":  '\b' 
    25             when char == "\f":    '\f' 
    26             when char == "\n":    '\n' 
    27             when char == "\r":    '\r' 
    28             when char == "\t":    '\t' 
    29             when char == '"':     '\"' 
    30             when char == '\\':    '\\\\'   
    31             when char.length > 1: "\\u#{'%04x' % char.unpack('U').first}" 
    32             else;                 char 
    33             end 
    34           end 
    35           value << '"' 
    36         end 
     31        '"' + string.gsub(/[\010\f\n\r\t"\\]/) { |s| 
     32          ESCAPED_CHARS[s] 
     33        }.gsub(/([\xC0-\xDF][\x80-\xBF]| 
     34                 [\xE0-\xEF][\x80-\xBF]{2}| 
     35                 [\xF0-\xF7][\x80-\xBF]{3})+/ux) { |s| 
     36          s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&') 
     37        } + '"' 
    3738      end 
    3839