Coder Social home page Coder Social logo

zig-csv's Introduction

zig-csv CI

Low-level CSV parser library for Zig language. Each non-empty line in input is parsed as one or more tokens of type field, followed by row_end.

This library was conceived as Zig learning project and it was not used by me in production software.

Features

  • Reads UTF-8 files.
  • Provides iterator interface to stream of tokens.
  • Handles quoted fields in which column/row separator can be used. Quote itself can be used in field by doubling it (e.g. "This is quote: "".")
  • Configurable column separator (default ,), row separator (\n) and quote (").
    • Currently only single byte characters.
  • Parser does not allocate โ€“ caller provides a buffer that parser operates in. Buffer must be longer than a longest field in input.

Example

Following code reads CSV tokens from a file while very naively printing them as table to standard output.

const std = @import("std");
const csv = @import("csv");

pub fn main() anyerror!void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();

    const allocator = &arena.allocator;
    var buffer = try allocator.alloc(u8, 4096);

    const args = try std.process.argsAlloc(allocator);
    defer std.process.argsFree(allocator, args);

    if (args.len != 2) {
        std.log.warn("Single arg is expected", .{});
        std.process.exit(1);
    }

    const file = try std.fs.cwd().openFile(args[1], .{});
    defer file.close();

    var csv_tokenizer = try csv.CsvTokenizer(std.fs.File.Reader).init(file.reader(), buffer, .{});
    const stdout = std.io.getStdOut().writer();

    while (try csv_tokenizer.next()) |token| {
        switch (token) {
            .field => |val| {
                try stdout.writeAll(val);
                try stdout.writeAll("\t");
            },
            .row_end => {
                try stdout.writeAll("\n");
            },
        }
    }
}

zig-csv's People

Contributors

beho avatar deins avatar nitinprakash96 avatar rofrol avatar xdbronch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.