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:
parent
38694016bc
commit
40d8d38909
25 changed files with 71 additions and 58 deletions
14
ChangeLog
14
ChangeLog
|
@ -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>
|
||||
|
||||
* string.c (rb_str_hash_cmp): lighter version of rb_str_cmp() for
|
||||
|
|
|
@ -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] |
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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(/'/, '“').
|
||||
|
|
|
@ -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(/"/, "``").
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{<tt>(.*?)</tt>}) { "+#$1+" } .
|
||||
gsub(%r{<code>(.*?)</code>}) { "+#$1+" } .
|
||||
gsub(%r{<b>(.*?)</b>}) { "*#$1*" } .
|
||||
gsub(%r{<em>(.*?)</em>}) { "_#$1_" }
|
||||
txt = txt.gsub(%r{<tt>(.*?)</tt>}, '+\1+')
|
||||
txt.gsub!(%r{<code>(.*?)</code>}, '+\1+')
|
||||
txt.gsub!(%r{<b>(.*?)</b>}, '*\1*')
|
||||
txt.gsub!(%r{<em>(.*?)</em>}, '_\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 = str.gsub(/&/n, '&')
|
||||
str.gsub!(/\"/n, '"')
|
||||
str.gsub!(/>/n, '>')
|
||||
str.gsub!(/</n, '<')
|
||||
str
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -482,7 +482,7 @@ module REXML
|
|||
rv.gsub!( /\r\n?/, "\n" )
|
||||
matches = rv.scan( REFERENCE_RE )
|
||||
return rv if matches.size == 0
|
||||
rv.gsub!( /�*((?:\d+)|(?:x[a-fA-F0-9]+));/ ) {|m|
|
||||
rv.gsub!( /�*((?:\d+)|(?:x[a-fA-F0-9]+));/ ) {
|
||||
m=$1
|
||||
m = "0#{m}" if m[0] == ?x
|
||||
[Integer(m)].pack('U*')
|
||||
|
|
|
@ -170,15 +170,12 @@ module REXML
|
|||
rest = path[ind+1..-1]
|
||||
|
||||
# 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 ) {
|
||||
"#$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
|
||||
|
|
|
@ -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*')
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue