def encoding

      position = 0
      charset = nil
      length = @html.length

      done = false

      while position < length && !done

        # First look for a standard HTML comment (ie <!-- blah -->)
        if @html[position, 4] == '<!--'

          position += 2

          position += (@html[position, length].index('-->') || length)

        # Then look for the start of a meta tag
        elsif  @html[position, 6] =~ /\A\<meta[\s\/]/i

          charset, position_increment = charset_from_meta(@html[position + 5, length])

          break if charset

          position += position_increment

        # Then look for <! or </ or <?
        elsif @html[position, 2] =~ /\A\<[\!\/\?]/

          # Advance position to the first > that appears next in string, or end
          position += @html[position, length].index('>') || length

        else
          # Do nothing. (This is just here to make the algorithm easier to follow)
        end

        # Advance position to next character
        position += 1
      end

      charset
    end
def encoding

      position = 0
      charset = nil
      length = @html.length

      done = false

      while position < length && !done

        # First look for a standard HTML comment (ie <!-- blah -->)
        if @html[position, 4] == '<!--'

          position += 2

          position += (@html[position, length].index('-->') || length)

        # Then look for the start of a meta tag
        elsif  @html[position, 6] =~ /\A\<meta[\s\/]/i

          charset, position_increment = charset_from_meta(@html[position + 5, length])

          break if charset

          position += position_increment

        # Then look for <! or </ or <?
        elsif @html[position, 2] =~ /\A\<[\!\/\?]/

          # Advance position to the first > that appears next in string, or end
          position += @html[position, length].index('>') || length

        else
          # Do nothing. (This is just here to make the algorithm easier to follow)
        end

        # Advance position to next character
        position += 1
      end

      charset
    end
Returns the encoding sniffed from the content of an HTML page, as determined using an
 implemention of the algorithm to 'prescan a byte stream to determine its encoding', as
 specified by the HTML specification:
 http://www.w3.org/html/wg/drafts/html/master/syntax.html#prescan-a-byte-stream-to-determine-its-encoding