CharacterEncoderImplementations__ISO10646_to_UTF16BE.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 05 May 2012 22:58:24 +0100
branchjv
changeset 17940 985e22966acb
parent 17911 a99f15c5efa5
child 17941 3651a18f3703
permissions -rw-r--r--
Merged with /trunk
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)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
"
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
examples
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
  Encoding (unicode to utf16BE)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
     ISO10646_to_UTF16BE encodeString:'hello'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
  Decoding (utf16BE to unicode):
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
     |t|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
     t := ISO10646_to_UTF16BE encodeString:''.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
     ISO10646_to_UTF16BE decodeString:t.
9325
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    56
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    57
  Decoding (utf16LE-Bytes to unicode):
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    58
     |bytes|
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    59
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    60
     bytes := #[ 16r40 0 16r41 0 16r42 0 16r43 0 16r44 0 ].
a4c635a6f8eb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8903
diff changeset
    61
     ISO10646_to_UTF16LE decodeString:bytes.
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
"
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
!ISO10646_to_UTF16BE methodsFor:'encoding & decoding'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
decode:aCode
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
    68
    ^ aCode
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
decodeString:aStringOrByteCollection
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    "given a byteArray (2-bytes per character) or unsignedShortArra in UTF16 encoding,
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
     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
    74
     Returns either a normal String, a TwoByte- or a FourByte-String instance.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
     Only useful, when reading from external sources.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
     This only handles up-to 30bit characters."
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    |sz nBitsRequired s newString idx bitsPerElementIn nextIn
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
    79
     codeIn codeIn1 codeIn2 estimatedSize out|
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
    aStringOrByteCollection isByteArray ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
        bitsPerElementIn := 8.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        aStringOrByteCollection isString ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
            bitsPerElementIn := aStringOrByteCollection bitsPerCharacter.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
            bitsPerElementIn := 16.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
        ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    ].
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
    90
    estimatedSize := aStringOrByteCollection size * bitsPerElementIn // 16.
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    bitsPerElementIn == 8 ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
        nextIn := [self nextTwoByteValueFrom:s].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
        nextIn := [s next].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    s := aStringOrByteCollection readStream.
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
    99
    out := CharacterWriteStream on:(String new:estimatedSize).
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    [s atEnd] whileFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
        codeIn := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
        codeIn <= 16rFF ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
                codeIn1 := codeIn.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
                codeIn2 := nextIn value.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
                codeIn := ((codeIn1 - 16rD800) bitShift:10)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
                          +
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
                          (codeIn2 - 16rDC00)
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
                          + 16r00010000.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
        ].
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   113
        out nextPut:(Character value:codeIn).
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
    ].
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   115
    newString := out contents.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   116
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   117
"/    nBitsRequired := 8.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   118
"/    sz := 0.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   119
"/    [s atEnd] whileFalse:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   120
"/        codeIn := nextIn value.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   121
"/        sz := sz + 1.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   122
"/
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   123
"/        codeIn <= 16rFF ifTrue:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   124
"/        ] ifFalse:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   125
"/            nBitsRequired := nBitsRequired max:16.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   126
"/            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   127
"/                nBitsRequired := 32.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   128
"/                codeIn2 := nextIn value.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   129
"/            ].
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   130
"/        ]
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   131
"/    ].
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   132
"/
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   133
"/    nBitsRequired == 8 ifTrue:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   134
"/        newString := String uninitializedNew:sz
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   135
"/    ] ifFalse:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   136
"/        nBitsRequired <= 16 ifTrue:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   137
"/            newString := Unicode16String new:sz
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   138
"/        ] ifFalse:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   139
"/            newString := Unicode32String new:sz
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   140
"/        ]
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   141
"/    ].
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   142
"/
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   143
"/    s := aStringOrByteCollection readStream.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   144
"/    idx := 1.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   145
"/    [s atEnd] whileFalse:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   146
"/        codeIn := nextIn value.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   147
"/        codeIn <= 16rFF ifTrue:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   148
"/        ] ifFalse:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   149
"/            nBitsRequired := nBitsRequired max:16.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   150
"/            (codeIn between:16rD800 and:16rDBFF) ifTrue:[
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   151
"/                nBitsRequired := 32.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   152
"/                codeIn1 := codeIn.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   153
"/                codeIn2 := nextIn value.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   154
"/                codeIn := ((codeIn1 - 16rD800) bitShift:10)
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   155
"/                          +
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   156
"/                          (codeIn2 - 16rDC00)
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   157
"/                          + 16r00010000.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   158
"/            ].
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   159
"/        ].
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   160
"/        newString at:idx put:(Character value:codeIn).
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   161
"/        idx := idx + 1.
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   162
"/    ].
8903
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
    ^ newString
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
     self new decodeString:#[ 16r00 16r42 ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
     self new decodeString:#[ 16r01 16r42 ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
     self new decodeString:#[ 16r00 16r48
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
                              16r00 16r69  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
                              16rD8 16r00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
                              16rDC 16r00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
                              16r00 16r21  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
                              16r00 16r21  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
                            ]            
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
     self new decodeString:#( 16r0048
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
                              16r0069  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
                              16rD800  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
                              16rDC00  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
                              16r0021  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
                              16r0021  
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
                            )
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
encode:aCode
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   187
    ^ aCode
