CharacterEncoderImplementations__ISO10646_to_JavaText.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 22476 b30058f26971
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

"{ NameSpace: CharacterEncoderImplementations }"

VariableBytesEncoder subclass:#ISO10646_to_JavaText
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Text-Encodings'
!

!ISO10646_to_JavaText class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Translates \uXXXX-escapes in the text
"
! !

!ISO10646_to_JavaText methodsFor:'encoding & decoding'!

decodeString:aStringOrByteCollection
    "given a string in JavaText encoding (i.e. with \uXXXX escaped characters),
     return a new string containing the same characters, in 16bit (or more) encoding.
     Returns either a normal String, a Unicode16String or a Unicode32String instance.
     Only useful, when reading Java property and resource files.
     This only handles up-to 30bit characters."

    |nBits ch 
     in out codePoint|

    nBits := 8.
    in := aStringOrByteCollection readStream.
    out := CharacterWriteStream on:(String new:10).
    [in atEnd] whileFalse:[
        ch := in next.
        ch == $\ ifTrue:[
            in peekOrNil == $u ifTrue:[
                in next.
                codePoint := 0.
                4 timesRepeat:[
                    codePoint := (codePoint * 16) + in next digitValue.
                ].
                out nextPut:(Character codePoint:codePoint).
            ] ifFalse:[
                out nextPut:ch
            ]
        ] ifFalse:[
            out nextPut:ch
        ].
    ].
    ^ out contents

    "
     CharacterEncoderImplementations::ISO10646_to_JavaText
        decodeString:'AB\u1234CD' 
    "

    "Modified: / 23-10-2006 / 13:23:18 / cg"
    "Modified: / 17-01-2018 / 18:34:52 / stefan"
!

encodeString:aUnicodeString
    "return the JavaText representation of aUnicodeString.
     The resulting string is only useful to be stored on some external file,
     not for being used inside ST/X."

    |ch in out codePoint|

    in := aUnicodeString readStream.
    out := WriteStream on:(String new:10).
    [in atEnd] whileFalse:[
        ch := in next.
        codePoint := ch codePoint.
        (codePoint between:16r20 and:16r7F) ifTrue:[
            out nextPut:ch.
        ] ifFalse:[
            out nextPutAll:'\u'.
            out nextPutAll:((codePoint printStringRadix:16) leftPaddedTo:4 with:$0).
        ].
    ].
    ^ out contents

    "
     CharacterEncoderImplementations::ISO10646_to_JavaText
        encodeString:'hello '  

     CharacterEncoderImplementations::ISO10646_to_JavaText
        decodeString:(CharacterEncoderImplementations::ISO10646_to_JavaText encodeString:'hello ') 
    "

    "Modified: / 23-10-2006 / 13:25:03 / cg"
! !

!ISO10646_to_JavaText methodsFor:'queries'!

characterSize:aCharacter
    (aCharacter codePoint between:16r20 and:16r7F) ifTrue:[
        ^ 1.
    ].
    ^ 6    "\u1234"

    "Created: / 17-01-2018 / 17:58:59 / stefan"
! !

!ISO10646_to_JavaText methodsFor:'stream support'!

readNextCharacterFrom:aStream
    |char codePoint|

    char := aStream next.
    (char ~~ $\ and:[aStream peek ~~ $u]) ifTrue:[
        ^ char.
    ].
    aStream next.
    codePoint := 0.
    4 timesRepeat:[
        codePoint := (codePoint * 16) + aStream next digitValue.
    ].
    ^ Character codePoint:codePoint.

    "Created: / 17-01-2018 / 18:33:22 / stefan"
! !

!ISO10646_to_JavaText class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !