ObjFW  Check-in [640b225ba7]

Overview
Comment:OFString: Improve API for Unicode strings.

* Instead of Unicode "strings", it uses the term "Unicode character
array".
* No terminating 0 character anymore, as these are not C strings
anyway.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 640b225ba7ab629d98a314f0954b09f3183098557d4de0440ee9c9b817cf7e5a
User & Date: js on 2012-12-29 17:56:05
Other Links: manifest | tags
Context
2012-12-30
00:19
-[performSelector:]: Never call NULL. check-in: 0389649320 user: js tags: trunk
2012-12-29
17:56
OFString: Improve API for Unicode strings. check-in: 640b225ba7 user: js tags: trunk
14:43
Prettify some code. check-in: 374956b098 user: js tags: trunk
Changes

Modified src/OFMutableString.m from [b484829d75] to [5f0048c446].

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
}

- initWithString: (OFString*)string
{
	return (id)[[OFMutableString_UTF8 alloc] initWithString: string];
}

- initWithUnicodeString: (const of_unichar_t*)string
{
	return (id)[[OFMutableString_UTF8 alloc] initWithUnicodeString: string];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFMutableString_UTF8 alloc]
	    initWithUnicodeString: string
			byteOrder: byteOrder];
}

- initWithUnicodeString: (const of_unichar_t*)string
		 length: (size_t)length
{
	return (id)[[OFMutableString_UTF8 alloc] initWithUnicodeString: string
								length: length];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
		 length: (size_t)length
{
	return (id)[[OFMutableString_UTF8 alloc]
	    initWithUnicodeString: string
			byteOrder: byteOrder
			   length: length];
}

- initWithUTF16String: (const uint16_t*)string
{
	return (id)[[OFMutableString_UTF8 alloc] initWithUTF16String: string];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFMutableString_UTF8 alloc]
	    initWithUTF16String: string

		      byteOrder: byteOrder];
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return (id)[[OFMutableString_UTF8 alloc] initWithUTF16String: string
							      length: length];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
	       length: (size_t)length

{
	return (id)[[OFMutableString_UTF8 alloc]
	    initWithUTF16String: string

		      byteOrder: byteOrder
			 length: length];
}

- initWithFormat: (OFConstantString*)format, ...
{
	id ret;
	va_list arguments;








|
<
<
<
<
<
<
<
<
<
<
<
<
<
|

|
|


|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|


|
>
|










<

>



>
|
<







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
}

- initWithString: (OFString*)string
{
	return (id)[[OFMutableString_UTF8 alloc] initWithString: string];
}

- initWithCharacters: (const of_unichar_t*)characters













	      length: (size_t)length
{
	return (id)[[OFMutableString_UTF8 alloc] initWithCharacters: characters
							     length: length];
}

- initWithCharacters: (const of_unichar_t*)characters

	      length: (size_t)length













	   byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFMutableString_UTF8 alloc]
	    initWithCharacters: characters
			length: length
		     byteOrder: byteOrder];
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return (id)[[OFMutableString_UTF8 alloc] initWithUTF16String: string
							      length: length];
}

- initWithUTF16String: (const uint16_t*)string

	       length: (size_t)length
	    byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFMutableString_UTF8 alloc]
	    initWithUTF16String: string
			 length: length
		      byteOrder: byteOrder];

}

