Skip to content

Commit

Permalink
Use normal method instead of ban ver. for String
Browse files Browse the repository at this point in the history
Ruby 2.2 or earlier only has String#force_encoding for changing
character encoding set without encode byte sequence, it changes receiver
itself. Ruby 2.3 or later has frozen_striing_literal magic comment and
String#new can take encoding keyword parameter for character encoding
set without encode byte sequence.

So implements RDoc::Encoding.change_encoding and RDoc::Comment#encode!
to disguise difference between #force_encoding and encoding keyword
parameter of #new on String.
  • Loading branch information
aycabta committed Nov 8, 2017
1 parent 3664b73 commit 4b28219
Show file tree
Hide file tree
Showing 37 changed files with 195 additions and 161 deletions.
8 changes: 4 additions & 4 deletions lib/rdoc/any_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ def param_list
if @block_params then
# If this method has explicit block parameters, remove any explicit
# &block
params.sub!(/,?\s*&\w+/, '')
params = params.sub(/,?\s*&\w+/, '')
else
params.sub!(/\&(\w+)/, '\1')
params = params.sub(/\&(\w+)/, '\1')
end

params = params.gsub(/\s+/, '').split(',').reject(&:empty?)
Expand Down Expand Up @@ -274,11 +274,11 @@ def param_seq
if @block_params then
# If this method has explicit block parameters, remove any explicit
# &block
params.sub!(/,?\s*&\w+/, '')
params = params.sub(/,?\s*&\w+/, '')

