Project

General

Profile

Defect #41464 » fix-guess_encoding.patch

Go MAEDA, 2024-10-10 09:28

View differences:

app/models/import.rb
encoding = lu(user, :general_csv_encoding)
if file_exists?
begin
content = File.read(filepath, 256)
content = File.read(filepath, 4.kilobytes)
separator = [',', ';'].max_by {|sep| content.count(sep)}
wrapper = ['"', "'"].max_by {|quote_char| content.count(quote_char)}
lib/redmine/codeset_util.rb
return if str.nil?
str = str.dup
# Truncate the data at the last LF character to ensure that a partial
# multibyte character, which could cause `String#valid_encoding?` to
# return false, is not included at the end of the data.
last_lf_index = str.rindex("\n")
str = str[..last_lf_index] if last_lf_index.to_i >= 64
encodings = Setting.repositories_encodings.split(',').collect(&:strip)
encodings = encodings.presence || ['UTF-8']
test/unit/lib/redmine/codeset_util_test.rb
assert_nil Redmine::CodesetUtil.guess_encoding(str)
end
end
def test_guess_encoding_handles_trailing_partial_multibyte_character
str = <<~STR
いろはにほへと ちりぬるを
わかよたれそ つねならむ
うゐのおくやま けふこえて
あさきゆめみし ゑひもせす
色は匂へど 散りぬるを
我が世誰ぞ 常ならむ
有為の奥山 今日越えて
浅き夢見し 酔ひもせず
STR
# UTF-8 string truncated at an incomplete character boundary
# str.byteslice(0, 256) => "いろは...\n浅き夢見\xE3\x81"
# "\xE3\x81" is a part of "し" ("\xE3\x81\x97")
str_with_partial_char = str.byteslice(0, 256)
assert_not str_with_partial_char.valid_encoding?
assert_equal 'UTF-8', Redmine::CodesetUtil.guess_encoding(str_with_partial_char)
end
end
(1-1/2)