- initWithFormat: (OFConstantString*)format, ...
{
	id ret;
	va_list arguments;

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

- (void)OF_convertWithWordStartTable: (const of_unichar_t *const[])startTable
		     wordMiddleTable: (const of_unichar_t *const[])middleTable
		  wordStartTableSize: (size_t)startTableSize
		 wordMiddleTableSize: (size_t)middleTableSize
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string = [self unicodeString];
	size_t i, length = [self length];
	BOOL isStart = YES;

	for (i = 0; i < length; i++) {
		const of_unichar_t *const *table;
		size_t tableSize;
		of_unichar_t c = string[i];

		if (isStart) {
			table = startTable;
			tableSize = middleTableSize;
		} else {
			table = middleTable;
			tableSize = middleTableSize;







|






|







231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

- (void)OF_convertWithWordStartTable: (const of_unichar_t *const[])startTable
		     wordMiddleTable: (const of_unichar_t *const[])middleTable
		  wordStartTableSize: (size_t)startTableSize
		 wordMiddleTableSize: (size_t)middleTableSize
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = [self characters];
	size_t i, length = [self length];
	BOOL isStart = YES;

	for (i = 0; i < length; i++) {
		const of_unichar_t *const *table;
		size_t tableSize;
		of_unichar_t c = characters[i];

		if (isStart) {
			table = startTable;
			tableSize = middleTableSize;
		} else {
			table = middleTable;
			tableSize = middleTableSize;
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

- (void)setCharacter: (of_unichar_t)character
	     atIndex: (size_t)index
{
	void *pool = objc_autoreleasePoolPush();
	OFString *string;

	string = [OFString stringWithUnicodeString: &character
					    length: 1];

	[self replaceCharactersInRange: of_range(index, 1)
			    withString: string];

	objc_autoreleasePoolPop(pool);
}








|
|







274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

- (void)setCharacter: (of_unichar_t)character
	     atIndex: (size_t)index
{
	void *pool = objc_autoreleasePoolPush();
	OFString *string;

	string = [OFString stringWithCharacters: &character
					 length: 1];

	[self replaceCharactersInRange: of_range(index, 1)
			    withString: string];

	objc_autoreleasePoolPop(pool);
}

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

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
			   options: (int)options
			     range: (of_range_t)range
{
	void *pool = objc_autoreleasePoolPush(), *pool2;
	const of_unichar_t *unicodeString;
	const of_unichar_t *searchString = [string unicodeString];
	size_t searchLength = [string length];
	size_t replacementLength = [replacement length];
	size_t i;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > [self length])
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (searchLength > range.length) {
		objc_autoreleasePoolPop(pool);
		return;
	}

	pool2 = objc_autoreleasePoolPush();
	unicodeString = [self unicodeString];

	for (i = range.location; i <= range.length - searchLength; i++) {
		if (memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t)))
			continue;

		[self replaceCharactersInRange: of_range(i, searchLength)
				    withString: replacement];

		range.length -= searchLength;
		range.length += replacementLength;

		i += replacementLength - 1;

		objc_autoreleasePoolPop(pool2);
		pool2 = objc_autoreleasePoolPush();

		unicodeString = [self unicodeString];
	}

	objc_autoreleasePoolPop(pool);
}

- (void)deleteLeadingWhitespaces
{







|
|














|


|














|







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

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
			   options: (int)options
			     range: (of_range_t)range
{
	void *pool = objc_autoreleasePoolPush(), *pool2;
	const of_unichar_t *characters;
	const of_unichar_t *searchCharacters = [string characters];
	size_t searchLength = [string length];
	size_t replacementLength = [replacement length];
	size_t i;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > [self length])
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if (searchLength > range.length) {
		objc_autoreleasePoolPop(pool);
		return;
	}

	pool2 = objc_autoreleasePoolPush();
	characters = [self characters];

	for (i = range.location; i <= range.length - searchLength; i++) {
		if (memcmp(characters + i, searchCharacters,
		    searchLength * sizeof(of_unichar_t)))
			continue;

		[self replaceCharactersInRange: of_range(i, searchLength)
				    withString: replacement];

		range.length -= searchLength;
		range.length += replacementLength;

		i += replacementLength - 1;

		objc_autoreleasePoolPop(pool2);
		pool2 = objc_autoreleasePoolPush();

		characters = [self characters];
	}

	objc_autoreleasePoolPop(pool);
}

- (void)deleteLeadingWhitespaces
{

Modified src/OFString.h from [5e9e46c622] to [05df72695a].

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
 * @brief Creates a new OFString from another string.
 *
 * @param string A string to initialize the OFString with
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithString: (OFString*)string;

/*!
 * @brief Creates a new OFString from a unicode string.
 *
 * @param string The unicode string
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string;

/*!
 * @brief Creates a new OFString from a unicode string, assuming the specified
 *	  byte order if no BOM is found.
 *
 * @param string The unicode string
 * @param byteOrder The byte order to assume if there is no BOM
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string
			      byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Creates a new OFString from a unicode string with the specified
 *	  length.
 *
 * @param string The unicode string
 * @param length The length of the unicode string
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string
				 length: (size_t)length;

/*!
 * @brief Creates a new OFString from a unicode string with the specified
 *	  length, assuming the specified byte order if no BOM is found.
 *
 * @param string The unicode string

 * @param byteOrder The byte order to assume if there is no BOM
 * @param length The length of the unicode string
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string
			      byteOrder: (of_byte_order_t)byteOrder
				 length: (size_t)length;

/*!
 * @brief Creates a new OFString from a big endian UTF-16 encoded string.
 *
 * @param string The UTF-16 string
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUTF16String: (const uint16_t*)string;

/*!
 * @brief Creates a new OFString from a UTF-16 encoded string, assuming the
 *	  specified byte order if no BOM is found.
 *
 * @param string The UTF-16 string
 * @param byteOrder The byte order to assume if there is no BOM
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			    byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Creates a new OFString from a big endian UTF-16 encoded string with
 *	  the specified length.
 *
 * @param string The UTF-16 string
 * @param length The length of the unicode string
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			       length: (size_t)length;

/*!
 * @brief Creates a new OFString from a UTF-16 encoded string with the
 *	  specified length, assuming the specified byte order if no BOM is
 *	  found.
 *
 * @param string The UTF-16 string
 * @param byteOrder The byte order to assume if there is no BOM
 * @param length The length of the unicode string

 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			    byteOrder: (of_byte_order_t)byteOrder
			       length: (size_t)length;


/*!
 * @brief Creates a new OFString from a format string.
 *
 * See printf for the format syntax. As an addition, %@ is available as format
 * specifier for objects.
 *







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




|
|


|
|





|
>

<


|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|


















<

>



<
|
>







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
 * @brief Creates a new OFString from another string.
 *
 * @param string A string to initialize the OFString with
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithString: (OFString*)string;




















/*!
 * @brief Creates a new OFString from a unicode string with the specified
 *	  length.
 *
 * @param characters An array of unicode characters
 * @param length The length of the unicode character array
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithCharacters: (const of_unichar_t*)characters
			      length: (size_t)length;

/*!
 * @brief Creates a new OFString from a unicode string with the specified
 *	  length, assuming the specified byte order if no BOM is found.
 *
 * @param characters An array of unicode characters
 * @param length The length of the unicode character array
 * @param byteOrder The byte order to assume if there is no BOM

 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithCharacters: (const of_unichar_t*)characters

			      length: (size_t)length


















			   byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Creates a new OFString from a big endian UTF-16 encoded string with
 *	  the specified length.
 *
 * @param string The UTF-16 string
 * @param length The length of the unicode string
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			       length: (size_t)length;

/*!
 * @brief Creates a new OFString from a UTF-16 encoded string with the
 *	  specified length, assuming the specified byte order if no BOM is
 *	  found.
 *
 * @param string The UTF-16 string

 * @param length The length of the unicode string
 * @param byteOrder The byte order to assume if there is no BOM
 * @return A new autoreleased OFString
 */
+ (instancetype)stringWithUTF16String: (const uint16_t*)string

			       length: (size_t)length
			    byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Creates a new OFString from a format string.
 *
 * See printf for the format syntax. As an addition, %@ is available as format
 * specifier for objects.
 *
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
417
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
 * @brief Initializes an already allocated OFString with another string.
 *
 * @param string A string to initialize the OFString with
 * @return An initialized OFString
 */
- initWithString: (OFString*)string;

/*!
 * @brief Initializes an already allocated OFString with a unicode string.
 *
 * @param string The unicode string
 * @return An initialized OFString
 */
- initWithUnicodeString: (const of_unichar_t*)string;

/*!
 * @brief Initializes an already allocated OFString with a unicode string,
 *	  assuming the specified byte order if no BOM is found.
 *
 * @param string The unicode string
 * @param byteOrder The byte order to assume if there is no BOM
 * @return An initialized OFString
 */
- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Initializes an already allocated OFString with a unicode string with
 *	  the specified length.
 *
 * @param string The unicode string
 * @param length The length of the unicode string
 * @return An initialized OFString
 */
- initWithUnicodeString: (const of_unichar_t*)string
		 length: (size_t)length;

/*!
 * @brief Initializes an already allocated OFString with a unicode string with
 *	  the specified length, assuming the specified byte order if no BOM is
 *	  found.
 *
 * @param string The unicode string

 * @param byteOrder The byte order to assume if there is no BOM
 * @param length The length of the unicode string
 * @return An initialized OFString
 */
- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
		 length: (size_t)length;

/*!
 * @brief Initializes an already allocated OFString with a UTF-16 string.
 *
 * @param string The UTF-16 string
 * @return An initialized OFString
 */
- initWithUTF16String: (const uint16_t*)string;

/*!
 * @brief Initializes an already allocated OFString with a UTF-16 string,
 *	  assuming the specified byte order if no BOM is found.
 *
 * @param string The UTF-16 string
 * @param byteOrder The byte order to assume if there is no BOM
 * @return An initialized OFString
 */
- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Initializes an already allocated OFString with a UTF-16 string with
 *	  the specified length.
 *
 * @param string The UTF-16 string
 * @param length The length of the UTF-16 string
 * @return An initialized OFString
 */
- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length;

/*!
 * @brief Initializes an already allocated OFString with a UTF-16 string with
 *	  the specified length, assuming the specified byte order if no BOM is
 *	  found.
 *
 * @param string The UTF-16 string
 * @param byteOrder The byte order to assume if there is no BOM
 * @param length The length of the UTF-16 string

 * @return An initialized OFString
 */
- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
	       length: (size_t)length;


/*!
 * @brief Initializes an already allocated OFString with a format string.
 *
 * See printf for the format syntax. As an addition, %@ is available as format
 * specifier for objects.
 *







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




|
|


|
|






|
>

<


|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|


















<

>



<
|
>







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
 * @brief Initializes an already allocated OFString with another string.
 *
 * @param string A string to initialize the OFString with
 * @return An initialized OFString
 */
- initWithString: (OFString*)string;




















/*!
 * @brief Initializes an already allocated OFString with a unicode string with
 *	  the specified length.
 *
 * @param characters An array of unicode characters
 * @param length The length of the unicode character array
 * @return An initialized OFString
 */
- initWithCharacters: (const of_unichar_t*)characters
	      length: (size_t)length;

/*!
 * @brief Initializes an already allocated OFString with a unicode string with
 *	  the specified length, assuming the specified byte order if no BOM is
 *	  found.
 *
 * @param characters An array of unicode characters
 * @param length The length of the unicode character array
 * @param byteOrder The byte order to assume if there is no BOM

 * @return An initialized OFString
 */
- initWithCharacters: (const of_unichar_t*)characters

	      length: (size_t)length


















	   byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Initializes an already allocated OFString with a UTF-16 string with
 *	  the specified length.
 *
 * @param string The UTF-16 string
 * @param length The length of the UTF-16 string
 * @return An initialized OFString
 */
- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length;

/*!
 * @brief Initializes an already allocated OFString with a UTF-16 string with
 *	  the specified length, assuming the specified byte order if no BOM is
 *	  found.
 *
 * @param string The UTF-16 string

 * @param length The length of the UTF-16 string
 * @param byteOrder The byte order to assume if there is no BOM
 * @return An initialized OFString
 */
- initWithUTF16String: (const uint16_t*)string

	       length: (size_t)length
	    byteOrder: (of_byte_order_t)byteOrder;

/*!
 * @brief Initializes an already allocated OFString with a format string.
 *
 * See printf for the format syntax. As an addition, %@ is available as format
 * specifier for objects.
 *
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *
 * @return The string as an array of Unicode characters
 */
- (const of_unichar_t*)unicodeString OF_RETURNS_INNER_POINTER;

/*!
 * @brief Returns the string in big endian UTF-16 encoding.
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.







|







829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *
 * @return The string as an array of Unicode characters
 */
- (const of_unichar_t*)characters OF_RETURNS_INNER_POINTER;

/*!
 * @brief Returns the string in big endian UTF-16 encoding.
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
954
955
956
957
958
959
960
961
962
963
964
965
#endif

#ifdef __cplusplus
extern "C" {
#endif
extern size_t of_string_utf8_encode(of_unichar_t, char*);
extern size_t of_string_utf8_decode(const char*, size_t, of_unichar_t*);
extern size_t of_string_utf16_length(const uint16_t*);
extern size_t of_string_unicode_length(const of_unichar_t*);
#ifdef __cplusplus
}
#endif







<
<



878
879
880
881
882
883
884


885
886
887
#endif

#ifdef __cplusplus
extern "C" {
#endif
extern size_t of_string_utf8_encode(of_unichar_t, char*);
extern size_t of_string_utf8_decode(const char*, size_t, of_unichar_t*);


#ifdef __cplusplus
}
#endif

Modified src/OFString.m from [03c484c2ac] to [2366afaf49].

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
		    ((buffer[2] & 0x3F) << 6) | (buffer[3] & 0x3F);
		return 4;
	}

	return 0;
}

size_t
of_string_unicode_length(const of_unichar_t *string)
{
	const of_unichar_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

size_t
of_string_utf16_length(const uint16_t *string)
{
	const uint16_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

static OFString*
standardize_path(OFArray *components, OFString *currentDirectory,
    OFString *parentDirectory, OFString *joinString)
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableArray *array;
	OFString *ret;







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







138
139
140
141
142
143
144






















145
146
147
148
149
150
151
		    ((buffer[2] & 0x3F) << 6) | (buffer[3] & 0x3F);
		return 4;
	}

	return 0;
}























static OFString*
standardize_path(OFArray *components, OFString *currentDirectory,
    OFString *parentDirectory, OFString *joinString)
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableArray *array;
	OFString *ret;
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
}

- initWithString: (OFString*)string
{
	return (id)[[OFString_UTF8 alloc] initWithString: string];
}

- initWithUnicodeString: (const of_unichar_t*)string
{
	return (id)[[OFString_UTF8 alloc] initWithUnicodeString: string];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFString_UTF8 alloc] initWithUnicodeString: string
						      byteOrder: byteOrder];
}

- initWithUnicodeString: (const of_unichar_t*)string
		 length: (size_t)length
{
	return (id)[[OFString_UTF8 alloc] initWithUnicodeString: string
							 length: length];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
		 length: (size_t)length
{
	return (id)[[OFString_UTF8 alloc] initWithUnicodeString: string
						      byteOrder: byteOrder
							 length: length];
}

- initWithUTF16String: (const uint16_t*)string
{
	return (id)[[OFString_UTF8 alloc] initWithUTF16String: string];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFString_UTF8 alloc] initWithUTF16String: string

						    byteOrder: byteOrder];
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return (id)[[OFString_UTF8 alloc] initWithUTF16String: string
						       length: length];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
	       length: (size_t)length

{
	return (id)[[OFString_UTF8 alloc] initWithUTF16String: string

						    byteOrder: byteOrder
						       length: length];
}

- initWithFormat: (OFConstantString*)format, ...
{
	id ret;
	va_list arguments;








|
<
<
<
<
<
<
<
<
<
<
<
<
|

|
|


|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
|

|
>
|










<

>


>
|
<







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
}

- initWithString: (OFString*)string
{
	return (id)[[OFString_UTF8 alloc] initWithString: string];
}

- initWithCharacters: (const of_unichar_t*)string












	      length: (size_t)length
{
	return (id)[[OFString_UTF8 alloc] initWithCharacters: string
						      length: length];
}

- initWithCharacters: (const of_unichar_t*)string

	      length: (size_t)length












	   byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFString_UTF8 alloc] initWithCharacters: string
						      length: length
						   byteOrder: byteOrder];
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return (id)[[OFString_UTF8 alloc] initWithUTF16String: string
						       length: length];
}

- initWithUTF16String: (const uint16_t*)string

	       length: (size_t)length
	    byteOrder: (of_byte_order_t)byteOrder
{
	return (id)[[OFString_UTF8 alloc] initWithUTF16String: string
						       length: length
						    byteOrder: byteOrder];

}

- initWithFormat: (OFConstantString*)format, ...
{
	id ret;
	va_list arguments;

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
}

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

+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string
{
	return [[[self alloc] initWithUnicodeString: string] autorelease];
}

+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string
			      byteOrder: (of_byte_order_t)byteOrder
{
	return [[[self alloc] initWithUnicodeString: string
					  byteOrder: byteOrder] autorelease];
}

+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string
				 length: (size_t)length
{
	return [[[self alloc] initWithUnicodeString: string
					     length: length] autorelease];
}

+ (instancetype)stringWithUnicodeString: (const of_unichar_t*)string
			      byteOrder: (of_byte_order_t)byteOrder
				 length: (size_t)length
{
	return [[[self alloc] initWithUnicodeString: string
					  byteOrder: byteOrder
					     length: length] autorelease];
}

+ (instancetype)stringWithUTF16String: (const uint16_t*)string
{
	return [[[self alloc] initWithUTF16String: string] autorelease];
}

+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			    byteOrder: (of_byte_order_t)byteOrder
{
	return [[[self alloc] initWithUTF16String: string

					byteOrder: byteOrder] autorelease];
}

+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			       length: (size_t)length
{
	return [[[self alloc] initWithUTF16String: string
					   length: length] autorelease];
}

+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			    byteOrder: (of_byte_order_t)byteOrder
			       length: (size_t)length

{
	return [[[self alloc] initWithUTF16String: string

					byteOrder: byteOrder
					   length: length] autorelease];
}

+ (instancetype)stringWithFormat: (OFConstantString*)format, ...
{
	id ret;
	va_list arguments;








|
<
<
<
<
<
<
<
<
<
<
<
<
|

|
|


|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
|

|
>
|










<

>


>
|
<







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
}

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

+ (instancetype)stringWithCharacters: (const of_unichar_t*)string












			      length: (size_t)length
{
	return [[[self alloc] initWithCharacters: string
					  length: length] autorelease];
}

+ (instancetype)stringWithCharacters: (const of_unichar_t*)string

			      length: (size_t)length












			   byteOrder: (of_byte_order_t)byteOrder
{
	return [[[self alloc] initWithCharacters: string
					  length: length
				       byteOrder: byteOrder] autorelease];
}

+ (instancetype)stringWithUTF16String: (const uint16_t*)string
			       length: (size_t)length
{
	return [[[self alloc] initWithUTF16String: string
					   length: length] autorelease];
}

+ (instancetype)stringWithUTF16String: (const uint16_t*)string

			       length: (size_t)length
			    byteOrder: (of_byte_order_t)byteOrder
{
	return [[[self alloc] initWithUTF16String: string
					   length: length
					byteOrder: byteOrder] autorelease];

}

+ (instancetype)stringWithFormat: (OFConstantString*)format, ...
{
	id ret;
	va_list arguments;

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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
		abort();
	} @catch (id e) {
		[self release];
		@throw e;
	}
}

- initWithUnicodeString: (const of_unichar_t*)string

{
	return [self initWithUnicodeString: string
				 byteOrder: OF_BYTE_ORDER_NATIVE
				    length: of_string_unicode_length(string)];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
{
	return [self initWithUnicodeString: string
				 byteOrder: byteOrder
				    length: of_string_unicode_length(string)];
}

- initWithUnicodeString: (const of_unichar_t*)string
		 length: (size_t)length
{
	return [self initWithUnicodeString: string
				 byteOrder: OF_BYTE_ORDER_NATIVE
				    length: length];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
		 length: (size_t)length
{
	@try {
		[self doesNotRecognizeSelector: _cmd];
		abort();
	} @catch (id e) {
		[self release];
		@throw e;
	}
}

- initWithUTF16String: (const uint16_t*)string

{
	return [self initWithUTF16String: string
			       byteOrder: OF_BYTE_ORDER_BIG_ENDIAN
				  length: of_string_utf16_length(string)];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
{
	return [self initWithUTF16String: string
			       byteOrder: byteOrder
				  length: of_string_utf16_length(string)];
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return [self initWithUTF16String: string
			       byteOrder: OF_BYTE_ORDER_BIG_ENDIAN
				  length: length];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
	       length: (size_t)length
{
	@try {
		[self doesNotRecognizeSelector: _cmd];
		abort();
	} @catch (id e) {
		[self release];
		@throw e;







|
>

|
<
|
<
<
<
<
<
<
|
<


|
|
<
<
<
<
<
<
<
|
<











>


<
|
<
<
<
<
<
<
|
<




<
<
<
<
<
<
<

<







618
619
620
621
622
623
624
625
626
627
628

629






630

631
632
633
634







635

636
637
638
639
640
641
642
643
644
645
646
647
648
649

650






651

652
653
654
655







656

657
658
659
660
661
662
663
		abort();
	} @catch (id e) {
		[self release];
		@throw e;
	}
}

- initWithCharacters: (const of_unichar_t*)string
	      length: (size_t)length
{
	return [self initWithCharacters: string

				 length: length






			      byteOrder: OF_BYTE_ORDER_NATIVE];

}

- initWithCharacters: (const of_unichar_t*)string
	      length: (size_t)length







	   byteOrder: (of_byte_order_t)byteOrder

{
	@try {
		[self doesNotRecognizeSelector: _cmd];
		abort();
	} @catch (id e) {
		[self release];
		@throw e;
	}
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return [self initWithUTF16String: string

				  length: length






			       byteOrder: OF_BYTE_ORDER_BIG_ENDIAN];

}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length







	    byteOrder: (of_byte_order_t)byteOrder

{
	@try {
		[self doesNotRecognizeSelector: _cmd];
		abort();
	} @catch (id e) {
		[self release];
		@throw e;
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
	}

	return self;
}

- (const char*)cStringUsingEncoding: (of_string_encoding_t)encoding
{
	const of_unichar_t *unicodeString = [self unicodeString];
	size_t i, length = [self length];
	OFObject *object = [[[OFObject alloc] init] autorelease];
	char *cString;

	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
		size_t cStringLength, j = 0;

		cString = [object allocMemoryWithSize: (length * 4) + 1];
		cStringLength = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(unicodeString[i],
			    buffer);

			switch (len) {
			case 1:
				cString[j++] = buffer[0];
				break;
			case 2:







|













|







861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
	}

	return self;
}

- (const char*)cStringUsingEncoding: (of_string_encoding_t)encoding
{
	const of_unichar_t *characters = [self characters];
	size_t i, length = [self length];
	OFObject *object = [[[OFObject alloc] init] autorelease];
	char *cString;

	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
		size_t cStringLength, j = 0;

		cString = [object allocMemoryWithSize: (length * 4) + 1];
		cStringLength = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(characters[i],
			    buffer);

			switch (len) {
			case 1:
				cString[j++] = buffer[0];
				break;
			case 2:
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
		}

		break;
	case OF_STRING_ENCODING_ASCII:
		cString = [object allocMemoryWithSize: length + 1];

		for (i = 0; i < length; i++) {
			if (unicodeString[i] > 0x80)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (char)unicodeString[i];
		}

		cString[i] = '\0';

		break;
	case OF_STRING_ENCODING_ISO_8859_1:
		cString = [object allocMemoryWithSize: length + 1];

		for (i = 0; i < length; i++) {
			if (unicodeString[i] > 0xFF)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (uint8_t)unicodeString[i];
		}

		cString[i] = '\0';

		break;
	default:
		@throw [OFNotImplementedException







|



|









|



|







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
		}

		break;
	case OF_STRING_ENCODING_ASCII:
		cString = [object allocMemoryWithSize: length + 1];

		for (i = 0; i < length; i++) {
			if (characters[i] > 0x80)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (char)characters[i];
		}

		cString[i] = '\0';

		break;
	case OF_STRING_ENCODING_ISO_8859_1:
		cString = [object allocMemoryWithSize: length + 1];

		for (i = 0; i < length; i++) {
			if (characters[i] > 0xFF)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (uint8_t)characters[i];
		}

		cString[i] = '\0';

		break;
	default:
		@throw [OFNotImplementedException
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
	abort();
}

- (size_t)lengthOfBytesUsingEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
		const of_unichar_t *unicodeString;
		size_t i, length, UTF8StringLength = 0;

		unicodeString = [self unicodeString];
		length = [self length];

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(unicodeString[i],
			    buffer);

			if (len == 0)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			UTF8StringLength += len;







|


|




|







973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
	abort();
}

- (size_t)lengthOfBytesUsingEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
		const of_unichar_t *characters;
		size_t i, length, UTF8StringLength = 0;

		characters = [self characters];
		length = [self length];

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(characters[i],
			    buffer);

			if (len == 0)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			UTF8StringLength += len;
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
		buffer[i] = [self characterAtIndex: range.location + i];
}

- (BOOL)isEqual: (id)object
{
	void *pool;
	OFString *otherString;
	const of_unichar_t *unicodeString, *otherUnicodeString;
	size_t length;

	if (object == self)
		return YES;

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

	otherString = object;
	length = [self length];

	if ([otherString length] != length)
		return NO;

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	otherUnicodeString = [otherString unicodeString];

	if (memcmp(unicodeString, otherUnicodeString,
	    length * sizeof(of_unichar_t))) {
		objc_autoreleasePoolPop(pool);
		return NO;
	}

	objc_autoreleasePoolPop(pool);








|
















|
|

|







1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
		buffer[i] = [self characterAtIndex: range.location + i];
}

- (BOOL)isEqual: (id)object
{
	void *pool;
	OFString *otherString;
	const of_unichar_t *characters, *otherCharacters;
	size_t length;

	if (object == self)
		return YES;

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

	otherString = object;
	length = [self length];

	if ([otherString length] != length)
		return NO;

	pool = objc_autoreleasePoolPush();

	characters = [self characters];
	otherCharacters = [otherString characters];

	if (memcmp(characters, otherCharacters,
	    length * sizeof(of_unichar_t))) {
		objc_autoreleasePoolPop(pool);
		return NO;
	}

	objc_autoreleasePoolPop(pool);

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
1238
1239
1240
1241
1242
1243
1244
1245
	return [[OFMutableString alloc] initWithString: self];
}

- (of_comparison_result_t)compare: (id <OFComparing>)object
{
	void *pool;
	OFString *otherString;
	const of_unichar_t *unicodeString, *otherUnicodeString;
	size_t i, minimumLength;

	if (object == self)
		return OF_ORDERED_SAME;

	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherString = (OFString*)object;
	minimumLength = ([self length] > [otherString length]
	    ? [otherString length] : [self length]);

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	otherUnicodeString = [otherString unicodeString];

	for (i = 0; i < minimumLength; i++) {
		if (unicodeString[i] > otherUnicodeString[i]) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_DESCENDING;
		}

		if (unicodeString[i] < otherUnicodeString[i]) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_ASCENDING;
		}
	}

	objc_autoreleasePoolPop(pool);

	if ([self length] > [otherString length])
		return OF_ORDERED_DESCENDING;
	if ([self length] < [otherString length])
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string, *otherUnicodeString;
	size_t i, length, otherLength, minimumLength;

	if (otherString == self)
		return OF_ORDERED_SAME;

	string = [self unicodeString];
	otherUnicodeString = [otherString unicodeString];
	length = [self length];
	otherLength = [otherString length];

	minimumLength = (length > otherLength ? otherLength : length);

	for (i = 0; i < minimumLength; i++) {
		of_unichar_t c = string[i];
		of_unichar_t oc = otherUnicodeString[i];

		if (c >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
			of_unichar_t tc =
			    of_unicode_casefolding_table[c >> 8][c & 0xFF];

			if (tc)
				c = tc;







|
















|
|


|




|


















|





|
|






|
|







1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
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
	return [[OFMutableString alloc] initWithString: self];
}

- (of_comparison_result_t)compare: (id <OFComparing>)object
{
	void *pool;
	OFString *otherString;
	const of_unichar_t *characters, *otherCharacters;
	size_t i, minimumLength;

	if (object == self)
		return OF_ORDERED_SAME;

	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherString = (OFString*)object;
	minimumLength = ([self length] > [otherString length]
	    ? [otherString length] : [self length]);

	pool = objc_autoreleasePoolPush();

	characters = [self characters];
	otherCharacters = [otherString characters];

	for (i = 0; i < minimumLength; i++) {
		if (characters[i] > otherCharacters[i]) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_DESCENDING;
		}

		if (characters[i] < otherCharacters[i]) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_ASCENDING;
		}
	}

	objc_autoreleasePoolPop(pool);

	if ([self length] > [otherString length])
		return OF_ORDERED_DESCENDING;
	if ([self length] < [otherString length])
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters, *otherCharacters;
	size_t i, length, otherLength, minimumLength;

	if (otherString == self)
		return OF_ORDERED_SAME;

	characters = [self characters];
	otherCharacters = [otherString characters];
	length = [self length];
	otherLength = [otherString length];

	minimumLength = (length > otherLength ? otherLength : length);

	for (i = 0; i < minimumLength; i++) {
		of_unichar_t c = characters[i];
		of_unichar_t oc = otherCharacters[i];

		if (c >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
			of_unichar_t tc =
			    of_unicode_casefolding_table[c >> 8][c & 0xFF];

			if (tc)
				c = tc;
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (uint32_t)hash
{
	const of_unichar_t *unicodeString = [self unicodeString];
	size_t i, length = [self length];
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (i = 0; i < length; i++) {
		const of_unichar_t c = unicodeString[i];

		OF_HASH_ADD(hash, (c & 0xFF0000) >> 16);
		OF_HASH_ADD(hash, (c & 0x00FF00) >>  8);
		OF_HASH_ADD(hash,  c & 0x0000FF);
	}

	OF_HASH_FINALIZE(hash);







|






|







1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (uint32_t)hash
{
	const of_unichar_t *characters = [self characters];
	size_t i, length = [self length];
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (i = 0; i < length; i++) {
		const of_unichar_t c = characters[i];

		OF_HASH_ADD(hash, (c & 0xFF0000) >> 16);
		OF_HASH_ADD(hash, (c & 0x00FF00) >>  8);
		OF_HASH_ADD(hash,  c & 0x0000FF);
	}

	OF_HASH_FINALIZE(hash);
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
1415
1416
1417
1418
1419
1420
1421
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
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
}

- (of_range_t)rangeOfString: (OFString*)string
		    options: (int)options
		      range: (of_range_t)range
{
	void *pool;
	const of_unichar_t *searchString;
	of_unichar_t *unicodeString;
	size_t i, searchLength;

	if ((searchLength = [string length]) == 0)
		return of_range(0, 0);

	if (searchLength > range.length)
		return of_range(OF_NOT_FOUND, 0);

	if (range.length > SIZE_MAX / sizeof(of_unichar_t))
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();

	searchString = [string unicodeString];
	unicodeString = malloc(range.length * sizeof(of_unichar_t));

	if (unicodeString == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: [self class]
			 requestedSize: range.length * sizeof(of_unichar_t)];

	@try {
		[self getCharacters: unicodeString
			    inRange: range];

		if (options & OF_STRING_SEARCH_BACKWARDS) {
			for (i = range.length - searchLength;; i--) {
				if (!memcmp(unicodeString + i, searchString,
				    searchLength * sizeof(of_unichar_t))) {
					objc_autoreleasePoolPop(pool);
					return of_range(range.location + i,
					    searchLength);
				}

				/* No match and we're at the last character */
				if (i == 0)
					break;
			}
		} else {
			for (i = 0; i <= range.length - searchLength; i++) {
				if (!memcmp(unicodeString + i, searchString,
				    searchLength * sizeof(of_unichar_t))) {
					objc_autoreleasePoolPop(pool);
					return of_range(range.location + i,
					    searchLength);
				}
			}
		}
	} @finally {
		free(unicodeString);
	}

	objc_autoreleasePoolPop(pool);

	return of_range(OF_NOT_FOUND, 0);
}

