Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -30,13 +30,13 @@ if (str == NULL) { length = 0; string = NULL; } else { length = strlen(str); - if ((string = [self getMem: length]) == NULL) + if ((string = [self getMem: length + 1]) == NULL) return NULL; - memcpy(string, str, length); + memcpy(string, str, length + 1); } } return self; } @@ -63,13 +63,13 @@ return self; } newlen = strlen(str); - if ((newstr = [self getMem: newlen]) == NULL) + if ((newstr = [self getMem: newlen + 1]) == NULL) return nil; - memcpy(newstr, str, newlen); + memcpy(newstr, str, newlen + 1); if (string != NULL) [self freeMem: string]; length = newlen; @@ -97,15 +97,13 @@ /* FIXME: Add error handling */ if ((newstr = [self resizeMem: string toSize: newlen + 1]) == NULL) return nil; - string = newstr; - - memcpy(string + length, str, strlength); - string[newlen] = '\0'; + memcpy(newstr + length, str, strlength + 1); length = newlen; + string = newstr; return self; } @end Index: src/OFWideString.m ================================================================== --- src/OFWideString.m +++ src/OFWideString.m @@ -31,14 +31,14 @@ if (wstr == NULL) { length = 0; wstring = NULL; } else { length = wcslen(wstr); - if ((wstring = - [self getMem: length * sizeof(wchar_t)]) == NULL) + if (NULL == (wstring = + [self getMem: (length + 1) * sizeof(wchar_t)])) return NULL; - memcpy(wstring, wstr, length * sizeof(wchar_t)); + memcpy(wstring, wstr, (length + 1) * sizeof(wchar_t)); } } return self; } @@ -65,13 +65,13 @@ return self; } newlen = wcslen(wstr); - if ((newstr = [self getMem: newlen * sizeof(wchar_t)]) == NULL) + if ((newstr = [self getMem: (newlen + 1) * sizeof(wchar_t)]) == NULL) return nil; - memcpy(newstr, wstr, newlen * sizeof(wchar_t)); + memcpy(newstr, wstr, (newlen + 1) * sizeof(wchar_t)); if (wstring != NULL) [self freeMem: wstring]; length = newlen; @@ -96,19 +96,18 @@ strlength = wcslen(wstr); newlen = length + strlength; /* FIXME: Add error handling */ if ((newstr = [self resizeMem: wstring - toSize: newlen * sizeof(wchar_t) + 2]) == NULL) + toSize: (newlen + 1 ) * sizeof(wchar_t)]) == + NULL) return nil; - wstring = newstr; - - memcpy(wstring + length * sizeof(wchar_t), wstr, - strlength * sizeof(wchar_t)); - wstring[newlen] = '\0'; + memcpy(newstr + length * sizeof(wchar_t), wstr, + (strlength + 1) * sizeof(wchar_t)); length = newlen; + wstring = newstr; return self; } @end Index: tests/OFWideString/OFWideString.m ================================================================== --- tests/OFWideString/OFWideString.m +++ tests/OFWideString/OFWideString.m @@ -7,11 +7,11 @@ * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ -#import +#define _ISOC99_SOURCE #import #import "OFWideString.h" /* TODO: Do real checks */ @@ -22,11 +22,11 @@ OFWideString *s1 = [OFWideString new: L"foo"]; OFWideString *s2 = [[OFWideString alloc] init: L""]; OFWideString *s3; OFWideString *s4 = [OFWideString new]; - printf("%p\n", [s2 append: L"bar"]); + [s2 append: L"bar"]; s3 = [s1 clone]; [s4 setTo: [s2 wcString]]; wprintf(L"s1 = %S\n", [s1 wcString]); @@ -34,14 +34,14 @@ wprintf(L"s3 = %S\n", [s3 wcString]); wprintf(L"s4 = %S\n", [s4 wcString]); [s1 append: [s2 wcString]]; wprintf(L"s1 append s2 = %S\n", [s1 wcString]); - wprintf(L"strlen(s1) = %zd, [s1 length] = %zd\n", + wprintf(L"wcslen(s1) = %zd, [s1 length] = %zd\n", wcslen([s1 wcString]), [s1 length]); [s1 free]; [s2 free]; [s3 free]; [s4 free]; return 0; }