block = @block_params.tr_s("\n ", " ")
if block[0] == ?(
block.sub!(/^\(/, '').sub!(/\)/, '')
block = block.sub(/^\(/, '').sub(/\)/, '')
end
params << " { |#{block}| ... }"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/code_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def comment=(comment)
# HACK correct fix is to have #initialize create @comment
# with the correct encoding
if String === @comment and @comment.empty? then
@comment.force_encoding comment.encoding
@comment = RDoc::Encoding.change_encoding @comment, comment.encoding
end
@comment
end
Expand Down
28 changes: 19 additions & 9 deletions lib/rdoc/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class RDoc::Comment

def initialize text = nil, location = nil
@location = location
@text = text
@text = text.nil? ? nil : text.dup

@document = nil
@format = 'rdoc'
Expand Down Expand Up @@ -114,10 +114,14 @@ def extract_call_seq method

method.call_seq = seq.chomp

elsif @text.sub!(/^\s*:?call-seq:(.*?)(^\s*$|\z)/m, '') then
seq = $1
seq.gsub!(/^\s*/, '')
method.call_seq = seq
else
regexp = /^\s*:?call-seq:(.*?)(^\s*$|\z)/m
if regexp =~ @text then
@text = @text.sub(regexp, '')
seq = $1
seq.gsub!(/^\s*/, '')
method.call_seq = seq
end
end

method
Expand All @@ -133,8 +137,14 @@ def empty?
##
# HACK dubious

def force_encoding encoding
@text.force_encoding encoding
def encode! encoding
# TODO: Remove this condition after Ruby 2.2 EOL
if RUBY_VERSION < '2.3.0'
@text = @text.force_encoding encoding
else
@text = String.new @text, encoding: encoding
end
self
end

##
Expand Down Expand Up @@ -200,7 +210,7 @@ def parse
def remove_private
# Workaround for gsub encoding for Ruby 1.9.2 and earlier
empty = ''
empty.force_encoding @text.encoding
empty = RDoc::Encoding.change_encoding empty, @text.encoding

@text = @text.gsub(%r%^\s*([#*]?)--.*?^\s*(\1)\+\+\n?%m, empty)
@text = @text.sub(%r%^\s*[#*]?--.*%m, '')
Expand All @@ -216,7 +226,7 @@ def text= text
@text.nil? and @document

@document = nil
@text = text
@text = text.nil? ? nil : text.dup
end

##
Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def add_attribute attribute

if known then
known.comment = attribute.comment if known.comment.empty?
elsif registered = @methods_hash[attribute.pretty_name << '='] and
elsif registered = @methods_hash[attribute.pretty_name + '='] and
RDoc::Attr === registered then
registered.rw = 'RW'
else
Expand All @@ -249,7 +249,7 @@ def add_attribute attribute
end

if attribute.rw.index 'W' then
key = attribute.pretty_name << '='
key = attribute.pretty_name + '='
known = @methods_hash[key]

if known then
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/context/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def initialize parent, title, comment
@parent = parent
@title = title ? title.strip : title

@@sequence.succ!
@@sequence = @@sequence.succ
@sequence = @@sequence.dup

@comments = []
Expand Down
56 changes: 39 additions & 17 deletions lib/rdoc/encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ def self.read_file filename, encoding, force_transcode = false

utf8 = content.sub!(/\A\xef\xbb\xbf/, '')

RDoc::Encoding.set_encoding content
content = RDoc::Encoding.set_encoding content

begin
encoding ||= Encoding.default_external
orig_encoding = content.encoding

if not orig_encoding.ascii_compatible? then
content.encode! encoding
content = content.encode encoding
elsif utf8 then
content.force_encoding Encoding::UTF_8
content.encode! encoding
content = RDoc::Encoding.change_encoding content, Encoding::UTF_8
content = content.encode encoding
else
# assume the content is in our output encoding
content.force_encoding encoding
content = RDoc::Encoding.change_encoding content, encoding
end

unless content.valid_encoding? then
# revert and try to transcode
content.force_encoding orig_encoding
content.encode! encoding
content = RDoc::Encoding.change_encoding content, orig_encoding
content = content.encode encoding
end

unless content.valid_encoding? then
Expand All @@ -52,10 +52,11 @@ def self.read_file filename, encoding, force_transcode = false
rescue Encoding::InvalidByteSequenceError,
Encoding::UndefinedConversionError => e
if force_transcode then
content.force_encoding orig_encoding
content.encode!(encoding,
:invalid => :replace, :undef => :replace,
:replace => '?')
content = RDoc::Encoding.change_encoding content, orig_encoding
content = content.encode(encoding,
:invalid => :replace,
:undef => :replace,
:replace => '?')
return content
else
warn "unable to convert #{e.message} for #{filename}, skipping"
Expand All @@ -77,15 +78,17 @@ def self.remove_frozen_string_literal string
first_line = $1

if first_line =~ /\A# +frozen[-_]string[-_]literal[=:].+$/i
string.sub! first_line, ''
string = string.sub first_line, ''
end

string
end

##
# Sets the encoding of +string+ based on the magic comment

def self.set_encoding string
remove_frozen_string_literal string
string = remove_frozen_string_literal string

string =~ /\A(?:#!.*\n)?(.*\n)/

Expand All @@ -94,15 +97,34 @@ def self.set_encoding string
name = case first_line
when /^<\?xml[^?]*encoding=(["'])(.*?)\1/ then $2
when /\b(?:en)?coding[=:]\s*([^\s;]+)/i then $1
else return
else return string
end

string.sub! first_line, ''
string = string.sub first_line, ''

remove_frozen_string_literal string
string = remove_frozen_string_literal string

enc = Encoding.find name
string.force_encoding enc if enc
string = RDoc::Encoding.change_encoding string, enc if enc

string
end

##
# Changes encoding based on +encoding+ without converting and returns new
# string

def self.change_encoding text, encoding
if text.kind_of? RDoc::Comment
text.encode! encoding
else
# TODO: Remove this condition after Ruby 2.2 EOL
if RUBY_VERSION < '2.3.0'
text.force_encoding encoding
else
String.new text, encoding: encoding
end
end
end

end
4 changes: 2 additions & 2 deletions lib/rdoc/generator/pot/po.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def add entry
def to_s
po = ''
sort_entries.each do |entry|
po << "\n" unless po.empty?
po << entry.to_s
po += "\n" unless po.empty?
po += entry.to_s
end
po
end
Expand Down
20 changes: 10 additions & 10 deletions lib/rdoc/generator/pot/po_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def initialize msgid, options = {}

def to_s
entry = ''
entry << format_translator_comment
entry << format_extracted_comment
entry << format_references
entry << format_flags
entry << <<-ENTRY
entry += format_translator_comment
entry += format_extracted_comment
entry += format_references
entry += format_flags
entry += <<-ENTRY
msgid #{format_message(@msgid)}
msgstr #{format_message(@msgstr)}
ENTRY
Expand Down Expand Up @@ -75,9 +75,9 @@ def format_comment mark, comment

formatted_comment = ''
comment.each_line do |line|
formatted_comment << "#{mark} #{line}"
formatted_comment += "#{mark} #{line}"
end
formatted_comment << "\n" unless formatted_comment.end_with?("\n")
formatted_comment += "\n" unless formatted_comment.end_with?("\n")
formatted_comment
end

Expand All @@ -94,7 +94,7 @@ def format_references

formatted_references = ''
@references.sort.each do |file, line|
formatted_references << "\#: #{file}:#{line}\n"
formatted_references += "\#: #{file}:#{line}\n"
end
formatted_references
end
Expand All @@ -111,8 +111,8 @@ def format_message message

formatted_message = '""'
message.each_line do |line|
formatted_message << "\n"
formatted_message << "\"#{escape(line)}\""
formatted_message += "\n"
formatted_message += "\"#{escape(line)}\""
end
formatted_message
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rdoc/i18n/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def translate(locale)
parse do |part|
case part[:type]
when :paragraph
translated_text << locale.translate(part[:paragraph])
translated_text += locale.translate(part[:paragraph])
when :empty_line
translated_text << part[:line]
translated_text += part[:line]
else
raise "should not reach here: unexpected type: #{type}"
end
Expand All @@ -69,14 +69,14 @@ def parse(&block)
if paragraph.empty?
emit_empty_line_event(line, line_no, &block)
else
paragraph << line
paragraph += line
emit_paragraph_event(paragraph, paragraph_start_line, line_no,
&block)
paragraph = ''
end
else
paragraph_start_line = line_no if paragraph.empty?
paragraph << line
paragraph += line
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/markup/attribute_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def add_special pattern, name
# Processes +str+ converting attributes, HTML and specials

def flow str
@str = str
@str = str.dup

mask_protected_sequences

Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/markup/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ def build_verbatim margin

min_indent = nil
generate_leading_spaces = true
line = ''
line = ''.dup

until @tokens.empty? do
type, data, column, = get

if type == :NEWLINE then
line << data
verbatim << line
line = ''
line = ''.dup
generate_leading_spaces = true
next
end
Expand Down
10 changes: 7 additions & 3 deletions lib/rdoc/markup/pre_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def handle text, code_object = nil, &block
# regexp helper (square brackets for optional)
# $1 $2 $3 $4 $5
# [prefix][\]:directive:[spaces][param]newline
text.gsub!(/^([ \t]*(?:#|\/?\*)?[ \t]*)(\\?):(\w+):([ \t]*)(.+)?(\r?\n|$)/) do
text = text.gsub(/^([ \t]*(?:#|\/?\*)?[ \t]*)(\\?):(\w+):([ \t]*)(.+)?(\r?\n|$)/) do
# skip something like ':toto::'
next $& if $4.empty? and $5 and $5[0, 1] == ':'

Expand All @@ -123,7 +123,11 @@ def handle text, code_object = nil, &block
handle_directive $1, $3, $5, code_object, text.encoding, &block
end

comment = text unless comment
if comment then
comment.text = text
else
comment = text
end

self.class.post_processors.each do |handler|
handler.call comment, code_object
Expand Down Expand Up @@ -212,7 +216,7 @@ def handle_directive prefix, directive, param, code_object = nil,
when 'yield', 'yields' then
return blankline unless code_object
# remove parameter &block
code_object.params.sub!(/,?\s*&\w+/, '') if code_object.params
code_object.params = code_object.params.sub(/,?\s*&\w+/, '') if code_object.params

code_object.block_params = param

Expand Down
3 changes: 2 additions & 1 deletion lib/rdoc/markup/to_joined_paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def accept_paragraph paragraph
paragraph.parts.each do |part|
if String === part then
if string then
string << part
string += part
parts[-1] = string
else
parts << part
string = part
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/method_attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def find_method_or_attribute name # :nodoc:
next if String === ancestor
next if parent == ancestor

other = ancestor.find_method_named('#' << name) ||
other = ancestor.find_method_named('#' + name) ||
ancestor.find_attribute_named(name)

return other if other
Expand Down
Loading

0 comments on commit 4b28219

Please sign in to comment.