- (BOOL)containsString: (OFString*)string
{
	void *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return YES;

	if (searchLength > (length = [self length]))
		return NO;

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	searchString = [string unicodeString];

	for (i = 0; i <= length - searchLength; i++) {
		if (!memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t))) {
			objc_autoreleasePoolPop(pool);
			return YES;
		}
	}

	objc_autoreleasePoolPop(pool);







|
|













|
|

|





|




|












|








|










|










|
|


|







1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
}

- (of_range_t)rangeOfString: (OFString*)string
		    options: (int)options
		      range: (of_range_t)range
{
	void *pool;
	const of_unichar_t *searchCharacters;
	of_unichar_t *characters;
	size_t i, searchLength;

	if ((searchLength = [string length]) == 0)
		return of_range(0, 0);

	if (searchLength > range.length)
		return of_range(OF_NOT_FOUND, 0);

	if (range.length > SIZE_MAX / sizeof(of_unichar_t))
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();

	searchCharacters = [string characters];
	characters = malloc(range.length * sizeof(of_unichar_t));

	if (characters == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: [self class]
			 requestedSize: range.length * sizeof(of_unichar_t)];

	@try {
		[self getCharacters: characters
			    inRange: range];

		if (options & OF_STRING_SEARCH_BACKWARDS) {
			for (i = range.length - searchLength;; i--) {
				if (!memcmp(characters + i, searchCharacters,
				    searchLength * sizeof(of_unichar_t))) {
					objc_autoreleasePoolPop(pool);
					return of_range(range.location + i,
					    searchLength);
				}

				/* No match and we're at the last character */
				if (i == 0)
					break;
			}
		} else {
			for (i = 0; i <= range.length - searchLength; i++) {
				if (!memcmp(characters + i, searchCharacters,
				    searchLength * sizeof(of_unichar_t))) {
					objc_autoreleasePoolPop(pool);
					return of_range(range.location + i,
					    searchLength);
				}
			}
		}
	} @finally {
		free(characters);
	}

	objc_autoreleasePoolPop(pool);

	return of_range(OF_NOT_FOUND, 0);
}

