<commit_before>﻿require_relative '../sodium'

module Sodium
  module Utils

    module_function

    def check_length(data, length, description)
      if data.is_a?(String) ||data.respond_to?(:bytesize)
        unless data.bytesize == length.to_int
          fail Sodium::LengthError, "Expected a length=#{length.to_int} bytes #{description}, got size=#{data.bytesize} bytes", caller
        end
      elsif data.is_a?(FFI::Pointer) ||data.respond_to?(:size)
        unless data.size == length.to_int
          fail Sodium::LengthError, "Expected a length=#{length.to_int} bytes #{description}, got size=#{data.size} bytes", caller
        end
      else
        fail ArgumentError, "#{description} must be of type String or FFI::Pointer and be length=#{length.to_int} bytes long", caller
      end
      true
    end

    def get_pointer(ptr)
      if ptr.is_a?(FFI::Pointer)
        ptr
      elsif ptr.respond_to?(:to_ptr)
        ptr.to_ptr
      else
        fail ArgumentError, "#{ptr.class} is not a FFI::Pointer", caller
      end
    end

    def get_string(string)
      if string.is_a?(String)
        string
      elsif string.respond_to?(:to_str)
        string.to_str
      elsif string.respond_to?(:read_string)
        string.read_string
      else
        fail ArgumentError, "#{string.class} is not a String", caller
      end
    end

    def get_int(int)
      if int.is_a?(Integer)
        int
      elsif int.respond_to?(:to_int)
        int.to_int
      else
        fail ArgumentError, "#{int.class} is not a Integer", caller
      end
    end

    def get_size(data)
      if data.is_a?(String) ||data.respond_to?(:bytesize)
        data.bytesize
      elsif data.is_a?(FFI::Pointer) ||data.respond_to?(:size)
        data.size
      else
        fail ArgumentError, "#{data.class} doesn't respond to :bytesize or :size", caller
      end
    end

    ZERO = "\0".force_encoding(Encoding::ASCII_8BIT).freeze

    def zeros(n)
      ZERO * n
    end
  end
end<commit_msg>return zero when data is nil<commit_after>﻿require_relative '../sodium'

module Sodium
  module Utils

    module_function

    def check_length(data, length, description)
      if data.is_a?(String) ||data.respond_to?(:bytesize)
        unless data.bytesize == length.to_int
          fail Sodium::LengthError, "Expected a length=#{length.to_int} bytes #{description}, got size=#{data.bytesize} bytes", caller
        end
      elsif data.is_a?(FFI::Pointer) ||data.respond_to?(:size)
        unless data.size == length.to_int
          fail Sodium::LengthError, "Expected a length=#{length.to_int} bytes #{description}, got size=#{data.size} bytes", caller
        end
      else
        fail ArgumentError, "#{description} must be of type String or FFI::Pointer and be length=#{length.to_int} bytes long", caller
      end
      true
    end

    def get_pointer(ptr)
      if ptr.is_a?(FFI::Pointer)
        ptr
      elsif ptr.respond_to?(:to_ptr)
        ptr.to_ptr
      else
        fail ArgumentError, "#{ptr.class} is not a FFI::Pointer", caller
      end
    end

    def get_string(string)
      if string.is_a?(String)
        string
      elsif string.respond_to?(:to_str)
        string.to_str
      elsif string.respond_to?(:read_string)
        string.read_string
      else
        fail ArgumentError, "#{string.class} is not a String", caller
      end
    end

    def get_int(int)
      if int.is_a?(Integer)
        int
      elsif int.respond_to?(:to_int)
        int.to_int
      else
        fail ArgumentError, "#{int.class} is not a Integer", caller
      end
    end

    def get_size(data)
      if data.is_a?(String) ||data.respond_to?(:bytesize)
        data.bytesize
      elsif data.is_a?(FFI::Pointer) ||data.respond_to?(:size)
        data.size
      elsif data.nil?
        0
      else
        fail ArgumentError, "#{data.class} doesn't respond to :bytesize or :size", caller
      end
    end

    ZERO = "\0".force_encoding(Encoding::ASCII_8BIT).freeze

    def zeros(n)
      ZERO * n
    end
  end
end
{'fraction_non_alphanumeric': 0.10998266039137974, 'fraction_numerical': 0.0012385434728758979, 'mean_word_length': 2.4512820512820515, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4583164', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1288, 'n_words': 325}