ObjFW  Check-in [6f4bf74c7d]

Overview
Comment:More style improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6f4bf74c7d363a9b63a0db0866542916fedaa2427afa1fce0a7673e7d3b7291d
User & Date: js on 2011-04-23 13:23:51
Other Links: manifest | tags
Context
2011-04-23
14:46
Add -[uppercaseString] and -[lowercaseString] to OFString. (check-in: becb4c9b86 user: js tags: trunk)
13:23
More style improvements. (check-in: 6f4bf74c7d user: js tags: trunk)
02:15
OFMutableString: Call madvise() on the correct length. (check-in: a4df72758c user: js tags: trunk)
Changes

Modified src/OFMutableString.h from [870877ba5c] to [960bff6aa8].

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







-
+

-
+




-
+

-
+




-
-
+
+

-
-
+
+








-
+

-
+








-
-
+
+

-
-
+
+




-
+

-
+





-
+

-
+





-
-
+
+

-
-
+
+




-
+

-
+



















-
-
+
+

-
-
+
+







/**
 * \brief A class for storing and modifying strings.
 */
@interface OFMutableString: OFString
/**
 * Sets the OFString to the specified UTF-8 encoded C string.
 *
 * \param str A UTF-8 encoded C string to set the OFString to.
 * \param string A UTF-8 encoded C string to set the OFString to.
 */
- (void)setToCString: (const char*)str;
- (void)setToCString: (const char*)string;

/**
 * Appends a UTF-8 encoded C string to the OFString.
 *
 * \param str A UTF-8 encoded C string to append
 * \param string A UTF-8 encoded C string to append
 */
- (void)appendCString: (const char*)str;
- (void)appendCString: (const char*)string;

/**
 * Appends a UTF-8 encoded C string with the specified length to the OFString.
 *
 * \param str A UTF-8 encoded C string to append
 * \param len The length of the UTF-8 encoded C string
 * \param string A UTF-8 encoded C string to append
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCString: (const char*)str
	   withLength: (size_t)len;
- (void)appendCString: (const char*)string
	   withLength: (size_t)length;

/**
 * Appends a UTF-8 encoded C string to the OFString without checking whether it
 * is valid UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param str A UTF-8 encoded C string to append
 * \param string A UTF-8 encoded C string to append
 */
- (void)appendCStringWithoutUTF8Checking: (const char*)str;
- (void)appendCStringWithoutUTF8Checking: (const char*)string;

/**
 * Appends a UTF-8 encoded C string with the specified length to the OFString
 * without checking whether it is valid UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param str A UTF-8 encoded C string to append
 * \param len The length of the UTF-8 encoded C string
 * \param string A UTF-8 encoded C string to append
 * \param length The length of the UTF-8 encoded C string
 */
- (void)appendCStringWithoutUTF8Checking: (const char*)str
				  length: (size_t)len;
- (void)appendCStringWithoutUTF8Checking: (const char*)string
				  length: (size_t)length;

/**
 * Appends another OFString to the OFString.
 *
 * \param str An OFString to append
 * \param string An OFString to append
 */
- (void)appendString: (OFString*)str;
- (void)appendString: (OFString*)string;

/**
 * Appends a formatted UTF-8 encoded C string to the OFString.
 * See printf for the format syntax.
 *
 * \param fmt A format string which generates the string to append
 * \param format A format string which generates the string to append
 */
- (void)appendFormat: (OFString*)fmt, ...;
- (void)appendFormat: (OFString*)format, ...;

/**
 * Appends a formatted UTF-8 encoded C string to the OFString.
 * See printf for the format syntax.
 *
 * \param fmt A format string which generates the string to append
 * \param args The arguments used in the format string
 * \param format A format string which generates the string to append
 * \param arguments The arguments used in the format string
 */
- (void)appendFormat: (OFString*)fmt
       withArguments: (va_list)args;
- (void)appendFormat: (OFString*)format
       withArguments: (va_list)arguments;

/**
 * Prepends another OFString to the OFString.
 *
 * \param str An OFString to prepend
 * \param string An OFString to prepend
 */
- (void)prependString: (OFString*)str;
- (void)prependString: (OFString*)string;

/**
 * Reverse the OFString.
 */
- (void)reverse;

/**
 * Upper the OFString.
 */
- (void)upper;

/**
 * Lower the OFString.
 */
- (void)lower;

/**
 * Inserts a string at the specified index.
 *
 * \param str The string to insert
 * \param idx The index
 * \param string The string to insert
 * \param index The index
 */
- (void)insertString: (OFString*)str
	     atIndex: (size_t)idx;
- (void)insertString: (OFString*)string
	     atIndex: (size_t)index;

/**
 * Deletes the characters at the specified range.
 *
 * \param start The index where the deletion should be started
 * \param end The index until which the characters should be deleted.
 *	      This points BEHIND the last character!
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
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







-
+



-
+





-
+


-
+




-
-
+
+

-
-
+
+








/**
 * Replaces the characters at the specified range.
 *
 * \param start The index where the replacement should be started
 * \param end The index until which the characters should be replaced.
 *	      This points BEHIND the last character!
 * \param repl The string to the replace the characters with
 * \param replacement The string to the replace the characters with
 */
- (void)replaceCharactersFromIndex: (size_t)start
			   toIndex: (size_t)end
			withString: (OFString*)repl;
			withString: (OFString*)replacement;

/**
 * Deletes the characters at the specified range.
 *
 * \param range The range of the characters which should be replaced
 * \param repl The string to the replace the characters with
 * \param replacement The string to the replace the characters with
 */
- (void)replaceCharactersInRange: (of_range_t)range
		      withString: (OFString*)repl;
		      withString: (OFString*)replacement;

/**
 * Deletes all occurrences of a string with another string.
 *
 * \param str The string to replace
 * \param repl The string with which it should be replaced
 * \param string The string to replace
 * \param replacement The string with which it should be replaced
 */
- (void)replaceOccurrencesOfString: (OFString*)str
			withString: (OFString*)repl;
- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement;

/**
 * Deletes all whitespaces at the beginning of a string.
 */
- (void)deleteLeadingWhitespaces;

