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 I'd like to have something like: stream := 'abcdef' readStream. stream skipTo: 'de'. stream next. -→ "But this returns nil" |
On Tue, Feb 9, 2016 at 8:29 PM, Leonardo Silva
<[hidden email]> 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 |
In reply to this post by Leonardo Silva
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 <[hidden email]> 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 <[hidden email]> wrote: Leonardo, |
Hi 2016-02-09 17:48 GMT+01:00 Leonardo Silva <[hidden email]>: Thank you both, PositionableStream>>upToAll worked fine ! you should remember that it produces garbage |
In reply to this post by Sven Van Caekenberghe-2
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 <[hidden email]> 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 <[hidden email]> wrote:
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, |
Free forum by Nabble | Edit this page |