##// END OF EJS Templates
unify duplicate codes (#24616)...
Toshi MARUYAMA -
r15890:281bc5c45441
parent child
Show More
@@ -1,72 +1,68
1 1
2 2 module Redmine
3 3 module CodesetUtil
4 4
5 5 def self.replace_invalid_utf8(str)
6 6 return str if str.nil?
7 7 str.force_encoding('UTF-8')
8 8 if ! str.valid_encoding?
9 9 str = str.encode("US-ASCII", :invalid => :replace,
10 10 :undef => :replace, :replace => '?').encode("UTF-8")
11 11 end
12 12 str
13 13 end
14 14
15 15 def self.to_utf8(str, encoding)
16 16 return str if str.nil?
17 17 str.force_encoding("ASCII-8BIT")
18 18 if str.empty?
19 19 str.force_encoding("UTF-8")
20 20 return str
21 21 end
22 22 enc = encoding.blank? ? "UTF-8" : encoding
23 23 if enc.upcase != "UTF-8"
24 24 str.force_encoding(enc)
25 25 str = str.encode("UTF-8", :invalid => :replace,
26 26 :undef => :replace, :replace => '?')
27 27 else
28 str.force_encoding("UTF-8")
29 if ! str.valid_encoding?
30 str = str.encode("US-ASCII", :invalid => :replace,
31 :undef => :replace, :replace => '?').encode("UTF-8")
32 end
28 str = replace_invalid_utf8(str)
33 29 end
34 30 str
35 31 end
36 32
37 33 def self.to_utf8_by_setting(str)
38 34 return str if str.nil?
39 35 self.to_utf8_by_setting_internal(str).force_encoding('UTF-8')
40 36 end
41 37
42 38 def self.to_utf8_by_setting_internal(str)
43 39 return str if str.nil?
44 40 str.force_encoding('ASCII-8BIT')
45 41 return str if str.empty?
46 42 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
47 43 str.force_encoding('UTF-8')
48 44 encodings = Setting.repositories_encodings.split(',').collect(&:strip)
49 45 encodings.each do |encoding|
50 46 begin
51 47 str.force_encoding(encoding)
52 48 utf8 = str.encode('UTF-8')
53 49 return utf8 if utf8.valid_encoding?
54 50 rescue
55 51 # do nothing here and try the next encoding
56 52 end
57 53 end
58 54 self.replace_invalid_utf8(str).force_encoding('UTF-8')
59 55 end
60 56
61 57 def self.from_utf8(str, encoding)
62 58 str ||= ''
63 59 str.force_encoding('UTF-8')
64 60 if encoding.upcase != 'UTF-8'
65 61 str = str.encode(encoding, :invalid => :replace,
66 62 :undef => :replace, :replace => '?')
67 63 else
68 64 str = self.replace_invalid_utf8(str)
69 65 end
70 66 end
71 67 end
72 68 end
General Comments 0
You need to be logged in to leave comments. Login now