/**

Modified src/OFMutableString.m from [4ddd782651] to [fa9bcee55c].

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
197
198
199

200
201

202
203

204
205
206
207



208
209
210
211


212
213
214
215
216



217
218
219
220

221
222

223
224
225
226

227
228
229

230
231

232
233
234
235
236




237
238
239
240


241
242
243
244
245

246
247
248
249

250
251
252
253
254
255
256
257
258
259
260

261
262

263
264
265
266
267
268
269
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
197
198
199

200
201

202
203

204
205



206
207
208
209
210


211
212
213
214



215
216
217
218
219
220

221
222

223
224
225
226

227
228
229

230
231

232
233




234
235
236
237
238
239


240
241
242
243
244
245

246
247
248
249

250
251
252
253
254
255
256
257
258
259
260

261
262

263
264
265
266
267
268
269
270







-
+


-
-
+
+

-
+


-
+











-
-
+
+



-
+


-
+

-
-
+
+



-
+





-
+


-
+

-
+

-
+

-
+

-
+



-
+



-
+

-
+





-
-
-
-
+
+
+
+
+





-
-
-
+
+
+


-
-
+
+


-
+





-
+

-
+
















-
+


-
+

-
+

-
+

-
+








-
-
-
+
+
+


-
-
+
+

-
+








-
-
-
+
+
+



-
+

-
+

-
+

-
-
-
+
+
+


-
-
+
+


-
-
-
+
+
+



-
+

-
+



-
+


-
+

-
+

-
-
-
-
+
+
+
+


-
-
+
+




-
+



-
+










-
+

-
+







#import "macros.h"

#import "of_asprintf.h"
#import "unicode.h"

@implementation OFMutableString
- (void)_applyTable: (const of_unichar_t* const[])table
	   withSize: (size_t)table_size
	   withSize: (size_t)tableSize
{
	of_unichar_t c;
	of_unichar_t *ustr;
	size_t ulen, nlen, clen;
	of_unichar_t *unicodeString;
	size_t unicodeLen, newLength, cLen;
	size_t i, j, d;
	char *nstr;
	char *newString;

	if (!isUTF8) {
		assert(table_size >= 1);
		assert(tableSize >= 1);

		uint8_t *p = (uint8_t*)string + length;
		uint8_t t;

		while (--p >= (uint8_t*)string)
			if ((t = table[0][*p]) != 0)
				*p = t;

		return;
	}

	ulen = [self length];
	ustr = [self allocMemoryForNItems: ulen
	unicodeLen = [self length];
	unicodeString = [self allocMemoryForNItems: unicodeLen
				 withSize: sizeof(of_unichar_t)];

	i = j = 0;
	nlen = 0;
	newLength = 0;

	while (i < length) {
		clen = of_string_utf8_to_unicode(string + i, length - i, &c);
		cLen = of_string_utf8_to_unicode(string + i, length - i, &c);

		if (clen == 0 || c > 0x10FFFF) {
			[self freeMemory: ustr];
		if (cLen == 0 || c > 0x10FFFF) {
			[self freeMemory: unicodeString];
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		if (c >> 8 < table_size) {
		if (c >> 8 < tableSize) {
			of_unichar_t tc = table[c >> 8][c & 0xFF];

			if (tc)
				c = tc;
		}
		ustr[j++] = c;
		unicodeString[j++] = c;

		if (c < 0x80)
			nlen++;
			newLength++;
		else if (c < 0x800)
			nlen += 2;
			newLength += 2;
		else if (c < 0x10000)
			nlen += 3;
			newLength += 3;
		else if (c < 0x110000)
			nlen += 4;
			newLength += 4;
		else {
			[self freeMemory: ustr];
			[self freeMemory: unicodeString];
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		i += clen;
		i += cLen;
	}

	@try {
		nstr = [self allocMemoryWithSize: nlen + 1];
		newString = [self allocMemoryWithSize: newLength + 1];
	} @catch (id e) {
		[self freeMemory: ustr];
		[self freeMemory: unicodeString];
		@throw e;
	}

	j = 0;

	for (i = 0; i < ulen; i++) {
		if ((d = of_string_unicode_to_utf8(ustr[i], nstr + j)) == 0) {
			[self freeMemory: ustr];
			[self freeMemory: nstr];
	for (i = 0; i < unicodeLen; i++) {
		if ((d = of_string_unicode_to_utf8(unicodeString[i],
		    newString + j)) == 0) {
			[self freeMemory: unicodeString];
			[self freeMemory: newString];
			@throw [OFInvalidEncodingException newWithClass: isa];
		}
		j += d;
	}

	assert(j == nlen);
	nstr[j] = 0;
	[self freeMemory: ustr];
	assert(j == newLength);
	newString[j] = 0;
	[self freeMemory: unicodeString];

	[self freeMemory: string];
	string = nstr;
	length = nlen;
	string = newString;
	length = newLength;
}

- (void)setToCString: (const char*)str
- (void)setToCString: (const char*)string_
{
	size_t len;

	[self freeMemory: string];

	len = strlen(str);
	len = strlen(string_);

	switch (of_string_check_utf8(str, len)) {
	switch (of_string_check_utf8(string_, len)) {
	case 0:
		isUTF8 = NO;
		break;
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		string = NULL;
		length = 0;
		isUTF8 = NO;

		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	length = len;
	string = [self allocMemoryWithSize: length + 1];
	memcpy(string, str, length + 1);
	memcpy(string, string_, length + 1);
}

- (void)appendCString: (const char*)str
- (void)appendCString: (const char*)string_
{
	size_t strlength;
	size_t len;

	strlength = strlen(str);
	len = strlen(string_);

	switch (of_string_check_utf8(str, strlength)) {
	switch (of_string_check_utf8(string_, len)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;
			     toSize: length + len + 1];
	memcpy(string + length, string_, len + 1);
	length += len;
}

- (void)appendCString: (const char*)str
	   withLength: (size_t)len
- (void)appendCString: (const char*)string_
	   withLength: (size_t)length_
{
	switch (of_string_check_utf8(str, len)) {
	switch (of_string_check_utf8(string_, length_)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	string = [self resizeMemory: string
			     toSize: length + len + 1];
	memcpy(string + length, str, len);
	length += len;
			     toSize: length + length_ + 1];
	memcpy(string + length, string_, length_);
	length += length_;
	string[length] = 0;
}

- (void)appendCStringWithoutUTF8Checking: (const char*)str
- (void)appendCStringWithoutUTF8Checking: (const char*)string_
{
	size_t strlength;
	size_t len;

	strlength = strlen(str);
	len = strlen(string_);
	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;
			     toSize: length + len + 1];
	memcpy(string + length, string_, len + 1);
	length += len;
}

- (void)appendCStringWithoutUTF8Checking: (const char*)str
				  length: (size_t)len
- (void)appendCStringWithoutUTF8Checking: (const char*)string_
				  length: (size_t)length_
{
	string = [self resizeMemory: string
			     toSize: length + len + 1];
	memcpy(string + length, str, len);
	length += len;
			     toSize: length + length_ + 1];
	memcpy(string + length, string_, length_);
	length += length_;
	string[length] = 0;
}

- (void)appendString: (OFString*)str
- (void)appendString: (OFString*)string_
{
	if (str == nil)
	if (string_ == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	[self appendCString: [str cString]];
	[self appendCString: [string_ cString]];
}

- (void)appendFormat: (OFString*)fmt, ...
- (void)appendFormat: (OFString*)format, ...
{
	va_list args;
	va_list arguments;

	va_start(args, fmt);
	[self appendFormat: fmt
	     withArguments: args];
	va_end(args);
	va_start(arguments, format);
	[self appendFormat: format
	     withArguments: arguments];
	va_end(arguments);
}

- (void)appendFormat: (OFString*)fmt
       withArguments: (va_list)args
- (void)appendFormat: (OFString*)format
       withArguments: (va_list)arguments
{
	char *t;
	int len;

	if (fmt == nil)
	if (format == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((len = of_vasprintf(&t, [fmt cString], args)) == -1)
	if ((len = of_vasprintf(&t, [format cString], arguments)) == -1)
		@throw [OFInvalidFormatException newWithClass: isa];

	@try {
		[self appendCString: t
			 withLength: len];
	} @finally {
		free(t);
	}
}

- (void)prependString: (OFString*)str
- (void)prependString: (OFString*)string_
{
	return [self insertString: str
	return [self insertString: string_
			  atIndex: 0];
}

- (void)reverse
{
	size_t i, j, len = length / 2;

360
361
362
363
364
365
366
367
368


369
370

371
372
373

374
375

376
377
378

379
380

381
382
383
384




385
386

387
388
389
390
391
392
393
361
362
363
364
365
366
367


368
369
370

371
372
373

374
375

376
377
378

379
380

381
382



383
384
385
386
387

388
389
390
391
392
393
394
395







-
-
+
+

-
+


-
+

-
+


-
+

-
+

-
-
-
+
+
+
+

-
+








- (void)lower
{
	[self _applyTable: of_unicode_lower_table
		 withSize: OF_UNICODE_LOWER_TABLE_SIZE];
}

- (void)insertString: (OFString*)str
	     atIndex: (size_t)idx
- (void)insertString: (OFString*)string_
	     atIndex: (size_t)index
{
	size_t nlen;
	size_t newLength;

	if (isUTF8)
		idx = of_string_index_to_position(string, idx, length);
		index = of_string_index_to_position(string, index, length);

	if (idx > length)
	if (index > length)
		@throw [OFOutOfRangeException newWithClass: isa];

	nlen = length + [str cStringLength];
	newLength = length + [string_ cStringLength];
	string = [self resizeMemory: string
			     toSize: nlen + 1];
			     toSize: newLength + 1];

	memmove(string + idx + [str cStringLength], string + idx, length - idx);
	memcpy(string + idx, [str cString], [str cStringLength]);
	string[nlen] = '\0';
	memmove(string + index + [string_ cStringLength], string + index,
	    length - index);
	memcpy(string + index, [string_ cString], [string_ cStringLength]);
	string[newLength] = '\0';

	length = nlen;
	length = newLength;
}

- (void)deleteCharactersFromIndex: (size_t)start
			  toIndex: (size_t)end
{
	if (isUTF8) {
		start = of_string_index_to_position(string, start, length);
418
419
420
421
422
423
424
425

426
427

428
429
430
431
432
433
434
435
436
437
438
439
440
441

442
443

444
445

446
447
448



449
450

451
452
453
454

455
456
457
458

459
460
461
462


463
464
465
466
467
468
469






470
471

472
473
474
475


476
477
478


479
480
481
482
483
484



485
486

487
488
489
490
491
492





493
494
495
496
497
498


499
500

501
502
503
504
505



506
507
508
509


510
511
512
513
514
515
516
420
421
422
423
424
425
426

427
428

429
430
431
432
433
434
435
436
437
438
439
440
441
442

443
444

445
446

447
448


449
450
451
452

453
454
455
456

457
458
459
460

461
462
463


464
465
466






467
468
469
470
471
472
473

474
475
476


477
478
479


480
481
482
483
484



485
486
487
488

489
490
491




492
493
494
495
496
497
498
499
500


501
502
503

504
505
506



507
508
509
510
511


512
513
514
515
516
517
518
519
520







-
+

-
+













-
+

-
+

-
+

-
-
+
+
+

-
+



-
+



-
+


-
-
+
+

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

-
+


-
-
+
+

-
-
+
+



-
-
-
+
+
+

-
+


-
-
-
-
+
+
+
+
+




-
-
+
+

-
+


-
-
-
+
+
+


-
-
+
+







{
	[self deleteCharactersFromIndex: range.start
				toIndex: range.start + range.length];
}

- (void)replaceCharactersFromIndex: (size_t)start
			   toIndex: (size_t)end
			withString: (OFString*)repl
			withString: (OFString*)replacement
{
	size_t nlen;
	size_t newLength;

	if (isUTF8) {
		start = of_string_index_to_position(string, start, length);
		end = of_string_index_to_position(string, end, length);
	}

	if (start > end)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if (end > length)
		@throw [OFOutOfRangeException newWithClass: isa];

	nlen = length - (end - start) + [repl cStringLength];
	newLength = length - (end - start) + [replacement cStringLength];
	string = [self resizeMemory: string
			     toSize: nlen + 1];
			     toSize: newLength + 1];

	memmove(string + end, string + start + [repl cStringLength],
	memmove(string + end, string + start + [replacement cStringLength],
	    length - end);
	memcpy(string + start, [repl cString], [repl cStringLength]);
	string[nlen] = '\0';
	memcpy(string + start, [replacement cString],
	    [replacement cStringLength]);
	string[newLength] = '\0';

	length = nlen;
	length = newLength;
}

- (void)replaceCharactersInRange: (of_range_t)range
		      withString: (OFString*)repl
		      withString: (OFString*)replacement
{
	[self replaceCharactersFromIndex: range.start
				 toIndex: range.start + range.length
			      withString: repl];
			      withString: replacement];
}

- (void)replaceOccurrencesOfString: (OFString*)str
			withString: (OFString*)repl
- (void)replaceOccurrencesOfString: (OFString*)string_
			withString: (OFString*)replacement
{
	const char *str_c = [str cString];
	const char *repl_c = [repl cString];
	size_t str_len = [str cStringLength];
	size_t repl_len = [repl cStringLength];
	size_t i, last, tmp_len;
	char *tmp;
	const char *cString = [string_ cString];
	const char *replacementCString = [replacement cString];
	size_t stringLen = [string_ cStringLength];
	size_t replacementLen = [replacement cStringLength];
	size_t i, last, newLength;
	char *newString;

	if (str_len > length)
	if (stringLen > length)
		return;

	tmp = NULL;
	tmp_len = 0;
	newString = NULL;
	newLength = 0;

	for (i = 0, last = 0; i <= length - str_len; i++) {
		if (memcmp(string + i, str_c, str_len))
	for (i = 0, last = 0; i <= length - stringLen; i++) {
		if (memcmp(string + i, cString, stringLen))
			continue;

		@try {
			tmp = [self resizeMemory: tmp
					  toSize: tmp_len + i - last +
						  repl_len + 1];
			newString = [self resizeMemory: newString
						toSize: newLength + i - last +
							replacementLen + 1];
		} @catch (id e) {
			[self freeMemory: tmp];
			[self freeMemory: newString];
			@throw e;
		}
		memcpy(tmp + tmp_len, string + last, i - last);
		memcpy(tmp + tmp_len + i - last, repl_c, repl_len);
		tmp_len += i - last + repl_len;
		i += str_len - 1;
		memcpy(newString + newLength, string + last, i - last);
		memcpy(newString + newLength + i - last, replacementCString,
		    replacementLen);
		newLength += i - last + replacementLen;
		i += stringLen - 1;
		last = i + 1;
	}

	@try {
		tmp = [self resizeMemory: tmp
				  toSize: tmp_len + length - last + 1];
		newString = [self resizeMemory: newString
					toSize: newLength + length - last + 1];
	} @catch (id e) {
		[self freeMemory: tmp];
		[self freeMemory: newString];
		@throw e;
	}
	memcpy(tmp + tmp_len, string + last, length - last);
	tmp_len += length - last;
	tmp[tmp_len] = 0;
	memcpy(newString + newLength, string + last, length - last);
	newLength += length - last;
	newString[newLength] = 0;

	[self freeMemory: string];
	string = tmp;
	length = tmp_len;
	string = newString;
	length = newLength;
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < length; i++)

Modified src/OFString+Hashing.m from [4e1492dfab] to [afd3c22e1b].

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







-
+












-
-
+
+




-
+








-
+












-
-
+
+




-
+




@implementation OFString (Hashing)
- (OFString*)MD5Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char cString[OF_MD5_DIGEST_SIZE * 2];
	char ret[OF_MD5_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: string
			length: length];
	digest = [hash digest];

	for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		cString[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		cString[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
		ret[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: cString
	return [OFString stringWithCString: ret
				    length: 32];
}

- (OFString*)SHA1Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char cString[OF_SHA1_DIGEST_SIZE * 2];
	char ret[OF_SHA1_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: string
			length: length];
	digest = [hash digest];

	for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		cString[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		cString[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
		ret[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: cString
	return [OFString stringWithCString: ret
				    length: 40];
}
@end

Modified src/OFString.h from [3da081c65e] to [8c8a66350c].

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







-
+


-
+




-
+



-
+






-
+

-
+


-
+

-
+





-
-
+
+


-
-
+
+





-
+


-
+




-
+


-
+




-
+


-
+







 * \return A new autoreleased OFString
 */