8903
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
encodeString:aUnicodeString
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
    "return the UTF-16 representation of a aUnicodeString.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
     The resulting string is only useful to be stored on some external file,
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
     not for being used inside ST/X."
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    |s|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
    s := WriteStream on:(ByteArray uninitializedNew:aUnicodeString size).
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
    aUnicodeString do:[:eachCharacter |
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
        |codePoint t hi low|
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
        codePoint := eachCharacter codePoint.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
        (codePoint <= 16rFFFF) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
            ((codePoint <= 16rD7FF) or:[ codePoint between:16rE000 and:16rFFFF]) ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
                self nextPutTwoByteValue:codePoint to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
            ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
                "/ unrepresentable: D800..DFFFF
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
                self error:'unrepresentable value (D800..DFFFF) in utf16Encode'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
        ] ifFalse:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
            t := codePoint - 16r00010000.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
            hi := t bitShift:-10.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
            low := t bitAnd:16r3FF.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
            hi > 16r3FF ifTrue:[
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
                "/ unrepresentable: above 110000
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
                self error:'unrepresentable value (> 10FFFF) in utf16Encode'.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
            ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
            self nextPutTwoByteValue:(hi + 16rD800) to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
            self nextPutTwoByteValue:(low + 16rDC00) to:s.
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
        ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
    ].
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
    ^ s contents
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
     (self encodeString:'hello')                                         #[0 104 0 101 0 108 0 108 0 111]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
     (self encodeString:(Character value:16r40) asString)                #[0 64]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
     (self encodeString:(Character value:16rFF) asString)                #[0 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
     (self encodeString:(Character value:16r100) asString)               #[1 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
     (self encodeString:(Character value:16r1000) asString)              #[16 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
     (self encodeString:(Character value:16r2000) asString)              #[32 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
     (self encodeString:(Character value:16r4000) asString)              #[64 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
     (self encodeString:(Character value:16r8000) asString)              #[128 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
     (self encodeString:(Character value:16rD7FF) asString)              #[215 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
     (self encodeString:(Character value:16rE000) asString)              #[224 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
     (self encodeString:(Character value:16rFFFF) asString)              #[255 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
     (self encodeString:(Character value:16r10000) asString)             #[216 64 220 0]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
     (self encodeString:(Character value:16r10FFF) asString)             #[216 67 223 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
     (self encodeString:(Character value:16r1FFFF) asString)             #[216 127 223 255]
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
     (self encodeString:(Character value:16r10FFFF) asString)            #[219 255 223 255]             
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
    error cases:
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
     (self encodeString:(Character value:16rD800) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
     (self encodeString:(Character value:16rD801) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
     (self encodeString:(Character value:16rDFFF) asString) 
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
     (self encodeString:(Character value:16r110000) asString)   
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
    "
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
!ISO10646_to_UTF16BE methodsFor:'private'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
nextPutTwoByteValue:anInteger to:aStream
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
    aStream nextPutShort:anInteger MSB:true
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
nextTwoByteValueFrom:aStream
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
    ^ aStream nextUnsignedShortMSB:true
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
!ISO10646_to_UTF16BE methodsFor:'queries'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
nameOfEncoding
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
    ^ #'utf8be'
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
! !
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
!ISO10646_to_UTF16BE class methodsFor:'documentation'!
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
4e15c297fadc initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
version
17940
985e22966acb Merged with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17911
diff changeset
   267
    ^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__ISO10646_to_UTF16BE.st,v 1.3 2009/11/04 00:46:08 cg Exp $'
17735
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   268
!
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   269
6a5bc05f696a Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17728
diff changeset
   270
version_CVS
17940
985e22966acb Merged with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17911
diff changeset
   271
    ^ '§Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__ISO10646_to_UTF16BE.st,v 1.3 2009/11/04 00:46:08 cg Exp §'
17865
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
   272
!
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
   273
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
   274
version_SVN
17940
985e22966acb Merged with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17911
diff changeset
   275
    ^ '$Id: CharacterEncoderImplementations__ISO10646_to_UTF16BE.st 10807 2012-05-05 21:58:24Z vranyj1 $'
17846
24edc476ac18 Merged with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17845
diff changeset
   276
! !
17892
d86c8bd5ece3 Merged with CVS
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17883
diff changeset
   277
17910
8d796ca8bd1d Merged with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17892
diff changeset
   278
17911
a99f15c5efa5 Updated with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17910
diff changeset
   279