Changeset 18
- Timestamp:
- 09/16/07 4:04:53 PM (10 months ago)
- Files:
-
- trunk/CocoaICU Tests-Info.plist (added)
- trunk/CocoaICU.xcodeproj/project.pbxproj (modified) (12 diffs)
- trunk/source/CocoaICU.m (modified) (2 diffs)
- trunk/source/ICUMatcher.m (modified) (6 diffs)
- trunk/source/ICUPattern.m (modified) (5 diffs)
- trunk/source/NSStringICUAdditions.h (modified) (1 diff)
- trunk/source/NSStringICUAdditions.m (modified) (2 diffs)
- trunk/source/TestCases.h (added)
- trunk/source/TestCases.m (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/CocoaICU.xcodeproj/project.pbxproj
r10 r18 8 8 9 9 /* Begin PBXBuildFile section */ 10 939322590B1E6AD000DD0AF0 /* ICUMatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = 939322580B1E6AD000DD0AF0 /* ICUMatcher.m */; }; 11 93E2C50F0B1A5495001193AC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; }; 12 93E2C5100B1A5495001193AC /* ICUPattern.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0D00B18D142001193AC /* ICUPattern.m */; }; 13 93E2C5120B1A5495001193AC /* libicucore.A.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9389EF6B0B10EDAA000ECD6C /* libicucore.A.dylib */; }; 14 93E2C5130B1A5495001193AC /* NSStringICUAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0D40B18D142001193AC /* NSStringICUAdditions.m */; }; 15 93E2C5320B1A59FC001193AC /* CocoaICU.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0CE0B18D142001193AC /* CocoaICU.m */; }; 10 93AB9EBA0C98AD7200B11910 /* TestCases.m in Sources */ = {isa = PBXBuildFile; fileRef = 93AB9E810C98A24C00B11910 /* TestCases.m */; }; 11 93AB9EBB0C98AD7200B11910 /* ICUPattern.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0D00B18D142001193AC /* ICUPattern.m */; }; 12 93AB9EBC0C98AD7200B11910 /* ICUMatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = 939322580B1E6AD000DD0AF0 /* ICUMatcher.m */; }; 13 93AB9EBD0C98AD7200B11910 /* NSStringICUAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0D40B18D142001193AC /* NSStringICUAdditions.m */; }; 14 93AB9EBF0C98AD8D00B11910 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; }; 15 93AB9EC00C98AD8D00B11910 /* libicucore.A.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9389EF6B0B10EDAA000ECD6C /* libicucore.A.dylib */; }; 16 93BC7A0E0C9D564D00181C32 /* CocoaICU.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0CE0B18D142001193AC /* CocoaICU.m */; }; 17 93BC7A0F0C9D564D00181C32 /* ICUPattern.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0D00B18D142001193AC /* ICUPattern.m */; }; 18 93BC7A100C9D564D00181C32 /* ICUMatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = 939322580B1E6AD000DD0AF0 /* ICUMatcher.m */; }; 19 93BC7A110C9D564D00181C32 /* NSStringICUAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E2C0D40B18D142001193AC /* NSStringICUAdditions.m */; }; 20 93BC7A8E0C9D565300181C32 /* libicucore.A.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9389EF6B0B10EDAA000ECD6C /* libicucore.A.dylib */; }; 21 93BC7A8F0C9D565300181C32 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; }; 16 22 /* End PBXBuildFile section */ 17 23 … … 22 28 939322570B1E6AD000DD0AF0 /* ICUMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ICUMatcher.h; path = source/ICUMatcher.h; sourceTree = "<group>"; }; 23 29 939322580B1E6AD000DD0AF0 /* ICUMatcher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ICUMatcher.m; path = source/ICUMatcher.m; sourceTree = "<group>"; }; 30 93AB9E800C98A24C00B11910 /* TestCases.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestCases.h; path = source/TestCases.h; sourceTree = "<group>"; }; 31 93AB9E810C98A24C00B11910 /* TestCases.m */ = {isa = PBXFileReference; fileEncoding = 4; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = TestCases.m; path = source/TestCases.m; sourceTree = "<group>"; }; 32 93AB9E8E0C98A30C00B11910 /* CocoaICU Tests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "CocoaICU Tests.octest"; sourceTree = BUILT_PRODUCTS_DIR; }; 33 93AB9E8F0C98A30C00B11910 /* CocoaICU Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = "CocoaICU Tests-Info.plist"; sourceTree = "<group>"; }; 34 93BC7A0C0C9D563E00181C32 /* ICUTool */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ICUTool; sourceTree = BUILT_PRODUCTS_DIR; }; 24 35 93E2C0CE0B18D142001193AC /* CocoaICU.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CocoaICU.m; path = source/CocoaICU.m; sourceTree = "<group>"; }; 25 36 93E2C0CF0B18D142001193AC /* ICUPattern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ICUPattern.h; path = source/ICUPattern.h; sourceTree = "<group>"; }; … … 27 38 93E2C0D30B18D142001193AC /* NSStringICUAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSStringICUAdditions.h; path = source/NSStringICUAdditions.h; sourceTree = "<group>"; }; 28 39 93E2C0D40B18D142001193AC /* NSStringICUAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSStringICUAdditions.m; path = source/NSStringICUAdditions.m; sourceTree = "<group>"; }; 29 93E2C4F00B1A53F0001193AC /* CocoaICU */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CocoaICU; sourceTree = BUILT_PRODUCTS_DIR; };30 40 /* End PBXFileReference section */ 31 41 32 42 /* Begin PBXFrameworksBuildPhase section */ 33 93 E2C4EE0B1A53F0001193AC/* Frameworks */ = {43 93AB9E8B0C98A30C00B11910 /* Frameworks */ = { 34 44 isa = PBXFrameworksBuildPhase; 35 45 buildActionMask = 2147483647; 36 46 files = ( 37 93E2C5120B1A5495001193AC /* libicucore.A.dylib in Frameworks */, 38 93E2C50F0B1A5495001193AC /* Foundation.framework in Frameworks */, 47 93AB9EBF0C98AD8D00B11910 /* Foundation.framework in Frameworks */, 48 93AB9EC00C98AD8D00B11910 /* libicucore.A.dylib in Frameworks */, 49 ); 50 runOnlyForDeploymentPostprocessing = 0; 51 }; 52 93BC7A0A0C9D563E00181C32 /* Frameworks */ = { 53 isa = PBXFrameworksBuildPhase; 54 buildActionMask = 2147483647; 55 files = ( 56 93BC7A8E0C9D565300181C32 /* libicucore.A.dylib in Frameworks */, 57 93BC7A8F0C9D565300181C32 /* Foundation.framework in Frameworks */, 39 58 ); 40 59 runOnlyForDeploymentPostprocessing = 0; … … 47 66 children = ( 48 67 93E2C0DE0B18D17D001193AC /* Other */, 68 93AB9E7D0C98A23900B11910 /* Tests */, 49 69 08FB7795FE84155DC02AAC07 /* Source */, 50 70 08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */, 51 71 1AB674ADFE9D54B511CA2CBB /* Products */, 72 93AB9E8F0C98A30C00B11910 /* CocoaICU Tests-Info.plist */, 52 73 ); 53 74 name = CocoaICU; … … 79 100 isa = PBXGroup; 80 101 children = ( 81 93E2C4F00B1A53F0001193AC /* CocoaICU */, 102 93AB9E8E0C98A30C00B11910 /* CocoaICU Tests.octest */, 103 93BC7A0C0C9D563E00181C32 /* ICUTool */, 82 104 ); 83 105 name = Products; 106 sourceTree = "<group>"; 107 }; 108 93AB9E7D0C98A23900B11910 /* Tests */ = { 109 isa = PBXGroup; 110 children = ( 111 93AB9E800C98A24C00B11910 /* TestCases.h */, 112 93AB9E810C98A24C00B11910 /* TestCases.m */, 113 ); 114 name = Tests; 84 115 sourceTree = "<group>"; 85 116 }; … … 96 127 97 128 /* Begin PBXNativeTarget section */ 98 93 E2C4EF0B1A53F0001193AC /* CocoaICU*/ = {129 93AB9E8D0C98A30C00B11910 /* CocoaICU Tests */ = { 99 130 isa = PBXNativeTarget; 100 buildConfigurationList = 93 E2C5010B1A5403001193AC /* Build configuration list for PBXNativeTarget "CocoaICU" */;131 buildConfigurationList = 93AB9E900C98A30D00B11910 /* Build configuration list for PBXNativeTarget "CocoaICU Tests" */; 101 132 buildPhases = ( 102 93E2C4ED0B1A53F0001193AC /* Sources */, 103 93E2C4EE0B1A53F0001193AC /* Frameworks */, 133 93AB9E890C98A30C00B11910 /* Resources */, 134 93AB9E8A0C98A30C00B11910 /* Sources */, 135 93AB9E8B0C98A30C00B11910 /* Frameworks */, 136 93AB9E8C0C98A30C00B11910 /* ShellScript */, 104 137 ); 105 138 buildRules = ( … … 107 140 dependencies = ( 108 141 ); 109 name = CocoaICU; 110 productName = CocoaICU; 111 productReference = 93E2C4F00B1A53F0001193AC /* CocoaICU */; 142 name = "CocoaICU Tests"; 143 productName = "CocoaICU Tests"; 144 productReference = 93AB9E8E0C98A30C00B11910 /* CocoaICU Tests.octest */; 145 productType = "com.apple.product-type.bundle"; 146 }; 147 93BC7A0B0C9D563E00181C32 /* ICU Test Program */ = { 148 isa = PBXNativeTarget; 149 buildConfigurationList = 93BC7A990C9D569200181C32 /* Build configuration list for PBXNativeTarget "ICU Test Program" */; 150 buildPhases = ( 151 93BC7A090C9D563E00181C32 /* Sources */, 152 93BC7A0A0C9D563E00181C32 /* Frameworks */, 153 ); 154 buildRules = ( 155 ); 156 comments = "A foundation tool useful for debugging and testing ICU."; 157 dependencies = ( 158 ); 159 name = "ICU Test Program"; 160 productName = ICUTool; 161 productReference = 93BC7A0C0C9D563E00181C32 /* ICUTool */; 112 162 productType = "com.apple.product-type.tool"; 113 163 }; … … 122 172 projectDirPath = ""; 123 173 targets = ( 124 93E2C4EF0B1A53F0001193AC /* CocoaICU */, 174 93AB9E8D0C98A30C00B11910 /* CocoaICU Tests */, 175 93BC7A0B0C9D563E00181C32 /* ICU Test Program */, 125 176 ); 126 177 }; 127 178 /* End PBXProject section */ 128 179 180 /* Begin PBXResourcesBuildPhase section */ 181 93AB9E890C98A30C00B11910 /* Resources */ = { 182 isa = PBXResourcesBuildPhase; 183 buildActionMask = 2147483647; 184 files = ( 185 ); 186 runOnlyForDeploymentPostprocessing = 0; 187 }; 188 /* End PBXResourcesBuildPhase section */ 189 190 /* Begin PBXShellScriptBuildPhase section */ 191 93AB9E8C0C98A30C00B11910 /* ShellScript */ = { 192 isa = PBXShellScriptBuildPhase; 193 buildActionMask = 2147483647; 194 files = ( 195 ); 196 inputPaths = ( 197 ); 198 outputPaths = ( 199 ); 200 runOnlyForDeploymentPostprocessing = 0; 201 shellPath = /bin/sh; 202 shellScript = "# Run the unit tests in this test bundle.\n\"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests\"\n"; 203 }; 204 /* End PBXShellScriptBuildPhase section */ 205 129 206 /* Begin PBXSourcesBuildPhase section */ 130 93 E2C4ED0B1A53F0001193AC/* Sources */ = {207 93AB9E8A0C98A30C00B11910 /* Sources */ = { 131 208 isa = PBXSourcesBuildPhase; 132 209 buildActionMask = 2147483647; 133 210 files = ( 134 93E2C5320B1A59FC001193AC /* CocoaICU.m in Sources */, 135 93E2C5100B1A5495001193AC /* ICUPattern.m in Sources */, 136 93E2C5130B1A5495001193AC /* NSStringICUAdditions.m in Sources */, 137 939322590B1E6AD000DD0AF0 /* ICUMatcher.m in Sources */, 211 93AB9EBA0C98AD7200B11910 /* TestCases.m in Sources */, 212 93AB9EBB0C98AD7200B11910 /* ICUPattern.m in Sources */, 213 93AB9EBC0C98AD7200B11910 /* ICUMatcher.m in Sources */, 214 93AB9EBD0C98AD7200B11910 /* NSStringICUAdditions.m in Sources */, 215 ); 216 runOnlyForDeploymentPostprocessing = 0; 217 }; 218 93BC7A090C9D563E00181C32 /* Sources */ = { 219 isa = PBXSourcesBuildPhase; 220 buildActionMask = 2147483647; 221 files = ( 222 93BC7A0E0C9D564D00181C32 /* CocoaICU.m in Sources */, 223 93BC7A0F0C9D564D00181C32 /* ICUPattern.m in Sources */, 224 93BC7A100C9D564D00181C32 /* ICUMatcher.m in Sources */, 225 93BC7A110C9D564D00181C32 /* NSStringICUAdditions.m in Sources */, 138 226 ); 139 227 runOnlyForDeploymentPostprocessing = 0; … … 166 254 name = Release; 167 255 }; 168 93E2C5020B1A5403001193AC /* Debug */ = { 256 93AB9E910C98A30D00B11910 /* Debug */ = { 257 isa = XCBuildConfiguration; 258 buildSettings = { 259 COPY_PHASE_STRIP = NO; 260 GCC_DYNAMIC_NO_PIC = NO; 261 GCC_ENABLE_FIX_AND_CONTINUE = NO; 262 GCC_ENABLE_OBJC_EXCEPTIONS = YES; 263 GCC_GENERATE_DEBUGGING_SYMBOLS = YES; 264 GCC_MODEL_TUNING = G5; 265 GCC_OPTIMIZATION_LEVEL = 0; 266 GCC_PRECOMPILE_PREFIX_HEADER = YES; 267 GCC_PREFIX_HEADER = "$(SYSTEM_LIBRARY_DIR)/Frameworks/Cocoa.framework/Headers/Cocoa.h"; 268 INFOPLIST_FILE = "CocoaICU Tests-Info.plist"; 269 INSTALL_PATH = "$(USER_LIBRARY_DIR)/Bundles"; 270 OTHER_LDFLAGS = ( 271 "-framework", 272 Cocoa, 273 "-framework", 274 SenTestingKit, 275 ); 276 PREBINDING = NO; 277 PRODUCT_NAME = "CocoaICU Tests"; 278 WRAPPER_EXTENSION = octest; 279 ZERO_LINK = NO; 280 }; 281 name = Debug; 282 }; 283 93AB9E920C98A30D00B11910 /* Release */ = { 284 isa = XCBuildConfiguration; 285 buildSettings = { 286 COPY_PHASE_STRIP = YES; 287 GCC_ENABLE_FIX_AND_CONTINUE = NO; 288 GCC_ENABLE_OBJC_EXCEPTIONS = YES; 289 GCC_GENERATE_DEBUGGING_SYMBOLS = NO; 290 GCC_MODEL_TUNING = G5; 291 GCC_PRECOMPILE_PREFIX_HEADER = YES; 292 GCC_PREFIX_HEADER = "$(SYSTEM_LIBRARY_DIR)/Frameworks/Cocoa.framework/Headers/Cocoa.h"; 293 INFOPLIST_FILE = "CocoaICU Tests-Info.plist"; 294 INSTALL_PATH = "$(USER_LIBRARY_DIR)/Bundles"; 295 OTHER_LDFLAGS = ( 296 "-framework", 297 Cocoa, 298 "-framework", 299 SenTestingKit, 300 ); 301 PREBINDING = NO; 302 PRODUCT_NAME = "CocoaICU Tests"; 303 WRAPPER_EXTENSION = octest; 304 ZERO_LINK = NO; 305 }; 306 name = Release; 307 }; 308 93BC7A9A0C9D569200181C32 /* Debug */ = { 169 309 isa = XCBuildConfiguration; 170 310 buildSettings = { … … 177 317 INSTALL_PATH = "$(HOME)/bin"; 178 318 PREBINDING = NO; 179 PRODUCT_NAME = CocoaICU;319 PRODUCT_NAME = ICUTool; 180 320 ZERO_LINK = YES; 181 321 }; 182 322 name = Debug; 183 323 }; 184 93 E2C5030B1A5403001193AC/* Release */ = {324 93BC7A9B0C9D569200181C32 /* Release */ = { 185 325 isa = XCBuildConfiguration; 186 326 buildSettings = { … … 191 331 INSTALL_PATH = "$(HOME)/bin"; 192 332 PREBINDING = NO; 193 PRODUCT_NAME = CocoaICU;333 PRODUCT_NAME = ICUTool; 194 334 ZERO_LINK = NO; 195 335 }; … … 208 348 defaultConfigurationName = Release; 209 349 }; 210 93 E2C5010B1A5403001193AC /* Build configuration list for PBXNativeTarget "CocoaICU" */ = {350 93AB9E900C98A30D00B11910 /* Build configuration list for PBXNativeTarget "CocoaICU Tests" */ = { 211 351 isa = XCConfigurationList; 212 352 buildConfigurations = ( 213 93E2C5020B1A5403001193AC /* Debug */, 214 93E2C5030B1A5403001193AC /* Release */, 353 93AB9E910C98A30D00B11910 /* Debug */, 354 93AB9E920C98A30D00B11910 /* Release */, 355 ); 356 defaultConfigurationIsVisible = 0; 357 defaultConfigurationName = Release; 358 }; 359 93BC7A990C9D569200181C32 /* Build configuration list for PBXNativeTarget "ICU Test Program" */ = { 360 isa = XCConfigurationList; 361 buildConfigurations = ( 362 93BC7A9A0C9D569200181C32 /* Debug */, 363 93BC7A9B0C9D569200181C32 /* Release */, 215 364 ); 216 365 defaultConfigurationIsVisible = 0; trunk/source/CocoaICU.m
r16 r18 4 4 #import "NSStringICUAdditions.h" 5 5 6 void runTest(); 6 7 7 void simpleFind(); 8 void findAndReplaceFirst(); 9 void findAndReplaceAll(); 10 void split(); 11 void unicodeFind(); 12 void groupFindAndReplace(); 13 void advancedFindAndReplace(); 14 8 // simple application useful for debugging or testing code 15 9 int main (int argc, const char * argv[]) { 16 10 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 17 11 18 simpleFind(); 19 findAndReplaceFirst(); 20 findAndReplaceAll(); 21 split(); 22 unicodeFind(); 23 groupFindAndReplace(); 24 advancedFindAndReplace(); 12 runTest(); 25 13 26 14 [pool release]; … … 28 16 } 29 17 30 void simpleFind() { 31 NSString *searchString = @"bbdfdbbababababaklsdjfababababab"; 32 NSString *patternString = @"([ab]+)"; 33 ICUPattern *p = [ICUPattern patternWithString:patternString]; 34 ICUMatcher *matcher = [ICUMatcher matcherWithPattern:p overString:searchString]; 18 void runTest() { 35 19 36 while([matcher findNext]) {37 NSRange r = [matcher rangeOfMatch];38 NSLog(@"match = %@", [searchString substringWithRange:r]);39 }40 20 } 41 21 42 void findAndReplaceFirst(){43 NSString *searchString = [NSString stringWithUTF8String:"åºåº¢åºåº¢åœåœ€åºåº¢"];44 NSString *patternString = [NSString stringWithUTF8String:"([åºåº¢]+)"];45 ICUPattern *p = [ICUPattern patternWithString:patternString];46 ICUMatcher *matcher = [ICUMatcher matcherWithPattern:p overString:searchString];47 NSString *result = [matcher replaceFirstWithString:[NSString stringWithUTF8String:"åŒåŒåŒåŒ"]];48 NSLog(@"result = %@", result);49 }50 51 void findAndReplaceAll() {52 NSString *searchString = [NSString stringWithUTF8String:"âžâââââââžâââââââžââââââ"];53 NSString *patternString = [NSString stringWithUTF8String:"(âžââââââ)"];54 ICUPattern *p = [ICUPattern patternWithString:patternString];55 ICUMatcher *matcher = [ICUMatcher matcherWithPattern:p overString:searchString];56 NSString *result = [matcher replaceAllWithString:[NSString stringWithUTF8String:"ÅÅÃÃÃÃà à à à À"]];57 NSLog(@"result = %@", result);58 }59 60 void split() {61 NSString *searchString = @"bbdfdbbababababaklsdjfababababab";62 NSString *patternString = @"[ab]+";63 ICUPattern *p = [ICUPattern patternWithString:patternString];64 NSArray *r = [p componentsSplitFromString:searchString];65 NSLog(@"result = %@", r);66 }67 68 void unicodeFind() {69 NSString *searchString = [NSString stringWithUTF8String:"âžâââââââ70 â71 ÅÅÀÀÀÀÀÅÅÄÄÄÀÀÀÅÅÅÃÃÃÃà à à à ÀÀÀÀÀ"];72 NSString *patternString = [NSString stringWithUTF8String:"À+"];73 ICUPattern *p = [ICUPattern patternWithString:patternString];74 ICUMatcher *matcher = [ICUMatcher matcherWithPattern:p overString:searchString];75 while([matcher findNext]) {76 NSLog(@"match = %@", [searchString substringWithRange:[matcher rangeOfMatch]]);77 }78 }79 80 void groupFindAndReplace() {81 NSString *searchString = @"ababcdcdababcdcdabefghiabbababbacdcdccdcd";82 NSString *patternString = @"([ab]+)([cd]+)";83 ICUPattern *p = [ICUPattern patternWithString:patternString];84 ICUMatcher *matcher = [ICUMatcher matcherWithPattern:p overString:searchString];85 while([matcher findNext]) {86 int i;87 for(i=0;i<=[matcher numberOfGroups];i++) {88 NSLog(@"group %i : %@", i, [matcher groupAtIndex:i]);89 }90 }91 }92 93 void advancedFindAndReplace() {94 NSString *searchString = @"aabbcc";95 NSString *patternString = @"(a+)..(c+)";96 NSString *replacedText = [searchString replaceOccurrencesOfPattern:patternString withString:@" $2 $1"];97 NSLog(@"%@", replacedText);98 }99 trunk/source/ICUMatcher.m
r16 r18 110 110 while(YES) { 111 111 UErrorCode status = 0; 112 UChar *dest = calloc(groupSize, sizeof(UChar));112 UChar *dest = (UChar *)NSZoneCalloc([self zone], groupSize, sizeof(UChar)); 113 113 int32_t buffSize = uregex_group(re, groupIndex, dest, groupSize, &status); 114 114 115 115 if(U_BUFFER_OVERFLOW_ERROR == status) { 116 116 groupSize *= 2; 117 free(dest);117 NSZoneFree([self zone], dest); 118 118 continue; 119 119 } … … 123 123 groupSize = InitialGroupSize; // reset to default 124 124 NSString *result = [[[NSString alloc] initWithBytes:dest length:buffSize*sizeof(UChar) encoding:[NSString nativeUTF16Encoding]] autorelease]; 125 free(dest);125 NSZoneFree([self zone], dest); 126 126 return result; 127 127 } … … 150 150 151 151 -(NSString *)performReplacementWithString:(NSString *)aReplacementString replaceAll:(BOOL)replacingAll { 152 152 153 UErrorCode status = 0; 153 154 UChar *replacementText = [aReplacementString UTF16String]; 154 155 URegularExpression *re = [[self pattern] re]; 155 int searchTextLength = [[[self pattern] stringToSearch] length];156 unsigned int searchTextLength = [[[self pattern] stringToSearch] length]; 156 157 157 158 BOOL replacementCompleted = NO; … … 161 162 while(!replacementCompleted) { 162 163 163 destString = calloc(destStringBufferSize, sizeof(UChar)); 164 if(!destString) // attempts to increase buffer happen on failure below 165 destString = NSZoneCalloc([self zone], destStringBufferSize, sizeof(UChar)); 166 167 if(!destString) 168 [NSException raise:@"Find Exception" 169 format:@"Could not allocate memory for replacement string"]; 170 171 status = 0; 164 172 if(replacingAll) 165 173 resultLength = uregex_replaceAll(re, replacementText, -1, destString, destStringBufferSize, &status); … … 167 175 resultLength = uregex_replaceFirst(re, replacementText, -1, destString, destStringBufferSize, &status); 168 176 177 // realloc some more space if possible 169 178 if(status == U_BUFFER_OVERFLOW_ERROR) { 170 status = 0; 171 free(destString); 172 destStringBufferSize *= 2; 173 } 174 else if(U_FAILURE(status)) { 175 free(destString); 179 180 destStringBufferSize = resultLength + 1; 181 182 UChar *prevString = destString; 183 destString = NSZoneRealloc([self zone], destString, destStringBufferSize*sizeof(UChar)); 184 185 if(destString == NULL) { 186 NSZoneFree([self zone], prevString); 187 [NSException raise:@"Find Exception" 188 format:@"Could not allocate memory for replacement string"]; 189 } 190 } else if(U_FAILURE(status)) { 191 NSZoneFree([self zone], destString); 176 192 [NSException raise:@"Find Exception" 177 193 format:@"Could not perform find and replace: %s", u_errorName(status)]; … … 184 200 length:resultLength * sizeof(UChar) 185 201 encoding:[NSString nativeUTF16Encoding]] autorelease]; 186 free(destString);202 NSZoneFree([self zone], destString); 187 203 return result; 188 204 } trunk/source/ICUPattern.m
r17 r18 82 82 83 83 if(re != NULL) 84 free(re);84 NSZoneFree([self zone], re); 85 85 86 86 if(textToSearch != NULL) … … 152 152 -(void)setRe:(URegularExpression *)p { 153 153 if(re != NULL) 154 free(re);154 NSZoneFree([self zone], re); 155 155 156 156 re = p; … … 191 191 192 192 while(!isDone) { 193 UChar *destBuf = calloc(destCapacity, sizeof(UChar));193 UChar *destBuf = (UChar *)NSZoneCalloc([self zone], destCapacity, sizeof(UChar)); 194 194 int requiredCapacity = 0; 195 195 UChar *destFields[destFieldsCapacity]; … … 201 201 destFieldsCapacity, 202 202 &status); 203 203 204 204 if(status == U_BUFFER_OVERFLOW_ERROR) { // buffer was too small, grow it 205 free(destBuf);205 NSZoneFree([self zone], destBuf); 206 206 NSAssert(destCapacity * 2 < INT_MAX, @"Overflow occurred splitting string."); 207 207 destCapacity = (destCapacity < requiredCapacity) ? requiredCapacity : destCapacity * 2; … … 210 210 destFieldsCapacity *= 2; 211 211 NSAssert(destFieldsCapacity *2 < INT_MAX, @"Overflow occurred splitting string."); 212 free(destBuf);212 NSZoneFree([self zone], destBuf); 213 213 status = 0; 214 214 } else if(U_FAILURE(status)) { 215 free(destBuf);215 NSZoneFree([self zone], destBuf); 216 216 isDone = YES; 217 217 } else { trunk/source/NSStringICUAdditions.h
r17 r18 12 12 @interface NSString (NSStringICUAdditions) 13 13 14 /*! 15 @method nativeUTF16Encoding 16 @abstract The native UTF16 encoding on the given machine. 17 @discussion The native UTF16 encoding on the given machine. 18 */ 14 19 +(NSStringEncoding)nativeUTF16Encoding; 15 20 21 /*! 22 @method stringWithICUString: 23 @abstract Create an NSString from a UTF16 encoded string. 24 @discussion Create an NSString from a UTF16 encoded string. 25 */ 16 26 +(NSString *)stringWithICUString:(void *)utf16EncodedString; 27 28 /*! 29 @method UTF16String 30 @abstract Returns a UTF16 encoded string in the native encoding. 31 @discussion This string has a retain policy equivalent to UTF8String. In 32 other words, if you want to keep this string around beyond the given autorelease 33 context, you need to copy the returned string. 34 */ 17 35 -(void *)UTF16String; 36 37 38 /*! 39 @method copyUTF16String 40 @abstract returns a copy 41 @discussion Returns a UTF16 encoded string in the native encoding. The returned 42 buffer must eventually be freed. 43 */ 18 44 -(void *)copyUTF16String; 19 45 20 -(NSArray *)findPattern:(NSString *)aRegex; 21 -(NSArray *)componentsSeparatedByPattern:(NSString *)aRegex; 22 -(NSString *)replaceOccurrencesOfPattern:(NSString *)aPattern withString:(NSString *)replacementText; 23 -(BOOL)matchesPattern:(NSString *)aRegex; 46 /*! 47 @method findPattern: 48 @abstract Finds the given expression and and groups. 49 @discussion Returns the match and any grouped matches in the returned 50 array. The first element of the array is the entire match and subsequent 51 elements are the groups in the order the matches occur. 52 */ 24 53 -(NSArray *)findPattern:(NSString *)aRegex; 25 54 55 /*! 56 @method componentsSeparatedByPattern: 57 @abstract Returns the components separated by the given pattern. 58 @discussion Break a string into components where components separated 59 by occurrences of the given pattern. 60 */ 61 -(NSArray *)componentsSeparatedByPattern:(NSString *)aRegex; 62 63 /*! 64 @method replaceOccurrencesOfPattern:withString: 65 @abstract Replace occurrences of the pattern with the replacement text. 66 @discussion Replace occurrences of the pattern with the replacement text. 67 The replacement text may contain backrerferences. 68 */ 69 -(NSString *)replaceOccurrencesOfPattern:(NSString *)aPattern withString:(NSString *)replacementText; 70 71 /*! 72 @method matchesPattern: 73 @abstract Returns YES if the string matches the entire pattern. 74 @discussion Returns YES if the string matches the entire pattern. 75 */ 76 -(BOOL)matchesPattern:(NSString *)aRegex; 77 26 78 @end trunk/source/NSStringICUAdditions.m
r17 r18 27 27 @end 28 28 29 @implementation NSString (NSStringICUAdditions) //29 @implementation NSString (NSStringICUAdditions) 30 30 31 31 -(NSString *)replaceOccurrencesOfPattern:(NSString *)aPattern withString:(NSString *)replacementText { … … 78 78 -(void *)UTF16String { 79 79 UChar *ret = (UChar *)[self cStringUsingEncoding:[NSString nativeUTF16Encoding]]; 80 80 81 81 // for some reason, the null-terminator doesn't always show up at the right place and this 82 82 // causes extra characters to be created in the unicode string. We remove them here by force. 83 int len = [self length];83 unsigned int len = [self length]; 84 84 ret[len] = '\0'; 85 85 return ret;