1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
naruse 2008-02-12 06:18:06 +00:00
parent 38694016bc
commit 40d8d38909
25 changed files with 71 additions and 58 deletions

View file

@ -1,3 +1,17 @@
Tue Feb 12 15:11:47 2008 NARUSE, Yui <naruse@ruby-lang.org>
* 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 <matz@ruby-lang.org> Tue Feb 12 12:16:45 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_hash_cmp): lighter version of rb_str_cmp() for * string.c (rb_str_hash_cmp): lighter version of rb_str_cmp() for

View file

@ -40,7 +40,7 @@ module JSON
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
# UTF16 big endian characters as \u????, and return it. # UTF16 big endian characters as \u????, and return it.
def utf8_to_json(string) # :nodoc: def utf8_to_json(string) # :nodoc:
string = string.gsub(/["\\\/\x0-\x1f]/) { |c| MAP[c] } string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] }
string.gsub!(/( string.gsub!(/(
(?: (?:
[\xc2-\xdf][\x80-\xbf] | [\xc2-\xdf][\x80-\xbf] |

View file

@ -122,7 +122,8 @@ module JSON
def parse_string def parse_string
if scan(STRING) if scan(STRING)
return '' if self[1].empty? 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]] if u = UNESCAPE_MAP[c[1]]
u u
else # \uXXXX else # \uXXXX

View file

@ -82,7 +82,8 @@ module OpenSSL
def expand_pair(str) def expand_pair(str)
return nil unless str return nil unless str
return str.gsub(Pair){|pair| return str.gsub(Pair){
pair = $&
case pair.size case pair.size
when 2 then pair[1,1] when 2 then pair[1,1]
when 3 then Integer("0x#{pair[1,2]}").chr when 3 then Integer("0x#{pair[1,2]}").chr
@ -93,7 +94,7 @@ module OpenSSL
def expand_hexstring(str) def expand_hexstring(str)
return nil unless 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) a1 = OpenSSL::ASN1.decode(der)
return a1.value, a1.tag return a1.value, a1.tag
end end

View file

@ -230,7 +230,7 @@ class WIN32COMGen
v.visible? && v.variable_kind == 'CONSTANT' v.visible? && v.variable_kind == 'CONSTANT'
}.each do |v| }.each do |v|
io.print " " io.print " "
io.print v.name.sub(/^./){|c| c.upcase} io.print v.name.sub(/^./){$&.upcase}
io.print " = " io.print " = "
io.puts v.value io.puts v.value
end end

View file

@ -215,9 +215,9 @@ class Date
:emit_a, :emit_ad, :emit_au :emit_a, :emit_ad, :emit_au
def strftime(fmt='%F') 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 = {} f = {}
a = $& m = $&
s, w, c = $1, $2, $3 s, w, c = $1, $2, $3
if s if s
s.scan(/./) do |k| s.scan(/./) do |k|
@ -327,7 +327,7 @@ class Date
when '%'; emit_a('%', 0, f) when '%'; emit_a('%', 0, f)
when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f) when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
else else
a m
end end
end end
end end

View file

@ -233,7 +233,7 @@ module IRB
def inspect def inspect
array = [] array = []
for ivar in instance_variables.sort{|e1, e2| e1 <=> e2} for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
name = ivar.sub(/^@(.*)$/){$1} name = ivar.sub(/^@(.*)$/, '\1')
val = instance_eval(ivar) val = instance_eval(ivar)
case ivar case ivar
when *NOPRINTING_IVARS when *NOPRINTING_IVARS

View file

@ -95,7 +95,7 @@ EOF
return nil if bt =~ /irb\/.*\.rb/ return nil if bt =~ /irb\/.*\.rb/
when 3 when 3
return nil if bt =~ /irb\/.*\.rb/ return nil if bt =~ /irb\/.*\.rb/
bt.sub!(/:\s*in `irb_binding'/){""} bt.sub!(/:\s*in `irb_binding'/, '')
end end
bt bt
end end

View file

@ -1521,7 +1521,7 @@ module Net #:nodoc:
private :encode_kvpair private :encode_kvpair
def urlencode(str) 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 end
private :urlencode private :urlencode

View file

@ -857,11 +857,11 @@ module Net
# Encode a string from UTF-8 format to modified UTF-7. # Encode a string from UTF-8 format to modified UTF-7.
def self.encode_utf7(s) def self.encode_utf7(s)
return s.gsub(/(&)|([^\x20-\x25\x27-\x7e]+)/u) { |x| return s.gsub(/(&)|([^\x20-\x25\x27-\x7e]+)/u) {
if $1 if $1
"&-" "&-"
else else
base64 = [x.unpack("U*").pack("n*")].pack("m") base64 = [$&.unpack("U*").pack("n*")].pack("m")
"&" + base64.delete("=\n").tr("/", ",") + "-" "&" + base64.delete("=\n").tr("/", ",") + "-"
end end
}.force_encoding("ASCII-8BIT") }.force_encoding("ASCII-8BIT")

View file

@ -523,7 +523,7 @@ module RDoc::Generator
def http_url(full_name, prefix) def http_url(full_name, prefix)
path = full_name.dup 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" ::File.join(prefix, path.split("::")) + ".html"
end end
@ -704,8 +704,8 @@ module RDoc::Generator
end end
def filename_to_label def filename_to_label
@context.file_relative_name.gsub(/%|\/|\?|\#/) do |s| @context.file_relative_name.gsub(/%|\/|\?|\#/) do
'%%%x' % s[0].unpack('C') '%%%x' % $&[0].unpack('C')
end end
end end

View file

@ -200,14 +200,14 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
gsub(/\.\.\.\./, '.&#8230;').gsub(/\.\.\./, '&#8230;'). gsub(/\.\.\.\./, '.&#8230;').gsub(/\.\.\./, '&#8230;').
# convert single closing quote # convert single closing quote
gsub(%r{([^ \t\r\n\[\{\(])\'}) { "#$1&#8217;" }. gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1&#8217;').
gsub(%r{\'(?=\W|s\b)}) { "&#8217;" }. gsub(%r{\'(?=\W|s\b)}, '&#8217;').
# convert single opening quote # convert single opening quote
gsub(/'/, '&#8216;'). gsub(/'/, '&#8216;').
# convert double closing quote # convert double closing quote
gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}) { "#$1&#8221;" }. gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}, '\1&#8221;').
# convert double opening quote # convert double opening quote
gsub(/'/, '&#8220;'). gsub(/'/, '&#8220;').

View file

@ -227,14 +227,14 @@ class RDoc::Markup::ToLaTeX < RDoc::Markup::Formatter
gsub(/\.\.\.\./, '.\ldots{}').gsub(/\.\.\./, '\ldots{}'). gsub(/\.\.\.\./, '.\ldots{}').gsub(/\.\.\./, '\ldots{}').
# convert single closing quote # convert single closing quote
gsub(%r{([^ \t\r\n\[\{\(])\'}) { "#$1'" }. gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1\'').
gsub(%r{\'(?=\W|s\b)}) { "'" }. gsub(%r{\'(?=\W|s\b)}, "'" ).
# convert single opening quote # convert single opening quote
gsub(/'/, '`'). gsub(/'/, '`').
# convert double closing quote # convert double closing quote
gsub(%r{([^ \t\r\n\[\{\(])\"(?=\W)}) { "#$1''" }. gsub(%r{([^ \t\r\n\[\{\(])\"(?=\W)}, "\\1''").
# convert double opening quote # convert double opening quote
gsub(/"/, "``"). gsub(/"/, "``").

View file

@ -766,7 +766,7 @@ module RDoc
# Removes #ifdefs that would otherwise confuse us # Removes #ifdefs that would otherwise confuse us
def handle_ifdefs_in(body) 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
end end

View file

@ -87,22 +87,22 @@ class RDoc::RI::Formatter
# Convert HTML entities back to ASCII # Convert HTML entities back to ASCII
def conv_html(txt) def conv_html(txt)
txt. txt = txt.gsub(/&gt;/, '>')
gsub(/&gt;/, '>'). txt.gsub!(/&lt;/, '<')
gsub(/&lt;/, '<'). txt.gsub!(/&quot;/, '"')
gsub(/&quot;/, '"'). txt.gsub!(/&amp;/, '&')
gsub(/&amp;/, '&') txt
end end
## ##
# Convert markup into display form # Convert markup into display form
def conv_markup(txt) def conv_markup(txt)
txt. txt = txt.gsub(%r{<tt>(.*?)</tt>}, '+\1+')
gsub(%r{<tt>(.*?)</tt>}) { "+#$1+" } . txt.gsub!(%r{<code>(.*?)</code>}, '+\1+')
gsub(%r{<code>(.*?)</code>}) { "+#$1+" } . txt.gsub!(%r{<b>(.*?)</b>}, '*\1*')
gsub(%r{<b>(.*?)</b>}) { "*#$1*" } . txt.gsub!(%r{<em>(.*?)</em>}, '_\1_')
gsub(%r{<em>(.*?)</em>}) { "_#$1_" } txt
end end
def display_list(list) def display_list(list)
@ -548,11 +548,11 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
end end
def escape(str) def escape(str)
str. str = str.gsub(/&/n, '&amp;')
gsub(/&/n, '&amp;'). str.gsub!(/\"/n, '&quot;')
gsub(/\"/n, '&quot;'). str.gsub!(/>/n, '&gt;')
gsub(/>/n, '&gt;'). str.gsub!(/</n, '&lt;')
gsub(/</n, '&lt;') str
end end
end end

View file

@ -482,7 +482,7 @@ module REXML
rv.gsub!( /\r\n?/, "\n" ) rv.gsub!( /\r\n?/, "\n" )
matches = rv.scan( REFERENCE_RE ) matches = rv.scan( REFERENCE_RE )
return rv if matches.size == 0 return rv if matches.size == 0
rv.gsub!( /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/ ) {|m| rv.gsub!( /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/ ) {
m=$1 m=$1
m = "0#{m}" if m[0] == ?x m = "0#{m}" if m[0] == ?x
[Integer(m)].pack('U*') [Integer(m)].pack('U*')

View file

@ -170,15 +170,12 @@ module REXML
rest = path[ind+1..-1] rest = path[ind+1..-1]
# have to change 'a [=<>] b [=<>] c' into 'a [=<>] b and b [=<>] c' # have to change 'a [=<>] b [=<>] c' into 'a [=<>] b and b [=<>] c'
predicate.gsub!( /([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)/u ) { predicate.gsub!( /([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)\s*([<>=])\s*([^\s(and)(or)<>=]+)/u,
"#$1 #$2 #$3 and #$3 #$4 #$5" '\1 \2 \3 and \3 \4 \5' )
}
# Let's do some Ruby trickery to avoid some work: # Let's do some Ruby trickery to avoid some work:
predicate.gsub!( /&/u, "&&" ) predicate.gsub!( /&/u, "&&" )
predicate.gsub!( /=/u, "==" ) predicate.gsub!( /=/u, "==" )
predicate.gsub!( /@(\w[-\w.]*)/u ) { predicate.gsub!( /@(\w[-\w.]*)/u, 'attribute("\1")' )
"attribute(\"#$1\")"
}
predicate.gsub!( /\bmod\b/u, "%" ) predicate.gsub!( /\bmod\b/u, "%" )
predicate.gsub!( /\b(\w[-\w.]*\()/u ) { predicate.gsub!( /\b(\w[-\w.]*\()/u ) {
fname = $1 fname = $1

View file

@ -345,7 +345,7 @@ module REXML
copy.gsub!( SETUTITSBUS[2], SLAICEPS[2] ) copy.gsub!( SETUTITSBUS[2], SLAICEPS[2] )
copy.gsub!( SETUTITSBUS[3], SLAICEPS[3] ) copy.gsub!( SETUTITSBUS[3], SLAICEPS[3] )
copy.gsub!( SETUTITSBUS[4], SLAICEPS[4] ) copy.gsub!( SETUTITSBUS[4], SLAICEPS[4] )
copy.gsub!( /&#0*((?:\d+)|(?:x[a-f0-9]+));/ ) {|m| copy.gsub!( /&#0*((?:\d+)|(?:x[a-f0-9]+));/ ) {
m=$1 m=$1
#m='0' if m=='' #m='0' if m==''
m = "0#{m}" if m[0] == ?x m = "0#{m}" if m[0] == ?x
@ -380,7 +380,8 @@ module REXML
# Unescapes all possible entities # Unescapes all possible entities
def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil ) 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[1] == ?#
if ref[2] == ?x if ref[2] == ?x
[ref[3...-1].to_i(16)].pack('U*') [ref[3...-1].to_i(16)].pack('U*')

View file

@ -199,9 +199,7 @@ module RSS
name = (@@class_names[uri] || {})[tag_name] name = (@@class_names[uri] || {})[tag_name]
return name if name return name if name
tag_name = tag_name.gsub(/[_\-]([a-z]?)/) do tag_name.gsub!(/[_\-]([a-z]?)/){$1.upcase}
$1.upcase
end
tag_name[0, 1].upcase + tag_name[1..-1] tag_name[0, 1].upcase + tag_name[1..-1]
end end

View file

@ -285,7 +285,8 @@ module URI
# perhaps unsafe is String object # perhaps unsafe is String object
unsafe = Regexp.new("[#{Regexp.quote(unsafe)}]", false, 'N') unsafe = Regexp.new("[#{Regexp.quote(unsafe)}]", false, 'N')
end end
str.gsub(unsafe) do |us| str.gsub(unsafe) do
us = $&
tmp = '' tmp = ''
us.each_byte do |uc| us.each_byte do |uc|
tmp << sprintf('%%%02X', uc) tmp << sprintf('%%%02X', uc)

View file

@ -1105,7 +1105,7 @@ module URI
@@to_s = Kernel.instance_method(:to_s) @@to_s = Kernel.instance_method(:to_s)
def inspect def inspect
@@to_s.bind(self).call.sub!(/>\z/) {" URL:#{self}>"} @@to_s.bind(self).call.sub!(/>\z/, " URL:#{self}>")
end end
def coerce(oth) def coerce(oth)

View file

@ -169,7 +169,7 @@ module WEBrick
if @http_version.major > 0 if @http_version.major > 0
data = status_line() data = status_line()
@header.each{|key, value| @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 data << "#{tmp}: #{value}" << CRLF
} }
@cookies.each{|cookie| @cookies.each{|cookie|

View file

@ -371,7 +371,7 @@ module WEBrick
if name == ".." if name == ".."
dname = "Parent Directory" dname = "Parent Directory"
elsif name.size > 25 elsif name.size > 25
dname = name.sub(/^(.{23})(.*)/){ $1 + ".." } dname = name.sub(/^(.{23})(?:.*)/, '\1..')
else else
dname = name dname = name
end end

View file

@ -132,7 +132,7 @@ module YAML
# Folding paragraphs within a column # Folding paragraphs within a column
# #
def fold( value ) 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" ) $1 || $2 + ( $3 || "\n" )
end end
end end

View file

@ -10,8 +10,8 @@ module YAML
def YAML.escape( value, skip = "" ) def YAML.escape( value, skip = "" )
value.gsub( /\\/, "\\\\\\" ). value.gsub( /\\/, "\\\\\\" ).
gsub( /"/, "\\\"" ). gsub( /"/, "\\\"" ).
gsub( /([\x00-\x1f])/ ) do |x| gsub( /([\x00-\x1f])/ ) do
skip[x] || ESCAPES[ x.unpack("C")[0] ] skip[$&] || ESCAPES[ $&.unpack("C")[0] ]
end end
end end
@ -19,7 +19,7 @@ module YAML
# Unescape the condenses escapes # Unescape the condenses escapes
# #
def YAML.unescape( value ) 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 if $3
["#$3".hex ].pack('U*') ["#$3".hex ].pack('U*')
elsif $2 elsif $2