076_sentinels.zig 3.5 KB

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