From 40d8d38909fe15a96887f4097d95d7323fc93fb7 Mon Sep 17 00:00:00 2001 From: naruse Date: Tue, 12 Feb 2008 06:18:06 +0000 Subject: * ext/json/lib/json/pure/generator.rb, ext/json/lib/json/pure/parser.rb, ext/openssl/lib/openssl/x509.rb, ext/win32ole/sample/olegen.rb, lib/date/format.rb, lib/irb/context.rb, lib/irb/workspace.rb, lib/net/http.rb, lib/net/imap.rb, lib/rdoc/generator.rb, lib/rdoc/markup/to_html.rb, lib/rdoc/markup/to_latex.rb, lib/rdoc/parsers/parse_c.rb, lib/rdoc/ri/formatter.rb, lib/rexml/parsers/baseparser.rb, lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rss/parser.rb, lib/uri/common.rb, lib/uri/generic.rb, lib/webrick/httpresponse.rb, lib/webrick/httpservlet/filehandler.rb, lib/yaml/baseemitter.rb, lib/yaml/encoding.rb: performance tuning arround String#gsub. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 14 ++++++++++++++ ext/json/lib/json/pure/generator.rb | 2 +- ext/json/lib/json/pure/parser.rb | 3 ++- ext/openssl/lib/openssl/x509.rb | 5 +++-- ext/win32ole/sample/olegen.rb | 2 +- lib/date/format.rb | 6 +++--- lib/irb/context.rb | 2 +- lib/irb/workspace.rb | 2 +- lib/net/http.rb | 2 +- lib/net/imap.rb | 4 ++-- lib/rdoc/generator.rb | 6 +++--- lib/rdoc/markup/to_html.rb | 6 +++--- lib/rdoc/markup/to_latex.rb | 6 +++--- lib/rdoc/parsers/parse_c.rb | 2 +- lib/rdoc/ri/formatter.rb | 30 +++++++++++++++--------------- lib/rexml/parsers/baseparser.rb | 2 +- lib/rexml/quickpath.rb | 9 +++------ lib/rexml/text.rb | 5 +++-- lib/rss/parser.rb | 4 +--- lib/uri/common.rb | 3 ++- lib/uri/generic.rb | 2 +- lib/webrick/httpresponse.rb | 2 +- lib/webrick/httpservlet/filehandler.rb | 2 +- lib/yaml/baseemitter.rb | 2 +- lib/yaml/encoding.rb | 6 +++--- 25 files changed, 71 insertions(+), 58 deletions(-) diff --git a/ChangeLog b/ChangeLog index eac72fe206..22abd0e54b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Tue Feb 12 15:11:47 2008 NARUSE, Yui + + * ext/json/lib/json/pure/generator.rb, + ext/json/lib/json/pure/parser.rb, ext/openssl/lib/openssl/x509.rb, + ext/win32ole/sample/olegen.rb, lib/date/format.rb, lib/irb/context.rb, + lib/irb/workspace.rb, lib/net/http.rb, lib/net/imap.rb, + lib/rdoc/generator.rb, lib/rdoc/markup/to_html.rb, + lib/rdoc/markup/to_latex.rb, lib/rdoc/parsers/parse_c.rb, + lib/rdoc/ri/formatter.rb, lib/rexml/parsers/baseparser.rb, + lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rss/parser.rb, + lib/uri/common.rb, lib/uri/generic.rb, lib/webrick/httpresponse.rb, + lib/webrick/httpservlet/filehandler.rb, lib/yaml/baseemitter.rb, + lib/yaml/encoding.rb: performance tuning arround String#gsub. + Tue Feb 12 12:16:45 2008 Yukihiro Matsumoto * string.c (rb_str_hash_cmp): lighter version of rb_str_cmp() for diff --git a/ext/json/lib/json/pure/generator.rb b/ext/json/lib/json/pure/generator.rb index 0fe73be41a..c8bbfd09ee 100644 --- a/ext/json/lib/json/pure/generator.rb +++ b/ext/json/lib/json/pure/generator.rb @@ -40,7 +40,7 @@ module JSON # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with # UTF16 big endian characters as \u????, and return it. def utf8_to_json(string) # :nodoc: - string = string.gsub(/["\\\/\x0-\x1f]/) { |c| MAP[c] } + string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] } string.gsub!(/( (?: [\xc2-\xdf][\x80-\xbf] | diff --git a/ext/json/lib/json/pure/parser.rb b/ext/json/lib/json/pure/parser.rb index e886ba8d2c..39bee54269 100644 --- a/ext/json/lib/json/pure/parser.rb +++ b/ext/json/lib/json/pure/parser.rb @@ -122,7 +122,8 @@ module JSON def parse_string if scan(STRING) return '' if self[1].empty? - self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c| + self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do + c = $& if u = UNESCAPE_MAP[c[1]] u else # \uXXXX diff --git a/ext/openssl/lib/openssl/x509.rb b/ext/openssl/lib/openssl/x509.rb index e711bda39c..1f81e4d5e6 100644 --- a/ext/openssl/lib/openssl/x509.rb +++ b/ext/openssl/lib/openssl/x509.rb @@ -82,7 +82,8 @@ module OpenSSL def expand_pair(str) return nil unless str - return str.gsub(Pair){|pair| + return str.gsub(Pair){ + pair = $& case pair.size when 2 then pair[1,1] when 3 then Integer("0x#{pair[1,2]}").chr @@ -93,7 +94,7 @@ module OpenSSL def expand_hexstring(str) return nil unless str - der = str.gsub(HexPair){|hex| Integer("0x#{hex}").chr } + der = str.gsub(HexPair){$&.to_i(16).chr } a1 = OpenSSL::ASN1.decode(der) return a1.value, a1.tag end diff --git a/ext/win32ole/sample/olegen.rb b/ext/win32ole/sample/olegen.rb index 6ef660326f..df6fe7adaa 100644 --- a/ext/win32ole/sample/olegen.rb +++ b/ext/win32ole/sample/olegen.rb @@ -230,7 +230,7 @@ class WIN32COMGen v.visible? && v.variable_kind == 'CONSTANT' }.each do |v| io.print " " - io.print v.name.sub(/^./){|c| c.upcase} + io.print v.name.sub(/^./){$&.upcase} io.print " = " io.puts v.value end diff --git a/lib/date/format.rb b/lib/date/format.rb index 3b152b65ba..46d2147b89 100644 --- a/lib/date/format.rb +++ b/lib/date/format.rb @@ -215,9 +215,9 @@ class Date :emit_a, :emit_ad, :emit_au def strftime(fmt='%F') - fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do |m| + fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do f = {} - a = $& + m = $& s, w, c = $1, $2, $3 if s s.scan(/./) do |k| @@ -327,7 +327,7 @@ class Date when '%'; emit_a('%', 0, f) when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f) else - a + m end end end diff --git a/lib/irb/context.rb b/lib/irb/context.rb index b6ffaacb31..6a5e77fb70 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -233,7 +233,7 @@ module IRB def inspect array = [] for ivar in instance_variables.sort{|e1, e2| e1 <=> e2} - name = ivar.sub(/^@(.*)$/){$1} + name = ivar.sub(/^@(.*)$/, '\1') val = instance_eval(ivar) case ivar when *NOPRINTING_IVARS diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 8ac44861d5..c27f7052d1 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -95,7 +95,7 @@ EOF return nil if bt =~ /irb\/.*\.rb/ when 3 return nil if bt =~ /irb\/.*\.rb/ - bt.sub!(/:\s*in `irb_binding'/){""} + bt.sub!(/:\s*in `irb_binding'/, '') end bt end diff --git a/lib/net/http.rb b/lib/net/http.rb index 06b176b3c6..d3e3d9f2f5 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -1521,7 +1521,7 @@ module Net #:nodoc: private :encode_kvpair def urlencode(str) - str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) } + str.gsub(/[^a-zA-Z0-9_\.\-]/n) { sprintf('%%%02x', $&[0]) } end private :urlencode diff --git a/lib/net/imap.rb b/lib/net/imap.rb index 394f0d9890..016f567b07 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -857,11 +857,11 @@ module Net # Encode a string from UTF-8 format to modified UTF-7. def self.encode_utf7(s) - return s.gsub(/(&)|([^\x20-\x25\x27-\x7e]+)/u) { |x| + return s.gsub(/(&)|([^\x20-\x25\x27-\x7e]+)/u) { if $1 "&-" else - base64 = [x.unpack("U*").pack("n*")].pack("m") + base64 = [$&.unpack("U*").pack("n*")].pack("m") "&" + base64.delete("=\n").tr("/", ",") + "-" end }.force_encoding("ASCII-8BIT") diff --git a/lib/rdoc/generator.rb b/lib/rdoc/generator.rb index b62cd00506..0e5b4a8cc0 100644 --- a/lib/rdoc/generator.rb +++ b/lib/rdoc/generator.rb @@ -523,7 +523,7 @@ module RDoc::Generator def http_url(full_name, prefix) path = full_name.dup - path.gsub!(/<<\s*(\w*)/) { "from-#$1" } if path['<<'] + path.gsub!(/<<\s*(\w*)/, 'from-\1') if path['<<'] ::File.join(prefix, path.split("::")) + ".html" end @@ -704,8 +704,8 @@ module RDoc::Generator end def filename_to_label - @context.file_relative_name.gsub(/%|\/|\?|\#/) do |s| - '%%%x' % s[0].unpack('C') + @context.file_relative_name.gsub(/%|\/|\?|\#/) do + '%%%x' % $&[0].unpack('C') end end diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index da42312ff5..4fe16f8e91 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -200,14 +200,14 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter gsub(/\.\.\.\./, '.…').gsub(/\.\.\./, '…'). # convert single closing quote - gsub(%r{([^ \t\r\n\[\{\(])\'}) { "#$1’" }. - gsub(%r{\'(?=\W|s\b)}) { "’" }. + gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1’'). + gsub(%r{\'(?=\W|s\b)}, '’'). # convert single opening quote gsub(/'/, '‘'). # convert double closing quote - gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}) { "#$1”" }. + gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}, '\1”'). # convert double opening quote gsub(/'/, '“'). diff --git a/lib/rdoc/markup/to_latex.rb b/lib/rdoc/markup/to_latex.rb index a679b3b06e..bbf958f2ed 100644 --- a/lib/rdoc/markup/to_latex.rb +++ b/lib/rdoc/markup/to_latex.rb @@ -227,14 +227,14 @@ class RDoc::Markup::ToLaTeX < RDoc::Markup::Formatter gsub(/\.\.\.\./, '.\ldots{}').gsub(/\.\.\./, '\ldots{}'). # convert single closing quote - gsub(%r{([^ \t\r\n\[\{\(])\'}) { "#$1'" }. - gsub(%r{\'(?=\W|s\b)}) { "'" }. + gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1\''). + gsub(%r{\'(?=\W|s\b)}, "'" ). # convert single opening quote gsub(/'/, '`'). # convert double closing quote - gsub(%r{([^ \t\r\n\[\{\(])\"(?=\W)}) { "#$1''" }. + gsub(%r{([^ \t\r\n\[\{\(])\"(?=\W)}, "\\1''"). # convert double opening quote gsub(/"/, "``"). diff --git a/lib/rdoc/parsers/parse_c.rb b/lib/rdoc/parsers/parse_c.rb index 07d65bc9d8..5771250cb9 100644 --- a/lib/rdoc/parsers/parse_c.rb +++ b/lib/rdoc/parsers/parse_c.rb @@ -766,7 +766,7 @@ module RDoc # Removes #ifdefs that would otherwise confuse us def handle_ifdefs_in(body) - body.gsub(/^#ifdef HAVE_PROTOTYPES.*?#else.*?\n(.*?)#endif.*?\n/m) { $1 } + body.gsub(/^#ifdef HAVE_PROTOTYPES.*?#else.*?\n(.*?)#endif.*?\n/m, '\1') end end diff --git a/lib/rdoc/ri/formatter.rb b/lib/rdoc/ri/formatter.rb index d88cef1e3c..df73bf5eb3 100644 --- a/lib/rdoc/ri/formatter.rb +++ b/lib/rdoc/ri/formatter.rb @@ -87,22 +87,22 @@ class RDoc::RI::Formatter # Convert HTML entities back to ASCII def conv_html(txt) - txt. - gsub(/>/, '>'). - gsub(/</, '<'). - gsub(/"/, '"'). - gsub(/&/, '&') + txt = txt.gsub(/>/, '>') + txt.gsub!(/</, '<') + txt.gsub!(/"/, '"') + txt.gsub!(/&/, '&') + txt end ## # Convert markup into display form def conv_markup(txt) - txt. - gsub(%r{(.*?)}) { "+#$1+" } . - gsub(%r{(.*?)}) { "+#$1+" } . - gsub(%r{(.*?)}) { "*#$1*" } . - gsub(%r{(.*?)}) { "_#$1_" } + txt = txt.gsub(%r{(.*?)}, '+\1+') + txt.gsub!(%r{(.*?)}, '+\1+') + txt.gsub!(%r{(.*?)}, '*\1*') + txt.gsub!(%r{(.*?)}, '_\1_') + txt end def display_list(list) @@ -548,11 +548,11 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter end def escape(str) - str. - gsub(/&/n, '&'). - gsub(/\"/n, '"'). - gsub(/>/n, '>'). - gsub(//n, '>') + str.gsub!(/] b [=<>] c' into 'a [=<>] b and b [=<>] c' - predicate.gsub!( /([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)/u ) { - "#$1 #$2 #$3 and #$3 #$4 #$5" - } + predicate.gsub!( /([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)/u, + '\1 \2 \3 and \3 \4 \5' ) # Let's do some Ruby trickery to avoid some work: predicate.gsub!( /&/u, "&&" ) predicate.gsub!( /=/u, "==" ) - predicate.gsub!( /@(\w[-\w.]*)/u ) { - "attribute(\"#$1\")" - } + predicate.gsub!( /@(\w[-\w.]*)/u, 'attribute("\1")' ) predicate.gsub!( /\bmod\b/u, "%" ) predicate.gsub!( /\b(\w[-\w.]*\()/u ) { fname = $1 diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb index 73a72ae527..8bc87dbf32 100644 --- a/lib/rexml/text.rb +++ b/lib/rexml/text.rb @@ -345,7 +345,7 @@ module REXML copy.gsub!( SETUTITSBUS[2], SLAICEPS[2] ) copy.gsub!( SETUTITSBUS[3], SLAICEPS[3] ) copy.gsub!( SETUTITSBUS[4], SLAICEPS[4] ) - copy.gsub!( /�*((?:\d+)|(?:x[a-f0-9]+));/ ) {|m| + copy.gsub!( /�*((?:\d+)|(?:x[a-f0-9]+));/ ) { m=$1 #m='0' if m=='' m = "0#{m}" if m[0] == ?x @@ -380,7 +380,8 @@ module REXML # Unescapes all possible entities def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil ) - string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) { |ref| + string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) { + ref = $& if ref[1] == ?# if ref[2] == ?x [ref[3...-1].to_i(16)].pack('U*') diff --git a/lib/rss/parser.rb b/lib/rss/parser.rb index 438c63b4ca..0cba624bcb 100644 --- a/lib/rss/parser.rb +++ b/lib/rss/parser.rb @@ -199,9 +199,7 @@ module RSS name = (@@class_names[uri] || {})[tag_name] return name if name - tag_name = tag_name.gsub(/[_\-]([a-z]?)/) do - $1.upcase - end + tag_name.gsub!(/[_\-]([a-z]?)/){$1.upcase} tag_name[0, 1].upcase + tag_name[1..-1] end diff --git a/lib/uri/common.rb b/lib/uri/common.rb index cf7d147cc4..f0d68884de 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -285,7 +285,8 @@ module URI # perhaps unsafe is String object unsafe = Regexp.new("[#{Regexp.quote(unsafe)}]", false, 'N') end - str.gsub(unsafe) do |us| + str.gsub(unsafe) do + us = $& tmp = '' us.each_byte do |uc| tmp << sprintf('%%%02X', uc) diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index d907e0b4b2..57fe0e4515 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -1105,7 +1105,7 @@ module URI @@to_s = Kernel.instance_method(:to_s) def inspect - @@to_s.bind(self).call.sub!(/>\z/) {" URL:#{self}>"} + @@to_s.bind(self).call.sub!(/>\z/, " URL:#{self}>") end def coerce(oth) diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb index b3f1abef88..5c580ffb68 100644 --- a/lib/webrick/httpresponse.rb +++ b/lib/webrick/httpresponse.rb @@ -169,7 +169,7 @@ module WEBrick if @http_version.major > 0 data = status_line() @header.each{|key, value| - tmp = key.gsub(/\bwww|^te$|\b\w/){|s| s.upcase } + tmp = key.gsub(/\bwww|^te$|\b\w/){ $&.upcase } data << "#{tmp}: #{value}" << CRLF } @cookies.each{|cookie| diff --git a/lib/webrick/httpservlet/filehandler.rb b/lib/webrick/httpservlet/filehandler.rb index ae30c436fe..c6f253e3b8 100644 --- a/lib/webrick/httpservlet/filehandler.rb +++ b/lib/webrick/httpservlet/filehandler.rb @@ -371,7 +371,7 @@ module WEBrick if name == ".." dname = "Parent Directory" elsif name.size > 25 - dname = name.sub(/^(.{23})(.*)/){ $1 + ".." } + dname = name.sub(/^(.{23})(?:.*)/, '\1..') else dname = name end diff --git a/lib/yaml/baseemitter.rb b/lib/yaml/baseemitter.rb index 1aef152749..a1992f498b 100644 --- a/lib/yaml/baseemitter.rb +++ b/lib/yaml/baseemitter.rb @@ -132,7 +132,7 @@ module YAML # Folding paragraphs within a column # def fold( value ) - value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do |s| + value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do $1 || $2 + ( $3 || "\n" ) end end diff --git a/lib/yaml/encoding.rb b/lib/yaml/encoding.rb index 37f5cfda64..57dc553606 100644 --- a/lib/yaml/encoding.rb +++ b/lib/yaml/encoding.rb @@ -10,8 +10,8 @@ module YAML def YAML.escape( value, skip = "" ) value.gsub( /\\/, "\\\\\\" ). gsub( /"/, "\\\"" ). - gsub( /([\x00-\x1f])/ ) do |x| - skip[x] || ESCAPES[ x.unpack("C")[0] ] + gsub( /([\x00-\x1f])/ ) do + skip[$&] || ESCAPES[ $&.unpack("C")[0] ] end end @@ -19,7 +19,7 @@ module YAML # Unescape the condenses escapes # def YAML.unescape( value ) - value.gsub( /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) { |x| + value.gsub( /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) { if $3 ["#$3".hex ].pack('U*') elsif $2 -- cgit v1.2.3