aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-04-19 21:45:18 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-04-19 21:45:18 +0200
commitb9d58fbbd94dc8f97f9f4c6c333039386bbbeaa1 (patch)
treecdf2b3454978614c420ec3454be25e182b6dea8a /src
parentec6f949c48323a0bea244b723af547f60e0db33f (diff)
feat(lib): zig specific function
Diffstat (limited to 'src')
-rw-r--r--src/link.zig18
-rw-r--r--src/root.zig16
2 files changed, 19 insertions, 15 deletions
diff --git a/src/link.zig b/src/link.zig
index fd29cb5..74cf08c 100644
--- a/src/link.zig
+++ b/src/link.zig
@@ -12,16 +12,13 @@ const doTestError = testing.doError;
pub const Error = error{InvalidLink} || Lexer.Error || content.Error;
pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
- var el = try Element.init(alloc, .content, "a");
- errdefer el.deinit();
const data = try parseData(alloc, l);
- const second = data.second orelse {
- el.deinit();
- return data.first.?;
- };
+ const second = data.second orelse return data.first.?;
defer alloc.free(second);
var in = if (data.first) |first| first else try Element.initLitEscaped(alloc, second);
errdefer in.deinit();
+ var el = try Element.init(alloc, .content, "a");
+ errdefer el.deinit();
try el.appendContent(in);
try el.setAttribute("href", second);
return el;
@@ -33,16 +30,15 @@ pub const Data = struct {
};
pub fn parseData(alloc: Allocator, l: *Lexer) Error!Data {
- var el = Element.initEmpty(alloc);
- errdefer el.deinit();
var v = (try l.next(alloc)).?;
defer v.deinit();
if (v.kind != .link) return Error.InvalidLink;
if (!eql(u8, v.content.items, "[")) {
- const first = try Element.initLitEscaped(alloc, v.content.items);
- el.deinit();
- return .{ .first = first, .second = null };
+ const el = try Element.initLitEscaped(alloc, v.content.items);
+ return .{ .first = el, .second = null };
}
+ var el = Element.initEmpty(alloc);
+ errdefer el.deinit();
while (l.nextKind()) |kind| {
switch (kind) {
.weak_delimiter, .strong_delimiter => return Error.InvalidLink,
diff --git a/src/root.zig b/src/root.zig
index 152170e..4750814 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -1,8 +1,8 @@
const std = @import("std");
-pub const parser = @import("parser.zig");
+const parser = @import("parser.zig");
pub const Error = parser.Error;
-fn getErrorCode(err: Error) u8 {
+inline fn getErrorCode(err: Error) u8 {
return switch (err) {
Error.OutOfMemory => 1,
Error.InvalidUtf8 => 2,
@@ -34,9 +34,10 @@ export fn getErrorString(code: u8) [*:0]const u8 {
/// Returns a not null strings and set the code to 0 if everything is fine.
/// Else, it returns null and set an error code above 0.
/// Use getErrorString to retrieve the string linked with the error code.
+/// Use zigParse if you are in Zig.
export fn parse(content: [*:0]const u8, code: *u8) ?[*:0]const u8 {
const alloc = std.heap.c_allocator;
- const res = parser.parse(alloc, std.mem.span(content)) catch |err| {
+ const res = zigParse(alloc, std.mem.span(content)) catch |err| {
code.* = getErrorCode(err);
return null;
};
@@ -48,11 +49,18 @@ export fn parse(content: [*:0]const u8, code: *u8) ?[*:0]const u8 {
};
}
+/// Parse the content.
+///
+/// Use parse if you are not in Zig.
+pub fn zigParse(alloc: std.mem.Allocator, content: []const u8) Error![]const u8 {
+ return parser.parse(alloc, content);
+}
+
test {
std.testing.refAllDeclsRecursive(@This());
}
-fn doTest(content: [*:0]const u8, exp: []const u8, exp_code: u8) !void {
+fn doTest(content: [*:0]const u8, exp: []const u8, comptime exp_code: u8) !void {
const expect = std.testing.expect;
var code: u8 = undefined;