<commit_before>package com.busymachines.commons

import scala.language.postfixOps
import scala.collection.mutable.ArrayBuffer
import java.io.Reader
import java.io.BufferedReader

/**
 * CSV parser compatible with RFC4180
 * @see http://tools.ietf.org/html/rfc4180
 * @author <NAME>
 */
object CsvParser {
  
  def parse(reader : Reader, separator: Char = ',') : Array[Array[String]] = {
    val bufferedReader = new BufferedReader(reader)
    val result = ArrayBuffer[Array[String]]()
    var currentLine = ArrayBuffer[String]()
    val currentField = new StringBuilder
    var current = bufferedReader.read
    var peek = bufferedReader.read
    def goNext() { current = peek; peek = bufferedReader.read; if (current == '\r') if (peek == '\n') goNext() else current = '\n' }
    def skipWhitespace() = while (current == ' ' || current == '\t') goNext()
    while (current != -1) {
      currentLine.clear()
      skipWhitespace()
      while (current != '\n' && current != -1) {
        currentField.clear()
        if (current == '"') {
          goNext()
          while (current != -1 && (current != '"' || peek == '"')) {
            if (current == '"') goNext()
            currentField append current.asInstanceOf[Char]
            goNext()
          }
          if (current == '"') goNext()
          skipWhitespace()
        }
        else {
          while (current != -1 && current != separator && current != '\n') {
            currentField append current.asInstanceOf[Char]
            goNext()
          }
        }
        if (current == separator) goNext()
        skipWhitespace()
        currentLine += currentField.toString.trim
      }
      if (current == '\n') goNext()
      if (!currentLine.isEmpty) 
        result += currentLine.toArray
    }
    result.toArray
  }
}<commit_msg>bugfix: close reader after reading CSV (file)<commit_after>package com.busymachines.commons

import scala.language.postfixOps
import scala.collection.mutable.ArrayBuffer
import java.io.Reader
import java.io.BufferedReader

/**
 * CSV parser compatible with RFC4180
 * @see http://tools.ietf.org/html/rfc4180
 * @author <NAME>
 */
object CsvParser {
  
  def parse(reader : Reader, separator: Char = ',') : Array[Array[String]] = {
    val bufferedReader = new BufferedReader(reader)
    val result = ArrayBuffer[Array[String]]()
    var currentLine = ArrayBuffer[String]()
    val currentField = new StringBuilder
    var current = bufferedReader.read
    var peek = bufferedReader.read
    def goNext() { current = peek; peek = bufferedReader.read; if (current == '\r') if (peek == '\n') goNext() else current = '\n' }
    def skipWhitespace() = while (current == ' ' || current == '\t') goNext()
    while (current != -1) {
      currentLine.clear()
      skipWhitespace()
      while (current != '\n' && current != -1) {
        currentField.clear()
        if (current == '"') {
          goNext()
          while (current != -1 && (current != '"' || peek == '"')) {
            if (current == '"') goNext()
            currentField append current.asInstanceOf[Char]
            goNext()
          }
          if (current == '"') goNext()
          skipWhitespace()
        }
        else {
          while (current != -1 && current != separator && current != '\n') {
            currentField append current.asInstanceOf[Char]
            goNext()
          }
        }
        if (current == separator) goNext()
        skipWhitespace()
        currentLine += currentField.toString.trim
      }
      if (current == '\n') goNext()
      if (!currentLine.isEmpty) 
        result += currentLine.toArray
    }
    bufferedReader.close()
    result.toArray
  }
}
{'fraction_non_alphanumeric': 0.1439084219133279, 'fraction_numerical': 0.006541291905151268, 'mean_word_length': 2.3824884792626726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1458615', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1047, 'n_words': 263}