authorgravatar for daniele.cocca@gmail.comDaniele Cocca <daniele.cocca@gmail.com> 2021-04-28 01:26:03+01:00
committergravatar for dcocca@google.comDaniele Cocca <dcocca@google.com> 2021-06-16 22:03:02+01:00
log9e88356282861e5812d95587cb8fab7f902bc85b
tree2d6321739d507435dc14bfca7057807b6a9f3c2a
parent9be2f767417ef4eb69f35b67348bb3e2ec752d4a

embedFile: change notation from [X:0] to [N:0]

This is for consistency with the documentation on sentinel-terminated {arrays,slices,pointers} which already use `N` for a comptime-inferred size rather than `X`. Also adds a behavioral test to assert that a string literal is returned.

3 files changed, 13 insertions(+), 1 deletions(-)

doc/langref.html.in+1-1
...@@ -7447,7 +7447,7 @@ test "main" {...@@ -7447,7 +7447,7 @@ test "main" {
7447 {#see_also|@divFloor|@divExact#}7447 {#see_also|@divFloor|@divExact#}
7448 {#header_close#}7448 {#header_close#}
7449 {#header_open|@embedFile#}7449 {#header_open|@embedFile#}
7450 <pre>{#syntax#}@embedFile(comptime path: []const u8) *const [X:0]u8{#endsyntax#}</pre>7450 <pre>{#syntax#}@embedFile(comptime path: []const u8) *const [N:0]u8{#endsyntax#}</pre>
7451 <p>7451 <p>
7452 This function returns a compile time constant pointer to null-terminated,7452 This function returns a compile time constant pointer to null-terminated,
7453 fixed-size array with length equal to the byte count of the file given by7453 fixed-size array with length equal to the byte count of the file given by
test/behavior/bugs/3779.zig+11
...@@ -29,3 +29,14 @@ test "@typeName() returns a string literal" {...@@ -29,3 +29,14 @@ test "@typeName() returns a string literal" {
29 try std.testing.expectEqualStrings("TestType", type_name);29 try std.testing.expectEqualStrings("TestType", type_name);
30 try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]);30 try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]);
31}31}
32
33const actual_contents = @embedFile("3779_file_to_embed.txt");
34const ptr_actual_contents: [*:0]const u8 = actual_contents;
35const expected_contents = "hello zig\n";
36
37test "@embedFile() returns a string literal" {
38 try std.testing.expectEqual(*const [expected_contents.len:0]u8, @TypeOf(actual_contents));
39 try std.testing.expect(std.mem.eql(u8, expected_contents, actual_contents));
40 try std.testing.expectEqualStrings(expected_contents, actual_contents);
41 try std.testing.expectEqualStrings(expected_contents, ptr_actual_contents[0..actual_contents.len]);
42}
test/behavior/bugs/3779_file_to_embed.txt created+1
...@@ -0,0 +1 @@
1hello zig