- (BOOL)containsString: (OFString*)string
{
	void *pool;
	const of_unichar_t *characters, *searchCharacters;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return YES;

	if (searchLength > (length = [self length]))
		return NO;

	pool = objc_autoreleasePoolPush();

	characters = [self characters];
	searchCharacters = [string characters];

	for (i = 0; i <= length - searchLength; i++) {
		if (!memcmp(characters + i, searchCharacters,
		    searchLength * sizeof(of_unichar_t))) {
			objc_autoreleasePoolPop(pool);
			return YES;
		}
	}

	objc_autoreleasePoolPop(pool);
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > [self length])
		@throw [OFOutOfRangeException
		    exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();
	ret = [[OFString alloc]
	    initWithUnicodeString: [self unicodeString] + range.location
			   length: range.length];
	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (OFString*)stringByAppendingString: (OFString*)string
{







|
|







1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > [self length])
		@throw [OFOutOfRangeException
		    exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();
	ret = [[OFString alloc]
	    initWithCharacters: [self characters] + range.location
			length: range.length];
	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (OFString*)stringByAppendingString: (OFString*)string
{
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690

	return new;
}

- (BOOL)hasPrefix: (OFString*)prefix
{
	of_unichar_t *tmp;
	const of_unichar_t *prefixString;
	size_t prefixLength;
	int compare;

	if ((prefixLength = [prefix length]) > [self length])
		return NO;

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: prefixLength];
	@try {
		void *pool = objc_autoreleasePoolPush();

		[self getCharacters: tmp
			    inRange: of_range(0, prefixLength)];

		prefixString = [prefix unicodeString];
		compare = memcmp(tmp, prefixString,
		    prefixLength * sizeof(of_unichar_t));

		objc_autoreleasePoolPop(pool);
	} @finally {
		[self freeMemory: tmp];
	}

	return !compare;
}

- (BOOL)hasSuffix: (OFString*)suffix
{
	of_unichar_t *tmp;
	const of_unichar_t *suffixString;
	size_t length, suffixLength;
	int compare;

	if ((suffixLength = [suffix length]) > [self length])
		return NO;

	length = [self length];

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: suffixLength];
	@try {
		void *pool = objc_autoreleasePoolPush();

		[self getCharacters: tmp
			    inRange: of_range(length - suffixLength,
					 suffixLength)];

		suffixString = [suffix unicodeString];
		compare = memcmp(tmp, suffixString,
		    suffixLength * sizeof(of_unichar_t));

		objc_autoreleasePoolPop(pool);
	} @finally {
		[self freeMemory: tmp];
	}








|














|
|













|

















|
|







1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590

	return new;
}

