CharacterEncoderImplementations__ISO10646_to_UTF16BE.st
author Claus Gittinger <cg@exept.de>
Thu, 26 Jan 2017 12:49:21 +0100
changeset 21300 9822704484f4
parent 19621 61bf7098b360
child 21387 e3865533e6a6
child 21475 6409725581b1
permissions -rw-r--r--
#OTHER by cg comment/format in: #documentation
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
"{ Package: 'stx:libbasic' }"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"{ NameSpace: CharacterEncoderImplementations }"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
TwoByteEncoder subclass:#ISO10646_to_UTF16BE
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:''
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Text-Encodings'
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!ISO10646_to_UTF16BE class methodsFor:'documentation'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
copyright
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
 COPYRIGHT (c) 2005 by eXept Software AG
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
              All Rights Reserved
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 This software is furnished under a license and may be used
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 only in accordance with the terms of that license and with the
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 be provided or otherwise made available to, or used by, any
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 other person.  No title to or ownership of the software is
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 hereby transferred.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
"
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
documentation
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    encodes/decodes UTF16 BigEndian (big-end-first)
21300
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    42
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    43
    Notice the naming (many are confused):
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    44
        Unicode is the set of number-to-glyph assignments
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    45
    whereas:
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    46
        UTF8, UTF16 etc. are a concrete way of xmitting Unicode codePoints (numbers).
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    47
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    48
    ST/X NEVER uses UTF8 or UTF16 internally - all characters are full 24bit characters.
9822704484f4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19621
diff changeset
    49
    Only when exchanging data, are these converted into UTF8 (or other) byte sequences.
8903
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
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
examples
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
  Encoding (unicode to utf16BE)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
     ISO10646_to_UTF16BE encodeString:'hello'.
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
  Decoding (utf16BE to unicode):
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
     |t|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
     t := ISO10646_to_UTF16BE encodeString:''.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
     ISO10646_to_UTF16BE decodeString:t.
9325
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    64
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    65
  Decoding (utf16LE-Bytes to unicode):
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    66
     |bytes|
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    67
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    68
     bytes := #[ 16r40 0 16r41 0 16r42 0 16r43 0 16r44 0 ].
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    69
     ISO10646_to_UTF16LE decodeString:bytes.
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!ISO10646_to_UTF16BE methodsFor:'encoding & decoding'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
decode:aCode
12432
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
    76
    ^ aCode
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
decodeString:aStringOrByteCollection
14170
eed0dbcc471c comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents: 12432
diff changeset
    80
    "given a byteArray (2-bytes per character) or unsignedShortArray in UTF16 encoding,
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
     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
    82
     Returns either a normal String, a TwoByte- or a FourByte-String instance.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
     Only useful, when reading from external sources.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
     This only handles up-to 30bit characters."
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
14170
eed0dbcc471c comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents: 12432
diff changeset
    86
    |s newString bitsPerElementIn nextIn
12432
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
    87
     codeIn codeIn1 codeIn2 estimatedSize out|
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    aStringOrByteCollection isByteArray ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
        bitsPerElementIn := 8.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
        aStringOrByteCollection isString ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
            bitsPerElementIn := aStringOrByteCollection bitsPerCharacter.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
        ] ifFalse:[
14175
84e1adb65a5d changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents: 14170
diff changeset
    95
            "can be a ShortArray"
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
            bitsPerElementIn := 16.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
        ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
14175
84e1adb65a5d changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents: 14170
diff changeset
   100
    s := aStringOrByteCollection readStream.
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
    bitsPerElementIn == 8 ifTrue:[
14175
84e1adb65a5d changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents: 14170
diff changeset
   102
        s size odd ifTrue:[
14208
4a7349aba15f changed: #decodeString:
Claus Gittinger <cg@exept.de>
parents: 14175
diff changeset
   103
            InvalidEncodingError raiseWith:aStringOrByteCollection errorString:' - size is not a multiple of 2 bytes'.
14175
84e1adb65a5d changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents: 14170
diff changeset
   104
        ].
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
        nextIn := [self nextTwoByteValueFrom:s].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
    ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
        nextIn := [s next].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
14175
84e1adb65a5d changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents: 14170
diff changeset
   110
    estimatedSize := s size * bitsPerElementIn // 16.
12432
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   111
    out := CharacterWriteStream on:(String new:estimatedSize).
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
    [s atEnd] whileFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
        codeIn := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
        codeIn <= 16rFF ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
                codeIn1 := codeIn.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
                codeIn2 := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
                codeIn := ((codeIn1 - 16rD800) bitShift:10)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
                          +
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
                          (codeIn2 - 16rDC00)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
                          + 16r00010000.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
        ].
12432
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   125
        out nextPut:(Character value:codeIn).
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
    ].
