<commit_before>require 'protocol_buffers'
require 'protocol_buffers/runtime/enum'
require 'protocol_buffers/runtime/varint'
require 'protocol_buffers/limited_io'

module ProtocolBuffers

  module WireTypes # :nodoc:
    VARINT = 0
    FIXED64 = 1
    LENGTH_DELIMITED = 2
    START_GROUP = 3 # deprecated, not supported in ruby
    END_GROUP = 4   # ditto
    FIXED32 = 5
  end

  # Acts like an Array, but type-checks each element
  class RepeatedField < Array # :nodoc:
    def initialize(field)
      super()
      @field = field
    end

    # ovverride all mutating methods.
    # I'm sure this will break down on each new major Ruby release, as new
    # mutating methods are added to Array. Ah, well. caveat emptor.

    def <<(obj)
      check(obj)
      super
    end

    def []=(*args)
      obj = args.last
      case obj
      when nil
        check(obj) if args.length == 2 && !args.first.is_a?(Range)
      when Array
        check_each(obj)
      else
        check(obj)
      end
      super
    end

    def collect!(&b)
      replace(collect(&b))
    end
    alias_method :map!, :collect!

    def concat(rhs)
      check_each(rhs)
      super
    end

    def fill(*args, &b)
      if block_given?
        super(*args) { |v| check(b.call(v)) }
      else
        check(args.first)
        super
      end
    end

    def insert(index, *objs)
      check_each(objs)
      super
    end

    def push(*objs)
      check_each(objs)
      super
    end

    def replace(array)
      check_each(array)
      super
    end

    def unshift(*objs)
      check_each(objs)
      super
    end

    private

    def check(value)
      @field.check_valid(value)
    end

    def check_each(iter)
      iter.each { |value| @field.check_valid(value) }
    end
  end

  class Field # :nodoc: all
    attr_reader :otype, :name, :tag

    def repeated?; otype == :repeated end
    def packed?; repeated? && @opts[:packed] end

    def self.create(sender, otype, type, name, tag, opts = {})
      if type.is_a?(Symbol)
        klass = Field.const_get("#{type.to_s.capitalize}Field") rescue nil
        raise("Type not found: #{type}") if klass.nil?
        field = klass.new(otype, name, tag, opts)
      elsif type.ancestors.include?(ProtocolBuffers::Enum)
        field = Field::EnumField.new(type, otype, name, tag, opts)
      elsif type.ancestors.include?(ProtocolBuffers::Message)
        if opts[:group]
          field = Field::GroupField.new(type, otype, name, tag, opts)
        else
          field = Field::MessageField.new(type, otype, name, tag, opts)
        end
      else
        raise("Type not found: #{type}")
      end
      return field
    end

    def initialize(otype, name, tag, opts = {})
      @otype = otype
      @name = name
      @tag = tag
      @opts = opts.dup
    end

    def add_reader_to(klass)
      if repeated?
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
        def #{name}
          unless @#{name}
            @#{name} = RepeatedField.new(fields[#{tag}])
          end
          @#{name}
        end
        EOF
      else
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
        def #{name}
          if @set_fields[#{tag}] == nil
            # first access of this field, generate it
            initialize_field(#{tag})
          end
          @#{name}
        end
        EOF
      end
    end

    def add_writer_to(klass)
      if repeated?
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
          def #{name}=(value)
            if value.nil?
              #{name}.clear
            else
              #{name}.clear
              value.each { |i| @#{name}.push i }
            end
          end
        EOF
      else
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
          def #{name}=(value)
            field = fields[#{tag}]
            if value.nil?
              @set_fields[#{tag}] = false
              @#{name} = field.default_value
            else
              field.check_valid(value)
              @set_fields[#{tag}] = true
              @#{name} = value
              if @parent_for_notify
                @parent_for_notify.default_changed(@tag_for_notify)
                @parent_for_notify = @tag_for_notify = nil
              end
            end
          end
        EOF
      end
    end

    def add_methods_to(klass)
      add_reader_to(klass)
      add_writer_to(klass)

      if repeated?<commit_msg>fix bug where "value" could not be the name of a field<commit_after>require 'protocol_buffers'
require 'protocol_buffers/runtime/enum'
require 'protocol_buffers/runtime/varint'
require 'protocol_buffers/limited_io'

module ProtocolBuffers

  module WireTypes # :nodoc:
    VARINT = 0
    FIXED64 = 1
    LENGTH_DELIMITED = 2
    START_GROUP = 3 # deprecated, not supported in ruby
    END_GROUP = 4   # ditto
    FIXED32 = 5
  end

  # Acts like an Array, but type-checks each element
  class RepeatedField < Array # :nodoc:
    def initialize(field)
      super()
      @field = field
    end

    # ovverride all mutating methods.
    # I'm sure this will break down on each new major Ruby release, as new
    # mutating methods are added to Array. Ah, well. caveat emptor.

    def <<(obj)
      check(obj)
      super
    end

    def []=(*args)
      obj = args.last
      case obj
      when nil
        check(obj) if args.length == 2 && !args.first.is_a?(Range)
      when Array
        check_each(obj)
      else
        check(obj)
      end
      super
    end

    def collect!(&b)
      replace(collect(&b))
    end
    alias_method :map!, :collect!

    def concat(rhs)
      check_each(rhs)
      super
    end

    def fill(*args, &b)
      if block_given?
        super(*args) { |v| check(b.call(v)) }
      else
        check(args.first)
        super
      end
    end

    def insert(index, *objs)
      check_each(objs)
      super
    end

    def push(*objs)
      check_each(objs)
      super
    end

    def replace(array)
      check_each(array)
      super
    end

    def unshift(*objs)
      check_each(objs)
      super
    end

    private

    def check(value)
      @field.check_valid(value)
    end

    def check_each(iter)
      iter.each { |value| @field.check_valid(value) }
    end
  end

  class Field # :nodoc: all
    attr_reader :otype, :name, :tag

    def repeated?; otype == :repeated end
    def packed?; repeated? && @opts[:packed] end

    def self.create(sender, otype, type, name, tag, opts = {})
      if type.is_a?(Symbol)
        klass = Field.const_get("#{type.to_s.capitalize}Field") rescue nil
        raise("Type not found: #{type}") if klass.nil?
        field = klass.new(otype, name, tag, opts)
      elsif type.ancestors.include?(ProtocolBuffers::Enum)
        field = Field::EnumField.new(type, otype, name, tag, opts)
      elsif type.ancestors.include?(ProtocolBuffers::Message)
        if opts[:group]
          field = Field::GroupField.new(type, otype, name, tag, opts)
        else
          field = Field::MessageField.new(type, otype, name, tag, opts)
        end
      else
        raise("Type not found: #{type}")
      end
      return field
    end

    def initialize(otype, name, tag, opts = {})
      @otype = otype
      @name = name
      @tag = tag
      @opts = opts.dup
    end

    def add_reader_to(klass)
      if repeated?
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
        def #{name}
          unless @#{name}
            @#{name} = RepeatedField.new(fields[#{tag}])
          end
          @#{name}
        end
        EOF
      else
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
        def #{name}
          if @set_fields[#{tag}] == nil
            # first access of this field, generate it
            initialize_field(#{tag})
          end
          @#{name}
        end
        EOF
      end
    end

    def add_writer_to(klass)
      if repeated?
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
          def #{name}=(__value)
            if __value.nil?
              #{name}.clear
            else
              #{name}.clear
              __value.each { |i| @#{name}.push i }
            end
          end
        EOF
      else
        klass.class_eval <<-EOF, __FILE__, __LINE__+1
          def #{name}=(__value)
            field = fields[#{tag}]
            if __value.nil?
              @set_fields[#{tag}] = false
              @#{name} = field.default_value
            else
              field.check_valid(__value)
              @set_fields[#{tag}] = true
              @#{name} = __value
              if @parent_for_notify
                @parent_for_notify.default_changed(@tag_for_notify)
                @parent_for_notify = @tag_for_notify = nil
              end
            end
          end
        EOF
      end
    end

    def add_methods_to(klass)
      add_reader_to(klass)
      add_writer_to(klass)

      if repeated?
{'fraction_non_alphanumeric': 0.10381713770971737, 'fraction_numerical': 0.003377997973201216, 'mean_word_length': 1.800126103404792, 'pattern_counts': {'":': 0, '<': 25, '<?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': '7541825', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2803, 'n_words': 722}