# HG changeset patch # User Stefan Vogel # Date 1481806649 -3600 # Node ID fc36f8c599ced07bf0b41307103b86a30451b5a5 # Parent 7a598959b48e1d223c386ff7850fd054431d27f8 #DOCUMENTATION by stefan class: PositionableStream category of: #nextBytes:into:startingAt: #nextLine #peek diff -r 7a598959b48e -r fc36f8c599ce PositionableStream.st --- a/PositionableStream.st Thu Dec 15 13:57:09 2016 +0100 +++ b/PositionableStream.st Thu Dec 15 13:57:29 2016 +0100 @@ -196,16 +196,6 @@ ^ collection ! -peek - "look ahead for and return the next element" - - |peekObject| - - peekObject := self next. - self backStep. - ^ peekObject -! - peekForAll:aCollection "return true and advance if the next elements are the same as aCollection. @@ -364,6 +354,48 @@ " ! ! +!PositionableStream methodsFor:'non homogenous reading'! + +nextBytes:numBytes into:aCollection startingAt:initialIndex + "return the next numBytes from the stream. If the end is + reached before, only that many bytes are copyied into the + collection. + Returns the number of bytes that have been actually read. + The receiver must support reading of binary bytes. + + Notice: this method is provided here for protocol completeness + with externalStreams - it is normally not used with other + streams." + + |max| + + (collection isByteCollection + and:[aCollection isByteCollection]) ifTrue:[ + "do it the fast way" + max := (readLimit - position) min: numBytes. + aCollection + replaceBytesFrom:initialIndex + to:(initialIndex + max - 1) + with:collection + startingAt:position+1. + position := position + max. + ^ max + ]. + "do it the hard way" + ^ super nextBytes:numBytes into:aCollection startingAt:initialIndex + + " + |s n buffer| + + buffer := ByteArray new:10. + + s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. + s next:3. + n := s nextBytes:9 into:buffer startingAt:1. + Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) + " +! ! + !PositionableStream methodsFor:'positioning'! backStep @@ -794,91 +826,14 @@ " ! -nextBytes:numBytes into:aCollection startingAt:initialIndex - "return the next numBytes from the stream. If the end is - reached before, only that many bytes are copyied into the - collection. - Returns the number of bytes that have been actually read. - The receiver must support reading of binary bytes. - - Notice: this method is provided here for protocol completeness - with externalStreams - it is normally not used with other - streams." - - |max| - - (collection isByteCollection - and:[aCollection isByteCollection]) ifTrue:[ - "do it the fast way" - max := (readLimit - position) min: numBytes. - aCollection - replaceBytesFrom:initialIndex - to:(initialIndex + max - 1) - with:collection - startingAt:position+1. - position := position + max. - ^ max - ]. - "do it the hard way" - ^ super nextBytes:numBytes into:aCollection startingAt:initialIndex - - " - |s n buffer| - - buffer := ByteArray new:10. - - s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. - s next:3. - n := s nextBytes:9 into:buffer startingAt:1. - Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) - " -! +peek + "look ahead for and return the next element" -nextLine - "return the characters upTo (but excluding) the next cr (carriage return) - character (i.e. read a single line of text). - If the previous-to-last character is a cr, this is also removed, - so it's possible to read alien (i.e. ms-dos) text as well. - Added for protocol compatibility with externalStreams." - - |start "{ Class:SmallInteger }" - end "{ Class:SmallInteger }"| - - collection isString ifTrue:[ - position == readLimit ifTrue:[ - ^ self pastEndRead - ]. - start := position+1. - end := collection indexOf:Character cr startingAt:start. + |peekObject| - (end == 0 or:[end > readLimit]) ifTrue:[ - end := position := readLimit. - ] ifFalse:[ - position := end. - end := end - 1. "skip lf" - ]. - start > end ifTrue:[ - ^ ''. - ]. - (collection at:end) == Character return ifTrue:[ - end := end - 1. "skip return" - ]. - ^ collection copyFrom:start to:end. - ]. - ^ super nextLine. - - " - '12345678' readStream nextLine - '12345678' allBold readStream nextLine - '12\34\56\78' withCRs readStream nextLine - '12\34\56\78' withCRs readStream nextLine; nextLine - (ReadStream on:('12\34\56\78' withCRs) from:1 to:4) nextLine; nextLine - ('12\' withCRs, Character return, '34') readStream nextLine; nextLine - Character cr asString readStream nextLine - Character return asString readStream nextLine - (Character return, Character cr) asString readStream nextLine - Character return asString readStream nextLine; nextLine - " + peekObject := self next. + self backStep. + ^ peekObject ! upToAll:aCollection @@ -997,6 +952,55 @@ " ! ! +!PositionableStream methodsFor:'reading-strings'! + +nextLine + "return the characters upTo (but excluding) the next cr (carriage return) + character (i.e. read a single line of text). + If the previous-to-last character is a cr, this is also removed, + so it's possible to read alien (i.e. ms-dos) text as well. + Added for protocol compatibility with externalStreams." + + |start "{ Class:SmallInteger }" + end "{ Class:SmallInteger }"| + + collection isString ifTrue:[ + position == readLimit ifTrue:[ + ^ self pastEndRead + ]. + start := position+1. + end := collection indexOf:Character cr startingAt:start. + + (end == 0 or:[end > readLimit]) ifTrue:[ + end := position := readLimit. + ] ifFalse:[ + position := end. + end := end - 1. "skip lf" + ]. + start > end ifTrue:[ + ^ ''. + ]. + (collection at:end) == Character return ifTrue:[ + end := end - 1. "skip return" + ]. + ^ collection copyFrom:start to:end. + ]. + ^ super nextLine. + + " + '12345678' readStream nextLine + '12345678' allBold readStream nextLine + '12\34\56\78' withCRs readStream nextLine + '12\34\56\78' withCRs readStream nextLine; nextLine + (ReadStream on:('12\34\56\78' withCRs) from:1 to:4) nextLine; nextLine + ('12\' withCRs, Character return, '34') readStream nextLine; nextLine + Character cr asString readStream nextLine + Character return asString readStream nextLine + (Character return, Character cr) asString readStream nextLine + Character return asString readStream nextLine; nextLine + " +! ! + !PositionableStream methodsFor:'testing'! atEnd