mirror of
				https://github.com/redmine/redmine.git
				synced 2025-11-03 20:06:24 +01:00 
			
		
		
		
	* test_invalid_utf8_sequences_in_paths_should_be_replaced git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6021 e93f8b46-1217-0410-a6f0-8f06a7374b81
		
			
				
	
	
		
			39 lines
		
	
	
		
			948 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			948 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
require 'iconv'
 | 
						|
 | 
						|
module Redmine
 | 
						|
  module CodesetUtil
 | 
						|
 | 
						|
    def self.replace_invalid_utf8(str)
 | 
						|
      return str if str.nil?
 | 
						|
      if str.respond_to?(:force_encoding)
 | 
						|
        str.force_encoding('UTF-8')
 | 
						|
        if ! str.valid_encoding?
 | 
						|
          str = str.encode("US-ASCII", :invalid => :replace,
 | 
						|
                :undef => :replace, :replace => '?').encode("UTF-8")
 | 
						|
        end
 | 
						|
      elsif RUBY_PLATFORM == 'java'
 | 
						|
        begin
 | 
						|
          ic = Iconv.new('UTF-8', 'UTF-8')
 | 
						|
          str = ic.iconv(str)
 | 
						|
        rescue
 | 
						|
          str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
 | 
						|
        end
 | 
						|
      else
 | 
						|
        ic = Iconv.new('UTF-8', 'UTF-8')
 | 
						|
        txtar = ""
 | 
						|
        begin
 | 
						|
          txtar += ic.iconv(str)
 | 
						|
        rescue Iconv::IllegalSequence
 | 
						|
          txtar += $!.success
 | 
						|
          str = '?' + $!.failed[1,$!.failed.length]
 | 
						|
          retry
 | 
						|
        rescue
 | 
						|
          txtar += $!.success
 | 
						|
        end
 | 
						|
        str = txtar
 | 
						|
      end
 | 
						|
      str
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |