|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.0 (the 'License'). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License."
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24: /*
25: NXCharacterSet.m
26: Copyright 1991, NeXT, Inc.
27: Responsibility:
28: */
29: #ifndef KERNEL
30: #ifdef SHLIB
31: #import "shlib.h"
32: #endif SHLIB
33:
34: #import "NXCharacterSet.h"
35:
36: // Although this implementation will work for unicodes, it should be more memory efficient
37: // while still keeping the O(1) nature. (For instance, a two-level tree; NULLs in the first
38: // level could indicate 256 zeros, -1 can indicate 256 1s; otherwise a pointer to a real
39: // block of 256 bits...)
40:
41: @implementation NXCharacterSet
42:
43: // ??? The next two numbers are CPU dependant.
44:
45: #define BITSPERINT 32 /* (CHAR_BIT * sizeof(unsigned int)) */
46: #define LOGBITSPERINT 5
47:
48: // ??? Depends on definition of unichar
49:
50: #define NUMCHARACTERS 256
51:
52: // Number of ints in the array keeping the bits.
53:
54: #define NUMINTS (NUMCHARACTERS / sizeof(unsigned int))
55:
56: #define ADDCH(ch) _bits[(ch) >> LOGBITSPERINT] |= (((unsigned)1) << (ch & (BITSPERINT - 1)))
57: #define DELCH(ch) _bits[(ch) >> LOGBITSPERINT] &= ~(((unsigned)1) << (ch & (BITSPERINT - 1)))
58: #define MEMCH(ch) ((_bits[(ch) >> LOGBITSPERINT] & (((unsigned)1) << (ch & (BITSPERINT - 1)))) ? YES : NO)
59:
60: - init
61: {
62: unsigned int cnt;
63:
64: [super init];
65: _bits = (unsigned int *)NXZoneMalloc([self zone], NUMINTS * sizeof(unsigned int));
66: for (cnt = 0; cnt < NUMINTS; cnt++) _bits[cnt] = 0;
67: return self;
68: }
69:
70: - (BOOL)characterIsMember:(unichar)ch
71: {
72: return MEMCH(ch);
73: }
74:
75: - addCharacters:(const unichar *)chars length:(unsigned)len
76: {
77: unsigned int cnt;
78:
79: for (cnt = 0; cnt < len; cnt++) {
80: ADDCH(chars[cnt]);
81: }
82: return self;
83: }
84:
85: - removeCharacters:(const unichar *)chars length:(unsigned)len
86: {
87: unsigned int cnt;
88:
89: for (cnt = 0; cnt < len; cnt++) {
90: DELCH(chars[cnt]);
91: }
92: return self;
93: }
94:
95: - addRange:(unichar)from :(unichar)to
96: {
97: unichar ch;
98: for (ch = from; ch < to; ch++) {
99: ADDCH(ch);
100: }
101: return self;
102: }
103:
104: - removeRange:(unichar)from :(unichar)to;
105: {
106: unichar ch;
107: for (ch = from; ch < to; ch++) {
108: DELCH(ch);
109: }
110: return self;
111: }
112:
113: - invert
114: {
115: int cnt;
116: for (cnt = NUMINTS - 1; cnt >= 0; cnt--) {
117: _bits[cnt] = ~_bits[cnt];
118: }
119: return self;
120: }
121:
122: - unionWith:(const NXCharacterSet *)otherSet
123: {
124: unsigned int *otherBits = ((struct {@defs(NXCharacterSet);} *)otherSet)->_bits;
125: int cnt;
126: for (cnt = NUMINTS - 1; cnt >= 0; cnt--) {
127: _bits[cnt] |= otherBits[cnt];
128: }
129: return self;
130: }
131:
132: - intersectWith:(const NXCharacterSet *)otherSet
133: {
134: unsigned int *otherBits = ((struct {@defs(NXCharacterSet);} *)otherSet)->_bits;
135: int cnt;
136: for (cnt = NUMINTS - 1; cnt >= 0; cnt--) {
137: _bits[cnt] &= otherBits[cnt];
138: }
139: return self;
140: }
141:
142: - copyFromZone:(NXZone *)zone
143: {
144: NXCharacterSet *newInstance = [super copyFromZone:zone];
145: newInstance->_bits = (unsigned int *)NXZoneMalloc(zone, NUMINTS * sizeof(unsigned int));
146: bcopy (_bits, newInstance->_bits, NUMINTS * sizeof(unsigned int));
147: return newInstance;
148: }
149:
150: - free
151: {
152: free (_bits);
153: return [super free];
154: }
155:
156: // ??? Doesn't work properly on little-endian machines. Also very wasteful of space;
157: // Need to write these out compressed.
158:
159: - write:(NXTypedStream *)s
160: {
161: [super write:s];
162: NXWriteArray (s, "i", NUMINTS, &_bits);
163: return self;
164: }
165:
166: - read:(NXTypedStream *)s
167: {
168: [super read:s];
169: NXReadArray (s, "i", NUMINTS, &_bits);
170: return self;
171: }
172:
173: @end
174:
175: /*
176:
177: Created: Jul 91 aozer (Seperated NXCharacterSet code from NXString.m)
178:
179: Modifications:
180: --------------
181:
182: */
183:
184:
185: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.