Changeset 10
- Timestamp:
- 12/11/06 1:03:49 PM (2 years ago)
- Files:
-
- trunk/CocoaICU.xcodeproj/project.pbxproj (modified) (2 diffs)
- trunk/source/ICUMatcher.h (modified) (1 diff)
- trunk/source/ICUMatcher.m (modified) (6 diffs)
- trunk/source/ICUPattern.h (modified) (1 diff)
- trunk/source/ICUPattern.m (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/CocoaICU.xcodeproj/project.pbxproj
r7 r10 22 22 939322570B1E6AD000DD0AF0 /* ICUMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ICUMatcher.h; path = source/ICUMatcher.h; sourceTree = "<group>"; }; 23 23 939322580B1E6AD000DD0AF0 /* ICUMatcher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ICUMatcher.m; path = source/ICUMatcher.m; sourceTree = "<group>"; }; 24 93E2BFE60B18BEC4001193AC /* CocoaICU Unit Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = "CocoaICU Unit Tests-Info.plist"; path = "resources/CocoaICU Unit Tests-Info.plist"; sourceTree = SOURCE_ROOT; };25 24 93E2C0CE0B18D142001193AC /* CocoaICU.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CocoaICU.m; path = source/CocoaICU.m; sourceTree = "<group>"; }; 26 25 93E2C0CF0B18D142001193AC /* ICUPattern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ICUPattern.h; path = source/ICUPattern.h; sourceTree = "<group>"; }; … … 51 50 08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */, 52 51 1AB674ADFE9D54B511CA2CBB /* Products */, 53 93E2BFE60B18BEC4001193AC /* CocoaICU Unit Tests-Info.plist */,54 52 ); 55 53 name = CocoaICU; trunk/source/ICUMatcher.h
r6 r10 10 10 @class ICUPattern; 11 11 12 /*! 13 @class ICUMatcher 14 @abstract ICUMatcher provides matching functionality for regular expression matching. 15 @discussion This class is based off of the <a href="http://icu.sourceforge.net/apiref/icu4c/classRegexMatcher.html">C++ ICU RegexMatcher class</a>. For examples of how to use the matcher, see the NSString category included in this project. 16 */ 12 17 @interface ICUMatcher : NSObject { 13 18 ICUPattern *pattern; 14 19 } 15 20 21 /*! 22 @method matcherWithPattern:overString: 23 @abstract Returns a matcher that can match the given pattern over the given string. 24 @discussion 25 */ 16 26 +(ICUMatcher *)matcherWithPattern:(ICUPattern *)p overString:(NSString *)stringToSearchOver; 27 28 /*! 29 @method initWithPattern:overString: 30 @abstract Initializes a matcher that can match the given patter over the given string. 31 @discussion 32 */ 17 33 -(ICUMatcher *)initWithPattern:(ICUPattern *)p overString:(NSString *)stringToSearchOver; 18 34 35 /*! 36 @method findNext 37 @abstract Finds the next occurrence of the pattern in the input string. 38 @discussion Use <code>group</code> and <code>rangeOfMatch</code> to extract the match. 39 */ 19 40 -(BOOL)findNext; 41 42 /*! 43 @method findFromIndex: 44 @abstract Resets the pattern and performs a match from the specified index. 45 @discussion Use <code>group</code> and <code>rangeOfMatch</code> to extract the match. 46 */ 20 47 -(BOOL)findFromIndex:(unsigned)index; 48 49 /*! 50 @method group 51 @abstract Returns the current match. 52 @discussion Each match has one or more subexpressions associated with the match. This returns the entire match. 53 */ 21 54 -(NSString *)group; 55 56 /*! 57 @method groupAtIndex: 58 @abstract Returns the given subexpression for the current match. 59 @discussion <code>group</code> is equivalent to <code>groupAtIndex:0</code>. The subexpressions for a match are indexed from 1. 60 */ 22 61 -(NSString *)groupAtIndex:(unsigned)groupIndex; 62 63 /*! 64 @method numberOfGroups 65 @abstract Returns the number of groups for the current match. 66 @discussion Group 0 is the entire match and groups 1..n represent the groups for the subexpressions. 67 */ 23 68 -(unsigned)numberOfGroups; 69 70 /*! 71 @method lookingAt: 72 @abstract Returns true if the pattern matches some prefix of the input string starting at the specified index. 73 @discussion This method returns YES when some prefix of the substring matches the input string. 74 */ 24 75 -(BOOL)lookingAt:(unsigned)index; 76 77 /*! 78 @method pattern 79 @abstract Returns the pattern for this matcher. 80 @discussion 81 */ 25 82 -(ICUPattern *)pattern; 83 -(void)setPattern:(ICUPattern *)p; 84 85 /*! 86 @method matches 87 @abstract Returns YES if the patterns matches the <b>entire</b> input string. 88 @discussion 89 */ 26 90 -(BOOL)matches; 27 -(void)setPattern:(ICUPattern *)p; 91 92 /*! 93 @method replaceAllWithString: 94 @abstract Replaces all occurrences of the pattern with the replacement string and returns the resulting string. 95 @discussion The replacement string can contain references to capture groups taking the form or $1, $2, etc. 96 */ 28 97 -(NSString *)replaceAllWithString:(NSString *)aReplacementString; 98 99 /*! 100 @method replaceFirstWithString: 101 @abstract Replaces the first occurrence of the pattern with the given replacement string and returns the resulting string. 102 @discussion The replacement string can contain references to capture groups taking the form or $1, $2, etc. 103 */ 29 104 -(NSString *)replaceFirstWithString:(NSString *)aReplacementString; 105 106 /*! 107 @method reset 108 @abstract Resets any state associated with the matcher and its pattern. 109 @discussion 110 */ 30 111 -(void)reset; 112 113 /*! 114 @method rangeOfMatch 115 @abstract Returns the range of the input string that corresponds to the current match. 116 @discussion 117 */ 31 118 -(NSRange)rangeOfMatch; 119 120 /*! 121 @method rangeOfMatchGroup: 122 @abstract Returns the range of the input string that corresponds to the specified capture group of the current match. 123 @discussion 124 */ 32 125 -(NSRange)rangeOfMatchGroup:(unsigned)groupNumber; 33 126 trunk/source/ICUMatcher.m
r6 r10 40 40 } 41 41 42 -(ICUMatcher *)initWithPattern:(ICUPattern *)p overString:(NSString *)aStringToSearch; 43 { 42 -(ICUMatcher *)initWithPattern:(ICUPattern *)p overString:(NSString *)aStringToSearch; { 44 43 if(![super init]) 45 44 return nil; … … 51 50 } 52 51 53 -(void)dealloc 54 { 52 -(void)dealloc { 55 53 [[self pattern] release]; 56 54 … … 90 88 } 91 89 92 -(BOOL)findFromIndex:(unsigned)index 93 { 90 -(BOOL)findFromIndex:(unsigned)index { 94 91 URegularExpression *re = [[self pattern] re]; 95 92 [self reset]; … … 102 99 } 103 100 104 -(NSString *)group 105 { 101 -(NSString *)group { 106 102 NSString *stringToMatch = [[self pattern] stringToSearch]; 107 103 return [stringToMatch substringWithRange:[self rangeOfMatch]]; 108 104 } 109 105 110 -(NSString *)groupAtIndex:(unsigned)groupIndex 111 { 106 -(NSString *)groupAtIndex:(unsigned)groupIndex { 112 107 size_t groupSize = InitialGroupSize; 113 108 URegularExpression *re = [[self pattern] re]; … … 133 128 } 134 129 135 -(unsigned)numberOfGroups 136 { 130 -(unsigned)numberOfGroups { 137 131 URegularExpression *re = [[self pattern] re]; 138 132 UErrorCode status = 0; … … 207 201 } 208 202 209 -(NSRange)rangeOfMatch 210 { 203 -(NSRange)rangeOfMatch { 211 204 return [self rangeOfMatchGroup:0]; 212 205 } 213 206 214 -(NSRange)rangeOfMatchGroup:(unsigned)groupNumber 215 { 207 -(NSRange)rangeOfMatchGroup:(unsigned)groupNumber { 216 208 UErrorCode status = 0; 217 209 URegularExpression *re = [[self pattern] re]; trunk/source/ICUPattern.h
r5 r10 20 20 extern unsigned UnicodeWordBoundaries; 21 21 22 /*! 23 @class ICUPattern 24 @abstract A compiled regular expression. 25 @discussion <a href="http://icu.sourceforge.net/">ICU</a> provides a widely used Unicode 26 regular expression library. This class can be roughly mapped to the <a href="http://icu.sourceforge.net/apiref/icu4c/classRegexPattern.html">ICU C++ Pattern class</a>. Notes about ICU regular expressions can be found at the 27 <a href="http://icu.sourceforge.net/userguide/regexp.html">ICU User Guide</a>. 28 */ 22 29 @interface ICUPattern: NSObject <NSCopying> { 23 30 void *re; 24 31 void *textToSearch; 25 32 unsigned flags; 33 NSString *stringToSearch; 26 34 } 27 35 28 // XOR flags (defaults to 0 when no flags are given) 36 /*! 37 @method patternWithString:flags: 38 @abstract Returns an autoreleased pattern with the specified flags set. 39 @discussion Flags are defined as the OR of the constants defined in the class. 40 */ 29 41 +(ICUPattern *)patternWithString:(NSString *)aPattern flags:(unsigned)flags; 42 43 /*! 44 @method patternWithString: 45 @abstract Returns an autoreleased pattern with the default flags. 46 @discussion Flags are given by 0. 47 */ 30 48 +(ICUPattern *)patternWithString:(NSString *)aPattern; 31 -(id)initWithString:(NSString *)aPattern; 49 50 /*! 51 @method initWithString:flags: 52 @abstract Returns a pattern with the specified flags set. 53 @discussion Flags are defined as the OR of the constants defined in the class. 54 */ 32 55 -(id)initWithString:(NSString *)aPattern flags:(unsigned)flags; 33 56 57 /*! 58 @method initWithString: 59 @abstract Returns a pattern with the specified flags set. 60 @discussion Flags are defined as 0. 61 */ 62 -(id)initWithString:(NSString *)aPattern; 63 64 /*! 65 @method componentsSplitFromString: 66 @abstract Splits the input string into fields delineated by the expression given by the pattern. 67 @discussion Returns an empty array if the pattern is not found. 68 */ 34 69 -(NSArray *)componentsSplitFromString:(NSString *)stringToSplit; 70 71 /*! 72 @method matchesString: 73 @abstract Returns YES if the pattern matches the entire input string. 74 @discussion Returns YES if the pattern matches the entire input string. 75 */ 35 76 -(BOOL)matchesString:(NSString *)stringToMatchAgainst; 77 78 /*! 79 @method pattern 80 @abstract Returns the string representing the regular expression of this pattern. 81 @discussion Returns the string representing the regular expression of this pattern. 82 */ 36 83 -(NSString *)pattern; 84 85 /*! 86 @method setStringToSearch: 87 @abstract Sets the string that is being searched by this pattern. 88 @discussion This method also resets any internal state of the pattern. 89 */ 37 90 -(void)setStringToSearch:(NSString *)aStringToSearchOver; 91 92 /*! 93 @method stringToSearch 94 @abstract Returns the string that is being searched over with the pattern. 95 @discussion This method creates an NSString from the underlying UTF16 character array used by ICU. For large 96 strings, this may be memory-intensive/time-consuming. Performance-critical applications may want to modify this 97 class to suite their needs to reduce the number of NSString objects that are created. 98 */ 38 99 -(NSString *)stringToSearch; 100 101 /*! 102 @method reset 103 @abstract Resets the pattern. 104 @discussion Patterns maintain state about the current match and this method resets that state. 105 */ 39 106 -(void)reset; 40 107 108 /*! 109 @method re 110 @abstract Returns the compiled ICU URegularExpression for this pattern. 111 @discussion This method primarily exists to allow the ICUMatcher to access the pattern. 112 */ 41 113 -(void *)re; 114 42 115 @end trunk/source/ICUPattern.m
r6 r10 85 85 free(re); 86 86 87 [stringToSearch release]; 87 88 [super dealloc]; 88 89 } 89 90 90 -(NSString *)stringToSearch 91 { 91 -(NSString *)stringToSearch { 92 92 return [NSString stringWithUTF16String:[self textToSearch]]; 93 93 } 94 94 95 -(void)setStringToSearch:(NSString *)aStringToSearchOver 96 { 95 -(void)setStringToSearch:(NSString *)aStringToSearchOver { 96 if(stringToSearch != nil) 97 [stringToSearch release]; 98 99 stringToSearch = [aStringToSearchOver retain]; 100 97 101 [self setTextToSearch:[aStringToSearchOver UTF16String]]; 98 102 } … … 102 106 103 107 uregex_setText([self re], utf16String, -1, &status); 104 105 textToSearch = utf16String; 108 109 [self reset]; 110 111 textToSearch = utf16String; // retained by the NSString 106 112 107 113 if(U_FAILURE(status)) { … … 156 162 } 157 163 158 -(void *)re 159 { 164 -(void *)re { 160 165 return re; 161 166 } … … 234 239 } 235 240 236 -(BOOL)matchesString:(NSString *)stringToMatchAgainst 237 { 241 -(BOOL)matchesString:(NSString *)stringToMatchAgainst { 238 242 ICUMatcher *m = [ICUMatcher matcherWithPattern:self overString:stringToMatchAgainst]; 239 243 return [m matches];