Skip to content

Commit d290bb0

Browse files
authored
Merge pull request #199 from pedropark99/dev-fix-chap4
Update book to Zig 0.16
2 parents 61fac33 + 25faee1 commit d290bb0

File tree

54 files changed

+1828
-1310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1828
-1310
lines changed

Assets/contributors.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ _blf_,@bengtfrost
2828
Karl Gaissmaier,@gaissmai
2929
Ben Scheirman,@subdigital
3030
Ariel Otilibili,@ariel-anieli
31+
kitajusSus,@kitajusSus
32+
Luciogi,@Luciogi
33+
Anish Gowda,@anishgowda21

Chapters/01-base64.qmd

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ to complete the window of 24 bits that the base64 algorithm likes to work on. Th
170170
the algorithm does, is to check how to divide the input bytes into groups of 6 bits.
171171

172172
If the algorithm notices that there is a group of 6 bits that it's not complete, meaning that, this group contains $nbits$, where $0 < nbits < 6$,
173-
174173
the algorithm simply adds extra zeros in this group to fill the space that it needs.
175174
That is why in @fig-base64-algo1, in the third group after the 6-bit transformation,
176175
2 extra zeros were added to fill the gap.
@@ -701,14 +700,17 @@ for brevity reasons.
701700
fn _char_index(self: Base64, char: u8) u8 {
702701
if (char == '=')
703702
return 64;
704-
var index: u8 = 0;
705-
for (0..63) |i| {
703+
704+
var i: u8 = 0;
705+
var output_index: u8 = 0;
706+
707+
while (i < 64) : (i += 1) {
706708
if (self._char_at(i) == char)
707709
break;
708-
index += 1;
710+
output_index += 1;
709711
}
710712
711-
return index;
713+
return output_index;
712714
}
713715
```
714716

Chapters/01-memory.qmd

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ knitr::opts_chunk$set(
1616

1717

1818

19-
# Memory and Allocators
19+
# Memory and Allocators {#sec-memory-chap}
2020

2121

2222
In this chapter, we will talk about memory. How does Zig control memory? What
@@ -685,7 +685,7 @@ _ = in1; _ = in2; _ = in3;
685685

686686
In the code example below, we are accessing the `stdin`, which is
687687
the standard input channel, to receive an input from the
688-
user. We read the input given by the user with the `readSliceAll()`
688+
user. We read the next line of input given by the user with the `takeDelimiterExclusive()`
689689
method.
690690

691691
Now, after reading the input of the user, we need to store this input somewhere in
@@ -715,24 +715,25 @@ can allocate the same array in the heap by using the expression `var input = try
715715
#| auto_main: false
716716
#| build_type: "lib"
717717
const std = @import("std");
718+
const io = std.testing.io;
718719
var stdin_buffer: [1024]u8 = undefined;
719-
var stdin_reader = std.fs.File.stdin().reader(&stdin_buffer);
720+
var stdin_reader = std.fs.File.stdin().reader(io, &stdin_buffer);
720721
const stdin = &stdin_reader.interface;
721722
722723
pub fn main() !void {
723724
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
724725
const allocator = gpa.allocator();
726+
725727
var input = try allocator.alloc(u8, 50);
726728
defer allocator.free(input);
727729
@memset(input[0..], 0);
728730
729-
// Read user input
730-
stdin.readSliceAll(input[0..]) catch |err| switch(err) {
731-
// Reached end of input, do nothing else
732-
error.EndOfStream => {},
733-
// If it's other kind of error, then return it
734-
else => return err,
735-
};
731+
// Read user input until a line break is found.
732+
const stream = try stdin.takeDelimiterExclusive('\n');
733+
// Store the input into our heap memory.
734+
@memcpy(input[0..stream.len], stream[0..]);
735+
// Print our heap memory, so that we can see what
736+
// was stored in it.
736737
std.debug.print("{s}\n", .{input});
737738
}
738739
```

Chapters/01-zig-weird.qmd

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,14 +1101,13 @@ const std = @import("std");
11011101
const builtin = @import("builtin");
11021102
11031103
fn read_file(allocator: std.mem.Allocator, path: []const u8) ![]u8 {
1104-
var file_buffer = try allocator.alloc(u8, 1024);
1105-
@memset(file_buffer[0..], 0);
1106-
11071104
const file = try std.fs.cwd().openFile(path, .{});
11081105
defer file.close();
11091106
1110-
var reader = file.reader();
1111-
const nbytes = try reader.read(file_buffer[0..]);
1107+
var file_buffer = try allocator.alloc(u8, 1024);
1108+
@memset(file_buffer[0..], 0);
1109+
1110+
const nbytes = try file.read(file_buffer[0..]);
11121111
return file_buffer[0..nbytes];
11131112
}
11141113

0 commit comments

Comments
 (0)