Skip to content

Commit e2c086d

Browse files
committed
Update Zig port to use pure Zig I/O instead of C imports
Remove @cImport usage that required libc linking and replace with native Zig 0.15 I/O API using explicit buffer management. Also update build.zig to use the new createModule() API required by Zig 0.15.
1 parent e0957c2 commit e2c086d

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

zig/build.zig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
47
const exe = b.addExecutable(.{
58
.name = "chat",
6-
.root_module = .{
9+
.root_module = b.createModule(.{
710
.root_source_file = b.path("src/main.zig"),
8-
},
11+
.target = target,
12+
.optimize = optimize,
13+
}),
914
});
1015

1116
b.installArtifact(exe);
@@ -19,7 +24,11 @@ pub fn build(b: *std.Build) void {
1924
run_step.dependOn(&run_cmd.step);
2025

2126
const tests = b.addTest(.{
22-
.root_source_file = b.path("src/chatbot.zig"),
27+
.root_module = b.createModule(.{
28+
.root_source_file = b.path("src/chatbot.zig"),
29+
.target = target,
30+
.optimize = optimize,
31+
}),
2332
});
2433

2534
const run_test = b.addRunArtifact(tests);

zig/src/main.zig

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
const std = @import("std");
22
const chatbot = @import("chatbot.zig");
3-
const c = @cImport({
4-
@cInclude("stdio.h");
5-
@cInclude("string.h");
6-
@cInclude("ctype.h");
7-
});
83

94
pub fn main() !void {
105
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
116
defer _ = gpa.deinit();
127
const allocator = gpa.allocator();
138

14-
_ = c.printf("$ Chatbot v1.0.0!\n", );
9+
// Zig 0.15 I/O: explicit buffer management
10+
var stdout_buf: [4096]u8 = undefined;
11+
var stdout = std.fs.File.stdout().writer(&stdout_buf);
12+
13+
var stdin_buf: [4096]u8 = undefined;
14+
var stdin = std.fs.File.stdin().reader(&stdin_buf);
15+
16+
try stdout.interface.print("$ Chatbot v1.0.0!\n", .{});
17+
try stdout.interface.flush();
1518

1619
// Create hash table
1720
var ht = try chatbot.HashTable.create(allocator, 65536);
@@ -25,17 +28,22 @@ pub fn main() !void {
2528
try ht.set("light", "I like light");
2629
try ht.set("What", "It is clear, ain't it?");
2730

28-
var buf: [chatbot.LineLength]u8 = undefined;
29-
3031
while (true) {
31-
_ = c.printf("\n$ (user) ", );
32+
try stdout.interface.print("\n$ (user) ", .{});
33+
try stdout.interface.flush();
3234

33-
const result = c.fgets(&buf, chatbot.LineLength, c.stdin());
34-
if (result == null) break;
35-
36-
if (@as(usize, c.strlen(&buf)) <= 1) break;
35+
// Read line using Zig 0.15 delimiter API
36+
const line = stdin.interface.takeDelimiterExclusive('\n') catch |err| {
37+
switch (err) {
38+
error.EndOfStream => break,
39+
error.StreamTooLong => {
40+
// Line too long, skip it
41+
continue;
42+
},
43+
else => return err,
44+
}
45+
};
3746

38-
const line = buf[0..c.strlen(&buf)];
3947
const trimmed = std.mem.trim(u8, line, " \t\r\n");
4048
if (trimmed.len == 0) break;
4149

@@ -45,19 +53,20 @@ pub fn main() !void {
4553
const lower_word = try allocator.alloc(u8, word.len);
4654
defer allocator.free(lower_word);
4755

48-
for (word, lower_word) |ch, *lch| {
49-
lch.* = std.ascii.toLower(ch);
56+
for (word, 0..) |ch, i| {
57+
lower_word[i] = std.ascii.toLower(ch);
5058
}
5159

5260
if (std.mem.eql(u8, lower_word, "exit")) {
5361
return;
5462
}
5563

5664
if (ht.get(lower_word)) |response| {
57-
_ = c.printf("\n$ (chatbot) %s\n", response.ptr);
65+
try stdout.interface.print("\n$ (chatbot) {s}\n", .{response});
5866
} else {
59-
_ = c.printf("\n$ (chatbot) %s\n", "Sorry, I don't know what to say about that");
67+
try stdout.interface.print("\n$ (chatbot) {s}\n", .{"Sorry, I don't know what to say about that"});
6068
}
69+
try stdout.interface.flush();
6170
}
6271
}
6372
}

0 commit comments

Comments
 (0)