076_sentinels.zig 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // A sentinel value indicates the end of data. Let's imagine a
  3. // sequence of lowercase letters where uppercase 'S' is the
  4. // sentinel, indicating the end of the sequence:
  5. //
  6. // abcdefS
  7. //
  8. // If our sequence also allows for uppercase letters, 'S' would
  9. // make a terrible sentinel since it could no longer be a regular
  10. // value in the sequence:
  11. //
  12. // abcdQRST
  13. // ^-- Oops! The last letter in the sequence is R!
  14. //
  15. // A popular choice for indicating the end of a string is the
  16. // value 0. ASCII and Unicode call this the "Null Character".
  17. //
  18. // Zig supports sentinel-terminated arrays, slices, and pointers:
  19. //
  20. // const a: [4:0]u32 = [4:0]u32{1, 2, 3, 4};
  21. // const b: [:0]const u32 = &[4:0]u32{1, 2, 3, 4};
  22. // const c: [*:0]const u32 = &[4:0]u32{1, 2, 3, 4};
  23. //
  24. // Array 'a' stores 5 u32 values, the last of which is 0.
  25. // However the compiler takes care of this housekeeping detail
  26. // for you. You can treat 'a' as a normal array with just 4
  27. // items.
  28. //
  29. // Slice 'b' is only allowed to point to zero-terminated arrays
  30. // but otherwise works just like a normal slice.
  31. //
  32. // Pointer 'c' is exactly like the many-item pointers we learned
  33. // about in exercise 054, but it is guaranteed to end in 0.
  34. // Because of this guarantee, we can safely find the end of this
  35. // many-item pointer without knowing its length. (We CAN'T do
  36. // that with regular many-item pointers!).
  37. //
  38. const print = @import("std").debug.print;
  39. pub fn main() void {
  40. // Here's a zero-terminated array of u32 values:
  41. var nums = [_:0]u32{ 1, 2, 3, 4, 5, 6 };
  42. // And here's a zero-terminated many-item pointer:
  43. var ptr: [*:0]u32 = &nums;
  44. // For fun, let's replace the value at position 3 with the
  45. // sentinel value 0. This seems kind of naughty.
  46. nums[3] = 0;
  47. // So now we have a zero-terminated array and a many-item
  48. // pointer that reference the same data: a sequence of
  49. // numbers that both ends in and CONTAINS the sentinal value.
  50. //
  51. // Attempting to loop through and print both of these should
  52. // demonstrate how they are similar and different.
  53. //
  54. // (It turns out that the array prints completely, including
  55. // the sentinel 0 in the middle. The many-item pointer must
  56. // stop at the first sentinel value. The difference is simply
  57. // that arrays have a known length and many-item pointers
  58. // don't.)
  59. printSequence(nums);
  60. printSequence(ptr);
  61. print("\n", .{});
  62. }
  63. // Here's our generic sequence printing function. It's nearly
  64. // complete, but there are a couple missing bits. Please fix
  65. // them!
  66. fn printSequence(my_seq: anytype) void {
  67. const my_type = @typeInfo(@TypeOf(my_seq));
  68. // The TypeInfo contained in my_type is a union. We use a
  69. // switch to handle printing the Array or Pointer fields,
  70. // depending on which type of my_seq was passed in:
  71. switch (my_type) {
  72. .Array => {
  73. print("Array:", .{});
  74. // Loop through the items in my_seq.
  75. for (???) |s| {
  76. print("{}", .{s});
  77. }
  78. },
  79. .Pointer => {
  80. // Check this out - it's pretty cool:
  81. const my_sentinel = my_type.Pointer.sentinel;
  82. print("Many-item pointer:", .{});
  83. // Loop through the items in my_seq until we hit the
  84. // sentinel value.
  85. var i: usize = 0;
  86. while (??? != my_sentinel) {
  87. print("{}", .{my_seq[i]});
  88. i += 1;
  89. }
  90. },
  91. else => unreachable,
  92. }
  93. print(". ", .{});
  94. }