Skipping to a substring in a stream
Hi, I'd like to skip to a specific position in a stream based on a substring. I got the following example that works for a single character: stream := 'abcdef' readStream. stream skipTo: $e. stream next. -â $f But it does not work for substrings. I'd like to have something like: stream := 'abcdef' readStream. stream skipTo: 'de'. stream next. -â "But this returns nil" Do you know how to do it? Thanks, Leonardo
On Tue, Feb 9, 2016 at 8:29 PM, Leonardo Silva <leonardo.humberto@gmail.com> wrote:
Hi,
I'd like to skip to a specific position in a stream based on a substring. I got the following example that works for a single character:
stream := 'abcdef' readStream. stream skipTo: $e. stream next. -â $f
But it does not work for substrings. I'd like to have something like:
stream := 'abcdef' readStream. stream skipTo: 'de'. stream next. -â "But this returns nil"
Do you know how to do it?
Thanks,
Leonardo
I see there is only one implementer PositionableStream skipTo: anObject "Set the access position of the receiver to be past the next occurrence of anObject. Answer whether anObject is found." [self atEnd] whileFalse: [self next = anObject ifTrue: [^true]]. ^false first btw, did you debug into this method to understand why it doesn't work with strings? It would help observation to change the method line this... whileFalse: [(x:=self next) = anObject ifTrue: [^true]]. I don't see anything useful in the superclass, but I see PositionableStream>>positionOfSubCollection: Short answer is that you skipTo: wont work with strings since the stream elements are not Strings, they are Characters. You will need to create your own method for this, perhaps named skipToSubCollection: cheers -ben
Leonardo, You could try PositionalStream>>#upToAll: But watch out: if the substring does not occur, it will effectively read #upToEnd HTH, Sven
On 09 Feb 2016, at 13:29, Leonardo Silva <leonardo.humberto@gmail.com> wrote:
Hi,
I'd like to skip to a specific position in a stream based on a substring. I got the following example that works for a single character:
stream := 'abcdef' readStream. stream skipTo: $e. stream next. -â $f
But it does not work for substrings. I'd like to have something like:
stream := 'abcdef' readStream. stream skipTo: 'de'. stream next. -â "But this returns nil"
Do you know how to do it?
Thanks,
Leonardo
Thank you both, PositionableStream>>upToAll worked fine ! On Tue, Feb 9, 2016 at 11:01 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
Leonardo,
You could try PositionalStream>>#upToAll:
But watch out: if the substring does not occur, it will effectively read #upToEnd
HTH,
Sven
On 09 Feb 2016, at 13:29, Leonardo Silva <leonardo.humberto@gmail.com> wrote:
Hi,
I'd like to skip to a specific position in a stream based on a substring. I got the following example that works for a single character:
stream := 'abcdef' readStream. stream skipTo: $e. stream next. -â $f
But it does not work for substrings. I'd like to have something like:
stream := 'abcdef' readStream. stream skipTo: 'de'. stream next. -â "But this returns nil"
Do you know how to do it?
Thanks,
Leonardo
On 09/02/16 14:01, Sven Van Caekenberghe wrote:
Leonardo,
You could try PositionalStream>>#upToAll:
But watch out: if the substring does not occur, it will effectively read #upToEnd
Also, for searching for substrings in long strings this is extremely slow, much slower than BMH and KMP Stephan
On 09 Feb 2016, at 19:54, Stephan Eggermont <stephan@stack.nl> wrote:
On 09/02/16 14:01, Sven Van Caekenberghe wrote:
Leonardo,
You could try PositionalStream>>#upToAll:
But watch out: if the substring does not occur, it will effectively read #upToEnd
Also, for searching for substrings in long strings this is extremely slow, much slower than BMH and KMP
Yes, true. I wanted to say something similar. But we don't how he is using it, or for what, maybe it is just some one off code that is not used frequently or totally not performance critical.
Stephan
On Tue, Feb 9, 2016 at 5:07 PM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 09 Feb 2016, at 19:54, Stephan Eggermont <stephan@stack.nl> wrote:
On 09/02/16 14:01, Sven Van Caekenberghe wrote:
Leonardo,
You could try PositionalStream>>#upToAll:
But watch out: if the substring does not occur, it will effectively read #upToEnd
Also, for searching for substrings in long strings this is extremely slow, much slower than BMH and KMP
Yes, true. I wanted to say something similar.
But we don't how he is using it, or for what, maybe it is just some one off code that is not used frequently or totally not performance critical.
I am sad to hear all that, I thought my problem was solved. :-) I basically open a txt file to look for the information I want. This txt file is the output of an external framework. I cannot change its structure. I believe that performance can be indeed an issue depending on the size of the file and the number of operations executed. I will study other alternatives. Cheers,
participants (5)
-
Ben Coman -
Denis Kudriashov -
Leonardo Silva -
Stephan Eggermont -
Sven Van Caekenberghe