Skip to content

eoan-ermine/zig-strparse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zig-strparse

API Reference Linux (Zig 0.16.0) Linux (Zig master)

Generic string parsing library for Zig.

Installation

For Zig vMAJOR.MINOR.PATCH:

zig fetch --save https://github.com/eoan-ermine/zig-strparse/archive/refs/tags/<REPLACE ME>.tar.gz

For Zig master branch:

zig fetch --save git+https://github.com/eoan-ermine/zig-strparse

Then add the following to build.zig:

const strparse = b.dependency("zig_strparse", .{});
exe.root_module.addImport("strparse", strparse.module("strparse"));

Examples

const Point = struct {
    x: i32,
    y: i32,

    pub const ParseError = error{
        InvalidFormat,
    } || std.fmt.ParseIntError;

    pub fn parse(s: []const u8) ParseError!Point {
        var it = std.mem.splitScalar(u8, s, ',');

        const x_str = it.next() orelse return error.InvalidFormat;
        const y_str = it.next() orelse return error.InvalidFormat;

        if (it.next() != null)
            return error.InvalidFormat;

        return .{
            .x = try std.fmt.parseInt(i32, x_str, 10),
            .y = try std.fmt.parseInt(i32, y_str, 10),
        };
    }
};

pub fn main() !void {
    const x = try strparse.parse(i32, "42");
    const y = try strparse.parse(f64, "3.14");
    const z = try strparse.parse(Point, "10,20");

    std.log.info("x: {}", .{x});
    std.log.info("y: {}", .{y});
    std.log.info("z: {},{}", .{ z.x, z.y });
}

About

Generic string parsing library for Zig

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages