diff lib/redmine/unified_diff.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children
line wrap: on
line diff
--- a/lib/redmine/unified_diff.rb	Fri Jun 14 09:01:12 2013 +0100
+++ b/lib/redmine/unified_diff.rb	Fri Jun 14 09:02:21 2013 +0100
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2012  Jean-Philippe Lang
+# Copyright (C) 2006-2013  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -28,17 +28,9 @@
       lines = 0
       @truncated = false
       diff_table = DiffTable.new(diff_type, diff_style)
-      diff.each do |line|
-        line_encoding = nil
-        if line.respond_to?(:force_encoding)
-          line_encoding = line.encoding
-          # TODO: UTF-16 and Japanese CP932 which is imcompatible with ASCII
-          #       In Japan, diffrence between file path encoding
-          #       and file contents encoding is popular.
-          line.force_encoding('ASCII-8BIT')
-        end
-        unless diff_table.add_line line
-          line.force_encoding(line_encoding) if line_encoding
+      diff.each do |line_raw|
+        line = Redmine::CodesetUtil.to_utf8_by_setting(line_raw)
+        unless diff_table.add_line(line)
           self << diff_table if diff_table.length > 0
           diff_table = DiffTable.new(diff_type, diff_style)
         end
@@ -83,7 +75,7 @@
           @parsing = true
         end
       else
-        if line =~ /^[^\+\-\s@\\]/
+        if line =~ %r{^[^\+\-\s@\\]}
           @parsing = false
           return false
         elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
@@ -207,10 +199,20 @@
         while starting < max && line_left[starting] == line_right[starting]
           starting += 1
         end
+        if (! "".respond_to?(:force_encoding)) && starting < line_left.size
+          while line_left[starting].ord.between?(128, 191) && starting > 0
+            starting -= 1
+          end
+        end
         ending = -1
         while ending >= -(max - starting) && line_left[ending] == line_right[ending]
           ending -= 1
         end
+        if (! "".respond_to?(:force_encoding)) && ending > (-1 * line_left.size)
+          while line_left[ending].ord.between?(128, 191) && ending > -1
+            ending -= 1
+          end
+        end
         unless starting == 0 && ending == -1
           [starting, ending]
         end
@@ -268,6 +270,12 @@
     private
 
     def line_to_html(line, offsets)
+      html = line_to_html_raw(line, offsets)
+      html.force_encoding('UTF-8') if html.respond_to?(:force_encoding)
+      html
+    end
+
+    def line_to_html_raw(line, offsets)
       if offsets
         s = ''
         unless offsets.first == 0