<commit_before>(ns bencode.decoder
  (:require [clojure [edn :as edn]])
  (:use [bencode.error]))

(defn- digit?
  "Returns whether ch is a digit, e.g., value between 0 and 9."
  [ch]
  (let [i (int ch)]
    (and (>= i 48) (<= i 57))))

(defmulti bdecode-type
  "Dispatches to the proper decoder method according to the first element
of seq."
  (fn [seq _]
    (let [f (first seq)]
      (cond
       (= f \i)   ::int
       (= f \l)   ::seq
       (= f \d)   ::dict
       (digit? f) ::str
       :else      ::unknown))))

(defn- read-digits
  "Reads from seq until a non-digit char is found, and returns an array
with the number's digits at index 0 and the remaining of seq at index 1."
  [seq]
  (loop [data [] rem seq]
    (let [f (first rem)]
      (if (digit? f)
        (recur (conj data f) (rest rem))
        [(apply str data) rem]))))

(defn- invalid-number?
  "Returns whether digits reprents an invalid number according to the spec."
  [digits]
  (or (empty? digits)
      (and (> (count digits) 1) (= \0 (first digits)))))

(defn- bdecode-dict-entry
  "Bdecodes a dictionary entry from sequence seq, where the key and its
corresponding value are concatenated."
  [seq opts]
  (let [[key rem] (bdecode-type seq opts)
        [val rem] (bdecode-type rem opts)]
    [key val rem]))

(defn- dict-key
  "Returns the final dictionary key according to the options defined in
opts."
  [key opts]
  (if (:str-keys? opts)
    key
    (keyword key)))

(defmethod bdecode-type ::unknown [seq opts]<commit_msg>Fixed comment typo.<commit_after>(ns bencode.decoder
  (:require [clojure [edn :as edn]])
  (:use [bencode.error]))

(defn- digit?
  "Returns whether ch is a digit, e.g., value between 0 and 9."
  [ch]
  (let [i (int ch)]
    (and (>= i 48) (<= i 57))))

(defmulti bdecode-type
  "Dispatches to the proper decoder method according to the first element
of seq."
  (fn [seq _]
    (let [f (first seq)]
      (cond
       (= f \i)   ::int
       (= f \l)   ::seq
       (= f \d)   ::dict
       (digit? f) ::str
       :else      ::unknown))))

(defn- read-digits
  "Reads from seq until a non-digit char is found, and returns an array
with the number's digits at index 0 and the remaining of seq at index 1."
  [seq]
  (loop [data [] rem seq]
    (let [f (first rem)]
      (if (digit? f)
        (recur (conj data f) (rest rem))
        [(apply str data) rem]))))

(defn- invalid-number?
  "Returns whether digits represents an invalid number according to the spec."
  [digits]
  (or (empty? digits)
      (and (> (count digits) 1) (= \0 (first digits)))))

(defn- bdecode-dict-entry
  "Bdecodes a dictionary entry from sequence seq, where the key and its
corresponding value are concatenated."
  [seq opts]
  (let [[key rem] (bdecode-type seq opts)
        [val rem] (bdecode-type rem opts)]
    [key val rem]))

(defn- dict-key
  "Returns the final dictionary key according to the options defined in
opts."
  [key opts]
  (if (:str-keys? opts)
    key
    (keyword key)))

(defmethod bdecode-type ::unknown [seq opts]
{'fraction_non_alphanumeric': 0.13969616908850727, 'fraction_numerical': 0.0066050198150594455, 'mean_word_length': 2.7580645161290325, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2191587', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1026, 'n_words': 413}