+ string;

/**
 * Creates a new OFString from a UTF-8 encoded C string.
 *
 * \param str A UTF-8 encoded C string to initialize the OFString with
 * \param string A UTF-8 encoded C string to initialize the OFString with
 * \return A new autoreleased OFString
 */
+ stringWithCString: (const char*)str;
+ stringWithCString: (const char*)string;

/**
 * Creates a new OFString from a C string with the specified encoding.
 *
 * \param str A C string to initialize the OFString with
 * \param string A C string to initialize the OFString with
 * \param encoding The encoding of the C string
 * \return A new autoreleased OFString
 */
+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
	   encoding: (of_string_encoding_t)encoding;

/**
 * Creates a new OFString from a C string with the specified encoding and
 * length.
 *
 * \param str A C string to initialize the OFString with
 * \param string A C string to initialize the OFString with
 * \param encoding The encoding of the C string
 * \param len The length of the C string
 * \param length The length of the C string
 * \return A new autoreleased OFString
 */
+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
	   encoding: (of_string_encoding_t)encoding
	     length: (size_t)len;
	     length: (size_t)length;

/**
 * Creates a new OFString from a UTF-8 encoded C string with the specified
 * length.
 *
 * \param str A UTF-8 encoded C string to initialize the OFString with
 * \param len The length of the UTF-8 encoded C string
 * \param string A UTF-8 encoded C string to initialize the OFString with
 * \param length The length of the UTF-8 encoded C string
 * \return A new autoreleased OFString
 */
+ stringWithCString: (const char*)str
	     length: (size_t)len;
+ stringWithCString: (const char*)string
	     length: (size_t)length;

/**
 * Creates a new OFString from a format string.
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \param format A string used as format to initialize the OFString
 * \return A new autoreleased OFString
 */
+ stringWithFormat: (OFString*)fmt, ...;
+ stringWithFormat: (OFString*)format, ...;

/**
 * Creates a new OFString containing the constructed specified path.
 *
 * \param first The first component of the path
 * \param firstComponent The first component of the path
 * \return A new autoreleased OFString
 */
+ stringWithPath: (OFString*)first, ...;
+ stringWithPath: (OFString*)firstComponent, ...;

/**
 * Creates a new OFString from another string.
 *
 * \param str A string to initialize the OFString with
 * \param string A string to initialize the OFString with
 * \return A new autoreleased OFString
 */
+ stringWithString: (OFString*)str;
+ stringWithString: (OFString*)string;

/**
 * Creates a new OFString with the contents of the specified UTF-8 encoded file.
 *
 * \param path The path to the file
 * \return A new autoreleased OFString
 */
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

197
198
199

200
201

202
203
204
205
206
207
208


209
210
211
212


213
214
215
216
217
218

219
220
221

222
223
224
225
226
227
228


229
230
231
232


233
234
235
236
237
238

239
240
241

242
243
244
245
246
247
248


249
250
251
252


253
254
255
256
257

258
259
260

261
262
263
264
265
266
267
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
197
198

199
200

201
202
203
204
205
206


207
208
209
210


211
212
213
214
215
216
217

218
219
220

221
222
223
224
225
226


227
228
229
230


231
232
233
234
235
236
237

238
239
240

241
242
243
244
245
246


247
248
249
250


251
252
253
254
255
256

257
258
259

260
261
262
263
264
265
266
267







-
+


-
+





-
+



-
+





-
+


-
+





-
+



-
+






-
+

-
+


-
+

-
+





-
-
+
+


-
-
+
+





-
+


-
+





-
-
+
+


-
-
+
+





-
+


-
+





-
-
+
+


-
-
+
+




-
+


-
+







+ stringWithContentsOfFile: (OFString*)path
		  encoding: (of_string_encoding_t)encoding;

/**
 * Creates a new OFString with the contents of the specified URL, assuming
 * UTF-8 encoding.
 *
 * \param url The URL to the contents for the string
 * \param URL The URL to the contents for the string
 * \return A new autoreleased OFString
 */
+ stringWithContentsOfURL: (OFURL*)url;
+ stringWithContentsOfURL: (OFURL*)URL;

/**
 * Creates a new OFString with the contents of the specified URL in the
 * specified encoding.
 *
 * \param url The URL to the contents for the string
 * \param URL The URL to the contents for the string
 * \param encoding The encoding to assume
 * \return A new autoreleased OFString
 */
+ stringWithContentsOfURL: (OFURL*)url
+ stringWithContentsOfURL: (OFURL*)URL
		 encoding: (of_string_encoding_t)encoding;

/**
 * Initializes an already allocated OFString from a UTF-8 encoded C string.
 *
 * \param str A UTF-8 encoded C string to initialize the OFString with
 * \param string A UTF-8 encoded C string to initialize the OFString with
 * \return An initialized OFString
 */
- initWithCString: (const char*)str;
- initWithCString: (const char*)string;

