CharacterEncoderImplementations__ISO10646_to_UTF16LE.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 24215 0653f0a9a05c
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) 2005 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 }"

ISO10646_to_UTF16BE subclass:#ISO10646_to_UTF16LE
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Text-Encodings'
!

!ISO10646_to_UTF16LE class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2005 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
"
    encodes/decodes UTF16 LittleEndian (little-end-first)

    Notice the naming (many are confused):
        Unicode is the set of number-to-glyph assignments
    whereas:
        UTF8, UTF16 etc. are a concrete way of xmitting Unicode codePoints (numbers).

    ST/X NEVER uses UTF8 or UTF16 internally - all characters are full 24bit characters.
    Only when exchanging data, are these converted into UTF8 (or other) byte sequences.
"
!

examples
"
  Encoding (unicode to utf16LE)
     ISO10646_to_UTF16LE encodeString:'hello'.


  Decoding (utf16LE to unicode):
     |t|

     t := ISO10646_to_UTF16LE encodeString:'ÄÖÜäöüß'.
     ISO10646_to_UTF16LE decodeString:t.

  Decoding (utf16LE-Bytes to unicode):
     |bytes|

     bytes := #[ 16r40 0 16r41 0 16r42 0 16r43 0 16r44 0 ].
     ISO10646_to_UTF16LE decodeString:bytes.
"
! !

!ISO10646_to_UTF16LE methodsFor:'encoding & decoding'!

decode:codePoint
    ^ codePoint swapBytes
!

encode:codePoint
    ^ codePoint swapBytes
!

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

    |stream size "{ Class:SmallInteger }"|

    size := aUnicodeString size.
    stream := WriteStream on:(ByteArray uninitializedNew:size * 2).

    1 to:size do:[:idx |
        stream nextPutUtf16Bytes:(aUnicodeString at:idx) MSB:false.
    ].

    ^ stream contents

    "Created: / 16-01-2018 / 19:45:15 / stefan"
    "Modified: / 28-05-2019 / 13:50:22 / Stefan Vogel"
!

encodeString:aUnicodeString on:aStream
    "given a string in unicode, encode it onto aStream."

     aStream nextPutAllUtf16Bytes:aUnicodeString MSB:false.

    "Created: / 16-02-2017 / 16:42:36 / stefan"
! !

!ISO10646_to_UTF16LE methodsFor:'private'!

nextTwoByteValueFrom:aStream
    ^ aStream nextUnsignedInt16MSB:false
! !

!ISO10646_to_UTF16LE methodsFor:'queries'!

nameOfEncoding
    ^ #utf16le
! !

!ISO10646_to_UTF16LE methodsFor:'stream support'!

encodeCharacter:aUnicodeCharacter on:aStream
    "given a character in unicode, encode it onto aStream."

     aStream nextPutUtf16Bytes:aUnicodeCharacter MSB:false.

    "Created: / 16-02-2017 / 16:42:19 / stefan"
    "Modified (comment): / 16-01-2018 / 19:22:50 / stefan"
! !

!ISO10646_to_UTF16LE class methodsFor:'documentation'!

version
    ^ '$Header$'
! !