CharacterEncoderImplementations__ISO10646_to_JavaText.st
author Jan Vrany <jan.vrany@labware.com>
Wed, 02 Aug 2023 13:04:52 +0100
branchjv
changeset 25462 548cb62b20c7
parent 23107 40173e082cbc
permissions -rw-r--r--
Add `NonPositionableExternalStream >> #setLocalEcho:` and `#setOutputCRLF:`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15944
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2006 by eXept Software AG
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18120
diff changeset
     3
 COPYRIGHT (c) 2009 Jan Vrany
15944
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
              All Rights Reserved
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 This software is furnished under a license and may be used
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 only in accordance with the terms of that license and with the
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 inclusion of the above copyright notice.   This software may not
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 be provided or otherwise made available to, or used by, any
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 other person.  No title to or ownership of the software is
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
 hereby transferred.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
"{ Package: 'stx:libbasic' }"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
"{ NameSpace: CharacterEncoderImplementations }"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
TwoByteEncoder subclass:#ISO10646_to_JavaText
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	instanceVariableNames:''
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	classVariableNames:''
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	poolDictionaries:''
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
	category:'Collections-Text-Encodings'
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
!ISO10646_to_JavaText class methodsFor:'documentation'!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
copyright
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 COPYRIGHT (c) 2006 by eXept Software AG
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18120
diff changeset
    29
 COPYRIGHT (c) 2009 Jan Vrany
15944
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
              All Rights Reserved
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 This software is furnished under a license and may be used
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 only in accordance with the terms of that license and with the
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 be provided or otherwise made available to, or used by, any
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
 other person.  No title to or ownership of the software is
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
 hereby transferred.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
documentation
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    Translates \uXXXX-escapes in the text
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
! !
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!ISO10646_to_JavaText methodsFor:'encoding & decoding'!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
decode:aCode
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    self shouldNotImplement "/ no single byte conversion possible
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
decodeString:aStringOrByteCollection
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
    "given a string in JavaText encoding (i.e. with \uXXXX escaped characters),
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
     return a new string containing the same characters, in 16bit (or more) encoding.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
     Returns either a normal String, a TwoByteString or a FourByteString instance.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
     Only useful, when reading Java property and resource files.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
     This only handles up-to 30bit characters."
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    |nBits ch 
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
     in out codePoint t|
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    nBits := 8.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
    in := aStringOrByteCollection readStream.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
    out := WriteStream on:(String new:10).
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    [in atEnd] whileFalse:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
        ch := in next.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
        ch == $\ ifTrue:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
            in peekOrNil == $u ifTrue:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
                in next.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
                codePoint := 0.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
                4 timesRepeat:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
                    ch := in peekOrNil.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
                    codePoint := (codePoint * 16) + ch digitValue.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
                    in next.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
                ].
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
                codePoint > 16rFF ifTrue:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
                    codePoint > 16rFFFF ifTrue:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
                        nBits < 32 ifTrue:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
                            t := out contents.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
                            out := WriteStream on:(Unicode32String fromString:t).
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
                            out position:t size.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
                            nBits := 32.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
                        ]
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
                    ] ifFalse:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
                        nBits < 16 ifTrue:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
                            t := out contents.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
                            out := WriteStream on:(Unicode16String fromString:t).
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
                            out position:t size.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
                            nBits := 16.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
                        ]
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
                    ]
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
                ].
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
                out nextPut:(Character value:codePoint).
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
            ] ifFalse:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
                out nextPut:ch
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
            ]
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
        ] ifFalse:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
            out nextPut:ch
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
        ].
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
    ].
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
    ^ out contents
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
    "
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
     CharacterEncoderImplementations::ISO10646_to_JavaText
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
        decodeString:'AB\u1234CD' 
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    "
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
    "Modified: / 23-10-2006 / 13:23:18 / cg"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
encode:aCode
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    self shouldNotImplement "/ no single byte conversion possible
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
encodeString:aUnicodeString
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
    "return the JavaText representation of aUnicodeString.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
     The resulting string is only useful to be stored on some external file,
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
     not for being used inside ST/X."
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
    |ch in out codePoint|
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
    in := aUnicodeString readStream.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
    out := WriteStream on:(String new:10).
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
    [in atEnd] whileFalse:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
        ch := in next.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
        codePoint := ch codePoint.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
        (codePoint between:16r20 and:16r7F) ifTrue:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
            out nextPut:ch.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
        ] ifFalse:[
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
            out nextPutAll:'\u'.
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
            out nextPutAll:((codePoint printStringRadix:16) leftPaddedTo:4 with:$0).
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
        ].
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
    ].
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
    ^ out contents
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
    "
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
     CharacterEncoderImplementations::ISO10646_to_JavaText
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
        encodeString:'hello '  
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
     CharacterEncoderImplementations::ISO10646_to_JavaText
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
        decodeString:(CharacterEncoderImplementations::ISO10646_to_JavaText encodeString:'hello ') 
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
    "
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
    "Modified: / 23-10-2006 / 13:25:03 / cg"
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
! !
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
!ISO10646_to_JavaText class methodsFor:'documentation'!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
version
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
    ^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__ISO10646_to_JavaText.st,v 1.1 2014-02-05 17:11:06 cg Exp $'
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
!
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
version_CVS
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
    ^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__ISO10646_to_JavaText.st,v 1.1 2014-02-05 17:11:06 cg Exp $'
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
! !
8299575b9314 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157