<commit_before># frozen_string_literal: true

require "nkf"
require "unicode/display_width"
require "json"
require "fileutils"

module Textbringer
  class Buffer
    extend Enumerable

    attr_accessor :mode, :keymap
    attr_reader :name, :file_name, :file_encoding, :file_format, :point, :marks
    attr_reader :current_line, :current_column, :visible_mark

    GAP_SIZE = 256
    UNDO_LIMIT = 1000

    UTF8_CHAR_LEN = Hash.new(1)
    [
      [0xc0..0xdf, 2],
      [0xe0..0xef, 3],
      [0xf0..0xf4, 4]
    ].each do |range, len|
      range.each do |c|
        UTF8_CHAR_LEN[c.chr] = len
      end
    end

    @@auto_detect_encodings = [
      Encoding::UTF_8,
      Encoding::EUC_JP,
      Encoding::Windows_31J
    ]

    DEFAULT_DETECT_ENCODING = ->(s) {
      @@auto_detect_encodings.find { |e|
        s.force_encoding(e)
        s.valid_encoding?
      }
    }

    NKF_DETECT_ENCODING = ->(s) {
      e = NKF.guess(s)
      e == Encoding::US_ASCII ? Encoding::UTF_8 : e
    }

    @@detect_encoding_proc = DEFAULT_DETECT_ENCODING

    @@table = {}
    @@list = []
    @@current = nil
    @@minibuffer = nil
    @@global_mark_ring = nil

    def self.auto_detect_encodings
      @@auto_detect_encodings
    end

    def self.auto_detect_encodings=(encodings)
      @@auto_detect_encodings = encodings<commit_msg>NKF.guess may return ASCII-8BIT for UTF-8 strings.<commit_after># frozen_string_literal: true

require "nkf"
require "unicode/display_width"
require "json"
require "fileutils"

module Textbringer
  class Buffer
    extend Enumerable

    attr_accessor :mode, :keymap
    attr_reader :name, :file_name, :file_encoding, :file_format, :point, :marks
    attr_reader :current_line, :current_column, :visible_mark

    GAP_SIZE = 256
    UNDO_LIMIT = 1000

    UTF8_CHAR_LEN = Hash.new(1)
    [
      [0xc0..0xdf, 2],
      [0xe0..0xef, 3],
      [0xf0..0xf4, 4]
    ].each do |range, len|
      range.each do |c|
        UTF8_CHAR_LEN[c.chr] = len
      end
    end

    @@auto_detect_encodings = [
      Encoding::UTF_8,
      Encoding::EUC_JP,
      Encoding::Windows_31J
    ]

    DEFAULT_DETECT_ENCODING = ->(s) {
      @@auto_detect_encodings.find { |e|
        s.force_encoding(e)
        s.valid_encoding?
      }
    }

    NKF_DETECT_ENCODING = ->(s) {
      e = NKF.guess(s)
      case e
      when Encoding::US_ASCII
        Encoding::UTF_8
      when Encoding::ASCII_8BIT
        s.force_encoding(Encoding::UTF_8)
        if s.valid_encoding?
          Encoding::UTF_8
        else
          s.force_encoding(Encoding::ASCII_8BIT)
          Encoding::ASCII_8BIT
        end
      else
        e
      end
    }

    @@detect_encoding_proc = DEFAULT_DETECT_ENCODING

    @@table = {}
    @@list = []
    @@current = nil
    @@minibuffer = nil
    @@global_mark_ring = nil

    def self.auto_detect_encodings
      @@auto_detect_encodings
    end

    def self.auto_detect_encodings=(encodings)
      @@auto_detect_encodings = encodings
{'fraction_non_alphanumeric': 0.10845402492421691, 'fraction_numerical': 0.02054563826204109, 'mean_word_length': 2.514792899408284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '631054', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1069, 'n_words': 199}