/**
 * Initializes an already allocated OFString from a C string with the specified
 * encoding.
 *
 * \param str A C string to initialize the OFString with
 * \param string A C string to initialize the OFString with
 * \param encoding The encoding of the C string
 * \return An initialized OFString
 */
- initWithCString: (const char*)str
- initWithCString: (const char*)string
	 encoding: (of_string_encoding_t)encoding;

/**
 * Initializes an already allocated OFString from a C string with the specified
 * encoding and length.
 *
 * \param str A C string to initialize the OFString with
 * \param string A C string to initialize the OFString with
 * \param encoding The encoding of the C string
 * \param len The length of the C string
 * \param length The length of the C string
 * \return An initialized OFString
 */
- initWithCString: (const char*)str
- initWithCString: (const char*)string
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)len;
	   length: (size_t)length;

/**
 * Initializes an already allocated OFString from a UTF-8 encoded C string with
 * the specified length.
 *
 * \param str A UTF-8 encoded C string to initialize the OFString with
 * \param len The length of the UTF-8 encoded C string
 * \param string A UTF-8 encoded C string to initialize the OFString with
 * \param length The length of the UTF-8 encoded C string
 * \return An initialized OFString
 */
- initWithCString: (const char*)str
	   length: (size_t)len;
- initWithCString: (const char*)string
	   length: (size_t)length;

/**
 * Initializes an already allocated OFString with a format string.
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \param format A string used as format to initialize the OFString
 * \return An initialized OFString
 */
- initWithFormat: (OFString*)fmt, ...;
- initWithFormat: (OFString*)format, ...;

/**
 * Initializes an already allocated OFString with a format string.
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \param args The arguments used in the format string
 * \param format A string used as format to initialize the OFString
 * \param arguments The arguments used in the format string
 * \return An initialized OFString
 */
- initWithFormat: (OFString*)fmt
       arguments: (va_list)args;
- initWithFormat: (OFString*)format
       arguments: (va_list)arguments;

/**
 * Initializes an already allocated OFString with the constructed specified
 * path.
 *
 * \param first The first component of the path
 * \param firstComponent The first component of the path
 * \return A new autoreleased OFString
 */
- initWithPath: (OFString*)first, ...;
- initWithPath: (OFString*)firstComponent, ...;

/**
 * Initializes an already allocated OFString with the constructed specified
 * path.
 *
 * \param first The first component of the path
 * \param args A va_list with the other components of the path
 * \param firstComponent The first component of the path
 * \param arguments A va_list with the other components of the path
 * \return A new autoreleased OFString
 */
- initWithPath: (OFString*)first
     arguments: (va_list)args;
- initWithPath: (OFString*)firstComponent
     arguments: (va_list)arguments;

/**
 * Initializes an already allocated OFString with another string.
 *
 * \param str A string to initialize the OFString with
 * \param string A string to initialize the OFString with
 * \return An initialized OFString
 */
- initWithString: (OFString*)str;
- initWithString: (OFString*)string;

/**
 * Initializes an already allocated OFString with the contents of the specified
 * file in the specified encoding.
 *
 * \param path The path to the file
 * \return An initialized OFString
279
280
281
282
283
284
285
286

287
288
289

290
291
292
293
294
295

296
297
298
299

300
301
302
303
304
305
306
279
280
281
282
283
284
285

286
287
288

289
290
291
292
293
294

295
296
297
298

299
300
301
302
303
304
305
306







-
+


-
+





-
+



-
+







- initWithContentsOfFile: (OFString*)path
		encoding: (of_string_encoding_t)encoding;

/**
 * Initializes an already allocated OFString with the contents of the specified
 * URL, assuming UTF-8 encoding.
 *
 * \param url The URL to the contents for the string
 * \param URL The URL to the contents for the string
 * \return An initialized OFString
 */
- initWithContentsOfURL: (OFURL*)url;
- initWithContentsOfURL: (OFURL*)URL;

/**
 * Initializes an already allocated OFString with the contents of the specified
 * URL in the specified encoding.
 *
 * \param url The URL to the contents for the string
 * \param URL The URL to the contents for the string
 * \param encoding The encoding to assume
 * \return An initialized OFString
 */
- initWithContentsOfURL: (OFURL*)url
- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding;

/**
 * \return The OFString as a UTF-8 encoded C string
 */
- (const char*)cString;

317
318
319
320
321
322
323
324

325
326
327

328
329
330
331
332
333
334
335
336

337
338
339
340

341
342
343

344
345
346
347

348
349
350

351
352
353

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

374
375
376

377
378
379
380
381
382
383
317
318
319
320
321
322
323

324
325
326

327
328
329
330
331
332
333
334
335

336
337
338
339

340
341
342

343
344
345
346

347
348
349

350
351
352

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

373
374
375

376
377
378
379
380
381
382
383







-
+


-
+








-
+



-
+


-
+



-
+


-
+


-
+



















-
+


-
+







/// \cond internal
- (BOOL)isUTF8;
/// \endcond

/**
 * Compares the OFString to another OFString without caring about the case.
 *
 * \param str A string to compare with
 * \param otherString A string to compare with
 * \return An of_comparison_result_t
 */
- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)str;
- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString;

/**
 * \param index The index of the Unicode character to return
 * \return The Unicode character at the specified index
 */
- (of_unichar_t)characterAtIndex: (size_t)index;

/**
 * \param str The string to search
 * \param string The string to search
 * \return The index of the first occurrence of the string or OF_INVALID_INDEX
 *	   if it was not found
 */
- (size_t)indexOfFirstOccurrenceOfString: (OFString*)str;
- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string;

/**
 * \param str The string to search
 * \param string The string to search
 * \return The index of the last occurrence of the string or OF_INVALID_INDEX if
 *	   it was not found
 */
- (size_t)indexOfLastOccurrenceOfString: (OFString*)str;
- (size_t)indexOfLastOccurrenceOfString: (OFString*)string;

/**
 * \param str The string to search
 * \param string The string to search
 * \return Whether the string contains the specified string
 */
- (BOOL)containsString: (OFString*)str;
- (BOOL)containsString: (OFString*)string;

/**
 * \param start The index where the substring starts
 * \param end The index where the substring ends.
 *	      This points BEHIND the last character!
 * \return The substring as a new autoreleased OFString
 */
- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end;

/**
 * \param range The range of the substring
 * \return The substring as a new autoreleased OFString
 */
- (OFString*)substringWithRange: (of_range_t)range;

/**
 * Creates a new string by appending another string.
 *
 * \param str The string to append
 * \param string The string to append
 * \return A new autoreleased OFString with the specified string appended
 */
- (OFString*)stringByAppendingString: (OFString*)str;
- (OFString*)stringByAppendingString: (OFString*)string;

/**
 * Creates a new string by deleting leading whitespaces.
 *
 * \return A new autoreleased OFString with leading whitespaces deleted
 */
- (OFString*)stringByDeletingLeadingWhitespaces;

Modified src/OFString.m from [ead9a0ea73] to [7863930129].

59
60
61
62
63
64
65
66

67
68
69
70
71

72
73

74
75
76
77
78
79
80
59
60
61
62
63
64
65

66
67
68
69
70

71
72

73
74
75
76
77
78
79
80







-
+




-
+

-
+







	_OFString_Hashing_reference = 1;
	_OFString_URLEncoding_reference = 1;
	_OFString_XMLEscaping_reference = 1;
	_OFString_XMLUnescaping_reference = 1;
};

static inline int
memcasecmp(const char *s1, const char *s2, size_t len)
memcasecmp(const char *first, const char *second, size_t len)
{
	size_t i;

	for (i = 0; i < len; i++) {
		if (tolower((int)s1[i]) > tolower((int)s2[i]))
		if (tolower((int)first[i]) > tolower((int)second[i]))
			return OF_ORDERED_DESCENDING;
		if (tolower((int)s1[i]) < tolower((int)s2[i]))
		if (tolower((int)first[i]) < tolower((int)second[i]))
			return OF_ORDERED_ASCENDING;
	}

	return OF_ORDERED_SAME;
}

int
242
243
244
245
246
247
248
249

250
251

252
253
254

255
256
257

258
259
260
261

262
263

264
265

266
267

268
269
270
271


272
273
274


275
276
277

278
279
280

281
282
283
284
285




286
287
288
289
290

291
292
293

294
295
296
297
298




299
300
301
302
303

304
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

321
322

323
324
325

326
327
328

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

348
349

350
351

352
353
354

355
356
357

358
359

360
361
362

363
364

365
366
367
368
369
370
371
372
373
374
375



376
377
378
379


380
381
382

383
384
385
386
387
388
389
390
391

392
393
394
395
396
397
398

399
400
401
402
403


404
405
406
407
408
409

410
411
412
413
414
415
416
242
243
244
245
246
247
248

249
250

251
252
253

254
255
256

257
258
259
260

261
262

263
264

265
266

267
268
269


270
271
272


273
274
275
276

277
278
279

280
281




282
283
284
285
286
287
288
289

290
291
292

293
294




295
296
297
298
299
300
301
302

303
304

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

320
321

322
323
324

325
326
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

347
348

349
350

351
352
353

354
355
356

357
358

359
360
361

362
363

364
365
366
367
368
369
370
371
372



