2 // NSString+FixFilename.m
5 // Created by Joe Wreschnig on 3/7/13.
9 #import "NSString+FixFilename.h"
11 @implementation NSCharacterSet (FixFilename)
13 + (NSCharacterSet *)invalidPathComponentCharacterSet {
14 return [NSCharacterSet characterSetWithCharactersInString:@"\"\\/:*?<>|"];
19 @implementation NSString (FixFilename)
21 - (NSString *)stringByFixingPathComponent {
22 NSCharacterSet *invalid = NSCharacterSet.invalidPathComponentCharacterSet;
23 NSCharacterSet *whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet;
24 NSArray *parts = [self componentsSeparatedByCharactersInSet:invalid];
25 NSString *name = [parts componentsJoinedByString:@"_"];
26 name = [name stringByTrimmingCharactersInSet:whitespace];
29 unichar first = [name characterAtIndex:0];
30 if (first == '.' || first == '-')
31 name = [@"_" stringByAppendingString:name];