summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authorChris Boesch <chrboesch@noreply.codeberg.org>2024-11-02 16:13:53 +0000
committerChris Boesch <chrboesch@noreply.codeberg.org>2024-11-02 16:13:53 +0000
commit150b3de2995dc0f6da3b03113151488fadab246b (patch)
tree278913282d760fea80ac004bf438bfa6281d9ce4 /exercises
parentd0d31ae73ad4d4445ccadcb0e891d3527230fcfc (diff)
parentfb018d212c1e6d8efc42740c6b99d38c62e52d92 (diff)
Merge pull request 'Improved maximumNarcissism' (#174) from pr173 into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/174
Diffstat (limited to 'exercises')
-rw-r--r--exercises/065_builtins2.zig15
1 files changed, 8 insertions, 7 deletions
diff --git a/exercises/065_builtins2.zig b/exercises/065_builtins2.zig
index 21a3911..6b8168c 100644
--- a/exercises/065_builtins2.zig
+++ b/exercises/065_builtins2.zig
@@ -110,7 +110,6 @@ pub fn main() void {
// name will not be printed if the field is of type 'void'
// (which is a zero-bit type that takes up no space at all!):
if (fields[0].??? != void) {
- print(" {s}", .{fields.name});
print(" {s}", .{fields[0].name});
}
@@ -137,14 +136,16 @@ pub fn main() void {
// But a change after Zig 0.10.0 added the source file name to the
// type. "Narcissus" became "065_builtins2.Narcissus".
//
-// To fix this, I've added this function to strip the filename from
-// the front of the type name in the dumbest way possible. (It returns
-// a slice of the type name starting at character 14 (assuming
-// single-byte characters).
+// To fix this, we've added this function to strip the filename from
+// the front of the type name. (It returns a slice of the type name
+// starting at the index + 1 of character ".")
//
// We'll be seeing @typeName again in Exercise 070. For now, you can
// see that it takes a Type and returns a u8 "string".
fn maximumNarcissism(myType: anytype) []const u8 {
- // Turn '065_builtins2.Narcissus' into 'Narcissus'
- return @typeName(myType)[14..];
+ const indexOf = @import("std").mem.indexOf;
+
+ // Turn "065_builtins2.Narcissus" into "Narcissus"
+ const name = @typeName(myType);
+ return name[indexOf(u8, name, ".").? + 1 ..];
}