-
Notifications
You must be signed in to change notification settings - Fork 42
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
Fix unnecessary uses of sentinel slices #37
Comments
Noting some examples here:
I guess there's no reason for this to be This probably too since we know the length of the string?
And using c.lua_pushlstring below would avoid
|
Thanks for pointing out these specific issues! I want to prioritize #33 before working on this. This will make any future changes so much easier to make
I never really loved the bytes/string naming I used, I like this a lot more |
I think there may be no really hard and fast rule for these. For example, in a lot of case a string literal can be directly passed from Zig down to Lua code, in which case |
After taking a look at this again after a few months, I struggled to find more than only a handful of functions where Re checkBytes
I believe this is a non-issue. Returning [:0] signifies that the string is null-terminated. It is possible that there are 0 bytes somewhere in the middle of the string too (per the Lua docs), but in Zig we can just use the .len of the slice to be safe. But using a :0 means that it is also usually safe to use .ptr on the slice, because all strings returned from Lua will be null terminated. The only time this could go wrong is if there is a 0 somewhere in the string before the true end. |
I took a look at your change and it looks all great! Regarding checkBytes: I think you're right. Also, I see that you've removed checkBytes since it's already covered by all those |
Thanks for taking a look! I'll go ahead and close this then |
For values returned from Lua, using sentinel slices (specifically
[:0]const u8
) makes sense. It accurately reflects the data returned, and can be easily used in Zig. For values passed into Lua, this often does not make sense. Most "strings" in Zig aren't null terminated, and it can require an extra allocation to pass a string to Lua this way.It appears I got a bit excited and used
[:0]const u8
far too often for function parameters. These should be audited and changed to[]const u8
wherever possible.The text was updated successfully, but these errors were encountered: