CharacterEncoderImplementations__TwoByteEncoder.st
changeset 22479 833b8c85dbda
parent 22428 b4f17397e9c3
equal deleted inserted replaced
22478:e511b09f7a97 22479:833b8c85dbda
    13 "
    13 "
    14 "{ Package: 'stx:libbasic' }"
    14 "{ Package: 'stx:libbasic' }"
    15 
    15 
    16 "{ NameSpace: CharacterEncoderImplementations }"
    16 "{ NameSpace: CharacterEncoderImplementations }"
    17 
    17 
    18 CharacterEncoder subclass:#TwoByteEncoder
    18 FixedBytesEncoder subclass:#TwoByteEncoder
    19 	instanceVariableNames:''
    19 	instanceVariableNames:''
    20 	classVariableNames:''
    20 	classVariableNames:''
    21 	poolDictionaries:''
    21 	poolDictionaries:''
    22 	category:'Collections-Text-Encodings'
    22 	category:'Collections-Text-Encodings'
    23 !
    23 !
    54     ^ self == CharacterEncoderImplementations::TwoByteEncoder
    54     ^ self == CharacterEncoderImplementations::TwoByteEncoder
    55 !
    55 !
    56 
    56 
    57 maxCode
    57 maxCode
    58     ^ 16rFFFF 
    58     ^ 16rFFFF 
    59 !
    59 ! !
    60 
    60 
    61 minCode
    61 !TwoByteEncoder methodsFor:'encoding & decoding'!
    62     ^ 0 
    62 
       
    63 encodeString:aUnicodeString
       
    64     "given a string in unicode, return a string in my encoding for it"
       
    65 
       
    66     |newString myCode stringSize "{ Class: SmallInteger }"|
       
    67 
       
    68     stringSize := aUnicodeString size.
       
    69     newString := self newString:stringSize.
       
    70 
       
    71     1 to:stringSize do:[:idx |
       
    72         myCode := self encode:((aUnicodeString at:idx) codePoint).
       
    73         newString at:idx put:(Character codePoint:myCode).
       
    74     ].
       
    75     ^ newString
       
    76 
       
    77     "Created: / 17-01-2018 / 16:33:52 / stefan"
    63 ! !
    78 ! !
    64 
    79 
    65 !TwoByteEncoder methodsFor:'queries'!
    80 !TwoByteEncoder methodsFor:'queries'!
    66 
    81 
    67 characterSize:charOrCodePoint
    82 characterSize:charOrCodePoint
    68     "return the number of bytes required to encode codePoint"
    83     "return the number of bytes required to encode codePoint"
    69 
    84 
    70     ^2
    85     ^ 2
    71 
    86 
    72     "Created: / 15-06-2005 / 15:12:01 / janfrog"
    87     "Created: / 15-06-2005 / 15:12:01 / janfrog"
       
    88     "Modified (format): / 16-01-2018 / 22:32:03 / stefan"
       
    89 !
       
    90 
       
    91 newString:size
       
    92     ^ TwoByteString new:size.
       
    93 
       
    94     "Created: / 17-01-2018 / 16:36:56 / stefan"
    73 ! !
    95 ! !
    74 
    96 
    75 !TwoByteEncoder methodsFor:'stream support'!
    97 !TwoByteEncoder methodsFor:'stream support'!
    76 
    98 
    77 readNextCharacterFrom:aStream
    99 encodeCharacter:aUnicodeCharacterOrCodePoint on:aStream
    78     | c |
   100     "given a character in unicode, encode it onto aStream.
       
   101      Subclasses can redefine this to avoid allocating many new string instances."
    79 
   102 
    80     c := aStream nextUnsignedInt16MSB:false.
   103     aStream nextPutInt16:(self encode:aUnicodeCharacterOrCodePoint codePoint) MSB:false.
    81     ^ c isNil 
   104 
    82         ifTrue: [nil]
   105     "
    83         ifFalse: [(self decode:c) asCharacter]
   106       CharacterEncoderImplementations::ISO8859_10 new encodeCharacter:260 on:Transcript
       
   107       CharacterEncoderImplementations::ISO8859_10 new encodeCharacter:$Ą  on:Transcript
       
   108     "
       
   109 
       
   110     "Created: / 17-01-2018 / 16:41:09 / stefan"
    84 !
   111 !
    85 
   112 
    86 readNextInputCharacterFrom:aStream
   113 readNextCharacterFrom:aStream
    87     ^ aStream nextUnsignedInt16MSB:false
   114     | codePoint |
       
   115 
       
   116     codePoint := aStream nextUnsignedInt16MSB:false.
       
   117     ^ codePoint isNil 
       
   118         ifTrue: [nil]
       
   119         ifFalse: [Character codePoint:codePoint]
       
   120 
       
   121     "Modified: / 16-01-2018 / 22:30:36 / stefan"
    88 ! !
   122 ! !
    89 
   123 
    90 !TwoByteEncoder class methodsFor:'documentation'!
   124 !TwoByteEncoder class methodsFor:'documentation'!
    91 
   125 
    92 version
   126 version