/[gnustep]/gnustep/core/base/Source/NSCharacterSet.m
ViewVC logotype

Contents of /gnustep/core/base/Source/NSCharacterSet.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (show annotations) (download)
Sat Jun 7 01:24:40 2003 UTC (20 years, 10 months ago) by fedor
Branch: MAIN
CVS Tags: pre-header-reorg-20030731, base-1_7_1, base-1_7_2, base-1_7_3, base-1_7_4, base-1_8_0
Branch point for: freeze_1_8_0
Changes since 1.40: +10 -10 lines
Change syntax of includes

1 /** NSCharacterSet - Character set holder
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 Written by: Adam Fedor <fedor@boulder.colorado.edu>
5 Date: Apr 1995
6
7 This file is part of the GNUstep Base Library.
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
22
23 <title>NSCharacterSet class reference</title>
24 $Date: 2003/04/28 02:29:49 $ $Revision: 1.40 $
25 */
26
27 #include "config.h"
28 #include "Foundation/NSArray.h"
29 #include "Foundation/NSBitmapCharSet.h"
30 #include "Foundation/NSException.h"
31 #include "Foundation/NSBundle.h"
32 #include "Foundation/NSData.h"
33 #include "Foundation/NSLock.h"
34 #include "Foundation/NSPathUtilities.h"
35 #include "Foundation/NSDictionary.h"
36
37 static NSString* NSCharacterSet_PATH = @"NSCharacterSets";
38
39 /* A simple array for caching standard bitmap sets */
40 #define MAX_STANDARD_SETS 15
41 static NSCharacterSet* cache_set[MAX_STANDARD_SETS];
42 static NSLock* cache_lock = nil;
43
44 @implementation NSCharacterSet
45
46 + (void) initialize
47 {
48 static BOOL one_time = NO;
49
50 if (one_time == NO)
51 {
52 unsigned i;
53
54 for (i = 0; i < MAX_STANDARD_SETS; i++)
55 {
56 cache_set[i] = 0;
57 }
58 one_time = YES;
59 }
60 }
61
62 /* Provide a default object for allocation */
63 + (id) allocWithZone: (NSZone*)zone
64 {
65 return NSAllocateObject([NSBitmapCharSet self], 0, zone);
66 }
67
68 // Creating standard character sets
69
70 + (NSCharacterSet*) _bitmapForSet: (NSString*)setname number: (int)number
71 {
72 NSCharacterSet *set;
73 NSString *set_path;
74 NSBundle *bundle;
75
76 if (!cache_lock)
77 cache_lock = [NSLock new];
78 [cache_lock lock];
79
80 set = nil; /* Quiet warnings */
81 if (cache_set[number] == nil)
82 {
83 NS_DURING
84
85 bundle = [NSBundle bundleForLibrary: @"gnustep-base"];
86 set_path = [bundle pathForResource: setname
87 ofType: @"dat"
88 inDirectory: NSCharacterSet_PATH];
89 if (set_path != nil)
90 {
91 NS_DURING
92 /* Load the character set file */
93 set = [self characterSetWithBitmapRepresentation:
94 [NSData dataWithContentsOfFile: set_path]];
95 NS_HANDLER
96 NSLog(@"Unable to read NSCharacterSet file %@", set_path);
97 set = nil;
98 NS_ENDHANDLER
99 }
100
101 /* If we didn't load a set then raise an exception */
102 if (!set)
103 {
104 [NSException raise: NSGenericException
105 format: @"Could not find bitmap file %@", setname];
106 /* NOT REACHED */
107 }
108 else
109 {
110 /* Else cache the set */
111 cache_set[number] = RETAIN(set);
112
113 }
114 NS_HANDLER
115 [cache_lock unlock];
116 [localException raise];
117 abort (); /* quiet warnings about `set' clobbered by longjmp. */
118 NS_ENDHANDLER
119 }
120 else
121 set = cache_set[number];
122
123 [cache_lock unlock];
124 return set;
125 }
126
127
128 + (NSCharacterSet*) alphanumericCharacterSet
129 {
130 return [self _bitmapForSet: @"alphanumericCharSet" number: 0];
131 }
132
133 + (NSCharacterSet*) controlCharacterSet
134 {
135 return [self _bitmapForSet: @"controlCharSet" number: 1];
136 }
137
138 /**
139 * Returns a character set containing characters that represent
140 * the decimal digits 0 through 9.
141 */
142 + (NSCharacterSet*) decimalDigitCharacterSet
143 {
144 return [self _bitmapForSet: @"decimalDigitCharSet" number: 2];
145 }
146
147 /**
148 * Returns a character set containing individual charactars that
149 * can be represented also by a composed character sequence.
150 */
151 + (NSCharacterSet*) decomposableCharacterSet
152 {
153 return [self _bitmapForSet: @"decomposableCharSet" number: 3];
154 }
155
156 /**
157 * Returns a character set containing unassigned (illegal)
158 * character values.
159 */
160 + (NSCharacterSet*) illegalCharacterSet
161 {
162 return [self _bitmapForSet: @"illegalCharSet" number: 4];
163 }
164
165 + (NSCharacterSet*) letterCharacterSet
166 {
167 return [self _bitmapForSet: @"letterCharSet" number: 5];
168 }
169
170 /**
171 * Returns a character set that contains the lowercase characters.
172 * This set does not include caseless characters, only those that
173 * have corresponding characters in uppercase and/or titlecase.
174 */
175 + (NSCharacterSet*) lowercaseLetterCharacterSet
176 {
177 return [self _bitmapForSet: @"lowercaseLetterCharSet" number: 6];
178 }
179
180 + (NSCharacterSet*) nonBaseCharacterSet
181 {
182 return [self _bitmapForSet: @"nonBaseCharSet" number: 7];
183 }
184
185 + (NSCharacterSet*) punctuationCharacterSet
186 {
187 return [self _bitmapForSet: @"punctuationCharSet" number: 8];
188 }
189
190 + (NSCharacterSet*) symbolAndOperatorCharacterSet
191 {
192 return [self _bitmapForSet: @"symbolAndOperatorCharSet" number: 9];
193 }
194
195 /**
196 * Returns a character set that contains the uppercase characters.
197 * This set does not include caseless characters, only those that
198 * have corresponding characters in lowercase and/or titlecase.
199 */
200 + (NSCharacterSet*) uppercaseLetterCharacterSet
201 {
202 return [self _bitmapForSet: @"uppercaseLetterCharSet" number: 10];
203 }
204
205 /**
206 * Returns a character set that contains the whitespace characters,
207 * plus the newline characters, values 0x000A and 0x000D.
208 */
209 + (NSCharacterSet*) whitespaceAndNewlineCharacterSet
210 {
211 return [self _bitmapForSet: @"whitespaceAndNlCharSet" number: 11];
212 }
213
214 /**
215 * Returns a character set that contains the whitespace characters.
216 */
217 + (NSCharacterSet*) whitespaceCharacterSet
218 {
219 return [self _bitmapForSet: @"whitespaceCharSet" number: 12];
220 }
221
222 // Creating custom character sets
223
224 /**
225 * Returns a character set containing characters as encoded in the
226 * data object.
227 */
228 + (NSCharacterSet*) characterSetWithBitmapRepresentation: (NSData*)data
229 {
230 return AUTORELEASE([[NSBitmapCharSet alloc] initWithBitmap: data]);
231 }
232
233 + (NSCharacterSet*) characterSetWithCharactersInString: (NSString*)aString
234 {
235 unsigned i;
236 unsigned length;
237 unsigned char *bytes;
238 NSMutableData *bitmap = [NSMutableData dataWithLength: BITMAP_SIZE];
239
240 if (!aString)
241 {
242 [NSException raise: NSInvalidArgumentException
243 format: @"Creating character set with nil string"];
244 /* NOT REACHED */
245 }
246
247 length = [aString length];
248 bytes = [bitmap mutableBytes];
249 for (i = 0; i < length; i++)
250 {
251 unichar letter = [aString characterAtIndex: i];
252
253 SETBIT(bytes[letter/8], letter % 8);
254 }
255
256 return [self characterSetWithBitmapRepresentation: bitmap];
257 }
258
259 + (NSCharacterSet*)characterSetWithRange: (NSRange)aRange
260 {
261 unsigned i;
262 unsigned char *bytes;
263 NSMutableData *bitmap = [NSMutableData dataWithLength: BITMAP_SIZE];
264
265 if (NSMaxRange(aRange) > UNICODE_SIZE)
266 {
267 [NSException raise: NSInvalidArgumentException
268 format: @"Specified range exceeds character set"];
269 /* NOT REACHED */
270 }
271
272 bytes = (unsigned char*)[bitmap mutableBytes];
273 for (i = aRange.location; i < NSMaxRange(aRange); i++)
274 {
275 SETBIT(bytes[i/8], i % 8);
276 }
277 return [self characterSetWithBitmapRepresentation: bitmap];
278 }
279
280 + (NSCharacterSet*) characterSetWithContentsOfFile: (NSString*)aFile
281 {
282 if ([@"bitmap" isEqual: [aFile pathExtension]])
283 {
284 NSData *bitmap = [NSData dataWithContentsOfFile: aFile];
285 return [self characterSetWithBitmapRepresentation: bitmap];
286 }
287 else
288 return nil;
289 }
290
291 /**
292 * Returns a bitmap representation of the receiver's character set
293 * suitable for archiving or writing to a file, in an NSData object.
294 */
295 - (NSData*) bitmapRepresentation
296 {
297 [self subclassResponsibility: _cmd];
298 return 0;
299 }
300
301 /**
302 * Returns YES if the receiver contains <em>aCharacter</em>, NO if
303 * it does not.
304 */
305 - (BOOL) characterIsMember: (unichar)aCharacter
306 {
307 [self subclassResponsibility: _cmd];
308 return 0;
309 }
310
311 - (void) encodeWithCoder: (NSCoder*)aCoder
312 {
313 [self subclassResponsibility: _cmd];
314 }
315
316 - (id) initWithCoder: (NSCoder*)aCoder
317 {
318 [self subclassResponsibility: _cmd];
319 return nil;
320 }
321
322 - (BOOL) isEqual: (id)anObject
323 {
324 if (anObject == self)
325 return YES;
326 if ([anObject isKindOfClass: [NSCharacterSet class]])
327 {
328 unsigned i;
329
330 for (i = 0; i <= 0xffff; i++)
331 {
332 if ([self characterIsMember: (unichar)i]
333 != [anObject characterIsMember: (unichar)i])
334 {
335 return NO;
336 }
337 }
338 return YES;
339 }
340 return NO;
341 }
342
343 /**
344 * Returns a character set containing only characters that the
345 * receiver does not contain.
346 */
347 - (NSCharacterSet*) invertedSet
348 {
349 unsigned i;
350 unsigned length;
351 unsigned char *bytes;
352 NSMutableData *bitmap;
353
354 bitmap = AUTORELEASE([[self bitmapRepresentation] mutableCopy]);
355 length = [bitmap length];
356 bytes = [bitmap mutableBytes];
357 for (i = 0; i < length; i++)
358 {
359 bytes[i] = ~bytes[i];
360 }
361 return [[self class] characterSetWithBitmapRepresentation: bitmap];
362 }
363
364
365 // NSCopying, NSMutableCopying
366 - (id) copyWithZone: (NSZone*)zone
367 {
368 if (NSShouldRetainWithZone(self, zone))
369 return RETAIN(self);
370 else
371 return NSCopyObject (self, 0, zone);
372 }
373
374 - (id) mutableCopyWithZone: (NSZone*)zone
375 {
376 NSData *bitmap;
377 bitmap = [self bitmapRepresentation];
378 return [[NSMutableBitmapCharSet allocWithZone: zone] initWithBitmap: bitmap];
379 }
380
381 @end
382
383 @implementation NSMutableCharacterSet
384
385 /* Provide a default object for allocation */
386 + (id) allocWithZone: (NSZone*)zone
387 {
388 return NSAllocateObject([NSMutableBitmapCharSet self], 0, zone);
389 }
390
391 /* Override this from NSCharacterSet to create the correct class */
392 + (NSCharacterSet*) characterSetWithBitmapRepresentation: (NSData*)data
393 {
394 return AUTORELEASE([[NSMutableBitmapCharSet alloc] initWithBitmap: data]);
395 }
396
397 /* Mutable subclasses must implement ALL of these methods. */
398 - (void) addCharactersInRange: (NSRange)aRange
399 {
400 [self subclassResponsibility: _cmd];
401 }
402
403 - (void) addCharactersInString: (NSString*)aString
404 {
405 [self subclassResponsibility: _cmd];
406 }
407
408 - (void) formUnionWithCharacterSet: (NSCharacterSet*)otherSet
409 {
410 [self subclassResponsibility: _cmd];
411 }
412
413 - (void) formIntersectionWithCharacterSet: (NSCharacterSet*)otherSet
414 {
415 [self subclassResponsibility: _cmd];
416 }
417
418 - (void) removeCharactersInRange: (NSRange)aRange
419 {
420 [self subclassResponsibility: _cmd];
421 }
422
423 - (void) removeCharactersInString: (NSString*)aString
424 {
425 [self subclassResponsibility: _cmd];
426 }
427
428 - (void) invert
429 {
430 [self subclassResponsibility: _cmd];
431 }
432
433 // NSCopying, NSMutableCopying
434 - (id) copyWithZone: (NSZone*)zone
435 {
436 NSData *bitmap;
437 bitmap = [self bitmapRepresentation];
438 return [[NSBitmapCharSet allocWithZone: zone] initWithBitmap: bitmap];
439 }
440
441 - (id) mutableCopyWithZone: (NSZone*)zone
442 {
443 return [super mutableCopyWithZone: zone];
444 }
445
446 @end

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26