12432
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   127
    newString := out contents.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   128
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   129
"/    nBitsRequired := 8.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   130
"/    sz := 0.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   131
"/    [s atEnd] whileFalse:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   132
"/        codeIn := nextIn value.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   133
"/        sz := sz + 1.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   134
"/
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   135
"/        codeIn <= 16rFF ifTrue:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   136
"/        ] ifFalse:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   137
"/            nBitsRequired := nBitsRequired max:16.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   138
"/            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   139
"/                nBitsRequired := 32.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   140
"/                codeIn2 := nextIn value.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   141
"/            ].
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   142
"/        ]
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   143
"/    ].
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   144
"/
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   145
"/    nBitsRequired == 8 ifTrue:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   146
"/        newString := String uninitializedNew:sz
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   147
"/    ] ifFalse:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   148
"/        nBitsRequired <= 16 ifTrue:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   149
"/            newString := Unicode16String new:sz
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   150
"/        ] ifFalse:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   151
"/            newString := Unicode32String new:sz
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   152
"/        ]
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   153
"/    ].
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   154
"/
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   155
"/    s := aStringOrByteCollection readStream.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   156
"/    idx := 1.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   157
"/    [s atEnd] whileFalse:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   158
"/        codeIn := nextIn value.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   159
"/        codeIn <= 16rFF ifTrue:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   160
"/        ] ifFalse:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   161
"/            nBitsRequired := nBitsRequired max:16.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   162
"/            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   163
"/                nBitsRequired := 32.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   164
"/                codeIn1 := codeIn.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   165
"/                codeIn2 := nextIn value.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   166
"/                codeIn := ((codeIn1 - 16rD800) bitShift:10)
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   167
"/                          +
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   168
"/                          (codeIn2 - 16rDC00)
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   169
"/                          + 16r00010000.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   170
"/            ].
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   171
"/        ].
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   172
"/        newString at:idx put:(Character value:codeIn).
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   173
"/        idx := idx + 1.
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   174
"/    ].
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
    ^ newString
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
     self new decodeString:#[ 16r00 16r42 ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
     self new decodeString:#[ 16r01 16r42 ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
     self new decodeString:#[ 16r00 16r48
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
                              16r00 16r69  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
                              16rD8 16r00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
                              16rDC 16r00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
                              16r00 16r21  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
                              16r00 16r21  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
                            ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
     self new decodeString:#( 16r0048
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
                              16r0069  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
                              16rD800  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
                              16rDC00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
                              16r0021  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
                              16r0021  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
                            )
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    "
14208
4a7349aba15f changed: #decodeString:
Claus Gittinger <cg@exept.de>
parents: 14175
diff changeset
   196
4a7349aba15f changed: #decodeString:
Claus Gittinger <cg@exept.de>
parents: 14175
diff changeset
   197
    "Modified: / 12-07-2012 / 19:56:12 / cg"
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
encode:aCode
12432
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   201
    ^ aCode
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
encodeString:aUnicodeString
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
    "return the UTF-16 representation of a aUnicodeString.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
     The resulting string is only useful to be stored on some external file,
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
     not for being used inside ST/X."
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
    |s|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
    s := WriteStream on:(ByteArray uninitializedNew:aUnicodeString size).
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
    aUnicodeString do:[:eachCharacter |
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
        |codePoint t hi low|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
        codePoint := eachCharacter codePoint.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
        (codePoint <= 16rFFFF) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
            ((codePoint <= 16rD7FF) or:[ codePoint between:16rE000 and:16rFFFF]) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
                self nextPutTwoByteValue:codePoint to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
            ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
                "/ unrepresentable: D800..DFFFF
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
                self error:'unrepresentable value (D800..DFFFF) in utf16Encode'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
            t := codePoint - 16r00010000.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
            hi := t bitShift:-10.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
            low := t bitAnd:16r3FF.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
            hi > 16r3FF ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
                "/ unrepresentable: above 110000
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
                self error:'unrepresentable value (> 10FFFF) in utf16Encode'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
            self nextPutTwoByteValue:(hi + 16rD800) to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
            self nextPutTwoByteValue:(low + 16rDC00) to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
        ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
    ^ s contents
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
     (self encodeString:'hello')                                         #[0 104 0 101 0 108 0 108 0 111]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
     (self encodeString:(Character value:16r40) asString)                #[0 64]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
     (self encodeString:(Character value:16rFF) asString)                #[0 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
     (self encodeString:(Character value:16r100) asString)               #[1 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
     (self encodeString:(Character value:16r1000) asString)              #[16 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
     (self encodeString:(Character value:16r2000) asString)              #[32 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
     (self encodeString:(Character value:16r4000) asString)              #[64 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
     (self encodeString:(Character value:16r8000) asString)              #[128 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
     (self encodeString:(Character value:16rD7FF) asString)              #[215 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
     (self encodeString:(Character value:16rE000) asString)              #[224 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
     (self encodeString:(Character value:16rFFFF) asString)              #[255 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
     (self encodeString:(Character value:16r10000) asString)             #[216 64 220 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
     (self encodeString:(Character value:16r10FFF) asString)             #[216 67 223 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
     (self encodeString:(Character value:16r1FFFF) asString)             #[216 127 223 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
     (self encodeString:(Character value:16r10FFFF) asString)            #[219 255 223 255]             
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
    error cases:
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
     (self encodeString:(Character value:16rD800) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
     (self encodeString:(Character value:16rD801) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
     (self encodeString:(Character value:16rDFFF) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
     (self encodeString:(Character value:16r110000) asString)   
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
!ISO10646_to_UTF16BE methodsFor:'private'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
nextPutTwoByteValue:anInteger to:aStream
19621
61bf7098b360 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 19217
diff changeset
   265
    aStream nextPutInt16MSB:anInteger
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
nextTwoByteValueFrom:aStream
19217
0a4b3101afec #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 14208
diff changeset
   269
    ^ aStream nextUnsignedInt16MSB:true
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
!ISO10646_to_UTF16BE methodsFor:'queries'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
nameOfEncoding
14170
eed0dbcc471c comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents: 12432
diff changeset
   275
    ^ #utf16be
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
!ISO10646_to_UTF16BE class methodsFor:'documentation'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
version
19217
0a4b3101afec #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 14208
diff changeset
   281
    ^ '$Header$'
12432
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   282
!
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   283
2c2adc733221 changed:
Claus Gittinger <cg@exept.de>
parents: 9325
diff changeset
   284
version_CVS
19217
0a4b3101afec #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 14208
diff changeset
   285
    ^ '$Header$'
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
! !
19217
0a4b3101afec #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 14208
diff changeset
   287