Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

codeset_util.rb 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. module Redmine
  3. module CodesetUtil
  4. def self.replace_invalid_utf8(str)
  5. return nil if str.nil?
  6. str = str.dup
  7. str.force_encoding('UTF-8')
  8. if ! str.valid_encoding?
  9. str = str.encode("UTF-16LE", :invalid => :replace,
  10. :undef => :replace, :replace => '?').encode("UTF-8")
  11. end
  12. str
  13. end
  14. def self.to_utf8(str, encoding)
  15. return if str.nil?
  16. str = str.b
  17. if str.empty?
  18. str.force_encoding("UTF-8")
  19. return str
  20. end
  21. enc = encoding.blank? ? "UTF-8" : encoding
  22. if enc.casecmp("UTF-8") != 0
  23. str.force_encoding(enc)
  24. str = str.encode("UTF-8", :invalid => :replace,
  25. :undef => :replace, :replace => '?')
  26. else
  27. str = replace_invalid_utf8(str)
  28. end
  29. str
  30. end
  31. def self.to_utf8_by_setting(str)
  32. return if str.nil?
  33. str = str.dup
  34. self.to_utf8_by_setting_internal(str).force_encoding('UTF-8')
  35. end
  36. def self.to_utf8_by_setting_internal(str)
  37. return if str.nil?
  38. str = str.b
  39. return str if str.empty?
  40. return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match?(str) # for us-ascii
  41. str.force_encoding('UTF-8')
  42. encodings = Setting.repositories_encodings.split(',').collect(&:strip)
  43. encodings.each do |encoding|
  44. begin
  45. str.force_encoding(encoding)
  46. utf8 = str.encode('UTF-8')
  47. return utf8 if utf8.valid_encoding?
  48. rescue
  49. # do nothing here and try the next encoding
  50. end
  51. end
  52. self.replace_invalid_utf8(str).force_encoding('UTF-8')
  53. end
  54. def self.from_utf8(str, encoding)
  55. return if str.nil?
  56. str = str.dup
  57. str ||= ''
  58. str.force_encoding('UTF-8')
  59. if encoding.casecmp('UTF-8') != 0
  60. str = str.encode(encoding, :invalid => :replace,
  61. :undef => :replace, :replace => '?')
  62. else
  63. str = self.replace_invalid_utf8(str)
  64. end
  65. end
  66. def self.guess_encoding(str)
  67. return if str.nil?
  68. str = str.dup
  69. encodings = Setting.repositories_encodings.split(',').collect(&:strip)
  70. encodings = encodings.presence || ['UTF-8']
  71. encodings.each do |encoding|
  72. begin
  73. str.force_encoding(encoding)
  74. rescue Encoding::ConverterNotFoundError
  75. # ignore if the encoding name is invalid
  76. end
  77. return encoding if str.valid_encoding?
  78. end
  79. nil
  80. end
  81. end
  82. end