PageRenderTime 51ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/redmine/codeset_util.rb

https://bitbucket.org/eimajenthat/redmine
Ruby | 161 lines | 153 code | 6 blank | 2 comment | 28 complexity | e1dfdbad318e07428fa47ac470a0b0af MD5 | raw file
Possible License(s): GPL-2.0
  1. if RUBY_VERSION < '1.9'
  2. require 'iconv'
  3. end
  4. module Redmine
  5. module CodesetUtil
  6. def self.replace_invalid_utf8(str)
  7. return str if str.nil?
  8. if str.respond_to?(:force_encoding)
  9. str.force_encoding('UTF-8')
  10. if ! str.valid_encoding?
  11. str = str.encode("US-ASCII", :invalid => :replace,
  12. :undef => :replace, :replace => '?').encode("UTF-8")
  13. end
  14. elsif RUBY_PLATFORM == 'java'
  15. begin
  16. ic = Iconv.new('UTF-8', 'UTF-8')
  17. str = ic.iconv(str)
  18. rescue
  19. str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
  20. end
  21. else
  22. ic = Iconv.new('UTF-8', 'UTF-8')
  23. txtar = ""
  24. begin
  25. txtar += ic.iconv(str)
  26. rescue Iconv::IllegalSequence
  27. txtar += $!.success
  28. str = '?' + $!.failed[1,$!.failed.length]
  29. retry
  30. rescue
  31. txtar += $!.success
  32. end
  33. str = txtar
  34. end
  35. str
  36. end
  37. def self.to_utf8(str, encoding)
  38. return str if str.nil?
  39. str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
  40. if str.empty?
  41. str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
  42. return str
  43. end
  44. enc = encoding.blank? ? "UTF-8" : encoding
  45. if str.respond_to?(:force_encoding)
  46. if enc.upcase != "UTF-8"
  47. str.force_encoding(enc)
  48. str = str.encode("UTF-8", :invalid => :replace,
  49. :undef => :replace, :replace => '?')
  50. else
  51. str.force_encoding("UTF-8")
  52. if ! str.valid_encoding?
  53. str = str.encode("US-ASCII", :invalid => :replace,
  54. :undef => :replace, :replace => '?').encode("UTF-8")
  55. end
  56. end
  57. elsif RUBY_PLATFORM == 'java'
  58. begin
  59. ic = Iconv.new('UTF-8', enc)
  60. str = ic.iconv(str)
  61. rescue
  62. str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
  63. end
  64. else
  65. ic = Iconv.new('UTF-8', enc)
  66. txtar = ""
  67. begin
  68. txtar += ic.iconv(str)
  69. rescue Iconv::IllegalSequence
  70. txtar += $!.success
  71. str = '?' + $!.failed[1,$!.failed.length]
  72. retry
  73. rescue
  74. txtar += $!.success
  75. end
  76. str = txtar
  77. end
  78. str
  79. end
  80. def self.to_utf8_by_setting(str)
  81. return str if str.nil?
  82. str = self.to_utf8_by_setting_internal(str)
  83. if str.respond_to?(:force_encoding)
  84. str.force_encoding('UTF-8')
  85. end
  86. str
  87. end
  88. def self.to_utf8_by_setting_internal(str)
  89. return str if str.nil?
  90. if str.respond_to?(:force_encoding)
  91. str.force_encoding('ASCII-8BIT')
  92. end
  93. return str if str.empty?
  94. return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
  95. if str.respond_to?(:force_encoding)
  96. str.force_encoding('UTF-8')
  97. end
  98. encodings = Setting.repositories_encodings.split(',').collect(&:strip)
  99. encodings.each do |encoding|
  100. if str.respond_to?(:force_encoding)
  101. begin
  102. str.force_encoding(encoding)
  103. utf8 = str.encode('UTF-8')
  104. return utf8 if utf8.valid_encoding?
  105. rescue
  106. # do nothing here and try the next encoding
  107. end
  108. else
  109. begin
  110. return Iconv.conv('UTF-8', encoding, str)
  111. rescue Iconv::Failure
  112. # do nothing here and try the next encoding
  113. end
  114. end
  115. end
  116. str = self.replace_invalid_utf8(str)
  117. if str.respond_to?(:force_encoding)
  118. str.force_encoding('UTF-8')
  119. end
  120. str
  121. end
  122. def self.from_utf8(str, encoding)
  123. str ||= ''
  124. if str.respond_to?(:force_encoding)
  125. str.force_encoding('UTF-8')
  126. if encoding.upcase != 'UTF-8'
  127. str = str.encode(encoding, :invalid => :replace,
  128. :undef => :replace, :replace => '?')
  129. else
  130. str = self.replace_invalid_utf8(str)
  131. end
  132. elsif RUBY_PLATFORM == 'java'
  133. begin
  134. ic = Iconv.new(encoding, 'UTF-8')
  135. str = ic.iconv(str)
  136. rescue
  137. str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
  138. end
  139. else
  140. ic = Iconv.new(encoding, 'UTF-8')
  141. txtar = ""
  142. begin
  143. txtar += ic.iconv(str)
  144. rescue Iconv::IllegalSequence
  145. txtar += $!.success
  146. str = '?' + $!.failed[1, $!.failed.length]
  147. retry
  148. rescue
  149. txtar += $!.success
  150. end
  151. str = txtar
  152. end
  153. end
  154. end
  155. end