2 // DoCoMoResultParser.m
5 // Created by Christian Brunschen on 25/06/2008.
7 * Copyright 2008 ZXing authors
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 #import "DoCoMoResultParser.h"
25 @implementation NSString (DoCoMoFieldParsing)
27 - (NSString *)backslashUnescaped {
28 NSRange backslashRange = [self rangeOfString:@"\\"];
29 if (backslashRange.location == NSNotFound) {
33 int max = [self length];
34 int startLocation = 0;
35 NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
36 while (backslashRange.location != NSNotFound) {
37 [result appendString:[self substringWithRange:NSMakeRange(startLocation,
38 backslashRange.location - startLocation)]];
39 [result appendFormat:@"%c", [self characterAtIndex:backslashRange.location + 1]];
40 startLocation = backslashRange.location + 2;
41 NSRange searchRange = NSMakeRange(startLocation, max - startLocation);
42 backslashRange = [self rangeOfString:@"\\" options:0 range:searchRange];
44 if (startLocation < max) {
45 [result appendString:[self substringWithRange:NSMakeRange(startLocation, max - startLocation)]];
47 return [NSString stringWithString:result];
50 - (NSArray *)fieldsWithPrefix:(NSString *)prefix {
51 return [self fieldsWithPrefix:prefix terminator:@";"];
54 - (NSArray *)fieldsWithPrefix:(NSString *)prefix terminator:(NSString *)term {
55 NSMutableArray *result = nil;
58 int max = [self length];
62 searchRange = NSMakeRange(i, max - i);
63 foundRange = [self rangeOfString:prefix options:0 range:searchRange];
64 if(foundRange.location == NSNotFound) {
68 int start = i = foundRange.location + foundRange.length;
71 searchRange = NSMakeRange(i, max - i);
72 NSRange termRange = [self rangeOfString:term options:0 range:searchRange];
73 if (termRange.location == NSNotFound) {
76 } else if ([self characterAtIndex:termRange.location-1] == (unichar)'\\') {
79 NSString *substring = [self substringWithRange:NSMakeRange(start, termRange.location - start)];
80 NSString *unescaped = [substring backslashUnescaped];
82 result = [NSMutableArray arrayWithObject:unescaped];
84 [result addObject:unescaped];
86 i = termRange.location + termRange.length;
95 - (NSString *)fieldWithPrefix:(NSString *)prefix {
96 return [self fieldWithPrefix:prefix terminator:@";"];
99 - (NSString *)fieldWithPrefix:(NSString *)prefix terminator:(NSString *)term {
100 NSArray *fields = [self fieldsWithPrefix:prefix terminator:term];
101 if (fields.count == 0) {
104 return [fields lastObject];
112 @implementation DoCoMoResultParser