Encoder_ISO10646_to_UTF16BE.st
author Claus Gittinger <cg@exept.de>
Fri, 08 Jul 2005 19:15:03 +0200
changeset 8913 b9498d27a554
parent 8903 4e15c297fadc
child 9325 a4c635a6f8eb
permissions -rw-r--r--
64bit; mkSmallInteger
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2005 by eXept Software AG
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
"{ Package: 'stx:libbasic' }"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
"{ NameSpace: CharacterEncoderImplementations }"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
TwoByteEncoder subclass:#ISO10646_to_UTF16BE
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	instanceVariableNames:''
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	classVariableNames:''
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	poolDictionaries:''
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
	category:'Collections-Text-Encodings'
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
!ISO10646_to_UTF16BE class methodsFor:'documentation'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
copyright
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 COPYRIGHT (c) 2005 by eXept Software AG
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
              All Rights Reserved
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 This software is furnished under a license and may be used
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 only in accordance with the terms of that license and with the
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 inclusion of the above copyright notice.   This software may not
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 be provided or otherwise made available to, or used by, any
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 other person.  No title to or ownership of the software is
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
 hereby transferred.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
documentation
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    encodes/decodes UTF16 BigEndian (big-end-first)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
examples
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
  Encoding (unicode to utf16BE)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
     ISO10646_to_UTF16BE encodeString:'hello'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
  Decoding (utf16BE to unicode):
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
     |t|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
     t := ISO10646_to_UTF16BE encodeString:''.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
     ISO10646_to_UTF16BE decodeString:t.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
