076_sentinels.zig 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 termined!
  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 sentinal 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 must
  59. // stop at the first sentinel value. The difference is simply
  60. // that arrays have a known length and many-item pointers
  61. // don't.)
  62. printSequence(nums);
  63. printSequence(ptr);
  64. print("\n", .{});
  65. }
  66. // Here's our generic sequence printing function. It's nearly
  67. // complete, but there are a couple missing bits. Please fix
  68. // them!
  69. fn printSequence(my_seq: anytype) void {
  70. const my_type = @typeInfo(@TypeOf(my_seq));
  71. // The TypeInfo contained in my_type is a union. We use a
  72. // switch to handle printing the Array or Pointer fields,
  73. // depending on which type of my_seq was passed in:
  74. switch (my_type) {
  75. .Array => {
  76. print("Array:", .{});
  77. // Loop through the items in my_seq.
  78. for (???) |s| {
  79. print("{}", .{s});
  80. }
  81. },
  82. .Pointer => {
  83. // Check this out - it's pretty cool:
  84. const my_sentinel = my_type.Pointer.sentinel;
  85. print("Many-item pointer:", .{});
  86. // Loop through the items in my_seq until we hit the
  87. // sentinel value.
  88. var i: usize = 0;
  89. while (??? != my_sentinel) {
  90. print("{}", .{my_seq[i]});
  91. i += 1;
  92. }
  93. },
  94. else => unreachable,
  95. }
  96. print(". ", .{});
  97. }