Skip to content

Commit d8b65fe

Browse files
authored
Merge pull request #53 from pedropark99/revision-4
Add revision for Chapters 4, 5 and 6
2 parents bf7add2 + c10dd6b commit d8b65fe

File tree

49 files changed

+555
-545
lines changed

Some content is hidden

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

49 files changed

+555
-545
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ foo.txt
1717
*.aux
1818
*.log
1919
*.toc
20+
21+
Chapters/15-vectors_files/

Chapters/01-base64.qmd

+93-108
Large diffs are not rendered by default.

Chapters/01-memory.qmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ object you declare is stored:
394394
One key aspect about Zig, is that there are "no hidden-memory allocations" in Zig.
395395
What that really means, is that "no allocations happen behind your back in the standard library" [@zigguide].
396396

397-
This is a known problem, specially in C++. Because in C++, there are some operators that do allocate
397+
This is a known problem, especially in C++. Because in C++, there are some operators that do allocate
398398
memory behind the scene, and there is no way for you to known that, until you actually read the
399399
source code of these operators, and find the memory allocation calls.
400400
Many programmers find this behaviour annoying and hard to keep track of.

Chapters/01-zig-weird.qmd

+5-5
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ try stdout.print("{any}\n", .{c});
920920
### Runtime versus compile-time known length in slices
921921

