@@ -28,27 +28,22 @@ - init:(const char*)str { if ((self = [super init])) { if (str == NULL) { - string = NULL; length = 0; + string = NULL; } else { - string = strdup(str); length = strlen(string); + if ((string = [self getMem:length]) == NULL) + return NULL; + memcpy(string, str, length); } } return self; } -- free -{ - if (string != NULL) - free(string); - return [super free]; -} - - (char*)cString { return string; } @@ -55,47 +50,67 @@ - (size_t)length { return length; } -- (void)setTo:(const char*)str +- (OFString*)setTo:(const char*)str { + char *newstr; + size_t newlen; + + if (str == NULL) { + [self freeMem:string]; + + length = 0; + string = NULL; + + return self; + } + + newlen = strlen(str); + if ((newstr = [self getMem:newlen]) == NULL) + return nil; + memcpy(newstr, str, newlen); + if (string != NULL) - free(string); + [self freeMem:string]; + + length = newlen; + string = newstr; - string = strdup(str); - length = strlen(str); + return self; } - (OFString*)clone { if (string != NULL) return [OFString new:string]; return [OFString new]; } -- (void)append: (const char*)str +- (OFString*)append: (const char*)str { char *new_string; size_t new_length, str_length; - if (str == NULL) { - [self setTo:str]; - return; - } + if (str == NULL) + return [self setTo:str]; str_length = strlen(str); new_length = length + str_length; - if ((new_string = realloc(string, new_length + 1)) == NULL) { + if ((new_string = + [self resizeMem:string toSize:new_length + 1]) == NULL) { /* FIXME: Add error handling */ - return; + return nil; } string = new_string; memcpy(string + length, str, str_length); string[new_length] = '\0'; length = new_length; + + return self; } @end