<commit_before># frozen-string-literal: true
module AsciiPlist
  class Reader
    class UnsupportedPlistFormatError < Error
      attr_reader :format
      def initialize(format)
        @format = format
      end

      def to_s
        "#{format} plists are currently unsupported"
      end
    end

    class ParseError < Error
      attr_accessor :location
      attr_accessor :plist_string
    end

    attr_accessor :plist

    def self.plist_type(plist_contents)
      case plist_contents
      when /\Abplist/
        :binary
      when /\A<\?xml/
        :xml
      else
        :ascii
      end
    end

    def self.from_file(file_path)
      new(File.read(file_path))
    end

    def initialize(contents)
      @scanner = StringScanner.new(contents)
    end

    def parse!
      plist_format = ensure_ascii_plist!
      read_string_encoding
      root_object = parse_object

      eat_whitespace!
      raise_parser_error ParseError, "unrecognized characters #{@scanner.rest.inspect} after parsing" unless @scanner.eos?

      AsciiPlist::Plist.new(root_object, plist_format)
    end

    private

    def ensure_ascii_plist!
      self.class.plist_type(@scanner.string).tap do |plist_format|
        raise UnsupportedPlistFormatError(plist_format) unless plist_format == :ascii
      end
    end

    def read_string_encoding<commit_msg>[Reader] Fix checking for ascii<commit_after># frozen-string-literal: true
module AsciiPlist
  class Reader
    class UnsupportedPlistFormatError < Error
      attr_reader :format
      def initialize(format)
        @format = format
      end

      def to_s
        "#{format} plists are currently unsupported"
      end
    end

    class ParseError < Error
      attr_accessor :location
      attr_accessor :plist_string
    end

    attr_accessor :plist

    def self.plist_type(plist_contents)
      case plist_contents
      when /\Abplist/
        :binary
      when /\A<\?xml/
        :xml
      else
        :ascii
      end
    end

    def self.from_file(file_path)
      new(File.read(file_path))
    end

    def initialize(contents)
      @scanner = StringScanner.new(contents)
    end

    def parse!
      plist_format = ensure_ascii_plist!
      read_string_encoding
      root_object = parse_object

      eat_whitespace!
      raise_parser_error ParseError, "unrecognized characters #{@scanner.rest.inspect} after parsing" unless @scanner.eos?

      AsciiPlist::Plist.new(root_object, plist_format)
    end

    private

    def ensure_ascii_plist!
      self.class.plist_type(@scanner.string).tap do |plist_format|
        raise UnsupportedPlistFormatError.new(plist_format) unless plist_format == :ascii
      end
    end

    def read_string_encoding
{'fraction_non_alphanumeric': 0.06713132795304476, 'fraction_numerical': 0.0, 'mean_word_length': 2.5323834196891193, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4407831', 'n_tokens_mistral': 877, 'n_tokens_neox': 808, 'n_words': 191}