Index: src/OFMutableString.m ================================================================== --- src/OFMutableString.m +++ src/OFMutableString.m @@ -132,17 +132,22 @@ length = newLength; } - (void)setToCString: (const char*)string_ { - size_t len; + size_t length_; [self freeMemory: string]; - len = strlen(string_); + length_ = strlen(string_); - switch (of_string_check_utf8(string_, len)) { + if (length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) { + string_ += 3; + length_ -= 3; + } + + switch (of_string_check_utf8(string_, length_)) { case 0: isUTF8 = NO; break; case 1: isUTF8 = YES; @@ -153,38 +158,46 @@ isUTF8 = NO; @throw [OFInvalidEncodingException newWithClass: isa]; } - length = len; + length = length_; string = [self allocMemoryWithSize: length + 1]; memcpy(string, string_, length + 1); } - (void)appendCString: (const char*)string_ { - size_t len; + size_t length_ = strlen(string_); - len = strlen(string_); + if (length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) { + string_ += 3; + length_ -= 3; + } - switch (of_string_check_utf8(string_, len)) { + switch (of_string_check_utf8(string_, length_)) { case 1: isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } string = [self resizeMemory: string - toSize: length + len + 1]; - memcpy(string + length, string_, len + 1); - length += len; + toSize: length + length_ + 1]; + memcpy(string + length, string_, length_ + 1); + length += length_; } - (void)appendCString: (const char*)string_ withLength: (size_t)length_ { + if (length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) { + string_ += 3; + length_ -= 3; + } + switch (of_string_check_utf8(string_, length_)) { case 1: isUTF8 = YES; break; case -1: Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -141,12 +141,12 @@ @"file://testfile.txt"] encoding: OF_STRING_ENCODING_ISO_8859_1]) && [s[1] isEqual: @"testäöü"]) TEST(@"-[appendCStringWithLength:]", - R([s[0] appendCString: "foobarqux" + 3 - withLength: 3]) && [s[0] isEqual: @"foobar"]) + R([s[0] appendCString: "foo\xEF\xBB\xBF" "barqux" + 3 + withLength: 6]) && [s[0] isEqual: @"foobar"]) EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1", OFInvalidEncodingException, [OFString stringWithCString: "\xE0\x80"]) EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #2",