Skip to content

Commit

Permalink
feat: support zig master (#36)
Browse files Browse the repository at this point in the history
* feat: support zig master

* update README
  • Loading branch information
jiacai2050 committed Jan 1, 2024
1 parent 53562ec commit 7c6cb83
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 26 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/ci.yml
Expand Up @@ -2,6 +2,8 @@ name: CI

on:
workflow_dispatch:
schedule:
- cron: '10 20 * * *'
pull_request:
paths:
- "**.zig"
Expand All @@ -21,12 +23,12 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
zig: [0.11.0, master]
steps:
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
# version: master
version: ${{ matrix.zig }}
- name: Start services
uses: ikalnytskyi/action-setup-postgres@v4
with:
Expand All @@ -41,13 +43,16 @@ jobs:
if: matrix.os == 'macos-latest'
run: |
echo "PKG_CONFIG_PATH=$(brew --prefix)/opt/libpq/lib/pkgconfig" >> ${GITHUB_ENV}
- name: Hack
if: matrix.zig == '0.11.0'
run: |
# 0.11.0 will report following error with zon file, so delete now.
# error: TarUnsupportedFileType
rm build.zig.zon
- name: Run examples(Unix)
if: matrix.os != 'windows-latest'
run: |
# TODO: 0.11.0 will report following error with zon file, so delete now.
# error: TarUnsupportedFileType
pkg-config --libs --cflags libpq
rm build.zig.zon
zig fmt --check src/
zig build run-all --summary all
Expand Down
4 changes: 2 additions & 2 deletions .tool-versions
@@ -1,2 +1,2 @@
zig 0.11.0
# zig master
# zig 0.11.0
zig master
6 changes: 6 additions & 0 deletions book-src/15-02-string.md
@@ -1 +1,7 @@
# String Parsing

## String to number/enum

```zig
{{#include ../src/15-02.zig }}
```
3 changes: 1 addition & 2 deletions book-src/SUMMARY.md
Expand Up @@ -25,7 +25,6 @@
- [Web Programming]()

- [Make HTTP requests](./05-01-http-requests.md)
- [Download]()

- [Algorithms]()

Expand Down Expand Up @@ -68,4 +67,4 @@

- [Text Processing]()
- [Regex Expressions](15-01-regex.md)
- [String](15-02-string.md)
- [String Parsing](15-02-string.md)
6 changes: 4 additions & 2 deletions book-src/intro.md
Expand Up @@ -3,17 +3,19 @@
[![](https://github.com/zigcc/zig-cookbook/actions/workflows/ci.yml/badge.svg)](https://github.com/zigcc/zig-cookbook/actions/workflows/ci.yml)
[![](https://github.com/zigcc/zig-cookbook/actions/workflows/pages.yml/badge.svg)](https://github.com/zigcc/zig-cookbook/actions/workflows/pages.yml)

[Zig cookbook](https://zigcc.github.io/zig-cookbook/) is a collection of simple Zig programs that demonstrate good practices to accomplish common programming tasks.
[Zig cookbook](https://github.com/zigcc/zig-cookbook) is a collection of simple Zig programs that demonstrate good practices to accomplish common programming tasks.

# How to use

[The website](https://zigcc.github.io/zig-cookbook/) is generated by [mdbook](https://rust-lang.github.io/mdBook/), `mdbook serve` will start a server at `http://localhost:3000` for preview.

Each recipe is accompanied by an illustrative example named after its corresponding sequence number. These examples can be executed using the command `zig build run-{chapter-num}-{sequence-num}`, or `zig build run-all` to execute all.

> - Currently Zig 0.11.0 is required to run those examples.
> - Currently both 0.11.0 and master are supported to run those recipes.
> - Some example may depend on system libraries, use `make install-deps` to install them.
> Note: Some recipes can't compile in 0.11.0, so they are skipped.
# Contributing

This cookbook is a work in progress, and we welcome contributions from the community. If you have a favorite recipe that you'd like to share, please submit a [pull request](https://github.com/zigcc/zig-cookbook/pulls).
Expand Down
15 changes: 11 additions & 4 deletions build.zig
Expand Up @@ -53,10 +53,17 @@ fn addExample(b: *std.Build, run_all: *std.build.Step) !void {
.target = .{},
});
lib.addIncludePath(.{ .path = "lib" });
lib.addCSourceFiles(
&.{"lib/regex_slim.c"},
&.{"-std=c99"},
);
if (gt_zig_0_11) {
lib.addCSourceFiles(.{
.files = &.{"lib/regex_slim.c"},
.flags = &.{"-std=c99"},
});
} else {
lib.addCSourceFiles(
&.{"lib/regex_slim.c"},
&.{"-std=c99"},
);
}
lib.linkLibC();
exe.linkLibrary(lib);
exe.addIncludePath(.{ .path = "lib" });
Expand Down
2 changes: 1 addition & 1 deletion src/01-02.zig
Expand Up @@ -24,7 +24,7 @@ pub fn main() !void {
const md = try file.metadata();
print("File size: {d}\n", .{md.size()});

var ptr = try std.os.mmap(
const ptr = try std.os.mmap(
null,
20,
std.os.PROT.READ | std.os.PROT.WRITE,
Expand Down
13 changes: 8 additions & 5 deletions src/01-03.zig
Expand Up @@ -4,21 +4,20 @@ const std = @import("std");
const builtin = @import("builtin");
const fs = std.fs;
const print = std.debug.print;

const current_zig = builtin.zig_version;
const is_zig_11 = builtin.zig_version.minor == 11;

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

// There are API changes between 0.11.0 and master, so we need to use different APIs depending on the version.
var iter_dir = if (comptime current_zig.minor == 11)
var iter_dir = if (is_zig_11)
try fs.cwd().openIterableDir(".", .{
.no_follow = true, // `true` means it won't dereference the symlinks.
})
else
fs.cwd().openDir(".", .{ .iterate = true });
try fs.cwd().openDir("src", .{ .iterate = true });
defer iter_dir.close();

var walker = try iter_dir.walk(allocator);
Expand All @@ -30,7 +29,11 @@ pub fn main() !void {
continue;
}

const stat = try iter_dir.dir.statFile(entry.path);
const stat = if (is_zig_11)
try iter_dir.dir.statFile(entry.path)
else
try iter_dir.statFile(entry.path);

const last_modified = stat.mtime;
const duration = now - last_modified;
if (duration < std.time.ns_per_hour * 24) {
Expand Down
4 changes: 4 additions & 0 deletions src/05-01.zig
@@ -1,8 +1,12 @@
const std = @import("std");
const print = std.debug.print;
const http = std.http;
const is_zig_11 = @import("builtin").zig_version.minor == 11;

pub fn main() !void {
if (!is_zig_11) {
return;
}
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
Expand Down
4 changes: 2 additions & 2 deletions src/10-02.zig
Expand Up @@ -12,15 +12,15 @@ pub fn main() !void {

// Encode
const encoded_length = Encoder.calcSize(src.len);
var encoded_buffer = try allocator.alloc(u8, encoded_length);
const encoded_buffer = try allocator.alloc(u8, encoded_length);
defer allocator.free(encoded_buffer);

_ = Encoder.encode(encoded_buffer, src);
try std.testing.expectEqualStrings("aGVsbG8gemln", encoded_buffer);

// Decode
const decoded_length = try Decoder.calcSizeForSlice(encoded_buffer);
var decoded_buffer = try allocator.alloc(u8, decoded_length);
const decoded_buffer = try allocator.alloc(u8, decoded_length);
defer allocator.free(decoded_buffer);

try Decoder.decode(decoded_buffer, encoded_buffer);
Expand Down
6 changes: 3 additions & 3 deletions src/11-01.zig
Expand Up @@ -4,9 +4,9 @@ const expectEqual = std.testing.expectEqual;
const Complex = std.math.Complex;

pub fn main() !void {
var complex_integer = Complex(i32).init(10, 20);
var complex_integer2 = Complex(i32).init(5, 17);
var complex_float = Complex(f32).init(10.1, 20.1);
const complex_integer = Complex(i32).init(10, 20);
const complex_integer2 = Complex(i32).init(5, 17);
const complex_float = Complex(f32).init(10.1, 20.1);

print("Complex integer: {}\n", .{complex_integer});
print("Complex float: {}\n", .{complex_float});
Expand Down
31 changes: 31 additions & 0 deletions src/15-02.zig
@@ -0,0 +1,31 @@
const std = @import("std");
const expectEqual = std.testing.expectEqual;
const expectError = std.testing.expectError;
const parseInt = std.fmt.parseInt;
const parseFloat = std.fmt.parseFloat;
const stringToEnum = std.meta.stringToEnum;

pub fn main() !void {
try expectEqual(parseInt(i32, "123", 10), 123);
try expectEqual(parseInt(i32, "-123", 10), -123);
try expectError(error.Overflow, parseInt(u4, "123", 10));

// 0 means auto detect the base.
// base = 16
try expectEqual(parseInt(i32, "0xF", 0), 15);
// base = 2
try expectEqual(parseInt(i32, "0b1111", 0), 15);
// base = 8
try expectEqual(parseInt(i32, "0o17", 0), 15);

try expectEqual(parseFloat(f32, "1.23"), 1.23);
try expectEqual(parseFloat(f32, "-1.23"), -1.23);

const Color = enum {
Red,
Blue,
Green,
};
try expectEqual(stringToEnum(Color, "Red").?, Color.Red);
try expectEqual(stringToEnum(Color, "Yello"), null);
}

0 comments on commit 7c6cb83

Please sign in to comment.