From 63b9c52697a579f53fefc476bcc630420de422ac Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 29 Sep 2020 02:31:08 +0000 Subject: encoding: use CharlockHolmes properly on first run Ruby case/when is not like C with fall-through, so we can't rely on fall-through behavior to use CharlockHolmes to detect legacy encodings. --- lib/dtas/encoding.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/dtas/encoding.rb b/lib/dtas/encoding.rb index dad54af..bbc6076 100644 --- a/lib/dtas/encoding.rb +++ b/lib/dtas/encoding.rb @@ -11,15 +11,14 @@ module DTAS::Encoding # :nodoc: private def try_enc_harder(str, enc, old) # :nodoc: + begin + require 'charlock_holmes' + @charlock_holmes = CharlockHolmes::EncodingDetector.new + rescue LoadError + @charlock_holmes = false + end if @charlock_holmes.nil? + case @charlock_holmes - when nil - begin - require 'charlock_holmes' - @charlock_holmes = CharlockHolmes::EncodingDetector.new - rescue LoadError - warn "`charlock_holmes` gem not available for encoding detection" - @charlock_holmes = false - end when false enc_fallback(str, enc, old) else -- cgit v1.2.3-24-ge0c7