Permalink
Browse files

ARC support

  • Loading branch information...
1 parent 821d855 commit 5a002e79cb0b36a34e08b94278858597b35f428c @psychs committed Aug 7, 2012
@@ -16,10 +16,20 @@
@interface OnigResult (Private)
- (id)initWithRegexp:(OnigRegexp*)expression region:(OnigRegion*)region target:(NSString*)target;
+- (NSMutableArray*)captureNameArray;
+@end
-- (NSMutableArray*) captureNameArray;
-@end
+static int captureNameCallback(const OnigUChar* name, const OnigUChar* end, int ngroups, int* group_list, OnigRegex re, void* arg) {
+ OnigResult *result = nil;
+#if __has_feature(objc_arc)
+ result = (__bridge OnigResult *)arg;
+#else
+ result = (OnigResult *)arg;
+#endif
+ [[result captureNameArray] addObject:[NSString stringWithCharacters:(unichar*)name length:((end-name)/CHAR_SIZE)]];
+ return 0;
+}
@implementation OnigRegexp
@@ -37,8 +47,10 @@ - (id)initWithEntity:(regex_t*)entity expression:(NSString*)expression
- (void)dealloc
{
if (_entity) onig_free(_entity);
+#if !__has_feature(objc_arc)
[_expression release];
[super dealloc];
+#endif
}
- (void)finalize
@@ -126,7 +138,11 @@ + (OnigRegexp*)compile:(NSString*)expression options:(OnigOption)theOptions erro
}
if (status == ONIG_NORMAL) {
- return [[[self alloc] initWithEntity:entity expression:expression] autorelease];
+ OnigRegexp* regexp = [[self alloc] initWithEntity:entity expression:expression];
+#if !__has_feature(objc_arc)
+ [regexp autorelease];
+#endif
+ return regexp;
}
else {
if(error != NULL) {
@@ -171,7 +187,11 @@ - (OnigResult*)search:(NSString*)target start:(int)start end:(int)end
ONIG_OPTION_NONE);
if (status != ONIG_MISMATCH) {
- return [[[OnigResult alloc] initWithRegexp:self region:region target:target] autorelease];
+ OnigResult* result = [[OnigResult alloc] initWithRegexp:self region:region target:target];
+#if !__has_feature(objc_arc)
+ [result autorelease];
+#endif
+ return result;
}
else {
onig_region_free(region, 1);
@@ -204,7 +224,11 @@ - (OnigResult*)match:(NSString*)target start:(int)start
ONIG_OPTION_NONE);
if (status != ONIG_MISMATCH) {
- return [[[OnigResult alloc] initWithRegexp:self region:region target:target] autorelease];
+ OnigResult* result = [[OnigResult alloc] initWithRegexp:self region:region target:target];
+#if !__has_feature(objc_arc)
+ [result autorelease];
+#endif
+ return result;
}
else {
onig_region_free(region, 1);
@@ -231,26 +255,30 @@ - (id)initWithRegexp:(OnigRegexp*)expression region:(OnigRegion*)region target:(
{
self = [super init];
if (self) {
- _expression = [expression retain];
+ _expression = expression;
+#if !__has_feature(objc_arc)
+ [_expression retain];
+#endif
_region = region;
_target = [target copy];
- _captureNames = [NSMutableArray array];
+ _captureNames = [NSMutableArray new];
}
return self;
}
- (void)dealloc
{
- [_expression release];
if (_region) onig_region_free(_region, 1);
+#if !__has_feature(objc_arc)
+ [_expression release];
[_target release];
[super dealloc];
+#endif
}
-- (void)finalize
+- (OnigRegexp*)_expression
{
- if (_region) onig_region_free(_region, 1);
- [super finalize];
+ return _expression;
}
- (NSString*)target
@@ -322,17 +350,15 @@ - (NSMutableArray*) captureNameArray {
return self->_captureNames;
}
-// Used to get list of names
-int co_name_callback(const OnigUChar* name, const OnigUChar* end, int ngroups, int* group_list, OnigRegex re, void* arg) {
- OnigResult *result = (OnigResult *)arg;
-
- [[result captureNameArray] addObject:[NSString stringWithCharacters:(unichar*)name length:((end-name)/CHAR_SIZE)]];
- return 0;
-}
-
- (NSArray*)captureNames
{
- onig_foreach_name([self->_expression entity], co_name_callback, self);
+ void* voidSelf = NULL;
+#if __has_feature(objc_arc)
+ voidSelf = (__bridge void*)self;
+#else
+ voidSelf = (void*)self;
+#endif
+ onig_foreach_name([[self _expression] entity], captureNameCallback, voidSelf);
return [NSArray arrayWithArray:self->_captureNames];
}
@@ -27,13 +27,8 @@
- (NSString*)replaceByRegexp:(id)pattern with:(NSString*)string;
- (NSString*)replaceAllByRegexp:(id)pattern with:(NSString*)string;
-- (NSString*)replaceByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel;
-- (NSString*)replaceAllByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel;
-
-#if defined(NS_BLOCKS_AVAILABLE)
- (NSString*)replaceByRegexp:(id)pattern withBlock:(NSString* (^)(OnigResult*))block;
- (NSString*)replaceAllByRegexp:(id)pattern withBlock:(NSString* (^)(OnigResult*))block;
-#endif
@end
@@ -47,11 +42,7 @@
- (NSMutableString*)replaceByRegexp:(id)pattern with:(NSString*)string;
- (NSMutableString*)replaceAllByRegexp:(id)pattern with:(NSString*)string;
-- (NSMutableString*)replaceByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel;
-- (NSMutableString*)replaceAllByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel;
-
-#if defined(NS_BLOCKS_AVAILABLE)
- (NSMutableString*)replaceByRegexp:(id)pattern withBlock:(NSString* (^)(OnigResult*))block;
- (NSMutableString*)replaceAllByRegexp:(id)pattern withBlock:(NSString* (^)(OnigResult*))block;
-#endif
+
@end
@@ -3,31 +3,32 @@
#import "OnigRegexpUtility.h"
-typedef NSString* (*ReplaceCallback)(OnigResult*, void*, SEL);
+typedef NSString* (*OnigReplaceCallback)(OnigResult*, void*, SEL);
-NSString* stringReplaceCallback(OnigResult* res, void* str, SEL sel)
+static NSString* stringReplaceCallback(OnigResult* res, void* str, SEL sel)
{
+#if __has_feature(objc_arc)
+ return (__bridge NSString*)str;
+#else
return (NSString*)str;
-}
-
-NSString* selectorReplaceCallback(OnigResult* res, void* str, SEL sel)
-{
- id object = str;
- return [object performSelector:sel withObject:res];
-}
+#endif
+}
-#if defined(NS_BLOCKS_AVAILABLE)
-NSString* blockReplaceCallback(OnigResult* res, void* str, SEL sel)
+static NSString* blockReplaceCallback(OnigResult* res, void* str, SEL sel)
{
- NSString* (^block)(OnigResult*) = (NSString* (^)(OnigResult*))str;
+ NSString* (^block)(OnigResult*) = nil;
+#if __has_feature(objc_arc)
+ block = (__bridge NSString* (^)(OnigResult*))str;
+#else
+ block = (NSString* (^)(OnigResult*))str;
+#endif
return block(res);
}
-#endif
@interface NSString (OnigRegexpNSStringUtilityPrivate)
-- (NSArray*)__split:(id)pattern limit:(NSNumber*)limit;
-- (NSString*)__replaceByRegexp:(id)pattern withCallback:(ReplaceCallback)cp data:(void*)data selector:(SEL)sel;
-- (NSString*)__replaceAllByRegexp:(id)pattern withCallback:(ReplaceCallback)cp data:(void*)data selector:(SEL)sel;
+- (NSArray*)split:(id)pattern limit:(NSNumber*)limit;
+- (NSString*)replaceByRegexp:(id)pattern withCallback:(OnigReplaceCallback)cp data:(void*)data selector:(SEL)sel;
+- (NSString*)replaceAllByRegexp:(id)pattern withCallback:(OnigReplaceCallback)cp data:(void*)data selector:(SEL)sel;
@end
@@ -50,19 +51,19 @@ - (NSArray*)split
- (NSArray*)splitByRegexp:(id)pattern
{
- return [self __split:pattern limit:nil];
+ return [self split:pattern limit:nil];
}
- (NSArray*)splitByRegexp:(id)pattern limit:(int)limit
{
- return [self __split:pattern limit:[NSNumber numberWithInt:limit]];
+ return [self split:pattern limit:[NSNumber numberWithInt:limit]];
}
//
// This implementation is based on ruby 1.8.
//
-- (NSArray*)__split:(id)pattern limit:(NSNumber*)limitNum
+- (NSArray*)split:(id)pattern limit:(NSNumber*)limitNum
{
NSString* target = self;
@@ -97,7 +98,10 @@ - (NSArray*)__split:(id)pattern limit:(NSNumber*)limitNum
}
else if (limit == 1) {
if ([target length] == 0) return [NSArray array];
- return [NSArray arrayWithObjects:[[target copy] autorelease], nil];
+#if !__has_feature(objc_arc)
+ target = [[target copy] autorelease];
+#endif
+ return [NSArray arrayWithObject:target];
}
i = 1;
}
@@ -156,53 +160,57 @@ - (NSArray*)__split:(id)pattern limit:(NSNumber*)limitNum
return array;
}
-- (NSString*)__replaceByRegexp:(id)pattern withCallback:(ReplaceCallback)cp data:(void*)data selector:(SEL)sel
+- (NSString*)replaceByRegexp:(id)pattern withCallback:(OnigReplaceCallback)callback data:(void*)data selector:(SEL)sel
{
if (![pattern isKindOfClass:[OnigRegexp class]]) {
pattern = [OnigRegexp compile:(NSString*)pattern];
}
OnigResult* res = [pattern search:self];
if (res) {
- NSMutableString* s = [[self mutableCopy] autorelease];
- [s replaceCharactersInRange:[res bodyRange] withString:cp(res, data, sel)];
+ NSMutableString* s = [self mutableCopy];
+#if !__has_feature(objc_arc)
+ [s autorelease];
+#endif
+ [s replaceCharactersInRange:[res bodyRange] withString:callback(res, data, sel)];
return s;
}
else {
- return [[self mutableCopy] autorelease];
+ NSString* s = [self copy];
+#if !__has_feature(objc_arc)
+ [s autorelease];
+#endif
+ return s;
}
}
- (NSString*)replaceByRegexp:(id)pattern with:(NSString*)string
{
- return [self __replaceByRegexp:pattern withCallback:stringReplaceCallback data:string selector:Nil];
-}
-
-- (NSString*)replaceByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel
-{
- return [self __replaceByRegexp:pattern withCallback:selectorReplaceCallback data:object selector:sel];
+ return [self replaceByRegexp:pattern withCallback:stringReplaceCallback data:(void*)string selector:Nil];
}
-#if defined(NS_BLOCKS_AVAILABLE)
- (NSString*)replaceByRegexp:(id)pattern withBlock:(NSString* (^)(OnigResult*))block
{
- return [self __replaceByRegexp:pattern withCallback:blockReplaceCallback data:block selector:Nil];
+ return [self replaceByRegexp:pattern withCallback:blockReplaceCallback data:(void*)block selector:Nil];
}
-#endif
//
// This implementation is based on ruby 1.8.
//
-- (NSString*)__replaceAllByRegexp:(id)pattern withCallback:(ReplaceCallback)cp data:(void*)data selector:(SEL)sel
+- (NSString*)__replaceAllByRegexp:(id)pattern withCallback:(OnigReplaceCallback)callback data:(void*)data selector:(SEL)sel
{
if (![pattern isKindOfClass:[OnigRegexp class]]) {
pattern = [OnigRegexp compile:(NSString*)pattern];
}
OnigResult* res = [pattern search:self];
if (!res) {
- return [[self mutableCopy] autorelease];
+ NSString* s = [self copy];
+#if !__has_feature(objc_arc)
+ [s autorelease];
+#endif
+ return s;
}
NSMutableString* s = [NSMutableString string];
@@ -212,7 +220,7 @@ - (NSString*)__replaceAllByRegexp:(id)pattern withCallback:(ReplaceCallback)cp d
NSRange range = [res bodyRange];
int len = range.location-offset;
if (len > 0) [s appendString:[self substringWithRange:NSMakeRange(offset, len)]];
- [s appendString:cp(res, data, sel)];
+ [s appendString:callback(res, data, sel)];
offset = NSMaxRange(range);
if (range.length == 0) {
@@ -234,20 +242,13 @@ - (NSString*)__replaceAllByRegexp:(id)pattern withCallback:(ReplaceCallback)cp d
- (NSString*)replaceAllByRegexp:(id)pattern with:(NSString*)string
{
- return [self __replaceAllByRegexp:pattern withCallback:stringReplaceCallback data:string selector:Nil];
+ return [self replaceAllByRegexp:pattern withCallback:stringReplaceCallback data:(void*)string selector:Nil];
}
-- (NSString*)replaceAllByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel
-{
- return [self __replaceAllByRegexp:pattern withCallback:selectorReplaceCallback data:object selector:sel];
-}
-
-#if defined(NS_BLOCKS_AVAILABLE)
- (NSString*)replaceAllByRegexp:(id)pattern withBlock:(NSString* (^)(OnigResult*))block
{
- return [self __replaceAllByRegexp:pattern withCallback:blockReplaceCallback data:block selector:Nil];
+ return [self replaceAllByRegexp:pattern withCallback:blockReplaceCallback data:(void*)block selector:Nil];
}
-#endif
@end
@@ -264,17 +265,6 @@ - (NSMutableString*)replaceAllByRegexp:(id)pattern with:(NSString*)string
return (NSMutableString*)[super replaceAllByRegexp:pattern with:string];
}
-- (NSMutableString*)replaceByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel
-{
- return (NSMutableString*)[super replaceByRegexp:pattern withCallback:object selector:sel];
-}
-
-- (NSMutableString*)replaceAllByRegexp:(id)pattern withCallback:(id)object selector:(SEL)sel
-{
- return (NSMutableString*)[super replaceAllByRegexp:pattern withCallback:object selector:sel];
-}
-
-#if defined(NS_BLOCKS_AVAILABLE)
- (NSMutableString*)replaceByRegexp:(id)pattern withBlock:(NSString* (^)(OnigResult*))block
{
return (NSMutableString*)[super replaceByRegexp:pattern withBlock:block];
@@ -284,6 +274,5 @@ - (NSMutableString*)replaceAllByRegexp:(id)pattern withBlock:(NSString* (^)(Onig
{
return (NSMutableString*)[super replaceAllByRegexp:pattern withBlock:block];
}
-#endif
@end
Oops, something went wrong.

0 comments on commit 5a002e7

Please sign in to comment.