forked from KosmicTask/Fragaria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSStringICUAdditions.m
95 lines (77 loc) · 2.65 KB
/
NSStringICUAdditions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// NSStringICUAdditions.m
// CocoaICU
//
// Created by Aaron Evans on 11/19/06.
// Copyright 2006 Aaron Evans. All rights reserved.
//
#import "NSStringICUAdditions.h"
#import "ICUPattern.h"
#import "ICUMatcher.h"
struct URegularExpression;
/**
* Structure representing a compiled regular rexpression, plus the results
* of a match operation.
* @draft ICU 3.0
*/
typedef struct URegularExpression URegularExpression;
#define U_HIDE_DRAFT_API 1
#define U_DISABLE_RENAMING 1
#import <unicode/uregex.h>
#import <unicode/ustring.h>
@interface NSString (NSStringICUPrivateAdditions)
@end
@implementation NSString (NSStringICUAdditions)
-(NSString *)replaceOccurrencesOfPattern:(NSString *)aPattern withString:(NSString *)replacementText {
ICUPattern *p = [ICUPattern patternWithString:aPattern];
ICUMatcher *m = [ICUMatcher matcherWithPattern:p overString:self];
return [m replaceAllWithString:replacementText];
}
-(BOOL)matchesPattern:(NSString *)aRegex {
ICUPattern *p = [ICUPattern patternWithString:aRegex];
ICUMatcher *m = [ICUMatcher matcherWithPattern:p overString:self];
return [m matches];
}
-(NSArray *)findPattern:(NSString *)aRegex {
ICUPattern *p = [ICUPattern patternWithString:aRegex];
ICUMatcher *matcher = [ICUMatcher matcherWithPattern:p overString:self];
NSMutableArray *foundGroups = [NSMutableArray array];
[matcher findFromIndex:0];
unsigned i;
for(i=0;i<=[matcher numberOfGroups];i++)
[foundGroups addObject:[matcher groupAtIndex:i]];
return [NSArray arrayWithArray:foundGroups];
}
-(NSArray *)componentsSeparatedByPattern:(NSString *)aRegex {
ICUPattern *p = [ICUPattern patternWithString:aRegex];
return [p componentsSplitFromString:self];
}
+(NSString *)stringWithICUString:(void *)utf16EncodedString {
return [[NSString alloc] initWithBytes:utf16EncodedString
length:u_strlen(utf16EncodedString)*sizeof(UChar)
encoding:[self nativeUTF16Encoding]];
}
+(NSStringEncoding)nativeUTF16Encoding {
CFStringEncoding stringEncoding;
#if __BIG_ENDIAN__
stringEncoding = kCFStringEncodingUTF16BE;
#elif __LITTLE_ENDIAN__
stringEncoding = kCFStringEncodingUTF16LE;
#endif
return CFStringConvertEncodingToNSStringEncoding(stringEncoding);
}
-(void *)UTF16String {
NSUInteger length = [self length];
UChar *utf16String = NSAllocateCollectable((length+1)*sizeof(UChar), 0);
[self getCharacters:utf16String range:NSMakeRange(0, length)];
utf16String[length] = 0;
return utf16String;
}
-(void *)copyUTF16String {
NSUInteger length = [self length];
UChar *utf16String = malloc((length+1)*sizeof(UChar));
[self getCharacters:utf16String range:NSMakeRange(0, length)];
utf16String[length] = 0;
return utf16String;
}
@end