-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
print contents of sentinel-terminated pointers in std.fmt #3972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
d8720f3
to
98e45a8
Compare
08b7020
to
28dfb1e
Compare
@@ -28,7 +28,7 @@ test "cstr fns" { | |||
|
|||
fn testCStrFnsImpl() void { | |||
testing.expect(cmp("aoeu", "aoez") == -1); | |||
testing.expect(mem.len(u8, "123456789") == 9); | |||
testing.expect(mem.len(u8, "123456789".*) == 9); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way the test was before is testing what it intends to test - calculating the length of a null terminated pointer. e.g. the zig equivalent of strlen
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it perhaps be .ptr
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of a string literal is *const [9:0]u8
; IMO asking for the length of a pointer seems weird, and we need to .*
to make sense...
ping, @daurnimator any chance you could rebase this and address Andrew's comments? |
Deprecates mem.toSliceConst and mem.toSlice
28dfb1e
to
1fc9432
Compare
Rebased and addressed some comments. I went with a new function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not ready for any of these changes to std.mem. Can you just make pointerToSlice
a private function in fmt.zig, or, better yet, inline the logic?
Here's a new set of functions to std.mem I would be willing to add (and adding deprecation notices to toSlice
, toSliceConst
, and len
):
/// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and
/// returns a slice. If there is a sentinel on the input type, there will be a
/// sentinel on the output type. The constness of the output type matches
/// the constness of the input type.
pub fn Slice(comptime T: type) type {
// TODO
}
/// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and
/// returns a slice. If there is a sentinel on the input type, there will be a
/// sentinel on the output type. The constness of the output type matches
/// the constness of the input type.
pub fn slice(ptr: var) Slice(@TypeOf(ptr)) {
// TODO
}
/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
/// or a slice, and returns the length.
/// TODO deprecate `len` and make this become the new `len`.
pub fn length(ptr: var) usize {
// TODO
}
I believe then std.mem.slice
would take the place of pointerToSlice
in the fmt.zig code.
The rest of the PR looks good. The changes to std.mem
are my only concern.
How would this then help with #4304 (comment) |
Not sure if we need to make error messages more explicit? Would be a good place to use #1669 (comment)