Re: adding charactersets
Re: adding charactersets
- Subject: Re: adding charactersets
- From: Christian Brunschen <email@hidden>
- Date: Sat, 1 Mar 2003 19:12:28 +0000
On Saturday, Mar 1, 2003, at 18:36 Europe/London, Koen van der Drift
wrote:
Hi,
Is there a way to "add" character sets? For instance, I want to test
if a
character is a letter of the alphabet or a space, tab, or newline. Or
should I just test against lowercaseLetterCharacterset,
uppercaseLetterCharacterset and whitespaceAndNewlineCharatcerSet:
if ([lowercaseSet characterIsMember:aChar] || [uppercaseSet
characterIsMember:aChar] || [whitespaceSet characterIsMember:aChar])
{
do someting...
You might want to investigate NSMutableCharacterSet, which allows you
to create unions and intersections between other NSCharacterSets using
the method
- (void)formIntersectionWithCharacterSet:(NSCharacterSet *)otherSet;
Something like the following code might work (this is entirely
untested, just typed into mail)
NSCharacterSet *characterSetFromUnionOfSets(NSArray *sets) {
NSCharacterSet *combinedSet;
NSEnuemerator *e = [sets objectEnumerator];
NSCharacterSet *set = [e nextObject];
NSMutableCharacterSet *tmp = [set mutableCopy];
while ((set = [e nextObject]) != nil) {
[tmp formUnionWithCharacterSet:set];
}
combinedSet = [tmp copy];
[tmp release];
return combinedSet;
}
/* You could even add a Category on NSCharacterSet to give you this
functionality */
@interface NSCharacterSet (CharacterSetUnion)
+ (NSCharacterSet *)characterSetFromUnionOfSets:(NSSrray *)sets;
- (NSCharacterSet *)characterSetFromUnionWithSet:(NSCharacterSet *)set;
@end
@implementation NSCharacterSet (CharacterSetUnion)
+ (NSCharacterSet *)characterSetFromUnionOfSets:(NSSrray *)sets {
return characterSetsFromUnionOfSets(sets);
}
- (NSCharacterSet *)characterSetFromUnionWithSet:(NSCharacterSet *)set {
return [self characterSetFromUnionOfSets:[NSArray
arrayWithObjects:self, set, nil]];
}
@end
/* in your own code */
NSCharacterSet *combinedSet = [NSCharacterSet
characterSetFromUnionOfSets:[NSArray arrayWithObjects:
lowercaseSet, uppercaseSet, whitespaceSet, nil]];
/* or */
NSCharacterSet *combinedSet = [[lowercaseSet
characterSetFromUnionWithSet:uppercaseSet]
characterSetFromUnionWithSet:whitespaceSet];
if ([combinedSet characterIsMember:aChar]) {
/* do something ... */
}
should work :)
thanks,
Hope this helps,
- Koen.
// Christian
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.