- (BOOL)hasPrefix: (OFString*)prefix
{
	of_unichar_t *tmp;
	const of_unichar_t *prefixCharacters;
	size_t prefixLength;
	int compare;

	if ((prefixLength = [prefix length]) > [self length])
		return NO;

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: prefixLength];
	@try {
		void *pool = objc_autoreleasePoolPush();

		[self getCharacters: tmp
			    inRange: of_range(0, prefixLength)];

		prefixCharacters = [prefix characters];
		compare = memcmp(tmp, prefixCharacters,
		    prefixLength * sizeof(of_unichar_t));

		objc_autoreleasePoolPop(pool);
	} @finally {
		[self freeMemory: tmp];
	}

	return !compare;
}

- (BOOL)hasSuffix: (OFString*)suffix
{
	of_unichar_t *tmp;
	const of_unichar_t *suffixCharacters;
	size_t length, suffixLength;
	int compare;

	if ((suffixLength = [suffix length]) > [self length])
		return NO;

	length = [self length];

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: suffixLength];
	@try {
		void *pool = objc_autoreleasePoolPush();

		[self getCharacters: tmp
			    inRange: of_range(length - suffixLength,
					 suffixLength)];

		suffixCharacters = [suffix characters];
		compare = memcmp(tmp, suffixCharacters,
		    suffixLength * sizeof(of_unichar_t));

		objc_autoreleasePoolPop(pool);
	} @finally {
		[self freeMemory: tmp];
	}

1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
				options: (int)options
{
	void *pool;
	OFMutableArray *array = [OFMutableArray array];
	const of_unichar_t *string, *delimiterString;
	BOOL skipEmpty = (options & OF_STRING_SKIP_EMPTY);
	size_t length = [self length];
	size_t delimiterLength = [delimiter length];
	size_t i, last;
	OFString *component;

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];
	delimiterString = [delimiter unicodeString];

	if (delimiterLength > length) {
		[array addObject: [[self copy] autorelease]];
		[array makeImmutable];

		objc_autoreleasePoolPop(pool);

		return array;
	}

	for (i = 0, last = 0; i <= length - delimiterLength; i++) {
		if (memcmp(string + i, delimiterString,
		    delimiterLength * sizeof(of_unichar_t)))
			continue;

		component = [self substringWithRange: of_range(last, i - last)];
		if (!skipEmpty || ![component isEqual: @""])
			[array addObject: component];








|








|
|











|







1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
				options: (int)options
{
	void *pool;
	OFMutableArray *array = [OFMutableArray array];
	const of_unichar_t *characters, *delimiterCharacters;
	BOOL skipEmpty = (options & OF_STRING_SKIP_EMPTY);
	size_t length = [self length];
	size_t delimiterLength = [delimiter length];
	size_t i, last;
	OFString *component;

	pool = objc_autoreleasePoolPush();

	characters = [self characters];
	delimiterCharacters = [delimiter characters];

	if (delimiterLength > length) {
		[array addObject: [[self copy] autorelease]];
		[array makeImmutable];

		objc_autoreleasePoolPop(pool);

		return array;
	}

	for (i = 0, last = 0; i <= length - delimiterLength; i++) {
		if (memcmp(characters + i, delimiterCharacters,
		    delimiterLength * sizeof(of_unichar_t)))
			continue;

		component = [self substringWithRange: of_range(last, i - last)];
		if (!skipEmpty || ![component isEqual: @""])
			[array addObject: component];

1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
	return array;
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	void *pool;
	const of_unichar_t *string;
	size_t i, last = 0, length = [self length];

	ret = [OFMutableArray array];

	if (length == 0)
		return ret;

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')
#endif
		length--;

	for (i = 0; i < length; i++) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIMITER) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			[ret addObject: [self substringWithRange:
			    of_range(last, i - last)]];

			last = i + 1;
		}
	}

	[ret addObject: [self substringWithRange: of_range(last, i - last)]];

	[ret makeImmutable];

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (OFString*)lastPathComponent
{
	void *pool;
	const of_unichar_t *string;
	size_t length = [self length];
	ssize_t i;

	if (length == 0)
		return @"";

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')
#endif
		length--;

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

	objc_autoreleasePoolPop(pool);







|









|


|

|





|

|




















|








|


|

|





|

|







1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
	return array;
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	void *pool;
	const of_unichar_t *characters;
	size_t i, last = 0, length = [self length];

	ret = [OFMutableArray array];

	if (length == 0)
		return ret;

	pool = objc_autoreleasePoolPush();

	characters = [self characters];

#ifndef _WIN32
	if (characters[length - 1] == OF_PATH_DELIMITER)
#else
	if (characters[length - 1] == '/' || characters[length - 1] == '\\')
#endif
		length--;

	for (i = 0; i < length; i++) {
#ifndef _WIN32
		if (characters[i] == OF_PATH_DELIMITER) {
#else
		if (characters[i] == '/' || characters[i] == '\\') {
#endif
			[ret addObject: [self substringWithRange:
			    of_range(last, i - last)]];

			last = i + 1;
		}
	}

	[ret addObject: [self substringWithRange: of_range(last, i - last)]];

	[ret makeImmutable];

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (OFString*)lastPathComponent
{
	void *pool;
	const of_unichar_t *characters;
	size_t length = [self length];
	ssize_t i;

	if (length == 0)
		return @"";

	pool = objc_autoreleasePoolPush();

	characters = [self characters];

#ifndef _WIN32
	if (characters[length - 1] == OF_PATH_DELIMITER)
#else
	if (characters[length - 1] == '/' || characters[length - 1] == '\\')
#endif
		length--;

	for (i = length - 1; i >= 0; i--) {
#ifndef _WIN32
		if (characters[i] == OF_PATH_DELIMITER) {
#else
		if (characters[i] == '/' || characters[i] == '\\') {
#endif
			i++;
			break;
		}
	}

	objc_autoreleasePoolPop(pool);
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884

	return [self substringWithRange: of_range(i, length - i)];
}

- (OFString*)stringByDeletingLastPathComponent
{
	void *pool;
	const of_unichar_t *string;
	size_t i, length = [self length];

	if (length == 0)
		return @"";

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')
#endif
		length--;

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return [self substringWithRange: of_range(0, 1)];
	}

	for (i = length - 1; i >= 1; i--) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIMITER) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			objc_autoreleasePoolPop(pool);
			return [self substringWithRange: of_range(0, i)];
		}
	}

#ifndef _WIN32
	if (string[0] == OF_PATH_DELIMITER) {
#else
	if (string[0] == '/' || string[0] == '\\') {
#endif
		objc_autoreleasePoolPop(pool);
		return [self substringWithRange: of_range(0, 1)];
	}

	objc_autoreleasePoolPop(pool);








|







|


|

|










|

|







|

|







1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784

	return [self substringWithRange: of_range(i, length - i)];
}

- (OFString*)stringByDeletingLastPathComponent
{
	void *pool;
	const of_unichar_t *characters;
	size_t i, length = [self length];

	if (length == 0)
		return @"";

	pool = objc_autoreleasePoolPush();

	characters = [self characters];

#ifndef _WIN32
	if (characters[length - 1] == OF_PATH_DELIMITER)
#else
	if (characters[length - 1] == '/' || characters[length - 1] == '\\')
#endif
		length--;

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return [self substringWithRange: of_range(0, 1)];
	}

	for (i = length - 1; i >= 1; i--) {
#ifndef _WIN32
		if (characters[i] == OF_PATH_DELIMITER) {
#else
		if (characters[i] == '/' || characters[i] == '\\') {
#endif
			objc_autoreleasePoolPop(pool);
			return [self substringWithRange: of_range(0, i)];
		}
	}

#ifndef _WIN32
	if (characters[0] == OF_PATH_DELIMITER) {
#else
	if (characters[0] == '/' || characters[0] == '\\') {
#endif
		objc_autoreleasePoolPop(pool);
		return [self substringWithRange: of_range(0, 1)];
	}

	objc_autoreleasePoolPop(pool);

1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909

1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966

1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
	return standardize_path( [self componentsSeparatedByString: @"/"],
	    @".", @"..", @"/");
}

- (intmax_t)decimalValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string = [self unicodeString];
	size_t length = [self length];
	int i = 0;
	intmax_t value = 0;
	BOOL expectWhitespace = NO;


	while (*string == ' ' || *string == '\t' || *string == '\n' ||
	    *string == '\r' || *string == '\f') {
		string++;
		length--;
	}

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return 0;
	}

	if (string[0] == '-' || string[0] == '+')
		i++;

	for (; i < length; i++) {
		if (expectWhitespace) {
			if (string[i] != ' ' && string[i] != '\t' &&
			    string[i] != '\n' && string[i] != '\r' &&
			    string[i] != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];
			continue;
		}

		if (string[i] >= '0' && string[i] <= '9') {
			if (INTMAX_MAX / 10 < value ||
			    INTMAX_MAX - value * 10 < string[i] - '0')
				@throw [OFOutOfRangeException
				    exceptionWithClass: [self class]];

			value = (value * 10) + (string[i] - '0');
		} else if (string[i] == ' ' || string[i] == '\t' ||
		    string[i] == '\n' || string[i] == '\r' ||
		    string[i] == '\f')
			expectWhitespace = YES;
		else
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];
	}

	if (string[0] == '-')
		value *= -1;

	objc_autoreleasePoolPop(pool);

	return value;
}

- (uintmax_t)hexadecimalValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string = [self unicodeString];
	size_t length = [self length];
	int i = 0;
	uintmax_t value = 0;
	BOOL expectWhitespace = NO, foundValue = NO;


	while (*string == ' ' || *string == '\t' || *string == '\n' ||
	    *string == '\r' || *string == '\f') {
		string++;
		length--;
	}

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return 0;
	}

	if (length >= 2 && string[0] == '0' && string[1] == 'x')
		i = 2;
	else if (length >= 1 && (string[0] == 'x' || string[0] == '$'))
		i = 1;

	for (; i < length; i++) {
		uintmax_t newValue;

		if (expectWhitespace) {
			if (string[i] != ' ' && string[i] != '\t' &&
			    string[i] != '\n' && string[i] != '\r' &&
			    string[i] != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];
			continue;
		}

		if (string[i] >= '0' && string[i] <= '9') {
			newValue = (value << 4) | (string[i] - '0');
			foundValue = YES;
		} else if (string[i] >= 'A' && string[i] <= 'F') {
			newValue = (value << 4) | (string[i] - 'A' + 10);
			foundValue = YES;
		} else if (string[i] >= 'a' && string[i] <= 'f') {
			newValue = (value << 4) | (string[i] - 'a' + 10);
			foundValue = YES;
		} else if (string[i] == 'h' || string[i] == ' ' ||
		    string[i] == '\t' || string[i] == '\n' ||
		    string[i] == '\r' || string[i] == '\f') {
			expectWhitespace = YES;
			continue;
		} else
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		if (newValue < value)







|





>
|
|
|








|




|
|
|





|

|



|
|
|
|






|










|





>
|
|
|








|

|






|
|
|





|
|

|
|

|
|

|
|
|







1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
	return standardize_path( [self componentsSeparatedByString: @"/"],
	    @".", @"..", @"/");
}

- (intmax_t)decimalValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = [self characters];
	size_t length = [self length];
	int i = 0;
	intmax_t value = 0;
	BOOL expectWhitespace = NO;

	while (length > 0 && (*characters == ' ' || *characters == '\t' ||
	    *characters == '\n' || *characters == '\r' ||
	    *characters == '\f')) {
		characters++;
		length--;
	}

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return 0;
	}

	if (characters[0] == '-' || characters[0] == '+')
		i++;

	for (; i < length; i++) {
		if (expectWhitespace) {
			if (characters[i] != ' ' && characters[i] != '\t' &&
			    characters[i] != '\n' && characters[i] != '\r' &&
			    characters[i] != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];
			continue;
		}

		if (characters[i] >= '0' && characters[i] <= '9') {
			if (INTMAX_MAX / 10 < value ||
			    INTMAX_MAX - value * 10 < characters[i] - '0')
				@throw [OFOutOfRangeException
				    exceptionWithClass: [self class]];

			value = (value * 10) + (characters[i] - '0');
		} else if (characters[i] == ' ' || characters[i] == '\t' ||
		    characters[i] == '\n' || characters[i] == '\r' ||
		    characters[i] == '\f')
			expectWhitespace = YES;
		else
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];
	}

	if (characters[0] == '-')
		value *= -1;

	objc_autoreleasePoolPop(pool);

	return value;
}

