diff --git a/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st b/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st index adda150..00f16bc 100644 --- a/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st +++ b/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st @@ -403,13 +403,12 @@ ZnBufferedReadStream >> uint8 [ { #category : 'accessing' } ZnBufferedReadStream >> upTo: value [ "Read upto but not including value and return them as a collection. - If value is not found, return the entire contents of the stream. - This could be further optimzed." + If value is not found, return the entire contents of the stream." ^ self collectionSpecies - streamContents: [ :writeStream | - [ self atEnd or: [ (self peek) = value ] ] whileFalse: [ - writeStream nextPut: self next ] ] + streamContents: [ :writeStream | | ch | + [ self atEnd or: [ (ch := self next) = value ] ] whileFalse: [ + writeStream nextPut: ch ] ] ] { #category : 'accessing' } @@ -421,9 +420,12 @@ aCollection isEmpty ifTrue: [ ^aCollection ]. startPos := self position. "upTo: will stop before aCollection first" x := self upTo: aCollection first. -(self atEnd or: [aCollection size = 1 ]) - ifTrue: [ ^ x ]. -self next. "move past the matching char from upTo:" +self atEnd + ifTrue: [ + aCollection size <= 1 + ifTrue: [ ^ x ]. + self position: startPos. + ^ self upToEnd]. 2 to: aCollection size do: [:i | | y | (y := self peek) = (aCollection at: i) ifTrue: [ self next ] diff --git a/src/Zinc-Character-Encoding-Tests/ZnCharacterStreamTests.class.st b/src/Zinc-Character-Encoding-Tests/ZnCharacterStreamTests.class.st index 7a50bbf..aba8dca 100644 --- a/src/Zinc-Character-Encoding-Tests/ZnCharacterStreamTests.class.st +++ b/src/Zinc-Character-Encoding-Tests/ZnCharacterStreamTests.class.st @@ -112,6 +112,18 @@ ZnCharacterStreamTests >> testReadStream [ self assert: stream peek isNil ] +{ #category : 'testing' } +ZnCharacterStreamTests >> testReadUpTo [ + | string | + string := '0123456789'. + {(self eightBitReadStreamOn: string). + (self utf8ReadStreamOn: string)} + do: [ :stream | + self assert: (stream upTo: $5) equals: '01234'. + self assert: stream upToEnd equals: '6789'. + self assert: stream atEnd ] +] + { #category : 'testing' } ZnCharacterStreamTests >> testSimpleUTF8ReadStream [ | string bytes stream | @@ -136,18 +148,6 @@ ZnCharacterStreamTests >> testSimpleUTF8WriteStream [ equals: bytes asByteArray ] -{ #category : 'testing' } -ZnCharacterStreamTests >> testUtf8EncodingStreamPosition [ - | string bytes stream res | - string := 'eißendeße'. - bytes := ZnUTF8Encoder new encodeString: string. - stream := (ZnCharacterReadStream on: bytes readStreamPortable). - res := stream next; next; next. - self assert: res equals: $ß. - self assert: stream position equals: 3. - -] - { #category : 'testing' } ZnCharacterStreamTests >> testUpTo [ | char1 char2 string1 string2 | @@ -206,6 +206,22 @@ ZnCharacterStreamTests >> testUpTo [ ZnCharacterStreamTests >> testUpToAll [ #( ('' '' '') + ('a' '' '') + ('a' 'a' '') + ('a' 'b' 'a') + ('ab' '' '') + ('ab' 'a' '') + ('ab' 'b' 'a') + ('ab' 'c' 'ab') + ('ab' 'ab' '') + ('abc' '' '') + ('abc' 'a' '') + ('abc' 'b' 'a') + ('abc' 'c' 'ab') + ('abc' 'd' 'abc') + ('abc' 'ab' '') + ('abc' 'bc' 'a') + ('abc' 'cd' 'abc') ('' 'ß' '') ('' 'ße' '') ('ß' '' '') @@ -228,22 +244,6 @@ ZnCharacterStreamTests >> testUpToAll [ ('eißen' 'ßend' 'eißen') ('eißendeße' 'ße' 'ei') ('abcdefgh' 'cd' 'ab') - ('a' '' '') - ('a' 'a' '') - ('a' 'b' 'a') - ('ab' '' '') - ('ab' 'a' '') - ('ab' 'b' 'a') - ('ab' 'c' 'ab') - ('ab' 'ab' '') - ('abc' '' '') - ('abc' 'a' '') - ('abc' 'b' 'a') - ('abc' 'c' 'ab') - ('abc' 'd' 'abc') - ('abc' 'ab' '') - ('abc' 'bc' 'a') - ('abc' 'cd' 'abc') ) do: [ :array | self assertUpToAll: array ] ] @@ -272,6 +272,30 @@ ZnCharacterStreamTests >> testUpToAllTwice [ ] +{ #category : 'testing' } +ZnCharacterStreamTests >> test8BitEncodingStreamPosition [ + | string bytes stream res | + string := 'eiSendeSe'. + bytes := Zn8BITEncoder new encodeString: string. + stream := (ZnCharacterReadStream on: bytes readStreamPortable). + res := stream next; next; next. + self assert: res equals: $S. + self assert: stream position equals: 3. + +] + +{ #category : 'testing' } +ZnCharacterStreamTests >> testUtf8EncodingStreamPosition [ + | string bytes stream res | + string := 'eißendeße'. + bytes := ZnUTF8Encoder new encodeString: string. + stream := (ZnCharacterReadStream on: bytes readStreamPortable). + res := stream next; next; next. + self assert: res equals: $ß. + self assert: stream position equals: 3. + +] + { #category : 'testing' } ZnCharacterStreamTests >> testUTF8ReadStreamReadInto [ | string bytes stream buffer |