373
374
375
376
377


378
379
380
381

382
383
384
385
386
387
388
389
390

391
392
393
394
395
396
397

398
399
400
401


402
403
404
405
406
407
408

409
410
411
412
413
414
415
416







-
+

-
+


-
+


-
+



-
+

-
+

-
+

-
+


-
-
+
+

-
-
+
+


-
+


-
+

-
-
-
-
+
+
+
+




-
+


-
+

-
-
-
-
+
+
+
+




-
+

-
+














-
+

-
+


-
+


-
+


















-
+

-
+

-
+


-
+


-
+

-
+


-
+

-
+








-
-
-
+
+
+


-
-
+
+


-
+








-
+






-
+



-
-
+
+





-
+








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

+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
{
	return [[[self alloc] initWithCString: str] autorelease];
	return [[[self alloc] initWithCString: string] autorelease];
}

+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
	   encoding: (of_string_encoding_t)encoding
{
	return [[[self alloc] initWithCString: str
	return [[[self alloc] initWithCString: string
				     encoding: encoding] autorelease];
}

+ stringWithCString: (const char*)str
+ stringWithCString: (const char*)string
	   encoding: (of_string_encoding_t)encoding
	     length: (size_t)len
	     length: (size_t)length
{
	return [[[self alloc] initWithCString: str
	return [[[self alloc] initWithCString: string
				     encoding: encoding
				       length: len] autorelease];
				       length: length] autorelease];
}

+ stringWithCString: (const char*)str
	     length: (size_t)len
+ stringWithCString: (const char*)string
	     length: (size_t)length
{
	return [[[self alloc] initWithCString: str
				       length: len] autorelease];
	return [[[self alloc] initWithCString: string
				       length: length] autorelease];
}

+ stringWithFormat: (OFString*)fmt, ...
+ stringWithFormat: (OFString*)format, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, fmt);
	ret = [[[self alloc] initWithFormat: fmt
				  arguments: args] autorelease];
	va_end(args);
	va_start(arguments, format);
	ret = [[[self alloc] initWithFormat: format
				  arguments: arguments] autorelease];
	va_end(arguments);

	return ret;
}

+ stringWithPath: (OFString*)first, ...
+ stringWithPath: (OFString*)firstComponent, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, first);
	ret = [[[self alloc] initWithPath: first
				arguments: args] autorelease];
	va_end(args);
	va_start(arguments, firstComponent);
	ret = [[[self alloc] initWithPath: firstComponent
				arguments: arguments] autorelease];
	va_end(arguments);

	return ret;
}

+ stringWithString: (OFString*)str
+ stringWithString: (OFString*)string
{
	return [[[self alloc] initWithString: str] autorelease];
	return [[[self alloc] initWithString: string] autorelease];
}

+ stringWithContentsOfFile: (OFString*)path
{
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
}

+ stringWithContentsOfFile: (OFString*)path
		  encoding: (of_string_encoding_t)encoding
{
	return [[[self alloc] initWithContentsOfFile: path
					    encoding: encoding] autorelease];
}

+ stringWithContentsOfURL: (OFURL*)url
+ stringWithContentsOfURL: (OFURL*)URL
{
	return [[[self alloc] initWithContentsOfURL: url] autorelease];
	return [[[self alloc] initWithContentsOfURL: URL] autorelease];
}

