Skip to content

warning: character class has duplicated range #2

Description

@dove-young

I encountered 'character class has duplicated range' warning in ruby 2.0 environment.

->   ruby  -dvw -I~/Shell/GitHub/ruby-readability/lib -I~/Shell/GitHub/guess_html_encoding/lib test/read.rb 
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
~/Shell/GitHub/guess_html_encoding/lib/guess_html_encoding.rb:14: warning: character class has duplicated range: /charset=([\w\d-]+);?/
~/Shell/GitHub/guess_html_encoding/lib/guess_html_encoding.rb:22: warning: character class has duplicated range: /charset=([\w\d-]+);?/
~/Shell/GitHub/guess_html_encoding/lib/guess_html_encoding.rb:24: warning: character class has duplicated range: /<meta\s+charset=["']([\w\d-]+)?/

Seems removing \d from regexp would recover it. Below is a diff from git

->   git diff
diff --git a/lib/guess_html_encoding.rb b/lib/guess_html_encoding.rb
index 2ac074f..c39d973 100644
--- a/lib/guess_html_encoding.rb
+++ b/lib/guess_html_encoding.rb
@@ -11,7 +11,7 @@ module GuessHtmlEncoding
       headers = headers.map {|k, v| "#{k}: #{v}" }.join("\n") if headers.is_a?(Hash)
       headers = headers.dup.force_encoding("ASCII-8BIT")
       headers.split("\n").map {|i| i.split(":")}.each do |k,v|
-        if k =~ /Content-Type/i && v =~ /charset=([\w\d-]+);?/i
+        if k =~ /Content-Type/i && v =~ /charset=([\w-]+);?/i
           out = $1.upcase
           break
         end
@@ -19,9 +19,9 @@ module GuessHtmlEncoding
     end

     if out.nil? || out.empty? || !encoding_loaded?(out)
-      if html =~ /<meta[^>]*HTTP-EQUIV=["']?Content-Type["']?[^>]*content=["']([^'"]*)["']/i && $1 =~ /charset=([\w\d-]+);?/i
+      if html =~ /<meta[^>]*HTTP-EQUIV=["']?Content-Type["']?[^>]*content=["']([^'"]*)["']/i && $1 =~ /charset=([\w-]+);?/i
         out = $1
-      elsif html =~ /<meta\s+charset=["']([\w\d-]+)?/i
+      elsif html =~ /<meta\s+charset=["']([\w-]+)?/i
         out = $1
       end
       out.upcase! unless out.nil?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions