<commit_before>module ScanBeacon
  class BeaconId
    include Comparable
    ENCODING = "ASCII-8BIT".freeze
    NULL_BYTE = "\x00".force_encoding(ENCODING).freeze

    def initialize(bytes: nil, hex: nil, number: nil, length: nil)
      self.set_data(bytes: bytes, hex: hex, number: number, length: length)
    end

    def self.id_with_length(id, length)
      return id if id.is_a? BeaconId
      if id.is_a? String
        BeaconId.new hex: id, length: length
      elsif id.is_a? Fixnum
        BeaconId.new number: id, length: length
      end
    end

    def set_data(bytes: nil, hex: nil, number: nil, length: nil)
      if bytes
        @data = bytes.force_encoding(ENCODING)
      elsif hex
        # zero pad hex if needed
        hex = "0"*(length*2-hex.size) + hex if length and hex.size < length*2
        @data = [hex].pack("H*")
      elsif number
        raise ArgumentError.new("Must also give a field length when you give a number") if length.nil?
        set_data(hex: number.to_s(16), length: length)
      end
    end

    def value
      if @data.size < 6
        self.to_i
      else
        self.to_hex
      end
    end

    def to_s<commit_msg>compatibility w/ ruby 1.9.3<commit_after>module ScanBeacon
  class BeaconId
    include Comparable
    ENCODING = "ASCII-8BIT".freeze
    NULL_BYTE = "\x00".force_encoding(ENCODING).freeze

    def initialize(opts = {})
      self.set_data(opts)
    end

    def self.id_with_length(id, length)
      return id if id.is_a? BeaconId
      if id.is_a? String
        BeaconId.new hex: id, length: length
      elsif id.is_a? Integer
        BeaconId.new number: id, length: length
      end
    end

    def set_data(opts = {})
      bytes = opts[:bytes]
      hex = opts[:hex]
      number = opts[:number]
      length = opts[:length]
      if bytes
        @data = bytes.force_encoding(ENCODING)
      elsif hex
        # zero pad hex if needed
        hex = "0"*(length*2-hex.size) + hex if length and hex.size < length*2
        @data = [hex].pack("H*")
      elsif number
        raise ArgumentError.new("Must also give a field length when you give a number") if length.nil?
        set_data(hex: number.to_s(16), length: length)
      end
    end

    def value
      if @data.size < 6
        self.to_i
      else
        self.to_hex
      end
    end

    def to_s
{'fraction_non_alphanumeric': 0.09653994019649723, 'fraction_numerical': 0.00897052541648868, 'mean_word_length': 2.1734417344173442, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4911031', 'n_tokens_mistral': 797, 'n_tokens_neox': 766, 'n_words': 225}