Skip to content

Instantly share code, notes, and snippets.

@pxpgraphics
Created January 20, 2015 19:00
Show Gist options
  • Save pxpgraphics/9d7495a25e3903ff91ff to your computer and use it in GitHub Desktop.
Save pxpgraphics/9d7495a25e3903ff91ff to your computer and use it in GitHub Desktop.
- (NSString *)normalizeString:(NSString *)string
{
NSString *normalizedString = [[string copy] stringByStandardizingPath]; // copy to keep thread-safe;
if ([normalizedString hasPrefix:@".."]) {
normalizedString = [normalizedString substringFromIndex:2];
}
if ([normalizedString containsString:@"/../"]) {
NSArray *components = [normalizedString componentsSeparatedByString:@"/../"];
NSMutableArray *tempComponents = [components mutableCopy];
for (NSString *component in components) {
if (![component containsString:@"/"]) {
continue;
}
NSRange range = [component rangeOfString:@"/"];
NSString *string = [component substringToIndex:range.location];
[tempComponents replaceObjectAtIndex:[components indexOfObject:component] withObject:string];
}
components = [tempComponents copy];
normalizedString = [components componentsJoinedByString:@"/"];
}
return normalizedString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment