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