Skip to content

Fix for Bad character encoding. #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Fix for Bad character encoding. #391

wants to merge 4 commits into from

Conversation

c-f
Copy link

@c-f c-f commented Oct 9, 2018

Signed-off-by: CF cf@data-co.de

Description

When files contain bad characters (e.g. \255) and commits are compared an ArgumentError is thrown.
This PR changes handling bad characters in the else statement.

In addition to PR #369 when converting from UTF-8 to UTF-8 no valid high acii characters are changed.

Proof of Concept

Test script

#!/bin/env ruby
# encoding: UTF-8
# Purpose: Encoding testing

# --[Test strings]--
test_strs = [
    "红宝石",
    "\255",
    "红宝石\255",
]

puts "Encoding Name is UTF-8:"
puts ["String","UTF-8","Valid?"].join("\t\t")
test_strs.each do |str| 
    puts [
        str,
        str.encoding.name == "UTF-8",
        str.valid_encoding?
    ].join("\t\t")
end
puts "------------------------"

puts "Get Encoding (fixed):"
puts ["String","Encoded","Split"].join("\t\t")
test_strs.each do |str|
    if str.encoding.name != "UTF-8"
        encoded = str.encode("UTF-8", "binary", { :invalid => :replace, :undef => :replace })
    else
        encoded = str.encode("UTF-8", "UTF-8",  { :invalid => :replace, :undef => :replace })
    end  
    puts [
        str,
        encoded,
        encoded.split("").join(""),
    ].join("\t\t")
end
puts "------------------------"

puts "Get Encoding (current):"
test_strs.each do |str|
    puts str.split("").join("")
end
puts "------------------------"

Output

Encoding Name is UTF-8:
String		UTF-8		Valid?
红宝石		true		true
�		true		false
红宝石�		true		false
------------------------
Get Encoding (fixed):
String		Encoded		Split
红宝石		红宝石		红宝石
�		�		�
红宝石�		红宝石�		红宝石�
------------------------
Get Encoding (current):
String		Encoded		Split
红宝石		红宝石		红宝石
Traceback (most recent call last):
	3: from test_utf8.rb:42:in `<main>'
	2: from test_utf8.rb:42:in `each'
	1: from test_utf8.rb:51:in `block in <main>'
test_utf8.rb:51:in `split': invalid byte sequence in UTF-8 (ArgumentError)

singpolyma and others added 4 commits October 9, 2018 15:25
Signed-off-by: Stephen Paul Weber <singpolyma@singpolyma.net>
Signed-off-by: invist@thinkpwn <cf@cw>
Signed-off-by: Evgenii Pecherkin <evgenii@toptal.com>
Signed-off-by: invist@thinkpwn <cf@cw>
Signed-off-by: CF <cf@data-co.de>

Signed-off-by: invist@thinkpwn <cf@cw>
Signed-off-by: invist@thinkpwn <cf@cw>
@c-f c-f closed this Oct 9, 2018
@c-f c-f deleted the utf8-fix branch October 9, 2018 13:31
@c-f
Copy link
Author

c-f commented Oct 9, 2018

Sorry - messed up with DCO -.-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants