ObjFW  Check-in [c7f702cc69]

Overview
Comment:API change.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c7f702cc69ffce15e80e18d3bbf884271ec0deb33b88fb99fc7c1fd68c507b70
User & Date: js on 2008-09-15 11:02:02
Other Links: manifest | tags
Context
2008-10-05
19:20
Reworked String API. check-in: cf85bee74f user: js tags: trunk
2008-09-15
11:02
API change. check-in: c7f702cc69 user: js tags: trunk
10:23
Add compare: for strings. check-in: 24ce530f56 user: js tags: trunk
Changes

Modified src/OFString.h from [669d4151c7] to [236184ed79].

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21
22
23
24
25
26

27
28
29


30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

27
28


29
30
31













+












-
+

-
-
+
+

/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * 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 <stddef.h>
#import "OFObject.h"
#import "OFConstString.h"

@interface OFString: OFObject
{
	char   *string;
	size_t length;
}

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

Modified src/OFString.m from [b274f069db] to [68b2726756].

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







-
+




-
+








-
+

-
+















-
+





-
+

-
+





-
+







-
+




}

- (size_t)length
{
	return length;
}

- (OFString*)setTo: (const char*)str
- (OFString*)setTo: (OFConstString*)str
{
	char *newstr;
	size_t newlen;
	
	if (str == NULL) {
	if ([str cString] == NULL) {
		[self freeMem: string];

		length = 0;
		string = NULL;

		return self;
	}

	newlen = strlen(str);
	newlen = [str length];
	newstr = [self getMem: newlen + 1];
	memcpy(newstr, str, newlen + 1);
	memcpy(newstr, [str cString], newlen + 1);

	if (string != NULL)
		[self freeMem: string];

	length = newlen;
	string = newstr;

	return self;
}

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

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

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

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

	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);
	memcpy(newstr + length, [str cString], strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

- (int)compare: (OFString*)str
- (int)compare: (OFConstString*)str
{
	return strcmp(string, [str cString]);
}
@end

Modified src/OFWideString.h from [ca4e9f67ee] to [fa40106b5c].

1
2
3
4
5
6
7
8
9
10
11
12
13

14

15
16
17
18
19
20
21
22
23
24
25
26
27

28
29
30


31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

29
30


31
32
33













+

+












-
+

-
-
+
+

/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * 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 <stddef.h>
#import <wchar.h>

#import "OFObject.h"
#import "OFConstWideString.h"

@interface OFWideString: OFObject
{
	wchar_t	*wstring;
	size_t	length;
}

+ new: (const wchar_t*)wstr;
- init;
- init: (const wchar_t*)wstr;
- (wchar_t*)wcString;
- (size_t)length;
- (OFWideString*)setTo: (const wchar_t*)wstr;
- (OFWideString*)setTo: (OFConstWideString*)wstr;
- (OFWideString*)clone;
- (OFWideString*)append: (const wchar_t*)wstr;
- (int)compare: (OFWideString*)str;
- (OFWideString*)append: (OFConstWideString*)wstr;
- (int)compare: (OFConstWideString*)str;
@end

Modified src/OFWideString.m from [1b2ac19bc0] to [ffeec9ddfb].

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







-
+















-
+




-
+








-
+

-
+















-
+




-
+


-
+





-
+







-
+




	if ((self = [super init])) {
		if (wstr == NULL) {
			length = 0;
			wstring = NULL;
		} else {
			length = wcslen(wstr);
			wstring = [self getMem: (length + 1) * sizeof(wchar_t)];
			memcpy(wstring, wstr, (length + 1) * sizeof(wchar_t));
			wmemcpy(wstring, wstr, length + 1);
		}
	}
	return self;
}

- (wchar_t*)wcString
{
	return wstring;
}

- (size_t)length
{
	return length;
}

- (OFWideString*)setTo: (const wchar_t*)wstr
- (OFWideString*)setTo: (OFConstWideString*)wstr
{
	wchar_t *newstr;
	size_t  newlen;
	
	if (wstr == NULL) {
	if ([wstr wcString] == NULL) {
		[self freeMem:wstring];

		length = 0;
		wstring = NULL;

		return self;
	}

	newlen = wcslen(wstr);
	newlen = [wstr length];
	newstr = [self getMem: (newlen + 1) * sizeof(wchar_t)];
	memcpy(newstr, wstr, (newlen + 1) * sizeof(wchar_t));
	wmemcpy(newstr, [wstr wcString], newlen + 1);

	if (wstring != NULL)
		[self freeMem: wstring];

	length = newlen;
	wstring = newstr;

	return self;
}

- (OFWideString*)clone
{
	return [OFWideString new: wstring];
}

- (OFWideString*)append: (const wchar_t*)wstr
- (OFWideString*)append: (OFConstWideString*)wstr
{
	wchar_t	*newstr;
	size_t	newlen, strlength;

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

	strlength = wcslen(wstr);
	strlength = [wstr length];
	newlen = length + strlength;

	newstr = [self resizeMem: wstring
			  toSize: (newlen + 1) * sizeof(wchar_t)];

	memcpy(newstr + length, wstr, (strlength + 1) * sizeof(wchar_t));
	wmemcpy(newstr + length, [wstr wcString], strlength + 1);

	length = newlen;
	wstring = newstr;

	return self;
}

- (int)compare: (OFWideString*)str
- (int)compare: (OFConstWideString*)str
{
	return wcscmp(wstring, [str wcString]);
}
@end

Modified tests/OFString/OFString.m from [1fdb086bd4] to [d5b6a278de].

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







-
+


-
+

-
+






-
+






-
+







main()
{
	OFString *s1 = [OFString new: "test"];
	OFString *s2 = [[OFString alloc] init: ""];
	OFString *s3;
	OFString *s4 = [OFString new];

	[s2 append: "123"];
	[s2 append: [OFConstString new: "123"]];
	s3 = [s1 clone];

	[s4 setTo: [s2 cString]];
	[s4 setTo: (OFConstString*)s2];

	if (![s1 compare: s3])
	if (![s1 compare: (OFConstString*)s3])
		puts("s1 and s3 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}

	if (![s2 compare: s4])
	if (![s2 compare: (OFConstString*)s4])
		puts("s2 and s4 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}

	if (!strcmp([[s1 append: [s2 cString]] cString], "test123"))
	if (!strcmp([[s1 append: (OFConstString*)s2] cString], "test123"))
		puts("s1 appended with s2 is the expected string! GOOD!");
	else {
		puts("s1 appended with s2 is not the expected string!");
		return 1;
	}

	if (strlen([s1 cString]) == [s1 length] && [s1 length] == 7)

Modified tests/OFWideString/OFWideString.m from [c00b389041] to [890eff7e87].

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







-
+


-
+

-
+






-
+






-
+







main()
{
	OFWideString *s1 = [OFWideString new: L"test"];
	OFWideString *s2 = [[OFWideString alloc] init: L""];
	OFWideString *s3;
	OFWideString *s4 = [OFWideString new];

	[s2 append: L"123"];
	[s2 append: [OFConstWideString new: L"123"]];
	s3 = [s1 clone];

	[s4 setTo: [s2 wcString]];
	[s4 setTo: (OFConstWideString*)s2];

	if (![s1 compare: s3])
	if (![s1 compare: (OFConstWideString*)s3])
		puts("s1 and s3 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}

	if (![s2 compare: s4])
	if (![s2 compare: (OFConstWideString*)s4])
		puts("s2 and s4 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}

	if (!wcscmp([[s1 append: [s2 wcString]] wcString], L"test123"))
	if (!wcscmp([[s1 append: (OFConstWideString*)s2] wcString], L"test123"))
		puts("s1 appended with s2 is the expected string! GOOD!");
	else {
		puts("s1 appended with s2 is not the expected string!");
		return 1;
	}

	if (wcslen([s1 wcString]) == [s1 length] && [s1 length] == 7)