Streams' copyFrom:to:

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

Streams' copyFrom:to:

S11001001
In 2.3.1 with compiler-cascade.diff and the 3 diffs I posted yesterday
and today, I noticed that FileStream>>#copyFrom:to: uses 0-index,
whereas PositionableStream>>#copyFrom:to: uses 1-index.

st> | fs |
    fs := FileStream fopen: '/home/sirian/TODO' mode: FileStream read.
    [(fs copyFrom: 1 to: 3) printNl] ensure: [fs close]!
' To'
st> ('* Today' readStream copyFrom: 1 to: 3) printNl!
'* T'

(/home/sirian/TODO starts with '* Today:')

I'll assume that 0-index is intended, because that's what
PositionableStream>>#segmentFrom:to: depends on in compiler/RBParser.st.

On a related note, I found something else in kernel/FileDescr.st:

copyFrom: from to: to
    "Answer the contents of the file between the two given positions"
    | offset fromPos toPos savePos |
    from > to ifTrue: [
        from = to + 1 ifTrue: [ ^self species new ].
        ^SystemExceptions.ArgumentOutOfRange signalOn: from mustBeBetween: 1 and: to + 1
    ].
    savePos := self fileOp: 5.
    ^[
        self position: fromPos.
        self next: toPos - fromPos + 1
    ] ensure: [
        self position: savePos
    ]

How do offset, fromPos, and toPos get set in this method?

--
Stephen Compall
http://scompall.nocandysw.com/blog

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Streams' copyFrom:to:

Paolo Bonzini

> I'll assume that 0-index is intended, because that's what
> PositionableStream>>#segmentFrom:to: depends on in compiler/RBParser.st.

Yes, it's right.

> On a related note, I found something else in kernel/FileDescr.st:
>
> copyFrom: from to: to
>     "Answer the contents of the file between the two given positions"
>     | offset fromPos toPos savePos |
>     from > to ifTrue: [
>         from = to + 1 ifTrue: [ ^self species new ].
>         ^SystemExceptions.ArgumentOutOfRange signalOn: from mustBeBetween: 1 and: to + 1
>     ].
>     savePos := self fileOp: 5.
>     ^[
>         self position: fromPos.
>         self next: toPos - fromPos + 1
>     ] ensure: [
>         self position: savePos
>     ]
>
> How do offset, fromPos, and toPos get set in this method?

This is blatantly wrong... s/Pos//g and remove the three temporaries.

Paolo



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk