NSRegularExpression
文字列を解凍するために使用します。@"A" を @",-1" に、@"B" を @",-2" に転送して解凍します。
問題は、@"A" to @",-1" or @"B" to @",-2"
個別に適用するだけで問題なく動作することです。しかし、 for ループで両方を適用すると、enumerateMatchesInString:options:range:usingBlock
search のときに一致が見つからないことがわかりました@"B"
。何か案は?
NSString *compressed = @"[303b18c01a,Ac24a6,Aa6,,Ah,A,a1,,,a8,b,c5,aad,bcg,dha9,fa4a7,ib4a9,da4a5,ca1a6,aha7,aea6,,aa6,BCa8,]";
NSMutableString *compressedCopy = [compressed mutableCopy];
//the array contains two patterns
NSArray *regulars = @[[self regexO:@"A" n:@",-1"],
[self regexO:@"B" n:@",-2"]];
NSInteger count = [regulars count];
for (int i = 0; i < count; i++) {
NSDictionary *regexInfo = regulars[i];
NSString *old = regexInfo[REGEX_OLD_KEY];
NSString *new = regexInfo[REGEX_NEW_KEY];
NSString *origin = [compressedCopy copy];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:old options:0 error:nil];
if (regex) {
__block int offset = 0;
[regex enumerateMatchesInString:origin options:0 range:NSMakeRange(0, [compressed length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange range = [result range];
range.location += offset;
[compressedCopy replaceCharactersInRange:range withString:new];
offset += [new length] - [old length];
}];
}
続いてパターンを保存します
- (NSDictionary *)regexO:(NSString *)old n:(NSString *)new
{
NSDictionary *regex = @{REGEX_OLD_KEY: old, REGEX_NEW_KEY: new};
return regex;
}
-------------------------- より明確にするために ------------------------------ ------
原点データは、次のような多くのポイント座標を含む配列です。
[131,61,648,1,1,-1,0]
私が処理する必要がある圧縮データは、-> NSString *compressed のようなものです。
解凍のアルゴリズムは、"A" を @",-1" に変更するなど、圧縮された文字列の文字を変更することです。