CharacterEncoderImplementations__SingleByteEncoder.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 22478 e511b09f7a97
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

"{ NameSpace: CharacterEncoderImplementations }"

FixedBytesEncoder subclass:#SingleByteEncoder
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Text-Encodings'
!

!SingleByteEncoder class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Common abstract superclass for all single-byte encodings.

    [author:]
        Claus Gittinger
"
! !

!SingleByteEncoder class methodsFor:'queries'!

isAbstract
    "Return if this class is an abstract class.
     True is returned here; false for subclasses.
     Abstract subclasses must redefine this again."

    ^ self == CharacterEncoderImplementations::SingleByteEncoder
!

maxCode
    ^ 255 
! !

!SingleByteEncoder methodsFor:'encoding & decoding'!

decodeString:anEncodedStringOrByteCollection
    "given a string in my encoding, return a unicode-string for it"

    |newString myCode uniCodePoint bits size "{ Class:SmallInteger }"|

    size := anEncodedStringOrByteCollection size.
    newString := String new:size.
    bits := newString bitsPerCharacter.

    1 to:size do:[:idx |
        uniCodePoint := (anEncodedStringOrByteCollection at:idx) codePoint.
        myCode := self decode:uniCodePoint.
        myCode > 16rFF ifTrue:[
            myCode > 16rFFFF ifTrue:[
                bits < 32 ifTrue:[
                    newString := Unicode32String fromString:newString.
                    bits := 32.
                ]
            ] ifFalse:[
                bits < 16 ifTrue:[
                    newString := Unicode16String fromString:newString.
                    bits := 16.
                ]
            ]
        ].
        newString at:idx put:(Character value:myCode).
    ].
    ^ newString

    "
     CharacterEncoderImplementations::ISO8859_1 decodeString:'hello'
    "

    "Created: / 16-01-2018 / 19:54:02 / stefan"
    "Modified (format): / 17-01-2018 / 14:15:00 / stefan"
!

encodeString:aUnicodeString
    "given a string in unicode, return a string in my encoding for it"

    |newString myCode stringSize "{ Class: SmallInteger }"|

    stringSize := aUnicodeString size.
    newString := String new:stringSize.

    1 to:stringSize do:[:idx |
        myCode := self encode:((aUnicodeString at:idx) codePoint).
        newString at:idx put:(Character codePoint:myCode).
    ].
    ^ newString

    "Created: / 16-01-2018 / 19:53:33 / stefan"
    "Modified: / 17-01-2018 / 14:15:39 / stefan"
! !

!SingleByteEncoder methodsFor:'queries'!

characterSize:charOrCodePoint
    "return the number of bytes required to encode codePoint"

    ^ 1

    "Created: / 15-06-2005 / 15:11:24 / janfrog"
    "Modified (format): / 16-01-2018 / 19:50:39 / stefan"
! !

!SingleByteEncoder methodsFor:'stream support'!

encodeCharacter:aUnicodeCharacterOrCodePoint on:aStream
    "given a character in unicode, encode it onto aStream.
     Subclasses can redefine this to avoid allocating many new string instances."

    aStream nextPutByte:(self encode:aUnicodeCharacterOrCodePoint codePoint).

    "
      CharacterEncoderImplementations::ISO8859_10 new encodeCharacter:260 on:Transcript
      CharacterEncoderImplementations::ISO8859_10 new encodeCharacter:$Ą  on:Transcript
    "

    "Created: / 17-01-2018 / 15:07:39 / stefan"
!

readNext:charactersToRead charactersFrom:stream 
    ^ self decodeString:(stream next:charactersToRead)
!

readNextCharacterFrom:aStream
    | code |

    code := aStream nextByte.

    ^ code isNil 
        ifTrue: [nil]
        ifFalse: [Character codePoint:(self decode:code)]

    "Created: / 16-01-2018 / 20:05:20 / stefan"
    "Modified: / 17-01-2018 / 15:19:03 / stefan"
! !

!SingleByteEncoder class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !