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