I have a ReadStream I would like to peek at the next few characters instead of only the very next character. Here's a simple 1-minute implementation proposal, but since it seems strange this was never added before, I wanted to check my sanity with the brilliant developer folk, here.
PositionableStream>>peek: anInteger "Answer what would be returned if the message next: anInteger without advancing the internal position pointer." | priorPos result | priorPos := position. result := self next: anInteger. position := priorPos. ^ result
Thanks, Chris
The only issue I can think of here is that it might be necessary to use "self position: priorPos" or similar in some forms of Stream. Stuff like socket streams perhaps, where moving forwards/backwards could possibly involve flushing & loading of buffers and other occasionally mind-warping actions.
On 2018-10-06, at 7:07 PM, Chris Muller asqueaker@gmail.com wrote:
I have a ReadStream I would like to peek at the next few characters instead of only the very next character. Here's a simple 1-minute implementation proposal, but since it seems strange this was never added before, I wanted to check my sanity with the brilliant developer folk, here.
PositionableStream>>peek: anInteger "Answer what would be returned if the message next: anInteger without advancing the internal position pointer." | priorPos result | priorPos := position. result := self next: anInteger. position := priorPos. ^ result
Thanks, Chris
tim -- tim Rowledge; tim@rowledge.org; http://www.rowledge.org/tim Strange OpCodes: BPB: Branch on Program Bug
Ah. Yes, it seems I was locked into my projects context, and knowing my own specific Stream types. I considered sending #position: instead, but wanted to avoid any possible side-effects -- I simply wanted to be sure the state of the object was not disturbed, but you've helped me realize the #next: could have side-effects itself beyond 'position' which may need undone.
I'm not up to testing all of the stream types so I probably will do this peeking higher up in my app code.
Thanks. On Sun, Oct 7, 2018 at 11:59 AM tim Rowledge tim@rowledge.org wrote:
The only issue I can think of here is that it might be necessary to use "self position: priorPos" or similar in some forms of Stream. Stuff like socket streams perhaps, where moving forwards/backwards could possibly involve flushing & loading of buffers and other occasionally mind-warping actions.
On 2018-10-06, at 7:07 PM, Chris Muller asqueaker@gmail.com wrote:
I have a ReadStream I would like to peek at the next few characters instead of only the very next character. Here's a simple 1-minute implementation proposal, but since it seems strange this was never added before, I wanted to check my sanity with the brilliant developer folk, here.
PositionableStream>>peek: anInteger "Answer what would be returned if the message next: anInteger without advancing the internal position pointer." | priorPos result | priorPos := position. result := self next: anInteger. position := priorPos. ^ result
Thanks, Chris
tim
tim Rowledge; tim@rowledge.org; http://www.rowledge.org/tim Strange OpCodes: BPB: Branch on Program Bug
squeak-dev@lists.squeakfoundation.org