+ stringWithContentsOfURL: (OFURL*)url
+ stringWithContentsOfURL: (OFURL*)URL
		 encoding: (of_string_encoding_t)encoding
{
	return [[[self alloc] initWithContentsOfURL: url
	return [[[self alloc] initWithContentsOfURL: URL
					   encoding: encoding] autorelease];
}

- init
{
	self = [super init];

	@try {
		string = [self allocMemoryWithSize: 1];
		string[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCString: (const char*)str
- initWithCString: (const char*)string_
{
	return [self initWithCString: str
	return [self initWithCString: string_
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: strlen(str)];
			      length: strlen(string_)];
}

- initWithCString: (const char*)str
- initWithCString: (const char*)string_
	 encoding: (of_string_encoding_t)encoding
{
	return [self initWithCString: str
	return [self initWithCString: string_
			    encoding: encoding
			      length: strlen(str)];
			      length: strlen(string_)];
}

- initWithCString: (const char*)str
- initWithCString: (const char*)string_
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)len
	   length: (size_t)length_
{
	self = [super init];

	@try {
		size_t i, j;
		const uint16_t *table;

		if (encoding == OF_STRING_ENCODING_UTF_8 &&
		    len >= 3 && !memcmp(str, "\xEF\xBB\xBF", 3)) {
			str += 3;
			len -= 3;
		    length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) {
			string_ += 3;
			length_ -= 3;
		}

		string = [self allocMemoryWithSize: len + 1];
		length = len;
		string = [self allocMemoryWithSize: length_ + 1];
		length = length_;

		if (encoding == OF_STRING_ENCODING_UTF_8) {
			switch (of_string_check_utf8(str, length)) {
			switch (of_string_check_utf8(string_, length)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			memcpy(string, str, length);
			memcpy(string, string_, length);
			string[length] = 0;

			return self;
		}

		if (encoding == OF_STRING_ENCODING_ISO_8859_1) {
			for (i = j = 0; i < len; i++) {
			for (i = j = 0; i < length_; i++) {
				char buf[4];
				size_t bytes;

				if (!(str[i] & 0x80)) {
					string[j++] = str[i];
				if (!(string_[i] & 0x80)) {
					string[j++] = string_[i];
					continue;
				}

				isUTF8 = YES;
				bytes = of_string_unicode_to_utf8(
				    (uint8_t)str[i], buf);
				    (uint8_t)string_[i], buf);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    newWithClass: isa];

				length += bytes - 1;
				string = [self resizeMemory: string
432
433
434
435
436
437
438
439

440
441
442


443
444
445


446
447
448
449

450
451

452
453
454
455
456


457
458

459
460
461
462

463
464
465
466
467


468
469
470
471
472
473
474
475
476
477
478
479
480


481
482

483
484

485
486
487

488
489
490

491
492
493
494
495




496
497
498
499
500
501


502
503
504
505
506
507
508

509
510
511
512


513
514
515
516
517
518
519
432
433
434
435
436
437
438

439
440


441
442
443


444
445
446
447
448

449
450

451
452
453
454
455

456
457
458

459
460
461
462

463
464
465
466


467
468
469
470
471
472
473
474
475
476
477
478
479


480
481
482

483
484

485
486
487

488
489
490

491
492




493
494
495
496
497
498
499
500


501
502
503
504
505
506
507
508

509
510
511
512

513
514
515
516
517
518
519
520
521







-
+

-
-
+
+

-
-
+
+



-
+

-
+




-
+
+

-
+



-
+



-
-
+
+











-
-
+
+

-
+

-
+


-
+


-
+

-
-
-
-
+
+
+
+




-
-
+
+






-
+



-
+
+







		case OF_STRING_ENCODING_WINDOWS_1252:
			table = of_windows_1252;
			break;
		default:
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		for (i = j = 0; i < len; i++) {
		for (i = j = 0; i < length_; i++) {
			char buf[4];
			of_unichar_t chr;
			size_t chr_bytes;
			of_unichar_t character;
			size_t characterBytes;

			if (!(str[i] & 0x80)) {
				string[j++] = str[i];
			if (!(string_[i] & 0x80)) {
				string[j++] = string_[i];
				continue;
			}

			chr = table[(uint8_t)str[i]];
			character = table[(uint8_t)string_[i]];

			if (chr == 0xFFFD)
			if (character == 0xFFFD)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			isUTF8 = YES;
			chr_bytes = of_string_unicode_to_utf8(chr, buf);
			characterBytes = of_string_unicode_to_utf8(character,
			    buf);

			if (chr_bytes == 0)
			if (characterBytes == 0)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			length += chr_bytes - 1;
			length += characterBytes - 1;
			string = [self resizeMemory: string
					     toSize: length + 1];

			memcpy(string + j, buf, chr_bytes);
			j += chr_bytes;
			memcpy(string + j, buf, characterBytes);
			j += characterBytes;
		}

		string[length] = 0;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCString: (const char*)str
	   length: (size_t)len
- initWithCString: (const char*)string_
	   length: (size_t)length_
{
	return [self initWithCString: str
	return [self initWithCString: string_
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: len];
			      length: length_];
}

- initWithFormat: (OFString*)fmt, ...
- initWithFormat: (OFString*)format, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, fmt);
	ret = [self initWithFormat: fmt
			 arguments: args];
	va_end(args);
	va_start(arguments, format);
	ret = [self initWithFormat: format
			 arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
- initWithFormat: (OFString*)format
       arguments: (va_list)arguments
{
	self = [super init];

	@try {
		int len;

		if (fmt == nil)
		if (format == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((len = of_vasprintf(&string, [fmt cString], args)) == -1)
		if ((len = of_vasprintf(&string, [format cString],
		    arguments)) == -1)
			@throw [OFInvalidFormatException newWithClass: isa];

		@try {
			length = len;

			switch (of_string_check_utf8(string, length)) {
			case 1:
532
533
534
535
536
537
538
539

540
541
542

543
544
545
546
547




548
549
550
551
552
553


554
555
556
557
558
559
560

561
562

563
564


565
566
567
568
569
570
571
572
573
574


575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592


593
594
595

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

612
613
614
615
616
617
618


619
620
621
622
623
624
625
534
535
536
537
538
539
540

541
542
543

544
545




546
547
548
549
550
551
552
553


554
555
556
557
558
559
560
561

562
563

564
565

566
567
568
569
570
571
572
573
574
575


576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593


594
595
596
597

598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613

614
615
616
617
618
619


620
621
622
623
624
625
626
627
628







-
+


-
+

-
-
-
-
+
+
+
+




-
-
+
+






-
+

-
+

-
+
+








-
-
+
+
















-
-
+
+


-
+















-
+





-
-
+
+







		[self release];
		@throw e;
	}

	return self;
}

- initWithPath: (OFString*)first, ...
- initWithPath: (OFString*)firstComponent, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, first);
	ret = [self initWithPath: first
		       arguments: args];
	va_end(args);
	va_start(arguments, firstComponent);
	ret = [self initWithPath: firstComponent
		       arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithPath: (OFString*)first
     arguments: (va_list)args
- initWithPath: (OFString*)firstComponent
     arguments: (va_list)arguments
{
	self = [super init];

	@try {
		OFString *component;
		size_t len, i;
		va_list args2;
		va_list argumentsCopy;

		length = [first cStringLength];
		length = [firstComponent cStringLength];

		switch (of_string_check_utf8([first cString], length)) {
		switch (of_string_check_utf8([firstComponent cString],
		    length)) {
		case 1:
			isUTF8 = YES;
			break;
		case -1:
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Calculate length */
		va_copy(args2, args);
		while ((component = va_arg(args2, OFString*)) != nil) {
		va_copy(argumentsCopy, arguments);
		while ((component = va_arg(argumentsCopy, OFString*)) != nil) {
			len = [component cStringLength];
			length += 1 + len;

			switch (of_string_check_utf8([component cString],
			    len)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}

		string = [self allocMemoryWithSize: length + 1];

		len = [first cStringLength];
		memcpy(string, [first cString], len);
		len = [firstComponent cStringLength];
		memcpy(string, [firstComponent cString], len);
		i = len;

		while ((component = va_arg(args, OFString*)) != nil) {
		while ((component = va_arg(arguments, OFString*)) != nil) {
			len = [component cStringLength];
			string[i] = OF_PATH_DELIM;
			memcpy(string + i + 1, [component cString], len);
			i += len + 1;
		}

		string[i] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithString: (OFString*)str
- initWithString: (OFString*)string_
{
	self = [super init];

	@try {
		/* We have no -[dealloc], so this is ok */
		string = (char*)[str cString];
		length = [str cStringLength];
		string = (char*)[string_ cString];
		length = [string_ cStringLength];

		switch (of_string_check_utf8(string, length)) {
		case 1:
			isUTF8 = YES;
			break;
		case -1:;
			@throw [OFInvalidEncodingException newWithClass: isa];
686
687
688
689
690
691
692
693

694
695

696
697
698
699

700
701
702
703
704


705
706
707
708
709
710
711
712
713
714


715
716
717
718
719
720
721


722
723

724
725
726
727



728
729

730
731

732
733
734
735
736
737
738
689
690
691
692
693
694
695

696
697

698
699
700
701

702
703
704
705


706
707
708
709
710
711
712
713
714
715


716
717
718
719
720
721
722


723
724
725

726
727



728
729
730
731

732
733

734
735
736
737
738
739
740
741







-
+

-
+



-
+



-
-
+
+








-
-
+
+





-
-
+
+

-
+

-
-
-
+
+
+

-
+

-
+







			    encoding: encoding
			      length: s.st_size];
	[self freeMemory: tmp];

	return self;
}

- initWithContentsOfURL: (OFURL*)url
- initWithContentsOfURL: (OFURL*)URL
{
	return [self initWithContentsOfURL: url
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfURL: (OFURL*)url
- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	OFAutoreleasePool *pool;
	OFHTTPRequest *req;
	OFHTTPRequestResult *res;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;
	Class c;

	c = isa;
	[self release];
	self = nil;

	pool = [[OFAutoreleasePool alloc] init];

	if ([[url scheme] isEqual: @"file"]) {
		self = [[c alloc] initWithContentsOfFile: [url path]
	if ([[URL scheme] isEqual: @"file"]) {
		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];
		[pool release];
		return self;
	}

	req = [OFHTTPRequest requestWithURL: url];
	res = [req perform];
	request = [OFHTTPRequest requestWithURL: URL];
	result = [request perform];

	if ([res statusCode] != 200)
	if ([result statusCode] != 200)
		@throw [OFHTTPRequestFailedException
		    newWithClass: [req class]
		     HTTPRequest: req
		      statusCode: [res statusCode]];
		    newWithClass: [request class]
		     HTTPRequest: request
		      statusCode: [result statusCode]];

	self = [[c alloc] initWithCString: (char*)[[res data] cArray]
	self = [[c alloc] initWithCString: (char*)[[result data] cArray]
				 encoding: encoding
				   length: [[res data] count]];
				   length: [[result data] count]];
	[pool release];
	return self;
}

- (const char*)cString
{
	return string;
751
752
753
754
755
756
757
758

759


760

761



762

763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778

779

780

781
782
783

784
785
786
787
788




789
790
791


792
793

794
795
796
797
798
799
800
801
802
803
804

805
806
807


808
809
810

811
812
813
814
815


816
817
818

819
820
821


822
823

824
825
826
827
828
829
830
831
832
833
834
835
836

837
838
839
840
841


842
843
844
845
846
847
848
754
755
756
757
758
759
760

761
762
763
764

765
766
767
768
769

770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785

786
787
788

789
790
791

792
793
794
795


796
797
798
799
800


801
802
803

804
805
806
807
808
809
810
811
812
813
814

815
816


817
818
819
820

821
822
823
824


825
826
827
828

829
830


831
832
833

834
835
836
837
838
839
840
841
842
843
844
845
846

847
848
849
850
851

852
853
854
855
856
857
858
859
860







-
+

+
+
-
+

+
+
+
-
+















-
+

+
-
+


-
+



-
-
+
+
+
+

-
-
+
+

-
+










-
+

-
-
+
+


-
+



-
-
+
+


-
+

-
-
+
+

-
+












-
+




-
+
+







}

- (BOOL)isUTF8
{
	return isUTF8;
}

- (BOOL)isEqual: (id)obj
- (BOOL)isEqual: (id)object
{
	OFString *otherString;

	if (![obj isKindOfClass: [OFString class]])
	if (![object isKindOfClass: [OFString class]])
		return NO;

	otherString = (OFString*)object;

	if (strcmp(string, [(OFString*)obj cString]))
	if (strcmp(string, [otherString cString]))
		return NO;

	return YES;
}

- copy
{
	return [self retain];
}

- mutableCopy
{
	return [[OFMutableString alloc] initWithString: self];
}

- (of_comparison_result_t)compare: (id)obj
- (of_comparison_result_t)compare: (id)object
{
	OFString *otherString;
	size_t str_len, min_len;
	size_t otherLen, minLen;
	int cmp;

	if (![obj isKindOfClass: [OFString class]])
	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	str_len = [(OFString*)obj cStringLength];
	min_len = (length > str_len ? str_len : length);
	otherString = (OFString*)object;

	otherLen = [otherString cStringLength];
	minLen = (length > otherLen ? otherLen : length);

	if ((cmp = memcmp(string, [(OFString*)obj cString], min_len)) == 0) {
		if (length > str_len)
	if ((cmp = memcmp(string, [otherString cString], minLen)) == 0) {
		if (length > otherLen)
			return OF_ORDERED_DESCENDING;
		if (length < str_len)
		if (length < otherLen)
			return OF_ORDERED_ASCENDING;
		return OF_ORDERED_SAME;
	}

	if (cmp > 0)
		return OF_ORDERED_DESCENDING;
	else
		return OF_ORDERED_ASCENDING;
}

- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)str
- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString;
{
	const char *str_cstr;
	size_t i, j, str_len, min_len;
	const char *otherCString;
	size_t i, j, otherLen, minLen;
	int cmp;

	if (![str isKindOfClass: [OFString class]])
	if (![otherString isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	str_cstr = [str cString];
	str_len = [str cStringLength];
	otherCString = [otherString cString];
	otherLen = [otherString cStringLength];

	if (![self isUTF8]) {
		min_len = (length > str_len ? str_len : length);
		minLen = (length > otherLen ? otherLen : length);

		if ((cmp = memcasecmp(string, [str cString], min_len)) == 0) {
			if (length > str_len)
		if ((cmp = memcasecmp(string, otherCString, minLen)) == 0) {
			if (length > otherLen)
				return OF_ORDERED_DESCENDING;
			if (length < str_len)
			if (length < otherLen)
				return OF_ORDERED_ASCENDING;
			return OF_ORDERED_SAME;
		}

		if (cmp > 0)
			return OF_ORDERED_DESCENDING;
		else
			return OF_ORDERED_ASCENDING;
	}

	i = j = 0;

	while (i < length && j < str_len) {
	while (i < length && j < otherLen) {
		of_unichar_t c1, c2;
		size_t l1, l2;

		l1 = of_string_utf8_to_unicode(string + i, length - i, &c1);
		l2 = of_string_utf8_to_unicode(str_cstr + j, str_len - j, &c2);
		l2 = of_string_utf8_to_unicode(otherCString + j, otherLen - j,
		    &c2);

		if (l1 == 0 || l2 == 0 || c1 > 0x10FFFF || c2 > 0x10FFFF)
			@throw [OFInvalidEncodingException newWithClass: isa];

		if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
			of_unichar_t tc =
			    of_unicode_casefolding_table[c1 >> 8][c1 & 0xFF];
864
865
866
867
868
869
870
871

872
873

874
875
876
877
878
879
880
876
877
878
879
880
881
882

883
884

885
886
887
888
889
890
891
892







-
+

-
+







		if (c1 < c2)
			return OF_ORDERED_ASCENDING;

		i += l1;
		j += l2;
	}

	if (length - i > str_len - j)
	if (length - i > otherLen - j)
		return OF_ORDERED_DESCENDING;
	else if (length - i < str_len - j)
	else if (length - i < otherLen - j)
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (uint32_t)hash
{
912
913
914
915
916
917
918
919

920
921
922


923
924
925

926
927
928

929
930
931
932


933
934
935
936
937
938

939
940
941


942
943
944

945
946
947

948
949
950
951


952
953
954
955
956
957
958
959
960

961
962
963


964
965
966

967
968
969

970
971
972
973


974
975
976
977
978
979
980
924
925
926
927
928
929
930

931
932


933
934
935
936

937
938
939

940
941
942


943
944
945
946
947
948
949

950
951


952
953
954
955

956
957
958

959
960
961


962
963
964
965
966
967
968
969
970
971

972
973


974
975
976
977

978
979
980

981
982
983


984
985
986
987
988
989
990
991
992







-
+

-
-
+
+


-
+


-
+


-
-
+
+





-
+

-
-
+
+


-
+


-
+


-
-
+
+








-
+

-
-
+
+


-
+


-
+


-
-
+
+








	if (!of_string_utf8_to_unicode(string + index, length - index, &c))
		@throw [OFInvalidEncodingException newWithClass: isa];

	return c;
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)str
- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string_
{
	const char *str_c = [str cString];
	size_t str_len = [str cStringLength];
	const char *cString = [string_ cString];
	size_t stringLen = [string_ cStringLength];
	size_t i;

	if (str_len == 0)
	if (stringLen == 0)
		return 0;

	if (str_len > length)
	if (stringLen > length)
		return OF_INVALID_INDEX;

	for (i = 0; i <= length - str_len; i++)
		if (!memcmp(string + i, str_c, str_len))
	for (i = 0; i <= length - stringLen; i++)
		if (!memcmp(string + i, cString, stringLen))
			return of_string_position_to_index(string, i);

	return OF_INVALID_INDEX;
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)str
- (size_t)indexOfLastOccurrenceOfString: (OFString*)string_
{
	const char *str_c = [str cString];
	size_t str_len = [str cStringLength];
	const char *cString = [string_ cString];
	size_t stringLen = [string_ cStringLength];
	size_t i;

	if (str_len == 0)
	if (stringLen == 0)
		return of_string_position_to_index(string, length);

	if (str_len > length)
	if (stringLen > length)
		return OF_INVALID_INDEX;

	for (i = length - str_len;; i--) {
		if (!memcmp(string + i, str_c, str_len))
	for (i = length - stringLen;; i--) {
		if (!memcmp(string + i, cString, stringLen))
			return of_string_position_to_index(string, i);

		/* Did not match and we're at the last char */
		if (i == 0)
			return OF_INVALID_INDEX;
	}
}

- (BOOL)containsString: (OFString*)str
- (BOOL)containsString: (OFString*)string_
{
	const char *str_c = [str cString];
	size_t str_len = [str cStringLength];
	const char *cString = [string_ cString];
	size_t stringLen = [string_ cStringLength];
	size_t i;

	if (str_len == 0)
	if (stringLen == 0)
		return YES;

	if (str_len > length)
	if (stringLen > length)
		return NO;

	for (i = 0; i <= length - str_len; i++)
		if (!memcmp(string + i, str_c, str_len))
	for (i = 0; i <= length - stringLen; i++)
		if (!memcmp(string + i, cString, stringLen))
			return YES;

	return NO;
}

- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end
997
998
999
1000
1001
1002
1003
1004

1005
1006
1007
1008
1009

1010
1011
1012
1013
1014
1015
1016
1009
1010
1011
1012
1013
1014
1015

1016
1017
1018
1019
1020

1021
1022
1023
1024
1025
1026
1027
1028







-
+




-
+








- (OFString*)substringWithRange: (of_range_t)range
{
	return [self substringFromIndex: range.start
				toIndex: range.start + range.length];
}

- (OFString*)stringByAppendingString: (OFString*)str
- (OFString*)stringByAppendingString: (OFString*)string_
{
	OFMutableString *new;

	new = [OFMutableString stringWithString: self];
	[new appendString: str];
	[new appendString: string_];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	new->isa = [OFString class];
1086
1087
1088
1089
1090
1091
1092
1093

1094
1095
1096
1097
1098
1099

1100
1101
1102
1103
1104
1105
1106
1107


1108
1109
1110
1111
1112

1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132

1133
1134
1135
1136

1137
1138
1139
1140
1141
1142

1143
1144

1145
1146

1147
1148

1149
1150
1151
1152
1153
1154
1155
1098
1099
1100
1101
1102
1103
1104

1105
1106
1107
1108
1109
1110

1111
1112
1113
1114
1115
1116
1117


1118
1119
1120
1121
1122
1123

1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143

1144
1145
1146
1147

1148
1149
1150
1151
1152
1153

1154
1155

1156
1157

1158
1159

1160
1161
1162
1163
1164
1165
1166
1167







-
+





-
+






-
-
+
+




-
+



















-
+



-
+





-
+

-
+

-
+

-
+







}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
{
	OFAutoreleasePool *pool;
	OFMutableArray *array;
	const char *delim = [delimiter cString];
	size_t delim_len = [delimiter cStringLength];
	size_t delimLen = [delimiter cStringLength];
	size_t i, last;

	array = [OFMutableArray array];
	pool = [[OFAutoreleasePool alloc] init];

	if (delim_len > length) {
	if (delimLen > length) {
		[array addObject: [[self copy] autorelease]];
		[pool release];

		return array;
	}

	for (i = 0, last = 0; i <= length - delim_len; i++) {
		if (memcmp(string + i, delim, delim_len))
	for (i = 0, last = 0; i <= length - delimLen; i++) {
		if (memcmp(string + i, delim, delimLen))
			continue;

		[array addObject: [OFString stringWithCString: string + last
						       length: i - last]];
		i += delim_len - 1;
		i += delimLen - 1;
		last = i + 1;
	}
	[array addObject: [OFString stringWithCString: string + last]];

	[pool release];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	array->isa = [OFArray class];
	return array;
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	OFAutoreleasePool *pool;
	size_t i, last = 0, path_len = length;
	size_t i, last = 0, pathLen = length;

	ret = [OFMutableArray array];

	if (path_len == 0)
	if (pathLen == 0)
		return ret;

	pool = [[OFAutoreleasePool alloc] init];

#ifndef _WIN32
	if (string[path_len - 1] == OF_PATH_DELIM)
	if (string[pathLen - 1] == OF_PATH_DELIM)
#else
	if (string[path_len - 1] == '/' || string[path_len - 1] == '\\')
	if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\')
#endif
		path_len--;
		pathLen--;

	for (i = 0; i < path_len; i++) {
	for (i = 0; i < pathLen; i++) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			[ret addObject:
			    [OFString stringWithCString: string + last
1170
1171
1172
1173
1174
1175
1176
1177

1178
1179
1180

1181
1182
1183
1184

1185
1186

1187
1188

1189
1190

1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209

1210
1211
1212
1213
1214

1215
1216

1217
1218
1219
1220

1221
1222

1223
1224

1225
1226

1227
1228
1229
1230

1231
1232
1233
1234
1235
1236
1237
1182
1183
1184
1185
1186
1187
1188

1189
1190
1191

1192
1193
1194
1195

1196
1197

1198
1199

1200
1201

1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220

1221
1222
1223
1224
1225

1226
1227

1228
1229
1230
1231

1232
1233

1234
1235

1236
1237

1238
1239
1240
1241

1242
1243
1244
1245
1246
1247
1248
1249







-
+


-
+



-
+

-
+

-
+

-
+


















-
+




-
+

-
+



-
+

-
+

-
+

-
+



-
+







	 */
	ret->isa = [OFArray class];
	return ret;
}

- (OFString*)lastPathComponent
{
	size_t path_len = length;
	size_t pathLen = length;
	ssize_t i;

	if (path_len == 0)
	if (pathLen == 0)
		return @"";

#ifndef _WIN32
	if (string[path_len - 1] == OF_PATH_DELIM)
	if (string[pathLen - 1] == OF_PATH_DELIM)
#else
	if (string[path_len - 1] == '/' || string[path_len - 1] == '\\')
	if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\')
#endif
		path_len--;
		pathLen--;

	for (i = path_len - 1; i >= 0; i--) {
	for (i = pathLen - 1; i >= 0; i--) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			i++;
			break;
		}
	}

	/*
	 * Only one component, but the trailing delimiter might have been
	 * removed, so return a new string anyway.
	 */
	if (i < 0)
		i = 0;

	return [OFString stringWithCString: string + i
				    length: path_len - i];
				    length: pathLen - i];
}

- (OFString*)stringByDeletingLastPathComponent;
{
	size_t i, path_len = length;
	size_t i, pathLen = length;

	if (path_len == 0)
	if (pathLen == 0)
		return @"";

#ifndef _WIN32
	if (string[path_len - 1] == OF_PATH_DELIM)
	if (string[pathLen - 1] == OF_PATH_DELIM)
#else
	if (string[path_len - 1] == '/' || string[path_len - 1] == '\\')
	if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\')
#endif
		path_len--;
		pathLen--;

	if (path_len == 0)
	if (pathLen == 0)
		return [OFString stringWithCString: string
					    length: 1];

	for (i = path_len - 1; i >= 1; i--)
	for (i = pathLen - 1; i >= 1; i--)
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM)
#else
		if (string[i] == '/' || string[i] == '\\')
#endif
			return [OFString stringWithCString: string
						    length: i];
1351
1352
1353
1354
1355
1356
1357
1358

1359
1360
1361
1362
1363
1364
1365

1366
1367
1368
1369
1370
1371




1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382

1383
1384
1385
1386
1387
1388
1389

1390
1391
1392
1393
1394
1395




1396
1397
1398
1399
1400
1401
1402
1363
1364
1365
1366
1367
1368
1369

1370
1371
1372
1373
1374
1375
1376

1377
1378
1379




1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393

1394
1395
1396
1397
1398
1399
1400

1401
1402
1403




1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414







-
+






-
+


-
-
-
-
+
+
+
+










-
+






-
+


-
-
-
-
+
+
+
+








	return num;
}

- (float)floatValue
{
	const char *str = string;
	char *endptr;
	char *endPtr;
	float value;

	/* Don't depend on isspace and thus the used locale */
	while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r')
		str++;

	value = strtof(str, &endptr);
	value = strtof(str, &endPtr);

	/* Check if there are any invalid chars left */
	if (endptr != NULL) {
		for (; *endptr != '\0'; endptr++)
			if (*endptr != ' ' && *endptr != '\t' &&
			    *endptr != '\n' && *endptr != '\r')
	if (endPtr != NULL) {
		for (; *endPtr != '\0'; endPtr++)
			if (*endPtr != ' ' && *endPtr != '\t' &&
			    *endPtr != '\n' && *endPtr != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
	}

	return value;
}

- (double)doubleValue
{
	const char *str = string;
	char *endptr;
	char *endPtr;
	double value;

	/* Don't depend on isspace and thus the used locale */
	while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r')
		str++;

	value = strtod(str, &endptr);
	value = strtod(str, &endPtr);

	/* Check if there are any invalid chars left */
	if (endptr != NULL) {
		for (; *endptr != '\0'; endptr++)
			if (*endptr != ' ' && *endptr != '\t' &&
			    *endptr != '\n' && *endptr != '\r')
	if (endPtr != NULL) {
		for (; *endPtr != '\0'; endPtr++)
			if (*endPtr != ' ' && *endPtr != '\t' &&
			    *endPtr != '\n' && *endPtr != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
	}

	return value;
}

1410
1411
1412
1413
1414
1415
1416
1417

1418
1419

1420
1421

1422
1423
1424
1425
1426
1427

1428
1429
1430
1431
1432
1433
1434
1422
1423
1424
1425
1426
1427
1428

1429
1430

1431
1432

1433
1434
1435
1436
1437
1438

1439
1440
1441
1442
1443
1444
1445
1446







-
+

-
+

-
+





-
+







	if ((ret = malloc((len + 1) * sizeof(of_unichar_t))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa];

	i = j = 0;

	while (i < length) {
		of_unichar_t c;
		size_t clen;
		size_t cLen;

		clen = of_string_utf8_to_unicode(string + i, length - 1, &c);
		cLen = of_string_utf8_to_unicode(string + i, length - 1, &c);

		if (clen == 0 || c > 0x10FFFF) {
		if (cLen == 0 || c > 0x10FFFF) {
			free(ret);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		ret[j++] = c;
		i += clen;
		i += cLen;
	}

	ret[j] = 0;

	return ret;
}

Modified src/base64.m from [5331624491] to [763d209bf9].

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







-
+


-
+





-
+

-
-
+
+












-
-
+
+







-
+







	 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
	20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1
};

OFString*
of_base64_encode(const char *data, size_t length)
of_base64_encode(const char *data, size_t len)
{
	OFMutableString *ret = [OFMutableString string];
	uint8_t *buffer = (uint8_t*)data;
	uint8_t *buf = (uint8_t*)data;
	size_t i;
	uint8_t rest;
	char tb[4];
	uint32_t sb;

	rest = length % 3;
	rest = len % 3;

	for (i = 0; i < length - rest; i += 3) {
		sb = (buffer[i] << 16) | (buffer[i + 1] << 8) | buffer[i + 2];
	for (i = 0; i < len - rest; i += 3) {
		sb = (buf[i] << 16) | (buf[i + 1] << 8) | buf[i + 2];

		tb[0] = of_base64_encode_table[(sb & 0xFC0000) >> 18];
		tb[1] = of_base64_encode_table[(sb & 0x03F000) >> 12];
		tb[2] = of_base64_encode_table[(sb & 0x000FC0) >> 6];
		tb[3] = of_base64_encode_table[sb & 0x00003F];

		[ret appendCStringWithoutUTF8Checking: tb
					       length: 4];
	}

	switch (rest) {
	case 1:
		tb[0] = of_base64_encode_table[buffer[i] >> 2];
		tb[1] = of_base64_encode_table[(buffer[i] & 3) << 4];
		tb[0] = of_base64_encode_table[buf[i] >> 2];
		tb[1] = of_base64_encode_table[(buf[i] & 3) << 4];
		tb[2] = tb[3] = '=';

		[ret appendCStringWithoutUTF8Checking: tb
					       length: 4];

		break;
	case 2:
		sb = (buffer[i] << 16) | (buffer[i + 1] << 8);
		sb = (buf[i] << 16) | (buf[i + 1] << 8);

		tb[0] = of_base64_encode_table[(sb & 0xFC0000) >> 18];
		tb[1] = of_base64_encode_table[(sb & 0x03F000) >> 12];
		tb[2] = of_base64_encode_table[(sb & 0x000FC0) >> 6];
		tb[3] = '=';

		[ret appendCStringWithoutUTF8Checking: tb
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
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







-
+

-
+











-
-
+
+


-
-
+
+


-
+

-
+


-
+




-
+




-
+




-
+







	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFString class];
	return ret;
}

BOOL
of_base64_decode(OFDataArray *data, const char *cString, size_t len)
of_base64_decode(OFDataArray *data, const char *str, size_t len)
{
	const uint8_t *buffer = (const uint8_t*)cString;
	const uint8_t *buf = (const uint8_t*)str;
	size_t i;

	if ((len & 3) != 0)
		return NO;

	for (i = 0; i < len; i += 4) {
		uint32_t sb = 0;
		uint8_t count = 3;
		char db[3];
		char tmp;

		if (buffer[i] > 0x7F || buffer[i + 1] > 0x7F ||
		    buffer[i + 2] > 0x7F || buffer[i + 3] > 0x7F)
		if (buf[i] > 0x7F || buf[i + 1] > 0x7F ||
		    buf[i + 2] > 0x7F || buf[i + 3] > 0x7F)
			return NO;

		if (buffer[i] == '=' || buffer[i + 1] == '=' ||
		    (buffer[i + 2] == '=' && buffer[i + 3] != '='))
		if (buf[i] == '=' || buf[i + 1] == '=' ||
		    (buf[i + 2] == '=' && buf[i + 3] != '='))
			return NO;

		if (buffer[i + 2] == '=')
		if (buf[i + 2] == '=')
			count--;
		if (buffer[i + 3] == '=')
		if (buf[i + 3] == '=')
			count--;

		if ((tmp = of_base64_decode_table[buffer[i]]) == -1)
		if ((tmp = of_base64_decode_table[buf[i]]) == -1)
			return NO;

		sb |= tmp << 18;

		if ((tmp = of_base64_decode_table[buffer[i + 1]]) == -1)
		if ((tmp = of_base64_decode_table[buf[i + 1]]) == -1)
			return NO;

		sb |= tmp << 12;

		if ((tmp = of_base64_decode_table[buffer[i + 2]]) == -1)
		if ((tmp = of_base64_decode_table[buf[i + 2]]) == -1)
			return NO;

		sb |= tmp << 6;

		if ((tmp = of_base64_decode_table[buffer[i + 3]]) == -1)
		if ((tmp = of_base64_decode_table[buf[i + 3]]) == -1)
			return NO;

		sb |= tmp;

		db[0] = (sb & 0xFF0000) >> 16;
		db[1] = (sb & 0x00FF00) >> 8;
		db[2] = sb & 0x0000FF;