ObjFW  Diff

Differences From Artifact [9f7f197b86]:

To Artifact [737d54ed05]:


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
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
117
118
119
120
121

122
123
124
125
126
127
128
129
130
131
132

133
134
135
136
137

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177


178
179
180

181
182

183
184
185
186

187
188
189

190
191
192
193
194
195
196
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
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







-
+
-















-
-
-
-
-

















-
-
-
-
-
-
-
+
+
-
-
-
+
-
-
-






-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+









-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
+










-
+




-
+




-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+


-
+

-
+



-
+
-

-
+







 * the packaging of this file.
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <wchar.h>
#import <ctype.h>
#import <wctype.h>

#import "OFString.h"
#import "OFExceptions.h"

@implementation OFString
+ new
{
	return [[self alloc] init];
}

+ newFromCString: (const char*)str
{
	return [[self alloc] initFromCString: str];
}

+ newFromWideCString: (const wchar_t*)str
{
	return [[self alloc] initFromWideCString: str];
}

- init
{
	if ((self = [super init])) {
		length = 0;
		string = NULL;
	}

	return self;
}

- initFromCString: (const char*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			if ((length = mbstowcs(NULL, str, 0)) == (size_t)-1) {
				[super free];
				@throw [OFCharsetConversionFailedException
				    newWithObject: nil];
			}

			string = [self getMemForNItems: length + 1
			length = strlen(str);
			string = [self getMemWithSize: length + 1];
						ofSize: sizeof(wchar_t)];

			if (mbstowcs(string, str, length + 1) != length) {
			memcpy(string, str, length + 1);
				[super free];
				return nil;
			}
		}
	}

	return self;
}

- initFromWideCString: (const wchar_t*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = wcslen(str);
			string = [self getMemForNItems: length + 1
						ofSize: sizeof(wchar_t)];
			wmemcpy(string, str, length + 1);
		}
	}

	return self;
}

- (const wchar_t*)wideCString
- (const char*)cString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (char*)getCString
{
	char *str;
	size_t len;

	if ((len = wcstombs(NULL, string, 0)) == (size_t)-1)
		@throw [OFCharsetConversionFailedException newWithObject: self];

	str = [self getMemWithSize: len + 1];

	if (wcstombs(str, string, len + 1) != len) {
		[self freeMem: str];
		@throw [OFCharsetConversionFailedException newWithObject: self];
	}

	return str;
}

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

- (OFString*)setTo: (OFString*)str
{
	[self free];
	return (self = [str clone]);
}

- (int)compareTo: (OFString*)str
{
	return wcscmp(string, [str wideCString]);
	return strcmp(string, [str cString]);
}

- append: (OFString*)str
{
	return [self appendWideCString: [str wideCString]];
	return [self appendCString: [str cString]];
}

- appendCString: (const char*)str
{
	wchar_t	*newstr, *tmpstr;
	size_t	newlen, strlength;

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

	if ((strlength = mbstowcs(NULL, str, 0)) == (size_t)-1)
		@throw [OFCharsetConversionFailedException newWithObject: self];

	tmpstr = [self getMemForNItems: strlength + 1
				ofSize: sizeof(wchar_t)];

	if (mbstowcs(tmpstr, str, strlength) != strlength) {
		[self freeMem: tmpstr];
		@throw [OFCharsetConversionFailedException newWithObject: self];
	}

	newlen = length + strlength;
	newstr = [self resizeMem: string
			toNItems: newlen + 1
			  ofSize: sizeof(wchar_t)];

	wmemcpy(newstr + length, tmpstr, strlength + 1);

	length = newlen;
	string = newstr;

	[self freeMem: tmpstr];

	return self;
}

- appendWideCString: (const wchar_t*)str
{
	wchar_t	*newstr;
	size_t	newlen, strlength;
	char   *newstr;
	size_t newlen, strlength;

	if (string == NULL) 
		return [self setTo: [OFString newFromWideCString: str]];
		return [self setTo: [OFString newFromCString: str]];

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

	newstr = [self resizeMem: string
			toNItems: newlen + 1
			  toSize: newlen + 1];
			  ofSize: sizeof(wchar_t)];

	wmemcpy(newstr + length, str, strlength + 1);
	memcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

208
209
210
211
212
213
214
215

216
217
218
219
220
221
222
223
224
225

226
227
228
229
122
123
124
125
126
127
128

129
130
131
132
133
134
135
136
137
138

139
140
141
142
143







-
+









-
+




}

- upper
{
	size_t i = length;

	while (i--) 
		string[i] = towupper(string[i]);
		string[i] = toupper(string[i]);

	return self;
}

- lower
{
	size_t i = length;

	while (i--) 
		string[i] = towlower(string[i]);
		string[i] = tolower(string[i]);

	return self;
}
@end