CharacterSet.st
author Claus Gittinger <cg@exept.de>
Fri, 28 Jan 2011 17:52:38 +0100
changeset 2526 86d810136a4b
child 2933 2325124dbff7
permissions -rw-r--r--
initial checkin

"
 COPYRIGHT (c) 2011 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:libbasic2' }"

Collection subclass:#CharacterSet
	instanceVariableNames:'bits'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Unordered'
!

!CharacterSet class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2011 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
"
    Used to represent 8-bit character sets (for now) as a bitmap.
    Bit[n] is set, if Character codePoint:(n-1) is included in the set.

    [author:]
        Claus Gittinger
"
! !

!CharacterSet class methodsFor:'instance creation'!

new
    ^ self basicNew initialize

    "Created: / 28-01-2011 / 17:29:24 / cg"
! !

!CharacterSet methodsFor:'adding & removing'!

add:aCharacter
    |cp byteIndex bitIndex|

    cp := aCharacter codePoint.
    self assert:(cp <= 255).
    byteIndex := (cp // 8) + 1.
    bitIndex := (cp \\ 8) + 1.
    byteIndex > bits size ifTrue:[
        bits := (ByteArray new:byteIndex) replaceFrom:1 with:bits.
    ].
    bits at:byteIndex put:((bits at:byteIndex) setBit:bitIndex).
    ^ aCharacter "/ sigh

    "Created: / 28-01-2011 / 17:44:21 / cg"
!

remove:aCharacter ifAbsent:exceptionValue
    |cp byteIndex bitIndex mask|

    cp := aCharacter codePoint.
    self assert:(cp <= 255).
    byteIndex := (cp // 8) + 1.
    bitIndex := (cp \\ 8) + 1.
    byteIndex <= bits size ifTrue:[
        ((mask := bits at:byteIndex) isBitSet:bitIndex) ifTrue:[
            bits at:byteIndex put:(mask clearBit:bitIndex).
            ^ aCharacter
        ].
    ].
    ^ exceptionValue value

    "Created: / 28-01-2011 / 17:51:22 / cg"
! !

!CharacterSet methodsFor:'initialization'!

initialize
    bits := nil "/ empty

    "Created: / 28-01-2011 / 17:29:48 / cg"
! !

!CharacterSet methodsFor:'queries'!

do:aBlock
    |cp|

    cp := 0.
    bits notNil ifTrue:[
        bits do:[:eachByte |
            eachByte ~~ 0 ifTrue:[
                #(1 2 4 8 16 32 64 128) do:[:mask |
                    (eachByte bitTest:mask) ifTrue:[
                        aBlock value:(Character codePoint:cp).
                    ].
                    cp := cp + 1.
                ].
            ] ifFalse:[
                cp := cp + 8.
            ].
        ]
    ].

    "Created: / 28-01-2011 / 17:39:16 / cg"
!

size
    |n|

    bits isNil ifTrue:[^ 0].

    n := 0.
    bits do:[:eachByte |
        n := n + (eachByte bitCount)
    ].
    ^ n

    "Created: / 28-01-2011 / 17:35:21 / cg"
! !

!CharacterSet class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/CharacterSet.st,v 1.1 2011-01-28 16:52:38 cg Exp $'
! !