Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

1.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

export function parseCsv(text: string): string[][] {
  // Tauri text reads keep the BOM our exports write; File.text() strips it.
  if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
  const rows: string[][] = [];
  let row: string[] = [];
  let i = 0;

  function finishRow() {
    rows.push(row);
    row = [];
  }

  while (i < text.length) {
    if (text[i] === '"') {
      i++;
      let cell = "";
      while (i < text.length) {
        if (text[i] === '"') {
          if (text[i + 1] === '"') {
            cell += '"';
            i += 2;
          } else {
            i++;
            break;
          }
        } else {
          cell += text[i++];
        }
      }
      row.push(cell);
      if (text[i] === ",") { i++; }
      else if (text[i] === "\r") { i++; if (text[i] === "\n") i++; finishRow(); }
      else if (text[i] === "\n") { i++; finishRow(); }
    } else if (text[i] === "\r") {
      i++;
      if (text[i] === "\n") i++;
      row.push("");
      finishRow();
    } else if (text[i] === "\n") {
      i++;
      row.push("");
      finishRow();
    } else {
      let cell = "";
      while (i < text.length && text[i] !== "," && text[i] !== "\r" && text[i] !== "\n") {
        cell += text[i++];
      }
      row.push(cell);
      if (text[i] === ",") { i++; }
      else if (text[i] === "\r") { i++; if (text[i] === "\n") i++; finishRow(); }
      else if (text[i] === "\n") { i++; finishRow(); }
    }
  }
  if (row.length > 0) rows.push(row);
  return rows;
}