Skipping to a substring in a stream

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Skipping to a substring in a stream

Leonardo Silva
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

Reply | Threaded
Open this post in threaded view
|

Re: Skipping to a substring in a stream

Ben Coman
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

Reply | Threaded
Open this post in threaded view
|

Re: Skipping to a substring in a stream

Sven Van Caekenberghe-2
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
>


Reply | Threaded
Open this post in threaded view
|

Re: Skipping to a substring in a stream

Leonardo Silva
Thank you both, PositionableStream>>upToAll worked fine !

On Tue, Feb 9, 2016 at 11:01 AM, Sven Van Caekenberghe <[hidden email]> 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 <[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
>



Reply | Threaded
Open this post in threaded view
|

Re: Skipping to a substring in a stream

Denis Kudriashov
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
Reply | Threaded
Open this post in threaded view
|

Re: Skipping to a substring in a stream

Stephan Eggermont-3
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


Reply | Threaded
Open this post in threaded view
|

Re: Skipping to a substring in a stream

Sven Van Caekenberghe-2

> 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


Reply | Threaded
Open this post in threaded view
|

Re: Skipping to a substring in a stream

Leonardo Silva

On Tue, Feb 9, 2016 at 5:07 PM, Sven Van Caekenberghe <[hidden email]> wrote:

> 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.

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,