/[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.42 - (show annotations) (download)
Tue Sep 30 17:47:35 2003 UTC (20 years, 7 months ago) by CaS
Branch: MAIN
CVS Tags: alex_latest_semistable
Changes since 1.41: +27 -3 lines
Minor bugfix patch

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

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