- (uintmax_t)hexadecimalValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = [self characters];
	size_t length = [self length];
	int i = 0;
	uintmax_t value = 0;
	BOOL expectWhitespace = NO, foundValue = NO;

	while (length > 0 && (*characters == ' ' || *characters == '\t' ||
	    *characters == '\n' || *characters == '\r' ||
	    *characters == '\f')) {
		characters++;
		length--;
	}

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return 0;
	}

	if (length >= 2 && characters[0] == '0' && characters[1] == 'x')
		i = 2;
	else if (length >= 1 && (characters[0] == 'x' || characters[0] == '$'))
		i = 1;

	for (; i < length; i++) {
		uintmax_t newValue;

		if (expectWhitespace) {
			if (characters[i] != ' ' && characters[i] != '\t' &&
			    characters[i] != '\n' && characters[i] != '\r' &&
			    characters[i] != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];
			continue;
		}

		if (characters[i] >= '0' && characters[i] <= '9') {
			newValue = (value << 4) | (characters[i] - '0');
			foundValue = YES;
		} else if (characters[i] >= 'A' && characters[i] <= 'F') {
			newValue = (value << 4) | (characters[i] - 'A' + 10);
			foundValue = YES;
		} else if (characters[i] >= 'a' && characters[i] <= 'f') {
			newValue = (value << 4) | (characters[i] - 'a' + 10);
			foundValue = YES;
		} else if (characters[i] == 'h' || characters[i] == ' ' ||
		    characters[i] == '\t' || characters[i] == '\n' ||
		    characters[i] == '\r' || characters[i] == '\f') {
			expectWhitespace = YES;
			continue;
		} else
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		if (newValue < value)
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140

	return value;
}

