Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (57 sloc) 2.36 KB
//
// NSString+roobasoft.m
// Copyright (c) 2008 roobasoft, LLC
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#import "NSString+roobasoft.h"
@implementation NSString(roobasoft)
+ (NSString *) stringWithUUID
{
CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID
// get the string representation of the UUID
NSString *newUUID = (NSString*)CFUUIDCreateString(nil, uuidObj);
CFRelease(uuidObj);
return [newUUID autorelease];
}
- (NSString *)urlEncode
{
NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("?=&+\" "), kCFStringEncodingUTF8);
return [result autorelease];
}
// assumes self is in form
// 2008-10-29T06:22:08-0600
// 2009-01-02T15:40:19Z
- (NSDate *) dateFromJSON
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"y-M-d H:m:sZ"];
NSDate *ret = [formatter dateFromString:[[self stringByReplacingOccurrencesOfString:@"T" withString:@" "] stringByReplacingOccurrencesOfString:@"Z" withString:@"+0000"]];
[formatter release];
return ret;
}
- (NSString *) truncate:(NSInteger)size
{
if ([self length] <= size)
return self;
return [NSString stringWithFormat:@"%@...", [[self substringToIndex:size-3] stringByReplacingOccurrencesOfString:@"\n" withString:@" "]];
}
@end