CharacterEncoderImplementations__TwoByteEncoder.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 22479 833b8c85dbda
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:#TwoByteEncoder
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Text-Encodings'
!

!TwoByteEncoder 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
"
    abstract superclass of all two-byte encoders
"
! !

!TwoByteEncoder 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::TwoByteEncoder
!

maxCode
    ^ 16rFFFF 
! !

!TwoByteEncoder methodsFor:'encoding & decoding'!

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

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

    stringSize := aUnicodeString size.
    newString := self newString:stringSize.

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

    "Created: / 17-01-2018 / 16:33:52 / stefan"
! !

!TwoByteEncoder methodsFor:'queries'!

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

    ^ 2

    "Created: / 15-06-2005 / 15:12:01 / janfrog"
    "Modified (format): / 16-01-2018 / 22:32:03 / stefan"
!

newString:size
    ^ TwoByteString new:size.

    "Created: / 17-01-2018 / 16:36:56 / stefan"
! !

!TwoByteEncoder 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 nextPutInt16:(self encode:aUnicodeCharacterOrCodePoint codePoint) MSB:false.

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

    "Created: / 17-01-2018 / 16:41:09 / stefan"
!

readNextCharacterFrom:aStream
    | codePoint |

    codePoint := aStream nextUnsignedInt16MSB:false.
    ^ codePoint isNil 
        ifTrue: [nil]
        ifFalse: [Character codePoint:codePoint]

    "Modified: / 16-01-2018 / 22:30:36 / stefan"
! !

!TwoByteEncoder class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !