Skip to content

Instantly share code, notes, and snippets.

@annidy
Last active January 30, 2023 03:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annidy/f58fac669f124aebead5 to your computer and use it in GitHub Desktop.
Save annidy/f58fac669f124aebead5 to your computer and use it in GitHub Desktop.
NSCharacterSet example
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self printCharacterSet:@selector(URLFragmentAllowedCharacterSet)];
[self printCharacterSet:@selector(URLPasswordAllowedCharacterSet)];
[self printCharacterSet:@selector(URLPathAllowedCharacterSet)];
[self printCharacterSet:@selector(URLQueryAllowedCharacterSet)];
[self printCharacterSet:@selector(URLUserAllowedCharacterSet)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)printCharacterSet:(SEL)character
{
NSCharacterSet *characterSet = [NSCharacterSet performSelector:character];
NSMutableArray *mutableCharacters = [NSMutableArray array];
for (NSUInteger plane = 0; plane <= 16; plane++) {
if ([characterSet hasMemberInPlane:plane]) {
for (UTF32Char character = (UTF32Char)(plane << 16);
character < (plane + 1) << 16; character++)
{
if ([characterSet longCharacterIsMember:character]) {
UTF32Char c = OSSwapHostToLittleInt32(character);
NSString *characterString = [[NSString alloc] initWithBytes:&c
length:sizeof(UTF32Char)
encoding:NSUTF32LittleEndianStringEncoding];
[mutableCharacters addObject:characterString];
}
}
}
}
printf("%s: ", [NSStringFromSelector(character) UTF8String]);
for(NSString *s in mutableCharacters) {
printf("%s", [s UTF8String]);
}
printf("\n");
// NSLog(@"%@: %@", NSStringFromSelector(character), [mutableCharacters componentsJoinedByString:@","]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment