CharacterEncoderImplementations__FixedBytesEncoder.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 22461 0d61027663a5
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 }"

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

!FixedBytesEncoder 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 encodings which encode to a fixed number of bytes.

    [author:]
        Stefan Vogel
"
! !

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

    "Modified: / 17-01-2018 / 17:06:13 / stefan"
!

maxCode
    ^ self subclassResponsibility

    "Modified: / 17-01-2018 / 16:54:48 / stefan"
!

minCode
    ^ 0 
! !

!FixedBytesEncoder methodsFor:'encoding & decoding'!

decode:anEncoding
    "given an integer in my encoding, return a unicode codePoint for it"

    self subclassResponsibility
!

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

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

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

    1 to:size do:[:idx |
        code := (anEncodedStringOrByteCollection at:idx) codePoint.
        myCode := self decode:code.
        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 codePoint:myCode).
    ].
    ^ newString

    "
     CharacterEncoderImplementations::ISO8859_1 decodeString:'hello'
    "

    "Created: / 16-01-2018 / 19:54:02 / stefan"
    "Modified (format): / 17-01-2018 / 16:30:59 / stefan"
!

encode:aCodePoint
    "given a codePoint in unicode, return a single byte in my encoding for it"

    self subclassResponsibility

    "Modified (comment): / 17-01-2018 / 14:52:28 / stefan"
!

encodeCharacter:aUnicodeCharacterOrCodePoint
    "encode aUnicodeCharacterOrCodePoint to a (8-bit) String or ByteArray"

    ^ (Character codePoint:(self encode:aUnicodeCharacterOrCodePoint codePoint)) asString.

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

    "Created: / 17-01-2018 / 14:13:03 / stefan"
! !

!FixedBytesEncoder class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !