From 4b28219e42563f7f906247faf96a049e962292dc Mon Sep 17 00:00:00 2001 From: Code Ass Date: Sun, 5 Nov 2017 00:16:05 +0900 Subject: [PATCH] Use normal method instead of ban ver. for String 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. --- lib/rdoc/any_method.rb | 8 ++-- lib/rdoc/code_object.rb | 2 +- lib/rdoc/comment.rb | 28 +++++++---- lib/rdoc/context.rb | 4 +- lib/rdoc/context/section.rb | 2 +- lib/rdoc/encoding.rb | 56 +++++++++++++++------- lib/rdoc/generator/pot/po.rb | 4 +- lib/rdoc/generator/pot/po_entry.rb | 20 ++++---- lib/rdoc/i18n/text.rb | 8 ++-- lib/rdoc/markup/attribute_manager.rb | 2 +- lib/rdoc/markup/parser.rb | 4 +- lib/rdoc/markup/pre_process.rb | 10 ++-- lib/rdoc/markup/to_joined_paragraph.rb | 3 +- lib/rdoc/method_attr.rb | 2 +- lib/rdoc/normal_class.rb | 4 +- lib/rdoc/options.rb | 10 ++-- lib/rdoc/parser/c.rb | 10 ++-- lib/rdoc/parser/changelog.rb | 8 ++-- lib/rdoc/parser/ruby.rb | 42 ++++++++-------- lib/rdoc/parser/simple.rb | 4 +- lib/rdoc/rd/inline.rb | 8 ++-- lib/rdoc/servlet.rb | 2 +- lib/rdoc/stats/normal.rb | 2 +- lib/rdoc/test_case.rb | 5 +- lib/rdoc/text.rb | 12 ++--- lib/rdoc/tom_doc.rb | 2 +- test/test_rdoc_code_object.rb | 4 +- test/test_rdoc_comment.rb | 8 ++-- test/test_rdoc_encoding.rb | 30 ++++++------ test/test_rdoc_generator_json_index.rb | 4 +- test/test_rdoc_markup_attribute_manager.rb | 12 ++--- test/test_rdoc_markup_formatter.rb | 2 +- test/test_rdoc_markup_pre_process.rb | 12 ++--- test/test_rdoc_markup_to_html_snippet.rb | 2 +- test/test_rdoc_parser_changelog.rb | 4 +- test/test_rdoc_text.rb | 12 ++--- test/test_rdoc_tom_doc.rb | 4 +- 37 files changed, 195 insertions(+), 161 deletions(-) diff --git a/lib/rdoc/any_method.rb b/lib/rdoc/any_method.rb index e61f263cdc..9b0d309653 100644 --- a/lib/rdoc/any_method.rb +++ b/lib/rdoc/any_method.rb @@ -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?) @@ -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 diff --git a/lib/rdoc/code_object.rb b/lib/rdoc/code_object.rb index 48a6761b87..aeb4b4762e 100644 --- a/lib/rdoc/code_object.rb +++ b/lib/rdoc/code_object.rb @@ -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 diff --git a/lib/rdoc/comment.rb b/lib/rdoc/comment.rb index f8482e2754..134f6440a0 100644 --- a/lib/rdoc/comment.rb +++ b/lib/rdoc/comment.rb @@ -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' @@ -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 @@ -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 ## @@ -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, '') @@ -216,7 +226,7 @@ def text= text @text.nil? and @document @document = nil - @text = text + @text = text.nil? ? nil : text.dup end ## diff --git a/lib/rdoc/context.rb b/lib/rdoc/context.rb index 52f01be809..58b1c54269 100644 --- a/lib/rdoc/context.rb +++ b/lib/rdoc/context.rb @@ -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 @@ -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 diff --git a/lib/rdoc/context/section.rb b/lib/rdoc/context/section.rb index 64877f944c..11f9ceaf87 100644 --- a/lib/rdoc/context/section.rb +++ b/lib/rdoc/context/section.rb @@ -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 = [] diff --git a/lib/rdoc/encoding.rb b/lib/rdoc/encoding.rb index 150865b623..54ecd89816 100644 --- a/lib/rdoc/encoding.rb +++ b/lib/rdoc/encoding.rb @@ -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 @@ -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" @@ -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)/ @@ -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 diff --git a/lib/rdoc/generator/pot/po.rb b/lib/rdoc/generator/pot/po.rb index 8cb61b8a1c..37d45e5258 100644 --- a/lib/rdoc/generator/pot/po.rb +++ b/lib/rdoc/generator/pot/po.rb @@ -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 diff --git a/lib/rdoc/generator/pot/po_entry.rb b/lib/rdoc/generator/pot/po_entry.rb index d537cb85e3..3c278826f4 100644 --- a/lib/rdoc/generator/pot/po_entry.rb +++ b/lib/rdoc/generator/pot/po_entry.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/rdoc/i18n/text.rb b/lib/rdoc/i18n/text.rb index 517abf1540..7ea6664442 100644 --- a/lib/rdoc/i18n/text.rb +++ b/lib/rdoc/i18n/text.rb @@ -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 @@ -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 diff --git a/lib/rdoc/markup/attribute_manager.rb b/lib/rdoc/markup/attribute_manager.rb index efa2f68387..a10f731615 100644 --- a/lib/rdoc/markup/attribute_manager.rb +++ b/lib/rdoc/markup/attribute_manager.rb @@ -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 diff --git a/lib/rdoc/markup/parser.rb b/lib/rdoc/markup/parser.rb index 9e6505da9d..f08587e676 100644 --- a/lib/rdoc/markup/parser.rb +++ b/lib/rdoc/markup/parser.rb @@ -249,7 +249,7 @@ def build_verbatim margin min_indent = nil generate_leading_spaces = true - line = '' + line = ''.dup until @tokens.empty? do type, data, column, = get @@ -257,7 +257,7 @@ def build_verbatim margin if type == :NEWLINE then line << data verbatim << line - line = '' + line = ''.dup generate_leading_spaces = true next end diff --git a/lib/rdoc/markup/pre_process.rb b/lib/rdoc/markup/pre_process.rb index c3c51b765c..0ac7a41934 100644 --- a/lib/rdoc/markup/pre_process.rb +++ b/lib/rdoc/markup/pre_process.rb @@ -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] == ':' @@ -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 @@ -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 diff --git a/lib/rdoc/markup/to_joined_paragraph.rb b/lib/rdoc/markup/to_joined_paragraph.rb index 5b63e43cf4..a34969daa6 100644 --- a/lib/rdoc/markup/to_joined_paragraph.rb +++ b/lib/rdoc/markup/to_joined_paragraph.rb @@ -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 diff --git a/lib/rdoc/method_attr.rb b/lib/rdoc/method_attr.rb index a1625a8872..3cef78c4a5 100644 --- a/lib/rdoc/method_attr.rb +++ b/lib/rdoc/method_attr.rb @@ -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 diff --git a/lib/rdoc/normal_class.rb b/lib/rdoc/normal_class.rb index 9278011778..6729b18448 100644 --- a/lib/rdoc/normal_class.rb +++ b/lib/rdoc/normal_class.rb @@ -47,9 +47,9 @@ def inspect # :nodoc: def to_s # :nodoc: display = "#{self.class.name} #{self.full_name}" if superclass - display << ' < ' << (superclass.is_a?(String) ? superclass : superclass.full_name) + display += ' < ' + (superclass.is_a?(String) ? superclass : superclass.full_name) end - display << ' -> ' << is_alias_for.to_s if is_alias_for + display += ' -> ' + is_alias_for.to_s if is_alias_for display end diff --git a/lib/rdoc/options.rb b/lib/rdoc/options.rb index e4fe4f75e9..17bbca81fe 100644 --- a/lib/rdoc/options.rb +++ b/lib/rdoc/options.rb @@ -624,16 +624,16 @@ def parse argv end parsers.sort.each do |parser, regexp| - opt.banner << " - #{parser}: #{regexp.join ', '}\n" + opt.banner += " - #{parser}: #{regexp.join ', '}\n" end - opt.banner << " - TomDoc: Only in ruby files\n" + opt.banner += " - TomDoc: Only in ruby files\n" - opt.banner << "\n The following options are deprecated:\n\n" + opt.banner += "\n The following options are deprecated:\n\n" name_length = DEPRECATED.keys.sort_by { |k| k.length }.last.length DEPRECATED.sort_by { |k,| k }.each do |name, reason| - opt.banner << " %*1$2$s %3$s\n" % [-name_length, name, reason] + opt.banner += " %*1$2$s %3$s\n" % [-name_length, name, reason] end opt.accept Template do |template| @@ -1087,7 +1087,7 @@ def parse argv unless quiet then deprecated.each do |opt| - $stderr.puts 'option ' << opt << ' is deprecated: ' << DEPRECATED[opt] + $stderr.puts 'option ' + opt + ' is deprecated: ' + DEPRECATED[opt] end end diff --git a/lib/rdoc/parser/c.rb b/lib/rdoc/parser/c.rb index 10061bfc7d..183538d54b 100644 --- a/lib/rdoc/parser/c.rb +++ b/lib/rdoc/parser/c.rb @@ -865,8 +865,8 @@ def find_override_comment class_name, meth_obj def handle_attr(var_name, attr_name, read, write) rw = '' - rw << 'R' if '1' == read - rw << 'W' if '1' == write + rw += 'R' if '1' == read + rw += 'W' if '1' == write class_name = @known_classes[var_name] @@ -982,8 +982,8 @@ def handle_constants(type, var_name, const_name, definition) if new_definition.empty? then # Default to literal C definition new_definition = definition else - new_definition.gsub!("\:", ":") - new_definition.gsub!("\\", '\\') + new_definition = new_definition.gsub("\:", ":") + new_definition = new_definition.gsub("\\", '\\') end new_definition.sub!(/\A(\s+)/, '') @@ -1237,7 +1237,7 @@ def rb_scan_args method_body # when scanning for classes and methods def remove_commented_out_lines - @content.gsub!(%r%//.*rb_define_%, '//') + @content = @content.gsub(%r%//.*rb_define_%, '//') end ## diff --git a/lib/rdoc/parser/changelog.rb b/lib/rdoc/parser/changelog.rb index f460321d3a..167892f543 100644 --- a/lib/rdoc/parser/changelog.rb +++ b/lib/rdoc/parser/changelog.rb @@ -29,13 +29,13 @@ def continue_entry_body entry_body, continuation if last =~ /\)\s*\z/ and continuation =~ /\A\(/ then last.sub!(/\)\s*\z/, ',') - continuation.sub!(/\A\(/, '') + continuation = continuation.sub(/\A\(/, '') end if last =~ /\s\z/ then last << continuation else - last << ' ' << continuation + last << ' ' + continuation end end @@ -162,12 +162,12 @@ def parse_entries entry_body = [] when /^(\t| {8})?\*\s*(.*)/ then # "\t* file.c (func): ..." - entry_body << $2 + entry_body << $2.dup when /^(\t| {8})?\s*(\(.*)/ then # "\t(func): ..." entry = $2 if entry_body.last =~ /:/ then - entry_body << entry + entry_body << entry.dup else continue_entry_body entry_body, entry end diff --git a/lib/rdoc/parser/ruby.rb b/lib/rdoc/parser/ruby.rb index 301b25a295..45934f485f 100644 --- a/lib/rdoc/parser/ruby.rb +++ b/lib/rdoc/parser/ruby.rb @@ -239,8 +239,8 @@ def get_visibility_information tk, single # :nodoc: def collect_first_comment skip_tkspace - comment = '' - comment.force_encoding @encoding if @encoding + comment = ''.dup + comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding first_line = true first_comment_tk_kind = nil @@ -342,7 +342,7 @@ def get_bool def get_class_or_module container, ignore_constants = false skip_tkspace name_t = get_tk - given_name = '' + given_name = ''.dup # class ::A -> A is in the top level if :on_op == name_t[:kind] and '::' == name_t[:text] then # bug @@ -379,7 +379,7 @@ def get_class_or_module container, ignore_constants = false if prev_container == container and !ignore_constants given_name = name_t[:text] else - given_name << '::' << name_t[:text] + given_name << '::' + name_t[:text] end end @@ -595,7 +595,7 @@ def look_for_directives_in container, comment # Adds useful info about the parser to +message+ def make_message message - prefix = "#{@file_name}:" + prefix = "#{@file_name}:".dup tk = peek_tk prefix << "#{tk[:line_no]}:#{tk[:char_no]}:" if tk @@ -914,7 +914,7 @@ def parse_constant container, tk, comment, ignore_constants = false return unless body - value.replace body + con.value = body record_location con con.line = line_no read_documentation_modifiers con, RDoc::CONSTANT_MODIFIERS @@ -929,7 +929,7 @@ def parse_constant container, tk, comment, ignore_constants = false def parse_constant_body container, constant, is_array_or_hash # :nodoc: nest = 0 - rhs_name = '' + rhs_name = ''.dup get_tkread @@ -991,14 +991,13 @@ def parse_comment container, tk, comment column = tk[:char_no] line_no = tk[:line_no] - text = comment.text - - singleton = !!text.sub!(/(^# +:?)(singleton-)(method:)/, '\1\3') + comment.text = comment.text.sub(/(^# +:?)(singleton-)(method:)/, '\1\3') + singleton = !!$~ co = - if text.sub!(/^# +:?method: *(\S*).*?\n/i, '') then - parse_comment_ghost container, text, $1, column, line_no, comment - elsif text.sub!(/# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i, '') then + if (comment.text = comment.text.sub(/^# +:?method: *(\S*).*?\n/i, '')) && !!$~ then + parse_comment_ghost container, comment.text, $1, column, line_no, comment + elsif (comment.text = comment.text.sub(/# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i, '')) && !!$~ then parse_comment_attr container, $1, $3, comment end @@ -1195,7 +1194,9 @@ def parse_meta_attr(context, single, tk, comment) tmp = RDoc::CodeObject.new read_documentation_modifiers tmp, RDoc::ATTR_MODIFIERS - if comment.text.sub!(/^# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i, '') then + regexp = /^# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i + if regexp =~ comment.text then + comment.text = comment.text.sub(regexp, '') rw = case $1 when 'attr_reader' then 'R' when 'attr_writer' then 'W' @@ -1228,7 +1229,8 @@ def parse_meta_method(container, single, tk, comment) skip_tkspace false - singleton = !!comment.text.sub!(/(^# +:?)(singleton-)(method:)/, '\1\3') + comment.text = comment.text.sub(/(^# +:?)(singleton-)(method:)/, '\1\3') + singleton = !!$~ name = parse_meta_method_name comment, tk @@ -1645,7 +1647,7 @@ def parse_rescue def parse_statements(container, single = NORMAL, current_method = nil, comment = new_comment('')) raise 'no' unless RDoc::Comment === comment - comment.force_encoding @encoding if @encoding + comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding nest = 1 save_visibility = container.visibility @@ -1688,12 +1690,12 @@ def parse_statements(container, single = NORMAL, current_method = nil, comment.empty? comment = '' - comment.force_encoding @encoding if @encoding + comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding end while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do - comment << tk[:text] - comment << "\n" unless "\n" == tk[:text].chars.to_a.last + comment += tk[:text] + comment += "\n" unless "\n" == tk[:text].chars.to_a.last if tk[:text].size > 1 && "\n" == tk[:text].chars.to_a.last then skip_tkspace false # leading spaces @@ -1812,7 +1814,7 @@ def parse_statements(container, single = NORMAL, current_method = nil, unless keep_comment then comment = new_comment '' - comment.force_encoding @encoding if @encoding + comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding container.params = nil container.block_params = nil end diff --git a/lib/rdoc/parser/simple.rb b/lib/rdoc/parser/simple.rb index bef6da177f..b1dabad0f8 100644 --- a/lib/rdoc/parser/simple.rb +++ b/lib/rdoc/parser/simple.rb @@ -19,7 +19,7 @@ def initialize(top_level, file_name, content, options, stats) preprocess = RDoc::Markup::PreProcess.new @file_name, @options.rdoc_include - preprocess.handle @content, @top_level + @content = preprocess.handle @content, @top_level end ## @@ -52,7 +52,7 @@ def remove_coding_comment text def remove_private_comment comment # Workaround for gsub encoding for Ruby 1.9.2 and earlier empty = '' - empty.force_encoding comment.encoding + empty = RDoc::Encoding.change_encoding empty, comment.encoding comment = comment.gsub(%r%^--\n.*?^\+\+\n?%m, empty) comment.sub(%r%^--\n.*%m, empty) diff --git a/lib/rdoc/rd/inline.rb b/lib/rdoc/rd/inline.rb index cf37ed9f05..e5cb545728 100644 --- a/lib/rdoc/rd/inline.rb +++ b/lib/rdoc/rd/inline.rb @@ -50,11 +50,11 @@ def == other # :nodoc: def append more case more when String then - @reference << more - @rdoc << more + @reference += more + @rdoc += more when RDoc::RD::Inline then - @reference << more.reference - @rdoc << more.rdoc + @reference += more.reference + @rdoc += more.rdoc else raise "unknown thingy #{more}" end diff --git a/lib/rdoc/servlet.rb b/lib/rdoc/servlet.rb index 89f26553ac..f2d6dd5adc 100644 --- a/lib/rdoc/servlet.rb +++ b/lib/rdoc/servlet.rb @@ -111,7 +111,7 @@ def asset generator_name, req, res # GET request entry point. Fills in +res+ for the path, etc. in +req+. def do_GET req, res - req.path.sub!(/^#{Regexp.escape @mount_path}/o, '') if @mount_path + req.path = req.path.sub(/^#{Regexp.escape @mount_path}/o, '') if @mount_path case req.path when '/' then diff --git a/lib/rdoc/stats/normal.rb b/lib/rdoc/stats/normal.rb index 9ab878e7e8..a3a6ff377e 100644 --- a/lib/rdoc/stats/normal.rb +++ b/lib/rdoc/stats/normal.rb @@ -42,7 +42,7 @@ def print_file files_so_far, filename if $stdout.tty? # Clean the line with whitespaces so that leftover output from the # previous line doesn't show up. - $stdout.print("\r" << (" " * @last_width) << ("\b" * @last_width) << "\r") if @last_width && @last_width > 0 + $stdout.print("\r" + (" " * @last_width) + ("\b" * @last_width) + "\r") if @last_width && @last_width > 0 @last_width = line.size $stdout.print("#{line}\r") else diff --git a/lib/rdoc/test_case.rb b/lib/rdoc/test_case.rb index 501f5c3b8b..22d3f14219 100644 --- a/lib/rdoc/test_case.rb +++ b/lib/rdoc/test_case.rb @@ -139,9 +139,8 @@ def list type = nil, *items # Enables pretty-print output def mu_pp obj # :nodoc: - s = '' - s = PP.pp obj, s - s = s.force_encoding Encoding.default_external + s = obj.pretty_inspect + s = RDoc::Encoding.change_encoding s, Encoding.default_external s.chomp end diff --git a/lib/rdoc/text.rb b/lib/rdoc/text.rb index 2890d1901f..7e714be0ad 100644 --- a/lib/rdoc/text.rb +++ b/lib/rdoc/text.rb @@ -60,7 +60,7 @@ def expand_tabs text text.each_line do |line| nil while line.gsub!(/(?:\G|\r)((?:.{8})*?)([^\t\r\n]{0,7})\t/) do r = "#{$1}#{$2}#{' ' * (8 - $2.size)}" - r.force_encoding text.encoding + r = RDoc::Encoding.change_encoding r, text.encoding r end @@ -82,7 +82,7 @@ def flush_left text end empty = '' - empty.force_encoding text.encoding + empty = RDoc::Encoding.change_encoding empty, text.encoding text.gsub(/^ {0,#{indent}}/, empty) end @@ -149,7 +149,7 @@ def strip_hashes text return text if text =~ /^(?>\s*)[^\#]/ empty = '' - empty.force_encoding text.encoding + empty = RDoc::Encoding.change_encoding empty, text.encoding text.gsub(/^\s*(#+)/) { $1.tr '#', ' ' }.gsub(/^\s+$/, empty) end @@ -172,14 +172,14 @@ def strip_stars text text = text.gsub %r%Document-method:\s+[\w:.#=!?]+%, '' space = ' ' - space.force_encoding encoding if encoding + space = RDoc::Encoding.change_encoding space, encoding if encoding text.sub! %r%/\*+% do space * $&.length end text.sub! %r%\*+/% do space * $&.length end text.gsub! %r%^[ \t]*\*%m do space * $&.length end empty = '' - empty.force_encoding encoding if encoding + empty = RDoc::Encoding.change_encoding empty, encoding if encoding text.gsub(/^\s+$/, empty) end @@ -188,7 +188,7 @@ def strip_stars text # trademark symbols in +text+ to properly encoded characters. def to_html text - html = ''.encode text.encoding + html = (''.encode text.encoding).dup encoded = RDoc::Text::TO_HTML_CHARACTERS[text.encoding] diff --git a/lib/rdoc/tom_doc.rb b/lib/rdoc/tom_doc.rb index 66a5ae7a33..2b594b7d84 100644 --- a/lib/rdoc/tom_doc.rb +++ b/lib/rdoc/tom_doc.rb @@ -222,7 +222,7 @@ def parse_text parent, indent # :nodoc: # Returns self. def tokenize text - text.sub!(/\A(Public|Internal|Deprecated):\s+/, '') + text = text.sub(/\A(Public|Internal|Deprecated):\s+/, '') setup_scanner text diff --git a/test/test_rdoc_code_object.rb b/test/test_rdoc_code_object.rb index bdf6d8ae85..d189ac1c4b 100644 --- a/test/test_rdoc_code_object.rb +++ b/test/test_rdoc_code_object.rb @@ -53,7 +53,7 @@ def test_comment_equals_encoding refute_equal Encoding::UTF_8, ''.encoding, 'Encoding sanity check' input = 'text' - input.force_encoding Encoding::UTF_8 + input = RDoc::Encoding.change_encoding input, Encoding::UTF_8 @co.comment = input @@ -65,7 +65,7 @@ def test_comment_equals_encoding_blank refute_equal Encoding::UTF_8, ''.encoding, 'Encoding sanity check' input = '' - input.force_encoding Encoding::UTF_8 + input = RDoc::Encoding.change_encoding input, Encoding::UTF_8 @co.comment = input diff --git a/test/test_rdoc_comment.rb b/test/test_rdoc_comment.rb index 9eba298017..567daae51c 100644 --- a/test/test_rdoc_comment.rb +++ b/test/test_rdoc_comment.rb @@ -207,7 +207,7 @@ def test_extract_call_seq_c_separator end def test_force_encoding - @comment.force_encoding Encoding::UTF_8 + @comment = RDoc::Encoding.change_encoding @comment, Encoding::UTF_8 assert_equal Encoding::UTF_8, @comment.text.encoding end @@ -347,7 +347,7 @@ def test_remove_private_encoding # this is private EOS - comment.force_encoding Encoding::IBM437 + comment = RDoc::Encoding.change_encoding comment, Encoding::IBM437 comment.remove_private @@ -471,7 +471,7 @@ def test_remove_private_toggle_encoding # This is text again. EOS - comment.force_encoding Encoding::IBM437 + comment = RDoc::Encoding.change_encoding comment, Encoding::IBM437 comment.remove_private @@ -486,7 +486,7 @@ def test_remove_private_toggle_encoding_ruby_bug? # This is text again. EOS - comment.force_encoding Encoding::IBM437 + comment = RDoc::Encoding.change_encoding comment, Encoding::IBM437 comment.remove_private diff --git a/test/test_rdoc_encoding.rb b/test/test_rdoc_encoding.rb index d9298880d7..9d9ded4bbd 100644 --- a/test/test_rdoc_encoding.rb +++ b/test/test_rdoc_encoding.rb @@ -37,7 +37,7 @@ def test_class_read_file_encoding def test_class_read_file_encoding_convert content = "" - content.encode! 'ISO-8859-1' + content = RDoc::Encoding.change_encoding content, 'ISO-8859-1' content << "# coding: ISO-8859-1\nhi \xE9verybody" @tempfile.write content @@ -65,7 +65,7 @@ def test_class_read_file_encoding_fail def test_class_read_file_encoding_fancy expected = "# -*- coding: utf-8; fill-column: 74 -*-\nhi everybody" - expected.encode! Encoding::UTF_8 + exptected = RDoc::Encoding.change_encoding expected, Encoding::UTF_8 @tempfile.write expected @tempfile.flush @@ -125,7 +125,7 @@ def test_class_read_file_encoding_iso_2022_jp contents = RDoc::Encoding.read_file @tempfile.path, Encoding::UTF_8 expected = ":\xe3\x82\xb3\xe3\x83\x9e\xe3\x83\xb3\xe3\x83\x89:" - expected.force_encoding Encoding::UTF_8 + expected = RDoc::Encoding.change_encoding expected, Encoding::UTF_8 assert_equal expected, contents assert_equal Encoding::UTF_8, contents.encoding @@ -133,24 +133,24 @@ def test_class_read_file_encoding_iso_2022_jp def test_class_set_encoding s = "# coding: UTF-8\n" - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s # sanity check for 1.8 assert_equal Encoding::UTF_8, s.encoding s = "#!/bin/ruby\n# coding: UTF-8\n" - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal Encoding::UTF_8, s.encoding s = "\n" - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal Encoding::UTF_8, s.encoding s = "\n" - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal Encoding::UTF_8, s.encoding end @@ -158,13 +158,13 @@ def test_class_set_encoding def test_class_set_encoding_strip s = "# coding: UTF-8\n# more comments" - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal "# more comments", s s = "#!/bin/ruby\n# coding: UTF-8\n# more comments" - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal "#!/bin/ruby\n# more comments", s end @@ -172,29 +172,29 @@ def test_class_set_encoding_strip def test_class_set_encoding_bad s = "" expected = s.encoding - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal expected, s.encoding s = "# vim:set fileencoding=utf-8:\n" expected = s.encoding - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal expected, s.encoding s = "# vim:set fileencoding=utf-8:\n" expected = s.encoding - RDoc::Encoding.set_encoding s + s = RDoc::Encoding.set_encoding s assert_equal expected, s.encoding assert_raises ArgumentError do - RDoc::Encoding.set_encoding "# -*- encoding: undecided -*-\n" + s = RDoc::Encoding.set_encoding "# -*- encoding: undecided -*-\n" end end def test_skip_frozen_string_literal - expected = "# frozen_string_literal: false\nhi everybody" + expected = "# frozen_string_literal: true\nhi everybody" @tempfile.write expected @tempfile.flush @@ -216,7 +216,7 @@ def test_skip_frozen_string_literal_after_coding end def test_skip_frozen_string_literal_before_coding - expected = "# frozen_string_literal: false\n# coding: utf-8\nhi everybody" + expected = "# frozen_string_literal: true\n# coding: utf-8\nhi everybody" @tempfile.write expected @tempfile.flush diff --git a/test/test_rdoc_generator_json_index.rb b/test/test_rdoc_generator_json_index.rb index 154dfe7c4b..6cb5463d29 100644 --- a/test/test_rdoc_generator_json_index.rb +++ b/test/test_rdoc_generator_json_index.rb @@ -199,7 +199,7 @@ def test_generate_gzipped def test_generate_utf_8 text = "5\xB0" - text.force_encoding Encoding::ISO_8859_1 + text = RDoc::Encoding.change_encoding text, Encoding::ISO_8859_1 @klass.add_comment comment(text), @top_level @g.generate @@ -215,7 +215,7 @@ def test_generate_utf_8 klass_record = @klass.search_record[2..-1] klass_record[-1] = "

