Re: feed an NSCharacterSet to numberOfRowsInSection delegate method?
Re: feed an NSCharacterSet to numberOfRowsInSection delegate method?
- Subject: Re: feed an NSCharacterSet to numberOfRowsInSection delegate method?
- From: glenn andreas <email@hidden>
- Date: Wed, 07 Jan 2009 09:16:11 -0600
On Jan 7, 2009, at 8:47 AM, Martijn van Exel wrote:
On Wed, Jan 7, 2009 at 15:19, Graham Cox <email@hidden>
wrote:
On 8 Jan 2009, at 12:26 am, Martijn van Exel wrote:
But [NSCharacterSet uppercaseLetterCharacterSet] returns a
NSCharacterSet
and not an NSArray, is there a way (without doing an iteration) to
turn a
NSCharacterSet into an NSArray of charachters?
There's no way even with doing an iteration. NSCharacterSet cannot be
enumerated and doesn't return a count.
If you know you just want A-Z why not just create a simple array
with those
letters in it?
--Graham
Yes, I could, but it just seemed like a nice shorthand solution to
be able
to use on of the available NSCharacterSet constants.
NSCharacterSet is conceptually implemented as an array of bits
indicating what characters are members and what aren't - you can get
this bitmap via the -bitMapRepresentation method:
A raw bitmap representation of a character set is a byte array of
2^16 bits (that is, 8192 bytes). The value of the bit at position n
represents the presence in the character set of the character with
decimal Unicode value n. To test for the presence of a character
with decimal Unicode value n in a raw bitmap representation, use an
expression such as the following:
unsigned char bitmapRep[8192];
if (bitmapRep[n >> 3] & (((unsigned int)1) << (n & 7))) {
/* Character is present. */
}
So you can see why there is no way short of iterating the bits to turn
the thing into an array of characters, which is fairly straightforward
(typed in mail - you'll probably want to add error checking, etc...):
@implementation NSCharacterSet(SetAsArray)
- (NSArray *) characterSetAsArrayOfCharacters;
{
NSMutableArray *retval = [NSMutableArray array];
const unsigned char *bitmapRep = (const unsigned char *)[[self
bitMapRepresentation] bytes];
for (int n=0;n<8192;n++) {
if (bitmapRep[n >> 3] & (((unsigned int)1) << (n & 7))) {
[retval addObject:[NSString stringWithFormat:@"%C",n]];
}
return retval;
}
This, of course, would be somewhat expensive (in terms of a whole lot
of "stringWithFormat" calls). It's probably better to use an
NSIndexSet (which is more compact, and yet allows for enumeration):
- (NSIndexSet *) characterSetAsIndexSet
{
NSMutableIndexSet *retval = [NSMutableIndexSet set];
const unsigned char *bitmapRep = (const unsigned char *)[[self
bitMapRepresentation] bytes];
for (int n=0;n<8192;n++) {
if (bitmapRep[n >> 3] & (((unsigned int)1) << (n & 7))) {
[retval addIndex: n];
}
return retval;
}
And note that if you want A-Z, you shouldn't be using
uppercaseLetterCharacterSet - that's documented as "containing the
characters in the categories of Uppercase Letters and Titlecase
Letters" which is far more than A-Z, since it includes a wide range of
characters from various languages/scripts (and note that "Titlecase"
is separate from "Uppercase" - 31 title case letters can be found <http://www.fileformat.info/info/unicode/category/Lt/list.htm
>, vs the 1241 "Uppercase" letters <http://www.fileformat.info/info/unicode/category/Lu/list.htm
>). So do you really want a table with 1272 rows, including many
things that may not even have glyphs in users currently installed
fonts (which will result in there being a whole lot of rows that are
just the "missing glyph box")?
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium2 | build, mutate, evolve, animate | images, textures,
fractals, art
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden