mirror of
https://codeberg.org/ziglings/exercises.git
synced 2025-06-08 18:17:03 +09:00
Space sentinel
This commit is contained in:
parent
0d46acfa02
commit
2bf830a4d5
3 changed files with 27 additions and 16 deletions
|
@ -913,7 +913,7 @@ const exercises = [_]Exercise{
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "076_sentinels.zig",
|
.main_file = "076_sentinels.zig",
|
||||||
.output = "Array:123056. Many-item pointer:123.",
|
.output = "Array:[ 1 2 3 0 5 6 ]. Many-item pointer:[ 1 2 3 ]. Array:[ 30 31 32 20 34 35 ]. Many-item pointer:[ 30 31 32 ].",
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "077_sentinels2.zig",
|
.main_file = "077_sentinels2.zig",
|
||||||
|
|
|
@ -48,22 +48,33 @@ pub fn main() void {
|
||||||
// And here's a zero-terminated many-item pointer:
|
// And here's a zero-terminated many-item pointer:
|
||||||
const ptr: [*:0]u32 = &nums;
|
const ptr: [*:0]u32 = &nums;
|
||||||
|
|
||||||
// For fun, let's replace the value at position 3 with the
|
// Just to show not everything must be zero-terminated, here
|
||||||
// sentinel value 0. This seems kind of naughty.
|
// is a space-terminated array of u8 values 👾:
|
||||||
nums[3] = 0;
|
var space = [_:' ']u8{ '0', '1', '2', '3', '4', '5' };
|
||||||
|
|
||||||
// So now we have a zero-terminated array and a many-item
|
// And here's a space-terminated many-item pointer:
|
||||||
// pointer that reference the same data: a sequence of
|
const spaceptr: [*:' ']u8 = &space;
|
||||||
// numbers that both ends in and CONTAINS the sentinel value.
|
|
||||||
|
// For fun, let's replace the value at position 3 with the
|
||||||
|
// sentinel value. This seems kind of naughty.
|
||||||
|
nums[3] = 0;
|
||||||
|
space[3] = ' ';
|
||||||
|
|
||||||
|
// Now we have a zero-terminated array, a space-terminated
|
||||||
|
// array, and a couple of many-item pointers that reference
|
||||||
|
// the same data: sequences of numbers that both end in and
|
||||||
|
// CONTAIN the sentinel value.
|
||||||
//
|
//
|
||||||
// Attempting to loop through and print both of these should
|
// Attempting to loop through and print both of these should
|
||||||
// demonstrate how they are similar and different.
|
// demonstrate how they are similar and different.
|
||||||
//
|
//
|
||||||
// (It turns out that the array prints completely, including
|
// (It turns out that the array prints completely, including
|
||||||
// the sentinel 0 in the middle. The many-item pointer stops
|
// the sentinel in the middle. The many-item pointer stops
|
||||||
// at the first sentinel value.)
|
// at the first sentinel value.)
|
||||||
printSequence(nums);
|
printSequence(nums);
|
||||||
printSequence(ptr);
|
printSequence(ptr);
|
||||||
|
printSequence(space);
|
||||||
|
printSequence(spaceptr);
|
||||||
|
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
}
|
}
|
||||||
|
@ -79,27 +90,27 @@ fn printSequence(my_seq: anytype) void {
|
||||||
// depending on which type of my_seq was passed in:
|
// depending on which type of my_seq was passed in:
|
||||||
switch (my_typeinfo) {
|
switch (my_typeinfo) {
|
||||||
.Array => {
|
.Array => {
|
||||||
print("Array:", .{});
|
print("Array:[", .{});
|
||||||
|
|
||||||
// Loop through the items in my_seq.
|
// Loop through the items in my_seq.
|
||||||
for (???) |s| {
|
for (???) |s| {
|
||||||
print("{}", .{s});
|
print(" {x}", .{s});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.Pointer => {
|
.Pointer => {
|
||||||
// Check this out - it's pretty cool:
|
// Check this out - it's pretty cool:
|
||||||
const my_sentinel = sentinel(@TypeOf(my_seq));
|
const my_sentinel = sentinel(@TypeOf(my_seq));
|
||||||
print("Many-item pointer:", .{});
|
print("Many-item pointer:[", .{});
|
||||||
|
|
||||||
// Loop through the items in my_seq until we hit the
|
// Loop through the items in my_seq until we hit the
|
||||||
// sentinel value.
|
// sentinel value.
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (??? != my_sentinel) {
|
while (??? != my_sentinel) {
|
||||||
print("{}", .{my_seq[i]});
|
print(" {x}", .{my_seq[i]});
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
}
|
}
|
||||||
print(". ", .{});
|
print(" ]. ", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
--- exercises/076_sentinels.zig 2023-10-03 22:15:22.125574535 +0200
|
--- exercises/076_sentinels.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
+++ answers/076_sentinels.zig 2023-10-05 20:04:07.186102649 +0200
|
+++ answers/076_sentinels.zig 2023-10-05 20:04:07.186102649 +0200
|
||||||
@@ -82,7 +82,7 @@
|
@@ -82,7 +82,7 @@
|
||||||
print("Array:", .{});
|
print("Array:[", .{});
|
||||||
|
|
||||||
// Loop through the items in my_seq.
|
// Loop through the items in my_seq.
|
||||||
- for (???) |s| {
|
- for (???) |s| {
|
||||||
+ for (my_seq) |s| {
|
+ for (my_seq) |s| {
|
||||||
print("{}", .{s});
|
print(" {x}", .{s});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -94,7 +94,7 @@
|
@@ -94,7 +94,7 @@
|
||||||
|
@ -15,6 +15,6 @@
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
- while (??? != my_sentinel) {
|
- while (??? != my_sentinel) {
|
||||||
+ while (my_seq[i] != my_sentinel) {
|
+ while (my_seq[i] != my_sentinel) {
|
||||||
print("{}", .{my_seq[i]});
|
print(" {x}", .{my_seq[i]});
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue