-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Unexpected readLine() / printing behaviour #1055
Comments
You sure it doesn't work...? Using try it outline it printed what you would expect, you can view it here. (Look under the 'debug' section since its a debug print) Of course if you give it extra spaces in input it'll print those out since that's what has been 'read in'. And note: it'll print out any junk that exists in that space unless '0'd out, because you are asking for the whole buffer to be printed. Initially it may seem like a string type is needed (I would know I proposed one), however the way Zig approaches things really means you just don't need one, string manipulation can be done with Unicode pretty easily (and I'm almost done with a library to do it easier) :). If it doesn't print that in the current version of Zig, then that's a compiler bug. |
Also, What might be useful though, would be for
|
So the main difference here versus C is that we don't use null-terminated strings for input, so using the buffer directly isn't going to be a direct analogue like with Some other alternative things we can do:
There a some tradeoffs in these approaches and they are useful in different scenarios. I think the main issue here is that the lack of stdlib documentation/usage for various stdlib functions means there is a barrier for new users. |
And that is the point. The function is named readLine() so it should only read upto a '\n' and therefore the buffer should only contain upto that and therefore only that should be printed. Coming from other languages that is the default behaviour you would expect. I also agree that it would be best for the function to return a []u8 directly. |
... Why would the buffer only contain that though? You set a buffer of 100 long then expect the buffer to change its length on you? To me that just sounds less conventional and obscure. If you want null terminated strings there are std lib functions to convert to them, and you could go that route. I don't know a language that would change the length of a static array? Also your confusing what the signature would be it would be this; |
Then have it take a resizable buffer. |
That is a 'solution', but I struggle to see the problem in the first place; why have it take a resizable buffer which is less efficient just to not have to deal with slicing? I think the solution of providing it a buffer and it returning the slice is a more 'elegant' solution that doesn't involve allocating memory along with the reasons others have given. |
I agree that this is a footgun. Let's update the standard library so that more users do not run into this problem. |
Fixed #1801 |
I would expect this code to echo back the content that is entered followed by the string " end". But it also prints the contents of the empty locations in the
input
variable.Output -
This is real surprising behaviour that you need to do
warn("{} end", input[0..line_len]);
for it to work as expected.IMO zig really needs a proper
string
type as this current behaviour is seriously going backwards even from C.The text was updated successfully, but these errors were encountered: