Sha256: b0a467e2428882cc297e95e2f4e7c482165335e0861de7dccc66f1a48a4e213b

Contents?: true

Size: 1.1 KB

Versions: 37

Compression:

Stored size: 1.1 KB

Contents

class ProjectSearch
  # This class discriminates between binary and textual data. 
  class BinaryDataDetector
    # Is the data binary?
    #
    # @param [String] data 
    def self.binary?(data)
      not textual?(data)
    end
    
    # Is the data plain text?
    #
    # @param [String] data 
    def self.textual?(data)
      found_good_byte = false
      data.each_byte do |b|
        return false if bad_byte?(b)
        found_good_byte = found_good_byte || good_byte?(b)
      end
      found_good_byte
    end
    
    INDIVIDUAL_GOOD_BYTES = [9, 10, 13]
    
    # At least one of these good bytes must be found for the text to be 
    # discriminated as plain text.
    #
    # @param [Fixnum] byte
    def self.good_byte?(byte)
      (byte >= 32 and byte <= 255) or INDIVIDUAL_GOOD_BYTES.include?(byte)
    end
    
    INDIVIDUAL_BAD_BYTES = [0, 6, 14]
    
    # If any of these bytes are found then we immediately discriminate the data 
    # as binary.
    #
    # @param [Fixnum] byte
    def self.bad_byte?(byte)
      (byte >= 14 and byte <= 31) or INDIVIDUAL_BAD_BYTES.include?(byte)
    end
  end
end


Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
redcar-0.13 plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.13.5dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.13.4dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.13.3dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.13.2dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.13.1dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-0.12.1 plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.13.0dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-0.12 plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.27dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.26dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.25dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.24dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.23dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.22dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.21dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.20dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.19dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.18dev plugins/project_search/lib/project_search/binary_data_detector.rb
redcar-dev-0.12.17dev plugins/project_search/lib/project_search/binary_data_detector.rb