- (float)floatValue
{
	void *pool = objc_autoreleasePoolPush();
	const char *cString = [self UTF8String];
	char *endPointer = NULL;
	float value;

	while (*cString == ' ' || *cString == '\t' || *cString == '\n' ||
	    *cString == '\r' || *cString == '\f')
		cString++;

	value = strtof(cString, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (double)doubleValue
{
	void *pool = objc_autoreleasePoolPush();
	const char *cString = [self UTF8String];
	char *endPointer = NULL;
	double value;

	while (*cString == ' ' || *cString == '\t' || *cString == '\n' ||
	    *cString == '\r' || *cString == '\f')
		cString++;

	value = strtod(cString, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (const of_unichar_t*)unicodeString
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	size_t length = [self length];
	of_unichar_t *ret;

	ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
				    count: length + 1];
	[self getCharacters: ret
		    inRange: of_range(0, length)];
	ret[length] = 0;

	return ret;
}

- (const uint16_t*)UTF16String
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *unicodeString = [self unicodeString];
	size_t length = [self length];
	uint16_t *ret;
	size_t i, j;

	/* Allocate memory for the worst case */
	ret = [object allocMemoryWithSize: sizeof(uint16_t)
				    count: length * 2 + 1];

	j = 0;

	for (i = 0; i < length; i++) {
		of_unichar_t c = unicodeString[i];

		if (c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		if (c > 0xFFFF) {
			c -= 0x10000;
			ret[j++] = OF_BSWAP16_IF_LE(0xD800 | (c >> 10));
			ret[j++] = OF_BSWAP16_IF_LE(0xDC00 | (c & 0x3FF));
		} else
			ret[j++] = OF_BSWAP16_IF_LE(c);
	}

	ret[j] = 0;

	@try {
		ret = [object resizeMemory: ret
				      size: sizeof(uint16_t)
				     count: j + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

	objc_autoreleasePoolPop(pool);

	return ret;







|



|
|
|

|


















|



|
|
|

|















|






|


<








|






|




|













<
<



|







1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994

1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028


2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039

	return value;
}

- (float)floatValue
{
	void *pool = objc_autoreleasePoolPush();
	const char *UTF8String = [self UTF8String];
	char *endPointer = NULL;
	float value;

	while (*UTF8String == ' ' || *UTF8String == '\t' ||
	    *UTF8String == '\n' || *UTF8String == '\r' || *UTF8String == '\f')
		UTF8String++;

	value = strtof(UTF8String, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (double)doubleValue
{
	void *pool = objc_autoreleasePoolPush();
	const char *UTF8String = [self UTF8String];
	char *endPointer = NULL;
	double value;

	while (*UTF8String == ' ' || *UTF8String == '\t' ||
	    *UTF8String == '\n' || *UTF8String == '\r' || *UTF8String == '\f')
		UTF8String++;

	value = strtod(UTF8String, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (const of_unichar_t*)characters
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	size_t length = [self length];
	of_unichar_t *ret;

	ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
				    count: length];
	[self getCharacters: ret
		    inRange: of_range(0, length)];


	return ret;
}

- (const uint16_t*)UTF16String
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = [self characters];
	size_t length = [self length];
	uint16_t *ret;
	size_t i, j;

	/* Allocate memory for the worst case */
	ret = [object allocMemoryWithSize: sizeof(uint16_t)
				    count: length * 2];

	j = 0;

	for (i = 0; i < length; i++) {
		of_unichar_t c = characters[i];

		if (c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		if (c > 0xFFFF) {
			c -= 0x10000;
			ret[j++] = OF_BSWAP16_IF_LE(0xD800 | (c >> 10));
			ret[j++] = OF_BSWAP16_IF_LE(0xDC00 | (c & 0x3FF));
		} else
			ret[j++] = OF_BSWAP16_IF_LE(c);
	}



	@try {
		ret = [object resizeMemory: ret
				      size: sizeof(uint16_t)
				     count: j];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

	objc_autoreleasePoolPop(pool);

	return ret;
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string = [self unicodeString];
	size_t i, last = 0, length = [self length];
	BOOL stop = NO, lastCarriageReturn = NO;

	for (i = 0; i < length && !stop; i++) {
		if (lastCarriageReturn && string[i] == '\n') {
			lastCarriageReturn = NO;
			last++;

			continue;
		}

		if (string[i] == '\n' || string[i] == '\r') {
			void *pool2 = objc_autoreleasePoolPush();

			block([self substringWithRange:
			    of_range(last, i - last)], &stop);
			last = i + 1;

			objc_autoreleasePoolPop(pool2);
		}

		lastCarriageReturn = (string[i] == '\r');
	}

	if (!stop)
		block([self substringWithRange: of_range(last, i - last)],
		    &stop);

	objc_autoreleasePoolPop(pool);
}
#endif
@end







|




|






|









|










2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = [self characters];
	size_t i, last = 0, length = [self length];
	BOOL stop = NO, lastCarriageReturn = NO;

	for (i = 0; i < length && !stop; i++) {
		if (lastCarriageReturn && characters[i] == '\n') {
			lastCarriageReturn = NO;
			last++;

			continue;
		}

		if (characters[i] == '\n' || characters[i] == '\r') {
			void *pool2 = objc_autoreleasePoolPush();

			block([self substringWithRange:
			    of_range(last, i - last)], &stop);
			last = i + 1;

			objc_autoreleasePoolPop(pool2);
		}

		lastCarriageReturn = (characters[i] == '\r');
	}

	if (!stop)
		block([self substringWithRange: of_range(last, i - last)],
		    &stop);

	objc_autoreleasePoolPop(pool);
}
#endif
@end

Modified src/OFString_UTF8.m from [e371dc8e8d] to [797a478d4d].

399
400
401
402
403
404
405
406
407
408

409
410
411
412
413
414
415
416
417
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
		[self release];
		@throw e;
	}

	return self;
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
		 length: (size_t)length

{
	self = [super init];

	@try {
		size_t i, j = 0;
		BOOL swap = NO;

		if (length > 0 && *string == 0xFEFF) {
			string++;
			length--;
		} else if (length > 0 && *string == 0xFFFE0000) {
			swap = YES;
			string++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = YES;

		s = &s_store;

		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t characterLen = of_string_utf8_encode(
			    (swap ? OF_BSWAP32(string[i]) : string[i]),
			    buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:







|
<
|
>







|
|

|

|













|







399
400
401
402
403
404
405
406

407
408
409
410
411
412
413
414
415
416
417
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
		[self release];
		@throw e;
	}

	return self;
}

- initWithCharacters: (const of_unichar_t*)characters

	      length: (size_t)length
	   byteOrder: (of_byte_order_t)byteOrder
{
	self = [super init];

	@try {
		size_t i, j = 0;
		BOOL swap = NO;

		if (length > 0 && *characters == 0xFEFF) {
			characters++;
			length--;
		} else if (length > 0 && *characters == 0xFFFE0000) {
			swap = YES;
			characters++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = YES;

		s = &s_store;

		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t characterLen = of_string_utf8_encode(
			    (swap ? OF_BSWAP32(characters[i]) : characters[i]),
			    buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
482
483
484
485
486
487
488
489
490

491
492
493
494
495
496
497
		@throw e;
	}

	return self;
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
	       length: (size_t)length

{
	self = [super init];

	@try {
		size_t i, j = 0;
		BOOL swap = NO;








<

>







482
483
484
485
486
487
488

489
490
491
492
493
494
495
496
497
		@throw e;
	}

	return self;
}

- initWithUTF16String: (const uint16_t*)string

	       length: (size_t)length
	    byteOrder: (of_byte_order_t)byteOrder
{
	self = [super init];

	@try {
		size_t i, j = 0;
		BOOL swap = NO;

949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
}

- (void)getCharacters: (of_unichar_t*)buffer
	      inRange: (of_range_t)range
{
	/* TODO: Could be slightly optimized */
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *unicodeString = [self unicodeString];

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	memcpy(buffer, unicodeString + range.location,
	    range.length * sizeof(of_unichar_t));

	objc_autoreleasePoolPop(pool);
}

- (of_range_t)rangeOfString: (OFString*)string
		    options: (int)options







|





|







949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
}

- (void)getCharacters: (of_unichar_t*)buffer
	      inRange: (of_range_t)range
{
	/* TODO: Could be slightly optimized */
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *characters = [self characters];

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > s->length)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	memcpy(buffer, characters + range.location,
	    range.length * sizeof(of_unichar_t));

	objc_autoreleasePoolPop(pool);
}

- (of_range_t)rangeOfString: (OFString*)string
		    options: (int)options
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
#endif
		return [OFString stringWithUTF8String: s->cString
					       length: 1];

	return @".";
}

- (const of_unichar_t*)unicodeString
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	of_unichar_t *ret;
	size_t i, j;

	ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
				    count: s->length + 1];

	i = j = 0;

	while (i < s->cStringLength) {
		of_unichar_t c;
		size_t cLen;

		cLen = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c);

		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

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

	ret[j] = 0;

	return ret;
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	void *pool;







|






|


















<
<







1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276


1277
1278
1279
1280
1281
1282
1283
#endif
		return [OFString stringWithUTF8String: s->cString
					       length: 1];

	return @".";
}

- (const of_unichar_t*)characters
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	of_unichar_t *ret;
	size_t i, j;

	ret = [object allocMemoryWithSize: sizeof(of_unichar_t)
				    count: s->length];

	i = j = 0;

	while (i < s->cStringLength) {
		of_unichar_t c;
		size_t cLen;

		cLen = of_string_utf8_decode(s->cString + i,
		    s->cStringLength - i, &c);

		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

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



	return ret;
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	void *pool;

Modified tests/OFStringTests.m from [bb5fb35102] to [e613d6ae06].

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

static OFString *module = @"OFString";
static OFString* whitespace[] = {
	@" \r \t\n\t \tasd  \t \t\t\r\n",
	@" \t\t  \t\t  \t \t"
};
static of_unichar_t ucstr[] = {
	0xFEFF, 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0x1F03A, 0
};
static of_unichar_t sucstr[] = {
	0xFFFE0000, 0x66000000, 0xF6000000, 0xF6000000, 0x62000000, 0xE4000000,
	0x72000000, 0x3AF00100, 0
};
static uint16_t utf16str[] = {
	0xFEFF, 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0xD83C, 0xDC3A, 0
};
static uint16_t sutf16str[] = {
	0xFFFE, 0x6600, 0xF600, 0xF600, 0x6200, 0xE400, 0x7200, 0x3CD8, 0x3ADC,
	0
};

@interface EntityHandler: OFObject <OFStringXMLUnescapingDelegate>
@end

@implementation EntityHandler
-	   (OFString*)string: (OFString*)string







|



|


|


|
<







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

static OFString *module = @"OFString";
static OFString* whitespace[] = {
	@" \r \t\n\t \tasd  \t \t\t\r\n",
	@" \t\t  \t\t  \t \t"
};
static of_unichar_t ucstr[] = {
	0xFEFF, 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0x1F03A
};
static of_unichar_t sucstr[] = {
	0xFFFE0000, 0x66000000, 0xF6000000, 0xF6000000, 0x62000000, 0xE4000000,
	0x72000000, 0x3AF00100
};
static uint16_t utf16str[] = {
	0xFEFF, 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0xD83C, 0xDC3A
};
static uint16_t sutf16str[] = {
	0xFFFE, 0x6600, 0xF600, 0xF600, 0x6200, 0xE400, 0x7200, 0x3CD8, 0x3ADC

};

@interface EntityHandler: OFObject <OFStringXMLUnescapingDelegate>
@end

@implementation EntityHandler
-	   (OFString*)string: (OFString*)string
153
154
155
156
157
158
159
160
161


162
163


164
165
166
167


168
169


170
171
172
173
174
175
176

	TEST(@"+[stringWithUTF8String:length:]",
	    (s[0] = [OFMutableString stringWithUTF8String: "\xEF\xBB\xBF"
							   "foobar"
						   length: 6]) &&
	    [s[0] isEqual: @"foo"])

	TEST(@"+[stringWithUnicodeString:]",
	    (is = [OFString stringWithUnicodeString: ucstr]) &&


	    [is isEqual: @"fööbär🀺"] &&
	    (is = [OFString stringWithUnicodeString: sucstr]) &&


	    [is isEqual: @"fööbär🀺"])

	TEST(@"+[stringWithUTF16String:]",
	    (is = [OFString stringWithUTF16String: utf16str]) &&


	    [is isEqual: @"fööbär🀺"] &&
	    (is = [OFString stringWithUTF16String: sutf16str]) &&


	    [is isEqual: @"fööbär🀺"])

	TEST(@"+[stringWithContentsOfFile:encoding]", (is = [OFString
	    stringWithContentsOfFile: @"testfile.txt"
			    encoding: OF_STRING_ENCODING_ISO_8859_1]) &&
	    [is isEqual: @"testäöü"])








|
|
>
>

|
>
>


|
|
>
>

|
>
>







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

	TEST(@"+[stringWithUTF8String:length:]",
	    (s[0] = [OFMutableString stringWithUTF8String: "\xEF\xBB\xBF"
							   "foobar"
						   length: 6]) &&
	    [s[0] isEqual: @"foo"])

	TEST(@"+[stringWithCharacters:length:]",
	    (is = [OFString stringWithCharacters: ucstr
					  length: sizeof(ucstr) /
						  sizeof(*ucstr)]) &&
	    [is isEqual: @"fööbär🀺"] &&
	    (is = [OFString stringWithCharacters: sucstr
					  length: sizeof(sucstr) /
						  sizeof(*sucstr)]) &&
	    [is isEqual: @"fööbär🀺"])

	TEST(@"+[stringWithUTF16String:length:]",
	    (is = [OFString stringWithUTF16String: utf16str
					   length: sizeof(utf16str) /
						   sizeof(*utf16str)]) &&
	    [is isEqual: @"fööbär🀺"] &&
	    (is = [OFString stringWithUTF16String: sutf16str
					   length: sizeof(sutf16str) /
						   sizeof(*sutf16str)]) &&
	    [is isEqual: @"fööbär🀺"])

	TEST(@"+[stringWithContentsOfFile:encoding]", (is = [OFString
	    stringWithContentsOfFile: @"testfile.txt"
			    encoding: OF_STRING_ENCODING_ISO_8859_1]) &&
	    [is isEqual: @"testäöü"])

399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

	EXPECT_EXCEPTION(@"Detect out of range in -[hexadecimalValue]",
	    OFOutOfRangeException,
	    [@"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	     @"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	    hexadecimalValue])

	TEST(@"-[unicodeString]", (ua = [@"fööbär🀺" unicodeString]) &&
	    !memcmp(ua, ucstr + 1, sizeof(ucstr) - sizeof(of_unichar_t)))

	TEST(@"-[UTF16String]", (u16a = [@"fööbär🀺" UTF16String]) &&
#ifdef OF_BIG_ENDIAN
	    !memcmp(u16a, utf16str + 1, sizeof(utf16str) - sizeof(uint16_t)))
#else
	    !memcmp(u16a, sutf16str + 1, sizeof(sutf16str) - sizeof(uint16_t)))
#endif







|
|







406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421

	EXPECT_EXCEPTION(@"Detect out of range in -[hexadecimalValue]",
	    OFOutOfRangeException,
	    [@"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	     @"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
	    hexadecimalValue])

	TEST(@"-[characters]", (ua = [@"fööbär🀺" characters]) &&
	    !memcmp(ua, ucstr + 1, sizeof(ucstr) / sizeof(*ucstr)))

	TEST(@"-[UTF16String]", (u16a = [@"fööbär🀺" UTF16String]) &&
#ifdef OF_BIG_ENDIAN
	    !memcmp(u16a, utf16str + 1, sizeof(utf16str) - sizeof(uint16_t)))
#else
	    !memcmp(u16a, sutf16str + 1, sizeof(sutf16str) - sizeof(uint16_t)))
#endif