!ISO10646_to_UTF16BE methodsFor:'encoding & decoding'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
decode:aCode
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    self shouldNotImplement "/ no single byte conversion possible
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
decodeString:aStringOrByteCollection
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
    "given a byteArray (2-bytes per character) or unsignedShortArra in UTF16 encoding,
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
     return a new string containing the same characters, in 8, 16bit (or more) encoding.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
     Returns either a normal String, a TwoByte- or a FourByte-String instance.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
     Only useful, when reading from external sources.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
     This only handles up-to 30bit characters."
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
    |sz nBitsRequired s newString idx bitsPerElementIn nextIn
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
     codeIn codeIn1 codeIn2|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    aStringOrByteCollection isByteArray ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
        bitsPerElementIn := 8.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
        aStringOrByteCollection isString ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
            bitsPerElementIn := aStringOrByteCollection bitsPerCharacter.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
            bitsPerElementIn := 16.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
        ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
    bitsPerElementIn == 8 ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
        nextIn := [self nextTwoByteValueFrom:s].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
    ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
        nextIn := [s next].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    nBitsRequired := 8.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    sz := 0.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    s := aStringOrByteCollection readStream.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
    [s atEnd] whileFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
        codeIn := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
        sz := sz + 1.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
        codeIn <= 16rFF ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
            nBitsRequired := nBitsRequired max:16.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
                nBitsRequired := 32.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
                codeIn2 := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
        ]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
    nBitsRequired == 8 ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
        newString := String uninitializedNew:sz
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
    ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
        nBitsRequired <= 16 ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
            newString := Unicode16String new:sz
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
            newString := Unicode32String new:sz
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
        ]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
    s := aStringOrByteCollection readStream.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
    idx := 1.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
    [s atEnd] whileFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
        codeIn := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
        codeIn <= 16rFF ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
            nBitsRequired := nBitsRequired max:16.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
                nBitsRequired := 32.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
                codeIn1 := codeIn.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
                codeIn2 := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
                codeIn := ((codeIn1 - 16rD800) bitShift:10)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
                          +
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
                          (codeIn2 - 16rDC00)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
                          + 16r00010000.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
        ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
        newString at:idx put:(Character value:codeIn).
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
        idx := idx + 1.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
    ^ newString
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
     self new decodeString:#[ 16r00 16r42 ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
     self new decodeString:#[ 16r01 16r42 ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
     self new decodeString:#[ 16r00 16r48
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
                              16r00 16r69  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
                              16rD8 16r00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
                              16rDC 16r00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
                              16r00 16r21  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
                              16r00 16r21  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
                            ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
     self new decodeString:#( 16r0048
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
                              16r0069  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
                              16rD800  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
                              16rDC00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
                              16r0021  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
                              16r0021  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
                            )
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
encode:aCode
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
    self shouldNotImplement "/ no single byte conversion possible
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
encodeString:aUnicodeString
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
    "return the UTF-16 representation of a aUnicodeString.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
     The resulting string is only useful to be stored on some external file,
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
     not for being used inside ST/X."
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
    |s|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
    s := WriteStream on:(ByteArray uninitializedNew:aUnicodeString size).
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
    aUnicodeString do:[:eachCharacter |
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
        |codePoint t hi low|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
        codePoint := eachCharacter codePoint.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
        (codePoint <= 16rFFFF) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
            ((codePoint <= 16rD7FF) or:[ codePoint between:16rE000 and:16rFFFF]) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
                self nextPutTwoByteValue:codePoint to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
            ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
                "/ unrepresentable: D800..DFFFF
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
                self error:'unrepresentable value (D800..DFFFF) in utf16Encode'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
            t := codePoint - 16r00010000.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
            hi := t bitShift:-10.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
            low := t bitAnd:16r3FF.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
            hi > 16r3FF ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
                "/ unrepresentable: above 110000
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
                self error:'unrepresentable value (> 10FFFF) in utf16Encode'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
            self nextPutTwoByteValue:(hi + 16rD800) to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
            self nextPutTwoByteValue:(low + 16rDC00) to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
        ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
    ^ s contents
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
     (self encodeString:'hello')                                         #[0 104 0 101 0 108 0 108 0 111]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
     (self encodeString:(Character value:16r40) asString)                #[0 64]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
     (self encodeString:(Character value:16rFF) asString)                #[0 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
     (self encodeString:(Character value:16r100) asString)               #[1 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
     (self encodeString:(Character value:16r1000) asString)              #[16 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
     (self encodeString:(Character value:16r2000) asString)              #[32 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
     (self encodeString:(Character value:16r4000) asString)              #[64 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
     (self encodeString:(Character value:16r8000) asString)              #[128 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
     (self encodeString:(Character value:16rD7FF) asString)              #[215 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
     (self encodeString:(Character value:16rE000) asString)              #[224 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
     (self encodeString:(Character value:16rFFFF) asString)              #[255 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
     (self encodeString:(Character value:16r10000) asString)             #[216 64 220 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
     (self encodeString:(Character value:16r10FFF) asString)             #[216 67 223 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
     (self encodeString:(Character value:16r1FFFF) asString)             #[216 127 223 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
     (self encodeString:(Character value:16r10FFFF) asString)            #[219 255 223 255]             
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
    error cases:
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
     (self encodeString:(Character value:16rD800) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
     (self encodeString:(Character value:16rD801) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
     (self encodeString:(Character value:16rDFFF) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
     (self encodeString:(Character value:16r110000) asString)   
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
!ISO10646_to_UTF16BE methodsFor:'private'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
nextPutTwoByteValue:anInteger to:aStream
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
    aStream nextPutShort:anInteger MSB:true
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
nextTwoByteValueFrom:aStream
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
    ^ aStream nextUnsignedShortMSB:true
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
!ISO10646_to_UTF16BE methodsFor:'queries'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
nameOfEncoding
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
    ^ #'utf8be'
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
!ISO10646_to_UTF16BE class methodsFor:'documentation'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
version
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
    ^ '$Header: /cvs/stx/stx/libbasic/Attic/Encoder_ISO10646_to_UTF16BE.st,v 1.1 2005-07-07 17:36:41 cg Exp $'
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
! !