5\xc2\xb0\n" - klass_record.last.force_encoding Encoding::UTF_8 + klass_record[-1] = RDoc::Encoding.change_encoding klass_record[-1], Encoding::UTF_8 info = [ klass_record, diff --git a/test/test_rdoc_markup_attribute_manager.rb b/test/test_rdoc_markup_attribute_manager.rb index 58741f2331..f6888facf7 100644 --- a/test/test_rdoc_markup_attribute_manager.rb +++ b/test/test_rdoc_markup_attribute_manager.rb @@ -171,21 +171,21 @@ def test_combined end def test_convert_attrs - str = '+foo+' + str = '+foo+'.dup attrs = RDoc::Markup::AttrSpan.new str.length @am.convert_attrs str, attrs assert_equal "\000foo\000", str - str = '+:foo:+' + str = '+:foo:+'.dup attrs = RDoc::Markup::AttrSpan.new str.length @am.convert_attrs str, attrs assert_equal "\000:foo:\000", str - str = '+x-y+' + str = '+x-y+'.dup attrs = RDoc::Markup::AttrSpan.new str.length @am.convert_attrs str, attrs @@ -299,17 +299,17 @@ def test_mask_protected_sequence def @am.str() @str end def @am.str=(str) @str = str end - @am.str = 'foo' + @am.str = 'foo'.dup @am.mask_protected_sequences assert_equal "foo", @am.str - @am.str = 'foo\\' + @am.str = 'foo\\'.dup @am.mask_protected_sequences assert_equal "foo<\x04/code>", @am.str, 'escaped close' - @am.str = 'foo\\\\' + @am.str = 'foo\\\\'.dup @am.mask_protected_sequences assert_equal "foo\\", @am.str, 'escaped backslash' diff --git a/test/test_rdoc_markup_formatter.rb b/test/test_rdoc_markup_formatter.rb index 1b0a011e6f..0e7f72fb16 100644 --- a/test/test_rdoc_markup_formatter.rb +++ b/test/test_rdoc_markup_formatter.rb @@ -12,7 +12,7 @@ def initialize markup end def accept_paragraph paragraph - @res << attributes(paragraph.text) + @res += attributes(paragraph.text) end def attributes text diff --git a/test/test_rdoc_markup_pre_process.rb b/test/test_rdoc_markup_pre_process.rb index 13e3ec13c0..3e0e505b0b 100644 --- a/test/test_rdoc_markup_pre_process.rb +++ b/test/test_rdoc_markup_pre_process.rb @@ -86,8 +86,7 @@ def test_handle text = "# :main: M\n" out = @pp.handle text - assert_same out, text - assert_equal "#\n", text + assert_equal "#\n", out end def test_handle_comment @@ -96,8 +95,7 @@ def test_handle_comment out = @pp.handle c - assert_same out, text - assert_equal "#\n", text + assert_equal "#\n", out end def test_handle_markup @@ -129,8 +127,7 @@ def test_handle_post_process out = @pp.handle text, cd - assert_same out, text - assert_equal "# a b c\n", text + assert_equal "# a b c\n", out assert_equal "# a b c\n", cd.metadata[:stuff] end @@ -138,8 +135,7 @@ def test_handle_unregistered text = "# :x: y\n" out = @pp.handle text - assert_same out, text - assert_equal "# :x: y\n", text + assert_equal text, out end def test_handle_directive_blankline diff --git a/test/test_rdoc_markup_to_html_snippet.rb b/test/test_rdoc_markup_to_html_snippet.rb index fa59de434f..d7ce211999 100644 --- a/test/test_rdoc_markup_to_html_snippet.rb +++ b/test/test_rdoc_markup_to_html_snippet.rb @@ -604,7 +604,7 @@ def test_convert_limit_over rdoc = "* text\n" * 2 expected = "

