root/trunk/source/NSStringICUAdditions.m

Revision 18, 2.7 kB (checked in by aaron, 1 year ago)
  • 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
Line 
1 //
2 //  NSStringICUAdditions.m
3 //  CocoaICU
4 //
5 //  Created by Aaron Evans on 11/19/06.
6 //  Copyright 2006 Aaron Evans. All rights reserved.
7 //
8
9 #import "NSStringICUAdditions.h"
10 #import "ICUPattern.h"
11 #import "ICUMatcher.h"
12
13 struct URegularExpression;
14 /**
15 * Structure represeting a compiled regular rexpression, plus the results
16  *    of a match operation.
17  * @draft ICU 3.0
18  */
19 typedef struct URegularExpression URegularExpression;
20
21 #define U_HIDE_DRAFT_API 1
22 #define U_DISABLE_RENAMING 1
23 #import <unicode/uregex.h>
24
25 @interface NSString (NSStringICUPrivateAdditions)
26
27 @end
28
29 @implementation NSString (NSStringICUAdditions)
30
31 -(NSString *)replaceOccurrencesOfPattern:(NSString *)aPattern withString:(NSString *)replacementText {
32         ICUPattern *p = [ICUPattern patternWithString:aPattern];
33         ICUMatcher *m = [ICUMatcher matcherWithPattern:p overString:self];
34         return [m replaceAllWithString:replacementText];
35 }
36
37 -(BOOL)matchesPattern:(NSString *)aRegex {
38         ICUPattern *p = [ICUPattern patternWithString:aRegex];
39         ICUMatcher *m = [ICUMatcher matcherWithPattern:p overString:self];
40         return [m matches];
41 }
42
43 -(NSArray *)findPattern:(NSString *)aRegex {
44         ICUPattern *p = [ICUPattern patternWithString:aRegex];
45         ICUMatcher *matcher = [ICUMatcher matcherWithPattern:p overString:self];
46         NSMutableArray *foundGroups = [NSMutableArray array];
47        
48         [matcher findFromIndex:0];     
49         int i;
50         for(i=0;i<=[matcher numberOfGroups];i++)
51                 [foundGroups addObject:[matcher groupAtIndex:i]];
52
53         return [NSArray arrayWithArray:foundGroups];
54 }
55
56 -(NSArray *)componentsSeparatedByPattern:(NSString *)aRegex {
57         ICUPattern *p = [ICUPattern patternWithString:aRegex];
58         return [p componentsSplitFromString:self];
59 }
60
61 +(NSString *)stringWithICUString:(void *)utf16EncodedString {
62         return [[[NSString alloc] initWithBytes:utf16EncodedString
63                                                                          length:u_strlen(utf16EncodedString)*sizeof(UChar)
64                                                                    encoding:[self nativeUTF16Encoding]] autorelease];   
65 }
66
67 +(NSStringEncoding)nativeUTF16Encoding {
68         CFStringEncoding stringEncoding;
69 #if __BIG_ENDIAN__
70         stringEncoding = kCFStringEncodingUTF16BE;
71 #elif __LITTLE_ENDIAN__
72         stringEncoding = kCFStringEncodingUTF16LE;
73 #endif
74
75         return CFStringConvertEncodingToNSStringEncoding(stringEncoding);
76 }
77
78 -(void *)UTF16String {
79         UChar *ret = (UChar *)[self cStringUsingEncoding:[NSString nativeUTF16Encoding]];
80        
81         // for some reason, the null-terminator doesn't always show up at the right place and this
82         // causes extra characters to be created in the unicode string.  We remove them here by force.
83         unsigned int len = [self length];
84         ret[len] = '\0';
85         return ret;     
86 }
87
88 -(void *)copyUTF16String {
89         unsigned int length = [self length];
90         UChar *utf16String = malloc((length+1)*sizeof(UChar));
91         [self getCharacters: utf16String];
92         utf16String[length] = 0;
93         return utf16String;
94 }
95
96 @end
Note: See TracBrowser for help on using the browser.