Wired Foundation  2.0
A foundation framework for the Wired implementation on Mac OS X
RegexKitLite.h
00001 //
00002 //  RegexKitLite.h
00003 //  http://regexkit.sourceforge.net/
00004 //  Licensed under the terms of the BSD License, as specified below.
00005 //
00006 
00007 /*
00008  Copyright (c) 2008-2010, John Engelhart
00009  
00010  All rights reserved.
00011  
00012  Redistribution and use in source and binary forms, with or without
00013  modification, are permitted provided that the following conditions are met:
00014  
00015  * Redistributions of source code must retain the above copyright
00016  notice, this list of conditions and the following disclaimer.
00017  
00018  * Redistributions in binary form must reproduce the above copyright
00019  notice, this list of conditions and the following disclaimer in the
00020  documentation and/or other materials provided with the distribution.
00021  
00022  * Neither the name of the Zang Industries nor the names of its
00023  contributors may be used to endorse or promote products derived from
00024  this software without specific prior written permission.
00025  
00026  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00032  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 */
00038 
00039 #ifdef    __OBJC__
00040 #import <Foundation/NSArray.h>
00041 #import <Foundation/NSError.h>
00042 #import <Foundation/NSObjCRuntime.h>
00043 #import <Foundation/NSRange.h>
00044 #import <Foundation/NSString.h>
00045 #endif // __OBJC__
00046 
00047 #include <limits.h>
00048 #include <stdint.h>
00049 #include <sys/types.h>
00050 #include <TargetConditionals.h>
00051 #include <AvailabilityMacros.h>
00052 
00053 #ifdef __cplusplus
00054 extern "C" {
00055 #endif
00056   
00057 #ifndef   REGEXKITLITE_VERSION_DEFINED
00058 #define   REGEXKITLITE_VERSION_DEFINED
00059 
00060 #define _RKL__STRINGIFY(b)       #b
00061 #define _RKL_STRINGIFY(a)        _RKL__STRINGIFY(a)
00062 #define _RKL_JOIN_VERSION(a,b)   _RKL_STRINGIFY(a##.##b)
00063 #define _RKL_VERSION_STRING(a,b) _RKL_JOIN_VERSION(a,b)
00064 
00065 #define REGEXKITLITE_VERSION_MAJOR 4
00066 #define REGEXKITLITE_VERSION_MINOR 0
00067 
00068 #define REGEXKITLITE_VERSION_CSTRING   _RKL_VERSION_STRING(REGEXKITLITE_VERSION_MAJOR, REGEXKITLITE_VERSION_MINOR)
00069 #define REGEXKITLITE_VERSION_NSSTRING  @REGEXKITLITE_VERSION_CSTRING
00070 
00071 #endif // REGEXKITLITE_VERSION_DEFINED
00072 
00073 #if !defined(RKL_BLOCKS) && defined(NS_BLOCKS_AVAILABLE) && (NS_BLOCKS_AVAILABLE == 1)
00074 #define RKL_BLOCKS 1
00075 #endif
00076   
00077 #if       defined(RKL_BLOCKS) && (RKL_BLOCKS == 1)
00078 #define _RKL_BLOCKS_ENABLED 1
00079 #endif // defined(RKL_BLOCKS) && (RKL_BLOCKS == 1)
00080 
00081 #if       defined(_RKL_BLOCKS_ENABLED) && !defined(__BLOCKS__)
00082 #warning RegexKitLite support for Blocks is enabled, but __BLOCKS__ is not defined.  This compiler may not support Blocks, in which case the behavior is undefined.  This will probably cause numerous compiler errors.
00083 #endif // defined(_RKL_BLOCKS_ENABLED) && !defined(__BLOCKS__)
00084 
00085 // For Mac OS X < 10.5.
00086 #ifndef   NSINTEGER_DEFINED
00087 #define   NSINTEGER_DEFINED
00088 #if       defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
00089 typedef long           NSInteger;
00090 typedef unsigned long  NSUInteger;
00091 #define NSIntegerMin   LONG_MIN
00092 #define NSIntegerMax   LONG_MAX
00093 #define NSUIntegerMax  ULONG_MAX
00094 #else  // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
00095 typedef int            NSInteger;
00096 typedef unsigned int   NSUInteger;
00097 #define NSIntegerMin   INT_MIN
00098 #define NSIntegerMax   INT_MAX
00099 #define NSUIntegerMax  UINT_MAX
00100 #endif // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
00101 #endif // NSINTEGER_DEFINED
00102 
00103 #ifndef   RKLREGEXOPTIONS_DEFINED
00104 #define   RKLREGEXOPTIONS_DEFINED
00105 
00106 // These must be identical to their ICU regex counterparts. See http://www.icu-project.org/userguide/regexp.html
00107 enum {
00108   RKLNoOptions             = 0,
00109   RKLCaseless              = 2,
00110   RKLComments              = 4,
00111   RKLDotAll                = 32,
00112   RKLMultiline             = 8,
00113   RKLUnicodeWordBoundaries = 256
00114 };
00115 typedef uint32_t RKLRegexOptions; // This must be identical to the ICU 'flags' argument type.
00116 
00117 #endif // RKLREGEXOPTIONS_DEFINED
00118 
00119 #ifndef   RKLREGEXENUMERATIONOPTIONS_DEFINED
00120 #define   RKLREGEXENUMERATIONOPTIONS_DEFINED
00121 
00122 enum {
00123   RKLRegexEnumerationNoOptions                               = 0UL,
00124   RKLRegexEnumerationCapturedStringsNotRequired              = 1UL << 9,
00125   RKLRegexEnumerationReleaseStringReturnedByReplacementBlock = 1UL << 10,
00126   RKLRegexEnumerationFastCapturedStringsXXX                  = 1UL << 11,
00127 };
00128 typedef NSUInteger RKLRegexEnumerationOptions;
00129   
00130 #endif // RKLREGEXENUMERATIONOPTIONS_DEFINED
00131 
00132 #ifndef _REGEXKITLITE_H_
00133 #define _REGEXKITLITE_H_
00134 
00135 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__APPLE_CC__) && (__APPLE_CC__ >= 5465)
00136 #define RKL_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
00137 #else
00138 #define RKL_DEPRECATED_ATTRIBUTE
00139 #endif
00140 
00141 #if       defined(NS_REQUIRES_NIL_TERMINATION)
00142 #define RKL_REQUIRES_NIL_TERMINATION NS_REQUIRES_NIL_TERMINATION
00143 #else  // defined(NS_REQUIRES_NIL_TERMINATION)
00144 #define RKL_REQUIRES_NIL_TERMINATION
00145 #endif // defined(NS_REQUIRES_NIL_TERMINATION)
00146   
00147 // This requires a few levels of rewriting to get the desired results.
00148 #define _RKL_CONCAT_2(c,d) c ## d
00149 #define _RKL_CONCAT(a,b) _RKL_CONCAT_2(a,b)
00150   
00151 #ifdef    RKL_PREPEND_TO_METHODS
00152 #define RKL_METHOD_PREPEND(x) _RKL_CONCAT(RKL_PREPEND_TO_METHODS, x)
00153 #else  // RKL_PREPEND_TO_METHODS
00154 #define RKL_METHOD_PREPEND(x) x
00155 #endif // RKL_PREPEND_TO_METHODS
00156   
00157 // If it looks like low memory notifications might be available, add code to register and respond to them.
00158 // This is (should be) harmless if it turns out that this isn't the case, since the notification that we register for,
00159 // UIApplicationDidReceiveMemoryWarningNotification, is dynamically looked up via dlsym().
00160 #if ((defined(TARGET_OS_EMBEDDED) && (TARGET_OS_EMBEDDED != 0)) || (defined(TARGET_OS_IPHONE) && (TARGET_OS_IPHONE != 0))) && (!defined(RKL_REGISTER_FOR_IPHONE_LOWMEM_NOTIFICATIONS) || (RKL_REGISTER_FOR_IPHONE_LOWMEM_NOTIFICATIONS != 0))
00161 #define RKL_REGISTER_FOR_IPHONE_LOWMEM_NOTIFICATIONS 1
00162 #endif
00163 
00164 #ifdef __OBJC__
00165 
00166 // NSException exception name.
00167 extern NSString * const RKLICURegexException;
00168 
00169 // NSError error domains and user info keys.
00170 extern NSString * const RKLICURegexErrorDomain;
00171 
00172 extern NSString * const RKLICURegexEnumerationOptionsErrorKey;
00173 extern NSString * const RKLICURegexErrorCodeErrorKey;
00174 extern NSString * const RKLICURegexErrorNameErrorKey;
00175 extern NSString * const RKLICURegexLineErrorKey;
00176 extern NSString * const RKLICURegexOffsetErrorKey;
00177 extern NSString * const RKLICURegexPreContextErrorKey;
00178 extern NSString * const RKLICURegexPostContextErrorKey;
00179 extern NSString * const RKLICURegexRegexErrorKey;
00180 extern NSString * const RKLICURegexRegexOptionsErrorKey;
00181 extern NSString * const RKLICURegexReplacedCountErrorKey;
00182 extern NSString * const RKLICURegexReplacedStringErrorKey;
00183 extern NSString * const RKLICURegexReplacementStringErrorKey;
00184 extern NSString * const RKLICURegexSubjectRangeErrorKey;
00185 extern NSString * const RKLICURegexSubjectStringErrorKey;
00186   
00187 @interface NSString (RegexKitLiteAdditions)
00188 
00189 + (void)RKL_METHOD_PREPEND(clearStringCache);
00190 
00191 // Although these are marked as deprecated, a bug in GCC prevents a warning from being issues for + class methods.  Filed bug with Apple, #6736857.
00192 + (NSInteger)RKL_METHOD_PREPEND(captureCountForRegex):(NSString *)regex RKL_DEPRECATED_ATTRIBUTE;
00193 + (NSInteger)RKL_METHOD_PREPEND(captureCountForRegex):(NSString *)regex options:(RKLRegexOptions)options error:(NSError **)error RKL_DEPRECATED_ATTRIBUTE;
00194 
00195 - (NSArray *)RKL_METHOD_PREPEND(componentsSeparatedByRegex):(NSString *)regex;
00196 - (NSArray *)RKL_METHOD_PREPEND(componentsSeparatedByRegex):(NSString *)regex range:(NSRange)range;
00197 - (NSArray *)RKL_METHOD_PREPEND(componentsSeparatedByRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error;
00198 
00199 - (BOOL)RKL_METHOD_PREPEND(isMatchedByRegex):(NSString *)regex;
00200 - (BOOL)RKL_METHOD_PREPEND(isMatchedByRegex):(NSString *)regex inRange:(NSRange)range;
00201 - (BOOL)RKL_METHOD_PREPEND(isMatchedByRegex):(NSString *)regex options:(RKLRegexOptions)options inRange:(NSRange)range error:(NSError **)error;
00202 
00203 - (NSRange)RKL_METHOD_PREPEND(rangeOfRegex):(NSString *)regex;
00204 - (NSRange)RKL_METHOD_PREPEND(rangeOfRegex):(NSString *)regex capture:(NSInteger)capture;
00205 - (NSRange)RKL_METHOD_PREPEND(rangeOfRegex):(NSString *)regex inRange:(NSRange)range;
00206 - (NSRange)RKL_METHOD_PREPEND(rangeOfRegex):(NSString *)regex options:(RKLRegexOptions)options inRange:(NSRange)range capture:(NSInteger)capture error:(NSError **)error;
00207 
00208 - (NSString *)RKL_METHOD_PREPEND(stringByMatching):(NSString *)regex;
00209 - (NSString *)RKL_METHOD_PREPEND(stringByMatching):(NSString *)regex capture:(NSInteger)capture;
00210 - (NSString *)RKL_METHOD_PREPEND(stringByMatching):(NSString *)regex inRange:(NSRange)range;
00211 - (NSString *)RKL_METHOD_PREPEND(stringByMatching):(NSString *)regex options:(RKLRegexOptions)options inRange:(NSRange)range capture:(NSInteger)capture error:(NSError **)error;
00212 
00213 - (NSString *)RKL_METHOD_PREPEND(stringByReplacingOccurrencesOfRegex):(NSString *)regex withString:(NSString *)replacement;
00214 - (NSString *)RKL_METHOD_PREPEND(stringByReplacingOccurrencesOfRegex):(NSString *)regex withString:(NSString *)replacement range:(NSRange)searchRange;
00215 - (NSString *)RKL_METHOD_PREPEND(stringByReplacingOccurrencesOfRegex):(NSString *)regex withString:(NSString *)replacement options:(RKLRegexOptions)options range:(NSRange)searchRange error:(NSError **)error;
00216 
00218 
00219 - (NSInteger)RKL_METHOD_PREPEND(captureCount);
00220 - (NSInteger)RKL_METHOD_PREPEND(captureCountWithOptions):(RKLRegexOptions)options error:(NSError **)error;
00221 
00222 - (BOOL)RKL_METHOD_PREPEND(isRegexValid);
00223 - (BOOL)RKL_METHOD_PREPEND(isRegexValidWithOptions):(RKLRegexOptions)options error:(NSError **)error;
00224 
00225 - (void)RKL_METHOD_PREPEND(flushCachedRegexData);
00226 
00227 - (NSArray *)RKL_METHOD_PREPEND(componentsMatchedByRegex):(NSString *)regex;
00228 - (NSArray *)RKL_METHOD_PREPEND(componentsMatchedByRegex):(NSString *)regex capture:(NSInteger)capture;
00229 - (NSArray *)RKL_METHOD_PREPEND(componentsMatchedByRegex):(NSString *)regex range:(NSRange)range;
00230 - (NSArray *)RKL_METHOD_PREPEND(componentsMatchedByRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range capture:(NSInteger)capture error:(NSError **)error;
00231 
00232 
00233 - (NSArray *)RKL_METHOD_PREPEND(captureComponentsMatchedByRegex):(NSString *)regex;
00234 - (NSArray *)RKL_METHOD_PREPEND(captureComponentsMatchedByRegex):(NSString *)regex range:(NSRange)range;
00235 - (NSArray *)RKL_METHOD_PREPEND(captureComponentsMatchedByRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error;
00236 
00237 - (NSArray *)RKL_METHOD_PREPEND(arrayOfCaptureComponentsMatchedByRegex):(NSString *)regex;
00238 - (NSArray *)RKL_METHOD_PREPEND(arrayOfCaptureComponentsMatchedByRegex):(NSString *)regex range:(NSRange)range;
00239 - (NSArray *)RKL_METHOD_PREPEND(arrayOfCaptureComponentsMatchedByRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error;
00240 
00242 
00243 - (NSArray *)RKL_METHOD_PREPEND(arrayOfDictionariesByMatchingRegex):(NSString *)regex withKeysAndCaptures:(id)firstKey, ... RKL_REQUIRES_NIL_TERMINATION;
00244 - (NSArray *)RKL_METHOD_PREPEND(arrayOfDictionariesByMatchingRegex):(NSString *)regex range:(NSRange)range withKeysAndCaptures:(id)firstKey, ... RKL_REQUIRES_NIL_TERMINATION;
00245 - (NSArray *)RKL_METHOD_PREPEND(arrayOfDictionariesByMatchingRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error withKeysAndCaptures:(id)firstKey, ... RKL_REQUIRES_NIL_TERMINATION;
00246 - (NSArray *)RKL_METHOD_PREPEND(arrayOfDictionariesByMatchingRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error withFirstKey:(id)firstKey arguments:(va_list)varArgsList;
00247 
00248 - (NSArray *)RKL_METHOD_PREPEND(arrayOfDictionariesByMatchingRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error withKeys:(id *)keys forCaptures:(int *)captures count:(NSUInteger)count;
00249 
00250 - (NSDictionary *)RKL_METHOD_PREPEND(dictionaryByMatchingRegex):(NSString *)regex withKeysAndCaptures:(id)firstKey, ... RKL_REQUIRES_NIL_TERMINATION;
00251 - (NSDictionary *)RKL_METHOD_PREPEND(dictionaryByMatchingRegex):(NSString *)regex range:(NSRange)range withKeysAndCaptures:(id)firstKey, ... RKL_REQUIRES_NIL_TERMINATION;
00252 - (NSDictionary *)RKL_METHOD_PREPEND(dictionaryByMatchingRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error withKeysAndCaptures:(id)firstKey, ... RKL_REQUIRES_NIL_TERMINATION;
00253 - (NSDictionary *)RKL_METHOD_PREPEND(dictionaryByMatchingRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error withFirstKey:(id)firstKey arguments:(va_list)varArgsList;
00254 
00255 - (NSDictionary *)RKL_METHOD_PREPEND(dictionaryByMatchingRegex):(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error withKeys:(id *)keys forCaptures:(int *)captures count:(NSUInteger)count;
00256 
00257 #ifdef    _RKL_BLOCKS_ENABLED
00258 
00259 - (BOOL)RKL_METHOD_PREPEND(enumerateStringsMatchedByRegex):(NSString *)regex usingBlock:(void (^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00260 - (BOOL)RKL_METHOD_PREPEND(enumerateStringsMatchedByRegex):(NSString *)regex options:(RKLRegexOptions)options inRange:(NSRange)range error:(NSError **)error enumerationOptions:(RKLRegexEnumerationOptions)enumerationOptions usingBlock:(void (^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00261 
00262 - (BOOL)RKL_METHOD_PREPEND(enumerateStringsSeparatedByRegex):(NSString *)regex usingBlock:(void (^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00263 - (BOOL)RKL_METHOD_PREPEND(enumerateStringsSeparatedByRegex):(NSString *)regex options:(RKLRegexOptions)options inRange:(NSRange)range error:(NSError **)error enumerationOptions:(RKLRegexEnumerationOptions)enumerationOptions usingBlock:(void (^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00264 
00265 - (NSString *)RKL_METHOD_PREPEND(stringByReplacingOccurrencesOfRegex):(NSString *)regex usingBlock:(NSString *(^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00266 - (NSString *)RKL_METHOD_PREPEND(stringByReplacingOccurrencesOfRegex):(NSString *)regex options:(RKLRegexOptions)options inRange:(NSRange)range error:(NSError **)error enumerationOptions:(RKLRegexEnumerationOptions)enumerationOptions usingBlock:(NSString *(^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00267 
00268 #endif // _RKL_BLOCKS_ENABLED
00269 
00270 @end
00271 
00272 @interface NSMutableString (RegexKitLiteAdditions)
00273 
00274 - (NSInteger)RKL_METHOD_PREPEND(replaceOccurrencesOfRegex):(NSString *)regex withString:(NSString *)replacement;
00275 - (NSInteger)RKL_METHOD_PREPEND(replaceOccurrencesOfRegex):(NSString *)regex withString:(NSString *)replacement range:(NSRange)searchRange;
00276 - (NSInteger)RKL_METHOD_PREPEND(replaceOccurrencesOfRegex):(NSString *)regex withString:(NSString *)replacement options:(RKLRegexOptions)options range:(NSRange)searchRange error:(NSError **)error;
00277 
00279 
00280 #ifdef    _RKL_BLOCKS_ENABLED
00281 
00282 - (NSInteger)RKL_METHOD_PREPEND(replaceOccurrencesOfRegex):(NSString *)regex usingBlock:(NSString *(^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00283 - (NSInteger)RKL_METHOD_PREPEND(replaceOccurrencesOfRegex):(NSString *)regex options:(RKLRegexOptions)options inRange:(NSRange)range error:(NSError **)error enumerationOptions:(RKLRegexEnumerationOptions)enumerationOptions usingBlock:(NSString *(^)(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop))block;
00284 
00285 #endif // _RKL_BLOCKS_ENABLED
00286 
00287 @end
00288 
00289 #endif // __OBJC__
00290 
00291 #endif // _REGEXKITLITE_H_
00292 
00293 #ifdef __cplusplus
00294 }  // extern "C"
00295 #endif
 All Classes