ObjFW  Check-in [0256ab4f09]

Overview
Comment:OFString uses OFObject's getMem: and resizeMem:toSize: now.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0256ab4f0931fec5cea25356ffb535c762a2ddcd4d566ee6b8151a7c0e0c128a
User & Date: js on 2008-09-12 18:21:50
Other Links: manifest | tags
Context
2008-09-12
18:26
Add OFConstString. check-in: a566e7bb03 user: js tags: trunk
18:21
OFString uses OFObject's getMem: and resizeMem:toSize: now. check-in: 0256ab4f09 user: js tags: trunk
17:56
Fix printf, add missing files. check-in: 958016704c user: js tags: trunk
Changes

Modified src/OFString.h from [ceca91fa6c] to [9ae2f04448].

17
18
19
20
21
22
23
24
25
26
27

28
29

30
31
32
17
18
19
20
21
22
23

24
25

26
27

28
29
30
31







-


-
+

-
+



	char	*string;
	size_t	length;
}

+ new:(const char*)str;
- init;
- init:(const char*)str;
- free;
- (char*)cString;
- (size_t)length;
- (void)setTo:(const char*)str;
- (OFString*)setTo:(const char*)str;
- (OFString*)clone;
- (void)append:(const char*)str;
- (OFString*)append:(const char*)str;
@end

/* vim: se syn=objc: */

Modified src/OFString.m from [53999e7524] to [c26b0e5b15].

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
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
112

113
114
115
116







-

+

-

+
+
+





-
-
-
-
-
-
-










-
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+

+
-
-
+
+
+









-
+




-
-
+
+
-
-




-
+
+

-
+








-
+
+
+

	return [self init:NULL];
}

- 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;
}

- (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 = strdup(str);
	length = strlen(str);
	string = newstr;

	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];
	if (str == NULL)
		return [self setTo:str];
		return;
	}

	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