commit: bbc8b2a61544af2859276cf8badb53ca3b1244c5 Author: Alex Legler <alex <AT> a3li <DOT> li> AuthorDate: Mon Feb 23 20:56:21 2015 +0000 Commit: Alex Legler <a3li <AT> gentoo <DOT> org> CommitDate: Mon Feb 23 20:56:21 2015 +0000 URL: http://sources.gentoo.org/gitweb/?p=proj/ag.git;a=commit;h=bbc8b2a6
Test (hopefully) faster encoding fix method --- lib/utils.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/utils.rb b/lib/utils.rb index aaa04c7..fc4427e 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -2,8 +2,15 @@ require 'charlock_holmes' module Ag module Utils + module_function - def fix_encoding(str, fail_hard = false) + def fix_encoding(str) + s = str.encode('UTF-8', 'UTF-8', invalid: :replace, replace: '') + s = s.unpack('C*').pack('U*') unless s.valid_encoding? + s + end + + def fix_encoding_old(str, fail_hard = false) detection = CharlockHolmes::EncodingDetector.detect(str) CharlockHolmes::Converter.convert(str, detection[:encoding], 'UTF-8') rescue => e