Index: src/OFINICategory.m ================================================================== --- src/OFINICategory.m +++ src/OFINICategory.m @@ -20,12 +20,13 @@ #include "config.h" #import "OFINICategory.h" #import "OFINICategory+Private.h" #import "OFArray.h" -#import "OFString.h" +#import "OFCharacterSet.h" #import "OFStream.h" +#import "OFString.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" @interface OFINICategoryPair: OFObject @@ -40,20 +41,21 @@ @public OFString *_comment; } @end +static OFCharacterSet *needsEscapeCharacterSet; + static OFString * escapeString(OFString *string) { OFMutableString *mutableString; - /* FIXME: Optimize */ - if (![string hasPrefix: @" "] && ![string hasPrefix: @"\t"] && - ![string hasPrefix: @"\f"] && ![string hasSuffix: @" "] && - ![string hasSuffix: @"\t"] && ![string hasSuffix: @"\f"] && - ![string containsString: @"\""] && ![string containsString: @"="]) + if (![string hasPrefix: @" "] && ![string hasSuffix: @" "] && + ![string hasPrefix: @"\t"] && ![string hasSuffix: @"\t"] && + [string indexOfCharacterFromSet: needsEscapeCharacterSet] == + OFNotFound) return string; mutableString = [[string mutableCopy] autorelease]; [mutableString replaceOccurrencesOfString: @"\\" withString: @"\\\\"]; @@ -99,10 +101,19 @@ } @end @implementation OFINICategory @synthesize name = _name; + ++ (void)initialize +{ + if (self != [OFINICategory class]) + return; + + needsEscapeCharacterSet = [[OFCharacterSet alloc] + initWithCharactersInString: @"\r\n\f\"\\="]; +} - (instancetype)of_initWithName: (OFString *)name OF_DIRECT { self = [super init]; Index: tests/OFINIFileTests.m ================================================================== --- tests/OFINIFileTests.m +++ tests/OFINIFileTests.m @@ -122,13 +122,13 @@ @"\r\n" @"[foobar]\r\n" @"#foobarcomment\r\n" @"qux=\" asd\"\r\n" @"quxquxqux=\"hello\\\"wörld\"\r\n" - @"qux2=\"a\\f\"\r\n" + @"qux2=\"a\\n\"\r\n" @"\"asd=asd\"=foobar\r\n" - @"qux3=a\fb\r\n" + @"qux3=\"a\\fb\"\r\n" @"\r\n" @"[types]\r\n" @"integer=16\r\n" @"bool=false\r\n" @"float=0.25\r\n" Index: tests/testfile.ini ================================================================== --- tests/testfile.ini +++ tests/testfile.ini @@ -9,11 +9,11 @@ [foobar] #foobarcomment qux=" asd" "quxqux " = asd quxquxqux="hello\"wörld" -qux2="a\f" +qux2="a\n" "asd=asd"=foobar [types] integer = 0x20 bool = true