922922
We are going to talk a lot about the differences between compile-time known
923-
and runtime known across this book, specially at @sec-compile-time.
923+
and runtime known across this book, especially at @sec-compile-time.
924924
But the basic idea is that a thing is compile-time known, when we know
925925
everything (the value, the attributes and the characteristics) about this thing at compile-time.
926926
In contrast, a runtime known thing is when the exact value of a thing is calculated only at runtime.
@@ -1065,7 +1065,7 @@ const object: []const u8 = "A string object";
10651065
Zig always assumes that this sequence of bytes is UTF-8 encoded. This might not be true for every
10661066
sequence of bytes you have it, but is not really Zig's job to fix the encoding of your strings
10671067
(you can use [`iconv`](https://www.gnu.org/software/libiconv/)[^libiconv] for that).
1068-
Today, most of the text in our modern world, specially on the web, should be UTF-8 encoded.
1068+
Today, most of the text in our modern world, especially on the web, should be UTF-8 encoded.
10691069
So if your string literal is not UTF-8 encoded, then, you will likely
10701070
have problems in Zig.
10711071

@@ -1418,7 +1418,7 @@ the data produced by all of this technology becomes one of the most important
14181418
(and also, one of the most dangerous) assets that we have.
14191419

14201420
This is probably the main reason why modern low-level programming languages
1421-
have been giving great attention to safety, specially memory safety, because
1421+
have been giving great attention to safety, especially memory safety, because
14221422
memory corruption is still the main target for hackers to exploit.
14231423
The reality is that we don't have an easy solution for this problem.
14241424
For now, we only have techniques and strategies that mitigates these
@@ -1453,7 +1453,7 @@ pattern.
14531453

14541454
In contrast, the Zig language is not a memory safe language by default.
14551455
There are some memory safety features that you get for free in Zig,
1456-
specially in arrays and pointer objects. But there are other tools
1456+
especially in arrays and pointer objects. But there are other tools
14571457
offered by the language, that are not used by default.
14581458
In other words, the `zig` compiler does not obligates you to use such tools.
14591459

@@ -1482,7 +1482,7 @@ We already learned a lot about Zig's syntax, and also, some pretty technical
14821482
details about it. Just as a quick recap:
14831483

14841484
- We talked about how functions are written in Zig at @sec-root-file and @sec-main-file.
1485-
- How to create new objects/identifiers at @sec-root-file and specially at @sec-assignments.
1485+
- How to create new objects/identifiers at @sec-root-file and especially at @sec-assignments.
14861486
- How strings work in Zig at @sec-zig-strings.
14871487
- How to use arrays and slices at @sec-arrays.
14881488
- How to import functionality from other Zig modules at @sec-root-file.

Chapters/02-debugging.qmd

+41-28
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ knitr::opts_chunk$set(
1818

1919
# Debugging Zig applications
2020

21-
Being able to debug your programs is essential to any programmer who wants to
21+
Being able to debug your applications is essential for any programmer who wants to
2222
do serious programming in any language. That is why, in this chapter, we are going to talk about the
2323
available strategies and tools to debug applications written in Zig.
2424

2525

26-
## Printing debugging
26+
## Print debugging
2727

2828
We begin with the classic and battle-tested *print debugging* strategy.
2929
The key advantage that debugging offers you is *visibility*.
3030
With *print statements* you can easily see what results and objects
31-
that are being generated within your functions.
31+
are being produced by your application.
3232

3333
That is the essence of *print debugging*. Is to use
3434
print expressions to see the values that are being generated by your program,
@@ -37,20 +37,20 @@ is behaving.
3737

3838
Many programmers often resort to the print functions in Zig, such as the `stdout.print()`,
3939
or, the `std.debug.print()`, to get a better understanding of their programs.
40-
This is an known and old strategy that is very simple and effective, and it is better known within
40+
This is a known and old strategy that is very simple and effective, and it is better known within
4141
the programming community as *print debugging*.
42-
In Zig, you can either print information to the `stdout` or `stderr` streams of your system.
42+
In Zig, you can print information to the `stdout` or `stderr` streams of your system.
4343

4444
Let's begin with `stdout`. First, you
4545
need to get access to the `stdout`, by calling the `getStdOut()` method, from
46-
the Zig standard library. This method returns a *file descriptor* object,
46+
the Zig Standard Library. This method returns a *file descriptor* object,
4747
and, through this object you can read/write to the `stdout`.
4848
I recommend you to check out all methods available in this object, by [checking the page in
4949
the Zig Standard Library Official Reference for the type `File`](https://ziglang.org/documentation/master/std/#std.fs.File)[^zig-fiile-reference].
5050

5151
[^zig-fiile-reference]: <https://ziglang.org/documentation/master/std/#std.fs.File>.
5252

53-
For our purpose here, which is to write something to the `stdout`, specially to debug our
53+
For our purpose here, which is to write something to the `stdout`, especially to debug our
5454
program, I recommend you to use the `writer()` method, which gives your a *writer* object.
5555
This *writer* object offers some helper methods to write stuff into the file descriptor object
5656
that represents the `stdout` stream. In special, the `print()` method.
@@ -64,7 +64,7 @@ in the second argument, you provide a list of values (or objects) that you want
6464
into your template message.
6565

6666
Ideally, the template string in the first argument should contain some format specifier.
67-
Each format specifier is matched to a value (or object) that you listed in the second argument.
67+
Each format specifier is matched to a value (or object) that you have listed in the second argument.
6868
So, if you provided 5 different objects in the second argument, then, the template string
6969
should contain 5 format specifiers, one for each object provided.
7070

@@ -80,7 +80,7 @@ Here is a quick list of the most used format specifiers:
8080
- `x`: for printing hexadecimal values.
8181
- `any`: use any compatible format specifier (i.e. it automatically selects a format specifier for you).
8282

83-
The code example below, gives you an example of use of this `print()` method
83+
The code example below gives you an example of use of this `print()` method
8484
with the `d` format specifier.
8585

8686
```{zig}
@@ -122,11 +122,23 @@ pub fn main() !void {
122122
Result: 50
123123
```
124124

125+
You could also achieve the exact same result by getting a file descriptor object to `stderr`,
126+
then, creating a *writer* object to `stderr`, then, using the `print()` method of this
127+
*writer* object, like in the example below:
128+
129+
```{zig}
130+
#| eval: false
131+
const std = @import("std");
132+
const stderr = std.io.getStdErr().writer();
133+
// some more lines ...
134+
_ = try stderr.print("Result: {d}", .{result});
135+
```
136+
125137

126138

127139
## Debugging through debuggers
128140

129-
Although *print debugging* is a valid and very useful strategy,
141+
Although *print debugging* being a valid and very useful strategy,
130142
most programmers prefer to use a debugger to debug their programs.
131143
Since Zig is a low-level language, you can use either GDB (GNU Debugger),
132144
or LLDB (LLVM Project Debugger) as your debugger.
@@ -136,28 +148,28 @@ You choose the debugger of your preference, and you work with it.
136148
In this book, I will use LLDB as my debugger on the examples.
137149

138150

139-
### Compile your source code in Debug mode {#sec-compile-debug-mode}
151+
### Compile your source code in debug mode {#sec-compile-debug-mode}
140152

141153
In order to debug your program through a debugger, you must compile
142-
your source code in Debug mode. Because when you compile your
143-
source code in other modes (such as Release), the compiler usually
154+
your source code in `Debug` mode. Because when you compile your
155+
source code in other modes (such as `Release`), the compiler usually
144156
strips out some essential information that is used by the debugger
145157
to read and track your program, like PDB (*Program Database*) files.
146158

147-
By compiling your source code in Debug mode, you ensure that the debugger
159+
By compiling your source code in `Debug` mode, you ensure that the debugger
148160
will find the necessary information in your program to debug it.
149-
By default, the compiler uses the Debug mode. Having this in mind,
150-
when you compile your program with the `build-exe`
151-
command (that we exposed at @sec-compile-code), if you don't specify an explicit mode through the `-O` command-line [^oargument]
152-
argument, then, the compiler will compile your code in Debug mode.
161+
By default, the compiler uses the `Debug` mode when compiling your code.
162+
Having this in mind, when you compile your program with the `build-exe`
163+
command (which was described at @sec-compile-code), if you don't specify
164+
an explicit mode through the `-O` command-line [^oargument]
165+
argument, then, the compiler will compile your code in `Debug` mode.
153166

154167
[^oargument]: See <https://ziglang.org/documentation/master/#Debug>.
155168

156169

157170
### Let's debug a program
158171

159-
As an example, let's debug some Zig code, and demonstrate
160-
how can we use LLDB to navigate and check the following
172+
As an example, let's use LLDB to navigate and investigate the following
161173
piece of Zig code:
162174

163175
```{zig}
@@ -182,7 +194,7 @@ pub fn main() !void {
182194
There is nothing wrong with this program. But it is
183195
a good start for us. First, we need to compile
184196
this program with the `zig build-exe` command.
185-
For this example, suppose that I compiled the above
197+
For this example, suppose that I have compiled the above
186198
Zig code into a binary executable called `add_program`.
187199

188200
```bash
@@ -226,12 +238,12 @@ Process 8654 stopped
226238
14 }
227239
```
228240
229-
I can start to navigate through the code, and investigating the variables
230-
that are being generated. If you not familiar with the commands
241+
I can start navigating through the code, and checking the objects
242+
that are being generated. If you are not familiar with the commands
231243
available in LLDB, I recommend you to read the official documentation
232244
of the project[^lldb].
233-
You can also look for cheat sheets. Which quickly describes all commands
234-
available for you, and, as a result, are also good resources for you[^lldb-quick-list].
245+
You can also look for cheat sheets, which quickly describes all commands
246+
available for you[^lldb-quick-list].
235247
236248
[^lldb]: <https://lldb.llvm.org/>
237249
[^lldb-quick-list]: <https://gist.github.com/ryanchang/a2f738f0c3cc6fbd71fa>.
@@ -271,11 +283,11 @@ Process 4798 stopped
271283
272284
Now, on the next line of code, we are executing the `add_and_increment()` function once again.
273285
Why not step inside this function? Shall we? We can do that, by executing the `s` LLDB command.
274-
Notice in the example below that, after executing this command, we entered inside the context of the
286+
Notice in the example below that, after executing this command, we have entered into the context of the
275287
`add_and_increment()` function.
276288
277-
Also notice in the example below that, I walked two more lines in the functions body, then,
278-
I executed the `frame variable` LLDB command, to see at once, the value stored in each of the variables
289+
Also notice in the example below that, I have walked two more lines in the function's body, then,
290+
I execute the `frame variable` LLDB command, to see at once, the value stored in each of the variables
279291
that were created inside the current scope.
280292
281293
You can see in the output below that, the object `sum` stores the value `\f`,
@@ -337,3 +349,4 @@ pub fn main() !void {
337349
338350
This function is similar to the `type()` built-in function from Python,
339351
or, the `typeof` operator in Javascript.
352+

Chapters/03-structs.qmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ knitr::opts_chunk$set(
1818
# Control flow, structs, modules and types
1919

2020
We have discussed a lot of Zig's syntax in the last chapter,
21-
specially at @sec-root-file and @sec-main-file.
21+
especially at @sec-root-file and @sec-main-file.
2222
But we still need to discuss some other very important
2323
elements of the language. Elements that you will use constantly on your day-to-day
2424
routine.

Chapters/03-unittests.qmd

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ All 1 tests passed.
106106
One of the advantages of Zig is that it offers great tools
107107
that hep us, programmers, to avoid (but also detect) memory problems, such as
108108
memory leaks and double-frees. The `defer` keyword
109-
is specially helpful in this regard.
109+
is especially helpful in this regard.
110110

111111
When developing your
112112
source code, you, the programmer, is responsible for making
@@ -245,7 +245,7 @@ All 1 tests passed.
245245
In Zig, there are some different ways you can test for an equality.
246246
You already saw that we can use `expect()` with the logical operator `==`
247247
to essentially reproduce an equality test. But we also have
248-
some helper functions that you should know about, specially
248+
some helper functions that you should know about, especially
249249
`expectEqual()`, `expectEqualSlices()` and `expectEqualStrings()`.
250250

251251

Chapters/04-http-server.qmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ into our `main.zig` module, then, I add the function calls to `send_200()`
983983
and `send_404()`.
984984

985985
Notice that I'm using if statements to decide which "response function" to call,
986-
based specially on the URI present in the HTTP Request. If the user asked for
986+
based especially on the URI present in the HTTP Request. If the user asked for
987987
a content (or a document) that is not present in our server, we should respond
988988
with a 404 status code. But since we have just a simple HTTP server, with no
989989
real documents to send, we can just check if the URI is the root path (`/`)

0 commit comments

Comments
 (0)