-
-
Notifications
You must be signed in to change notification settings - Fork 702
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
Clarify that buf can be shortened in readln #7125
Conversation
Thanks for your pull request and interest in making D better, @mmtrebuchet! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + phobos#7125" |
So, if you want to use |
@CyberShadow I think it just adjusts buf.length but not the actual buf.capacity, so that if your lines are of length 20, 10, 15, it'd allocate 20 characters, then just set buf.length to 10 and 15, without ever shortening the underlying storage. (I'm not sure if this is the intended behavior, but this PR is just to make the documentation reflect the behavior of the function.) |
Here is what I mean: https://run.dlang.io/is/UwJsaG After shortening |
Oh, I didn't notice that. Yeah, that's a bad thing, I think. Definitely not how I'd expect that function to behave.
I've never tried to slice a pointer until today. Is this where the reallocation is occurring? |
I agree, it looks like the function interface was poorly designed. It should have been returning the slice, and only growing
"Shorten" in the upper-left corner.
The problem is that it's returning (and setting
It happens in the third |
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.
Let's make these even more unambiguous, considering that the mistake in the function's design causes it to deviate from what would be the expected one.
that makes the included example wrong, right?
Upon review, the example is correct; it would have been much simpler if readln
was better designed, though. The loop could have been replaced with just:
while (!stdin.eof)
words += stdin.readln(buf).split.length;
Co-Authored-By: Vladimir Panteleev <[email protected]>
Co-Authored-By: Vladimir Panteleev <[email protected]>
Urk. I guess we're stuck with this strange behavior. A shame that getline() hasn't made its way to core.stdc.stdio yet. |
It wasn't clear on inspection if buf could be shortened, or if it was just lengthened. "As necessary" suggested that buf would only be resized if absolutely necessary, which would only be when it needs to be lengthened.