Changeset 18

Show
Ignore:
Timestamp:
09/16/07 4:04:53 PM (10 months ago)
Author:
aaron
Message:
  • added test cases and target for running tests
  • modified ICUMatcher performReplacementWithString to use realloc when the
    buffer size needs to be increased instead of freeing and allocing a new buffer
  • chagned memory alloc/free from C functions to NSMallocZone and friends
  • made ints from [NSString length] unsigned
  • added comments for NSStringICUAdditions
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/CocoaICU.xcodeproj/project.pbxproj

    r10 r18  
    88 
    99/* 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 */; }; 
    1622/* End PBXBuildFile section */ 
    1723 
     
    2228                939322570B1E6AD000DD0AF0 /* ICUMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ICUMatcher.h; path = source/ICUMatcher.h; sourceTree = "<group>"; }; 
    2329                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; }; 
    2435                93E2C0CE0B18D142001193AC /* CocoaICU.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CocoaICU.m; path = source/CocoaICU.m; sourceTree = "<group>"; }; 
    2536                93E2C0CF0B18D142001193AC /* ICUPattern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ICUPattern.h; path = source/ICUPattern.h; sourceTree = "<group>"; }; 
     
    2738                93E2C0D30B18D142001193AC /* NSStringICUAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSStringICUAdditions.h; path = source/NSStringICUAdditions.h; sourceTree = "<group>"; }; 
    2839                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; }; 
    3040/* End PBXFileReference section */ 
    3141 
    3242/* Begin PBXFrameworksBuildPhase section */ 
    33                 93E2C4EE0B1A53F0001193AC /* Frameworks */ = { 
     43                93AB9E8B0C98A30C00B11910 /* Frameworks */ = { 
    3444                        isa = PBXFrameworksBuildPhase; 
    3545                        buildActionMask = 2147483647; 
    3646                        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 */, 
    3958                        ); 
    4059                        runOnlyForDeploymentPostprocessing = 0; 
     
    4766                        children = ( 
    4867                                93E2C0DE0B18D17D001193AC /* Other */, 
     68                                93AB9E7D0C98A23900B11910 /* Tests */, 
    4969                                08FB7795FE84155DC02AAC07 /* Source */, 
    5070                                08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */, 
    5171                                1AB674ADFE9D54B511CA2CBB /* Products */, 
     72                                93AB9E8F0C98A30C00B11910 /* CocoaICU Tests-Info.plist */, 
    5273                        ); 
    5374                        name = CocoaICU; 
     
    79100                        isa = PBXGroup; 
    80101                        children = ( 
    81                                 93E2C4F00B1A53F0001193AC /* CocoaICU */, 
     102                                93AB9E8E0C98A30C00B11910 /* CocoaICU Tests.octest */, 
     103                                93BC7A0C0C9D563E00181C32 /* ICUTool */, 
    82104                        ); 
    83105                        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; 
    84115                        sourceTree = "<group>"; 
    85116                }; 
     
    96127 
    97128/* Begin PBXNativeTarget section */ 
    98                 93E2C4EF0B1A53F0001193AC /* CocoaICU */ = { 
     129                93AB9E8D0C98A30C00B11910 /* CocoaICU Tests */ = { 
    99130                        isa = PBXNativeTarget; 
    100                         buildConfigurationList = 93E2C5010B1A5403001193AC /* Build configuration list for PBXNativeTarget "CocoaICU" */; 
     131                        buildConfigurationList = 93AB9E900C98A30D00B11910 /* Build configuration list for PBXNativeTarget "CocoaICU Tests" */; 
    101132                        buildPhases = ( 
    102                                 93E2C4ED0B1A53F0001193AC /* Sources */, 
    103                                 93E2C4EE0B1A53F0001193AC /* Frameworks */, 
     133                                93AB9E890C98A30C00B11910 /* Resources */, 
     134                                93AB9E8A0C98A30C00B11910 /* Sources */, 
     135                                93AB9E8B0C98A30C00B11910 /* Frameworks */, 
     136                                93AB9E8C0C98A30C00B11910 /* ShellScript */, 
    104137                        ); 
    105138                        buildRules = ( 
     
    107140                        dependencies = ( 
    108141                        ); 
    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 */; 
    112162                        productType = "com.apple.product-type.tool"; 
    113163                }; 
     
    122172                        projectDirPath = ""; 
    123173                        targets = ( 
    124                                 93E2C4EF0B1A53F0001193AC /* CocoaICU */, 
     174                                93AB9E8D0C98A30C00B11910 /* CocoaICU Tests */, 
     175                                93BC7A0B0C9D563E00181C32 /* ICU Test Program */, 
    125176                        ); 
    126177                }; 
    127178/* End PBXProject section */ 
    128179 
     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 
    129206/* Begin PBXSourcesBuildPhase section */ 
    130                 93E2C4ED0B1A53F0001193AC /* Sources */ = { 
     207                93AB9E8A0C98A30C00B11910 /* Sources */ = { 
    131208                        isa = PBXSourcesBuildPhase; 
    132209                        buildActionMask = 2147483647; 
    133210                        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 */, 
    138226                        ); 
    139227                        runOnlyForDeploymentPostprocessing = 0; 
     
    166254                        name = Release; 
    167255                }; 
    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 */ = { 
    169309                        isa = XCBuildConfiguration; 
    170310                        buildSettings = { 
     
    177317                                INSTALL_PATH = "$(HOME)/bin"; 
    178318                                PREBINDING = NO; 
    179                                 PRODUCT_NAME = CocoaICU
     319                                PRODUCT_NAME = ICUTool
    180320                                ZERO_LINK = YES; 
    181321                        }; 
    182322                        name = Debug; 
    183323                }; 
    184                 93E2C5030B1A5403001193AC /* Release */ = { 
     324                93BC7A9B0C9D569200181C32 /* Release */ = { 
    185325                        isa = XCBuildConfiguration; 
    186326                        buildSettings = { 
     
    191331                                INSTALL_PATH = "$(HOME)/bin"; 
    192332                                PREBINDING = NO; 
    193                                 PRODUCT_NAME = CocoaICU
     333                                PRODUCT_NAME = ICUTool
    194334                                ZERO_LINK = NO; 
    195335                        }; 
     
    208348                        defaultConfigurationName = Release; 
    209349                }; 
    210                 93E2C5010B1A5403001193AC /* Build configuration list for PBXNativeTarget "CocoaICU" */ = { 
     350                93AB9E900C98A30D00B11910 /* Build configuration list for PBXNativeTarget "CocoaICU Tests" */ = { 
    211351                        isa = XCConfigurationList; 
    212352                        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 */, 
    215364                        ); 
    216365                        defaultConfigurationIsVisible = 0; 
  • trunk/source/CocoaICU.m

    r16 r18  
    44#import "NSStringICUAdditions.h" 
    55 
     6void runTest(); 
    67 
    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 
    159int main (int argc, const char * argv[]) { 
    1610    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    1711 
    18         simpleFind(); 
    19         findAndReplaceFirst(); 
    20         findAndReplaceAll(); 
    21         split(); 
    22         unicodeFind(); 
    23         groupFindAndReplace(); 
    24         advancedFindAndReplace(); 
     12        runTest(); 
    2513 
    2614        [pool release]; 
     
    2816} 
    2917 
    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]; 
     18void runTest() { 
    3519         
    36         while([matcher findNext]) { 
    37                 NSRange r = [matcher rangeOfMatch]; 
    38                 NSLog(@"match = %@", [searchString substringWithRange:r]); 
    39         } 
    4020} 
    4121 
    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  
    110110        while(YES) {  
    111111                UErrorCode status = 0; 
    112                 UChar *dest = calloc(groupSize, sizeof(UChar)); 
     112                UChar *dest = (UChar *)NSZoneCalloc([self zone], groupSize, sizeof(UChar)); 
    113113                int32_t buffSize = uregex_group(re, groupIndex, dest, groupSize, &status); 
    114114 
    115115                if(U_BUFFER_OVERFLOW_ERROR == status) { 
    116116                        groupSize *= 2; 
    117                         free(dest); 
     117                        NSZoneFree([self zone], dest); 
    118118                        continue; 
    119119                } 
     
    123123                groupSize = InitialGroupSize; // reset to default 
    124124                NSString *result = [[[NSString alloc] initWithBytes:dest length:buffSize*sizeof(UChar) encoding:[NSString nativeUTF16Encoding]] autorelease]; 
    125                 free(dest); 
     125                NSZoneFree([self zone], dest); 
    126126                return result; 
    127127        } 
     
    150150 
    151151-(NSString *)performReplacementWithString:(NSString *)aReplacementString replaceAll:(BOOL)replacingAll { 
     152 
    152153        UErrorCode status = 0; 
    153154        UChar *replacementText = [aReplacementString UTF16String]; 
    154155        URegularExpression *re = [[self pattern] re]; 
    155         int searchTextLength = [[[self pattern] stringToSearch] length]; 
     156        unsigned int searchTextLength = [[[self pattern] stringToSearch] length]; 
    156157         
    157158        BOOL replacementCompleted = NO; 
     
    161162        while(!replacementCompleted) { 
    162163                 
    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; 
    164172                if(replacingAll) 
    165173                        resultLength = uregex_replaceAll(re, replacementText, -1, destString, destStringBufferSize, &status); 
     
    167175                        resultLength = uregex_replaceFirst(re, replacementText, -1, destString, destStringBufferSize, &status); 
    168176 
     177                // realloc some more space if possible 
    169178                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); 
    176192                        [NSException raise:@"Find Exception" 
    177193                                                format:@"Could not perform find and replace: %s", u_errorName(status)]; 
     
    184200                                                                                                 length:resultLength * sizeof(UChar) 
    185201                                                                                           encoding:[NSString nativeUTF16Encoding]] autorelease]; 
    186         free(destString); 
     202        NSZoneFree([self zone], destString); 
    187203        return result;   
    188204} 
  • trunk/source/ICUPattern.m

    r17 r18  
    8282 
    8383        if(re != NULL) 
    84                 free(re); 
     84                NSZoneFree([self zone], re); 
    8585 
    8686        if(textToSearch != NULL) 
     
    152152-(void)setRe:(URegularExpression *)p { 
    153153        if(re != NULL) 
    154                 free(re); 
     154                NSZoneFree([self zone], re); 
    155155 
    156156        re = p; 
     
    191191 
    192192        while(!isDone) { 
    193                 UChar *destBuf = calloc(destCapacity, sizeof(UChar)); 
     193                UChar *destBuf = (UChar *)NSZoneCalloc([self zone], destCapacity, sizeof(UChar)); 
    194194                int requiredCapacity = 0; 
    195195                UChar *destFields[destFieldsCapacity]; 
     
    201201                                                                                          destFieldsCapacity, 
    202202                                                                                          &status); 
    203  
     203                 
    204204                if(status == U_BUFFER_OVERFLOW_ERROR) { // buffer was too small, grow it 
    205                         free(destBuf); 
     205                        NSZoneFree([self zone], destBuf); 
    206206                        NSAssert(destCapacity * 2 < INT_MAX, @"Overflow occurred splitting string."); 
    207207                        destCapacity = (destCapacity < requiredCapacity) ? requiredCapacity : destCapacity * 2; 
     
    210210                        destFieldsCapacity *= 2; 
    211211                        NSAssert(destFieldsCapacity *2 < INT_MAX, @"Overflow occurred splitting string."); 
    212                         free(destBuf); 
     212                        NSZoneFree([self zone], destBuf); 
    213213                        status = 0; 
    214214                } else if(U_FAILURE(status)) { 
    215                         free(destBuf); 
     215                        NSZoneFree([self zone], destBuf); 
    216216                        isDone = YES; 
    217217                } else { 
  • trunk/source/NSStringICUAdditions.h

    r17 r18  
    1212@interface NSString (NSStringICUAdditions) 
    1313 
     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*/ 
    1419+(NSStringEncoding)nativeUTF16Encoding; 
    1520 
     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*/ 
    1626+(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*/ 
    1735-(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*/ 
    1844-(void *)copyUTF16String; 
    1945 
    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*/ 
    2453-(NSArray *)findPattern:(NSString *)aRegex; 
    2554 
     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 
    2678@end 
  • trunk/source/NSStringICUAdditions.m

    r17 r18  
    2727@end 
    2828 
    29 @implementation NSString (NSStringICUAdditions)// 
     29@implementation NSString (NSStringICUAdditions) 
    3030 
    3131-(NSString *)replaceOccurrencesOfPattern:(NSString *)aPattern withString:(NSString *)replacementText { 
     
    7878-(void *)UTF16String { 
    7979        UChar *ret = (UChar *)[self cStringUsingEncoding:[NSString nativeUTF16Encoding]]; 
    80  
     80         
    8181        // for some reason, the null-terminator doesn't always show up at the right place and this 
    8282        // 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]; 
    8484        ret[len] = '\0'; 
    8585        return ret;