diff options
author | Thomas Fuchs <thomas@fesch.at> | 2006-08-18 09:16:29 +0000 |
---|---|---|
committer | Thomas Fuchs <thomas@fesch.at> | 2006-08-18 09:16:29 +0000 |
commit | b006317b442c3eac2c41e975372d460942c48ba2 (patch) | |
tree | 8bae0e3a96209773e3a02437cebc70be59d0222e /activesupport/lib/active_support/json/encoders | |
parent | 0da426be96d9e02b1b7a34e364dff984cd4218f3 (diff) | |
download | rails-b006317b442c3eac2c41e975372d460942c48ba2.tar.gz rails-b006317b442c3eac2c41e975372d460942c48ba2.tar.bz2 rails-b006317b442c3eac2c41e975372d460942c48ba2.zip |
Greatly increased performance of String.to_json, which speeds up RJS considerably on large pages, fixes #3473 [Shugo Maeda]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4787 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/json/encoders')
-rw-r--r-- | activesupport/lib/active_support/json/encoders/core.rb | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/activesupport/lib/active_support/json/encoders/core.rb b/activesupport/lib/active_support/json/encoders/core.rb index 97f994ac9a..483471ee3e 100644 --- a/activesupport/lib/active_support/json/encoders/core.rb +++ b/activesupport/lib/active_support/json/encoders/core.rb @@ -16,24 +16,25 @@ module ActiveSupport define_encoder NilClass do 'null' end + + ESCAPED_CHARS = { + "\010" => '\b', + "\f" => '\f', + "\n" => '\n', + "\r" => '\r', + "\t" => '\t', + '"' => '\"', + '\\' => '\\\\' + } define_encoder String do |string| - returning value = '"' do - string.each_char do |char| - value << case - when char == "\010": '\b' - when char == "\f": '\f' - when char == "\n": '\n' - when char == "\r": '\r' - when char == "\t": '\t' - when char == '"': '\"' - when char == '\\': '\\\\' - when char.length > 1: "\\u#{'%04x' % char.unpack('U').first}" - else; char - end - end - value << '"' - end + '"' + string.gsub(/[\010\f\n\r\t"\\]/) { |s| + ESCAPED_CHARS[s] + }.gsub(/([\xC0-\xDF][\x80-\xBF]| + [\xE0-\xEF][\x80-\xBF]{2}| + [\xF0-\xF7][\x80-\xBF]{3})+/ux) { |s| + s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&') + } + '"' end define_encoder Numeric do |numeric| |