5 // Created by Christian Brunschen on 25/06/2008.
6 // Copyright 2008 Google Inc. All rights reserved.
9 #import "SMSResultParser.h"
10 #import "SMSParsedResult.h"
12 #define PREFIX @"sms:"
14 @implementation SMSResultParser
16 + (ParsedResult *)parsedResultForString:(NSString *)s {
17 NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
18 if (prefixRange.location == 0) {
20 int restStart = prefixRange.location + prefixRange.length;
22 // initial presuption: everything after the prefix is the number, and there is no body
23 NSRange numberRange = NSMakeRange(restStart, max - restStart);
24 NSRange bodyRange = NSMakeRange(NSNotFound, 0);
26 // is there a query string?
27 NSRange queryRange = [s rangeOfString:@"?" options:0 range:numberRange];
28 if (queryRange.location != NSNotFound) {
29 // truncate the number range at the beginning of the query string
30 numberRange.length = queryRange.location - numberRange.location;
32 int paramsStart = queryRange.location + queryRange.length;
33 NSRange paramsRange = NSMakeRange(paramsStart, max - paramsStart);
34 NSRange bodyPrefixRange = [s rangeOfString:@"body=" options:0 range:paramsRange];
35 if (bodyPrefixRange.location != NSNotFound) {
36 int bodyStart = bodyPrefixRange.location + bodyPrefixRange.length;
37 bodyRange = NSMakeRange(bodyStart, max - bodyStart);
38 NSRange ampRange = [s rangeOfString:@"&" options:0 range:bodyRange];
39 if (ampRange.location != NSNotFound) {
40 // we found a '&', so we truncate the body range there
41 bodyRange.length = ampRange.location - bodyRange.location;
46 // if there's a semicolon in the number, truncate the number there
47 NSRange semicolonRange = [s rangeOfString:@";" options:0 range:numberRange];
48 if (semicolonRange.location != NSNotFound) {
49 numberRange.length = semicolonRange.location - numberRange.location;
52 NSString *number = [s substringWithRange:numberRange];
53 NSString *body = bodyRange.location != NSNotFound ? [s substringWithRange:bodyRange] : nil;
54 return [[[SMSParsedResult alloc] initWithNumber:number body:body]