text\n" - expected.chomp! + expected = expected.chomp expected << " #{@ellipsis}\n" actual = @to.convert rdoc diff --git a/test/test_rdoc_parser_changelog.rb b/test/test_rdoc_parser_changelog.rb index 46cbb0d9a3..06c3ff206e 100644 --- a/test/test_rdoc_parser_changelog.rb +++ b/test/test_rdoc_parser_changelog.rb @@ -33,7 +33,7 @@ def test_class_can_parse def test_continue_entry_body parser = util_parser - entry_body = ['a'] + entry_body = ['a'.dup] parser.continue_entry_body entry_body, 'b' @@ -53,7 +53,7 @@ def test_continue_entry_body_empty def test_continue_entry_body_function parser = util_parser - entry_body = ['file: (func1)'] + entry_body = ['file: (func1)'.dup] parser.continue_entry_body entry_body, '(func2): blah' diff --git a/test/test_rdoc_text.rb b/test/test_rdoc_text.rb index 8d638bbc38..9f0e9480d3 100644 --- a/test/test_rdoc_text.rb +++ b/test/test_rdoc_text.rb @@ -61,7 +61,7 @@ def test_expand_tabs def test_expand_tabs_encoding inn = "hello\ns\tdave" - inn.force_encoding Encoding::BINARY + inn = RDoc::Encoding.change_encoding inn, Encoding::BINARY out = expand_tabs inn @@ -95,7 +95,7 @@ def test_flush_left_encoding The comments associated with TEXT - text.force_encoding Encoding::US_ASCII + text = RDoc::Encoding.change_encoding text, Encoding::US_ASCII expected = <<-EXPECTED @@ -303,7 +303,7 @@ def test_strip_hashes_encoding # The comments associated with TEXT - text.force_encoding Encoding::CP852 + text = RDoc::Encoding.change_encoding text, Encoding::CP852 expected = <<-EXPECTED @@ -332,7 +332,7 @@ def test_strip_newlines_encoding assert_equal Encoding::UTF_8, ''.encoding, 'Encoding sanity check' text = " \n" - text.force_encoding Encoding::US_ASCII + text = RDoc::Encoding.change_encoding text, Encoding::US_ASCII stripped = strip_newlines text @@ -386,7 +386,7 @@ def test_strip_stars_encoding */ TEXT - text.force_encoding Encoding::CP852 + text = RDoc::Encoding.change_encoding text, Encoding::CP852 expected = <<-EXPECTED @@ -410,7 +410,7 @@ def test_strip_stars_encoding2 */ TEXT - text.force_encoding Encoding::BINARY + text = RDoc::Encoding.change_encoding text, Encoding::BINARY expected = <<-EXPECTED diff --git a/test/test_rdoc_tom_doc.rb b/test/test_rdoc_tom_doc.rb index 6e60e67726..15bbd9b32d 100644 --- a/test/test_rdoc_tom_doc.rb +++ b/test/test_rdoc_tom_doc.rb @@ -131,7 +131,7 @@ def test_parse_paragraph def test_parse_multiline_paragraph text = "Public: Do some stuff\n" - text << "On a new line\n" + text += "On a new line\n" expected = doc( @@ -353,7 +353,7 @@ def test_tokenize_paragraph def test_tokenize_multiline_paragraph text = "Public: Do some stuff\n" - text << "On a new line\n" + text += "On a new line\n" @td.tokenize text