ObjFW  Diff

Differences From Artifact [7b8481ed3a]:

To Artifact [52bd147dc6]:


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98


99
100
101
102
103
104
105
106
107
108
109
110
111
 */

#import <stdlib.h>
#import <string.h>
#import "OFString.h"

@implementation OFString
+ new:(const char*)str
{
	return [[OFString alloc] init:str];
}

- init
{
	return [self init:NULL];
}

- init:(const char*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = strlen(str);
			if ((string = [self getMem:length]) == NULL)
				return NULL;
			memcpy(string, str, length);
		}
	}
	return self;
}

- (char*)cString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (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)
		[self freeMem:string];

	length = newlen;
	string = newstr;

	return self;
}

- (OFString*)clone
{
	return [OFString new:string];
}

- (OFString*)append: (const char*)str
{
	char	*newstr;
	size_t	newlen, strlength;

	if (str == NULL)
		return [self setTo:str];

	strlength = strlen(str);
	newlen = length + strlength;

	if ((newstr = [self resizeMem:string toSize:newlen + 1]) == NULL) {
		/* FIXME: Add error handling */


		return nil;
	}

	string = newstr;

	memcpy(string + length, str, strlength);
	string[newlen] = '\0';

	length = newlen;

	return self;
}
@end







|

|




|


|







|

















|





|








|




|









|




|
|







<
|
>
>

<











10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

97
98
99
100

101
102
103
104
105
106
107
108
109
110
111
 */

#import <stdlib.h>
#import <string.h>
#import "OFString.h"

@implementation OFString
+ new: (const char*)str
{
	return [[OFString alloc] init: str];
}

- init
{
	return [self init: NULL];
}

- init: (const char*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = strlen(str);
			if ((string = [self getMem: length]) == NULL)
				return NULL;
			memcpy(string, str, length);
		}
	}
	return self;
}

- (char*)cString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (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)
		[self freeMem: string];

	length = newlen;
	string = newstr;

	return self;
}

- (OFString*)clone
{
	return [OFString new: string];
}

- (OFString*)append: (const char*)str
{
	char   *newstr;
	size_t newlen, strlength;

	if (str == NULL)
		return [self setTo:str];

	strlength = strlen(str);
	newlen = length + strlength;


	/* 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';

	length = newlen;

	return self;
}
@end