076_sentinels.zig 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // Important: the sentinel value must be of the same type as the
  39. // data being terminated!
  40. //
  41. const print = @import("std").debug.print;
  42. pub fn main() void {
  43. // Here's a zero-terminated array of u32 values:
  44. var nums = [_:0]u32{ 1, 2, 3, 4, 5, 6 };
  45. // And here's a zero-terminated many-item pointer:
  46. var ptr: [*:0]u32 = &nums;
  47. // For fun, let's replace the value at position 3 with the
  48. // sentinel value 0. This seems kind of naughty.
  49. nums[3] = 0;
  50. // So now we have a zero-terminated array and a many-item
  51. // pointer that reference the same data: a sequence of
  52. // numbers that both ends in and CONTAINS the sentinel value.
  53. //
  54. // Attempting to loop through and print both of these should
  55. // demonstrate how they are similar and different.
  56. //
  57. // (It turns out that the array prints completely, including
  58. // the sentinel 0 in the middle. The many-item pointer stops
  59. // at the first sentinel value.)
  60. printSequence(nums);
  61. printSequence(ptr);
  62. print("\n", .{});
  63. }
  64. // Here's our generic sequence printing function. It's nearly
  65. // complete, but there are a couple missing bits. Please fix
  66. // them!
  67. fn printSequence(my_seq: anytype) void {
  68. const my_type = @typeInfo(@TypeOf(my_seq));
  69. // The TypeInfo contained in my_type is a union. We use a
  70. // switch to handle printing the Array or Pointer fields,
  71. // depending on which type of my_seq was passed in:
  72. switch (my_type) {
  73. .Array => {
  74. print("Array:", .{});
  75. // Loop through the items in my_seq.
  76. for (???) |s| {
  77. print("{}", .{s});
  78. }
  79. },
  80. .Pointer => {
  81. // Check this out - it's pretty cool:
  82. const my_sentinel = my_type.Pointer.sentinel;
  83. print("Many-item pointer:", .{});
  84. // Loop through the items in my_seq until we hit the
  85. // sentinel value.
  86. var i: usize = 0;
  87. while (??? != my_sentinel) {
  88. print("{}", .{my_seq[i]});
  89. i += 1;
  90. }
  91. },
  92. else => unreachable,
  93. }
  94. print(". ", .{});
  95. }