ObjFW  Check-in [0b846bcc4a]

Overview
Comment:Make it possible to add more ivars to OFConstantString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0b846bcc4a74754b4358dfd79cbb81d460a11aa98250c4ad68de4d03bc4da661
User & Date: js on 2011-07-08 15:29:27
Other Links: manifest | tags
Context
2011-07-08
15:34
Don't kill the old string in -[setToCString:] due to invalid encoding. check-in: 348ac73b77 user: js tags: trunk
15:29
Make it possible to add more ivars to OFConstantString. check-in: 0b846bcc4a user: js tags: trunk
2011-07-07
23:19
Fix missing character check in +[stringWithUTF16String:]. check-in: f6c05b68cf user: js tags: trunk
Changes

Modified src/OFBlock.m from [b8304c7ab5] to [4ae738487f].

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
- (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)allocMemoryForNItems: (size_t)nitems
                     withSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)resizeMemory: (void*)ptr
               toSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)resizeMemory: (void*)ptr
	     toNItems: (size_t)nitems







|






|







390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
- (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)allocMemoryForNItems: (size_t)nitems
		     withSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)resizeMemory: (void*)ptr
	       toSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)resizeMemory: (void*)ptr
	     toNItems: (size_t)nitems

Modified src/OFConstantString.h from [90fda79f41] to [1c0a70b9ab].

26
27
28
29
30
31
32







33
#endif
#endif

/**
 * \brief A class for storing constant strings using the \@"" literal.
 */
@interface OFConstantString: OFString







@end







>
>
>
>
>
>
>

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#endif
#endif

/**
 * \brief A class for storing constant strings using the \@"" literal.
 */
@interface OFConstantString: OFString
/**
 * \brief Completes initialization of the OFConstantString
 *
 * This method completes the initialization, as the constant strings created by
 * the compiler are not fully initialized.
 */
- (void)completeInitialization;
@end

Modified src/OFConstantString.m from [e051d86f17] to [9dcf20b1e7].

11
12
13
14
15
16
17



18
19
20

21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
































38
39
40
41
42
43
44
45


46
47
48
49
50
51


52
53
54
55
56
57
58


59
60
61
62
63
64
65
66


67
68
69
70
71
72
73


74








































































75
76
77
78
79


80
81
82
83
84
85
86


87
88
89
90








91

92


93











































94
95
96
97
98


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"




#import "OFConstantString.h"


#import "OFNotImplementedException.h"


#ifdef OF_APPLE_RUNTIME
# import <objc/runtime.h>

void *_OFConstantStringClassReference;
#endif

@implementation OFConstantString
#ifdef OF_APPLE_RUNTIME
+ (void)load
{
	objc_setFutureClass((Class)&_OFConstantStringClassReference,
	    "OFConstantString");
}
#endif

































+ alloc
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
}

- init
{


	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithCString: (const char*)str
{


	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithCString: (const char*)str
	 encoding: (of_string_encoding_t)encoding
{


	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithCString: (const char*)str
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)len
{


	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithCString: (const char*)str
           length: (size_t)len
{


	@throw [OFNotImplementedException newWithClass: isa








































































					      selector: _cmd];
}

- initWithFormat: (OFString*)fmt, ...
{


	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
{


	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}









- initWithString: (OFString*)str

{


	@throw [OFNotImplementedException newWithClass: isa











































					      selector: _cmd];
}

- (BOOL)isUTF8
{


	return YES;
}

- (void)addMemoryToPool: (void*)ptr
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)allocMemoryForNItems: (size_t)nitems
                     withSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)resizeMemory: (void*)ptr
	       toSize: (size_t)size







>
>
>



>

>
















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








>
>
|





>
>
|






>
>
|







>
>
|




|

>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|

>
>
|



|


>
>
|



>
>
>
>
>
>
>
>
|
>

>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<
<
>
>
|
<
|













|







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273


274
275
276

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#import "OFConstantString.h"

#import "OFInvalidEncodingException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"

#ifdef OF_APPLE_RUNTIME
# import <objc/runtime.h>

void *_OFConstantStringClassReference;
#endif

@implementation OFConstantString
#ifdef OF_APPLE_RUNTIME
+ (void)load
{
	objc_setFutureClass((Class)&_OFConstantStringClassReference,
	    "OFConstantString");
}
#endif

- (void)completeInitialization
{
	struct of_string_ivars *ivars;

	if (initialized == SIZE_MAX)
		return;

	if ((ivars = malloc(sizeof(*ivars))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa
					      requestedSize: sizeof(*ivars)];
	memset(ivars, 0, sizeof(*ivars));

	ivars->string = (char*)s;
	ivars->length = initialized;

	switch (of_string_check_utf8(ivars->string, ivars->length)) {
	case 1:
		ivars->isUTF8 = YES;
		break;
	case -1:
		free(ivars);
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	s = ivars;
	initialized = SIZE_MAX;
}

/*
 * The following methods are not available since it's a constant string, which
 * can't be allocated or initialized at runtime.
 */
+ alloc
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithCString: (const char*)str
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithCString: (const char*)str
	 encoding: (of_string_encoding_t)encoding
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithCString: (const char*)str
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)len
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithCString: (const char*)str
	   length: (size_t)len
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithString: (OFString*)string
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithUnicodeString: (of_unichar_t*)string
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithUnicodeString: (of_unichar_t*)string
	      byteOrder: (of_endianess_t)byteOrder
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithUnicodeString: (of_unichar_t*)string
		 length: (size_t)length
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithUnicodeString: (of_unichar_t*)string
	      byteOrder: (of_endianess_t)byteOrder
		 length: (size_t)length
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithUTF16String: (uint16_t*)string
	    byteOrder: (of_endianess_t)byteOrder
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithUTF16String: (uint16_t*)string
	       length: (size_t)length
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithUTF16String: (uint16_t*)string
	    byteOrder: (of_endianess_t)byteOrder
	       length: (size_t)length
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithFormat: (OFConstantString*)format, ...
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithFormat: (OFConstantString*)format
       arguments: (va_list)args
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithPath: (OFString*)firstComponent, ...
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithPath: (OFString*)firstComponent
     arguments: (va_list)arguments
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithContentsOfFile: (OFString*)path
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithContentsOfFile: (OFString*)path
		encoding: (of_string_encoding_t)encoding
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithContentsOfURL: (OFURL*)URL
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

/* From protocol OFSerializing */
- initWithSerialization: (OFXMLElement*)element
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}



/*
 * The following methods are not available because constant strings are
 * preallocated by the compiler and thus don't have a memory pool.

 */
- (void)addMemoryToPool: (void*)ptr
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)allocMemoryForNItems: (size_t)nitems
		     withSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void*)resizeMemory: (void*)ptr
	       toSize: (size_t)size
135
136
137
138
139
140
141




142
143
144
145
146
147
148

- (void)freeMemory: (void*)ptr
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}





- retain
{
	return self;
}

- autorelease
{







>
>
>
>







311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

- (void)freeMemory: (void*)ptr
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

/*
 * The following methods are unnecessary because constant strings are
 * singletons.
 */
- retain
{
	return self;
}

- autorelease
{
160
161
162
163
164
165
166














































































































































































































































































































167

- (void)dealloc
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
	[super dealloc];	/* Get rid of a stupid warning */
}














































































































































































































































































































@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649

- (void)dealloc
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
	[super dealloc];	/* Get rid of a stupid warning */
}

/*
 * In all following methods, it is checked whether the constant string has been
 * initialized. If not, it will be initialized. Finally, the implementation of
 * the superclass will be called.
 */

/* From protocol OFCopying */
- copy
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super copy];
}

/* From protocol OFMutableCopying */
- mutableCopy
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super mutableCopy];
}

/* From protocol OFComparing */
- (of_comparison_result_t)compare: (id)object
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super compare: object];
}

/* From protocol OFSerialization */
- (OFXMLElement*)XMLElementBySerializing
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super XMLElementBySerializing];
}

/* From OFObject, but reimplemented in OFString */
- (BOOL)isEqual: (id)object
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super isEqual: object];
}

- (uint32_t)hash
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super hash];
}

- (OFString*)description
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super description];
}

/* From OFString */
- (const char*)cString
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super cString];
}

- (size_t)length
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super length];
}

- (size_t)cStringLength
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super cStringLength];
}

- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super caseInsensitiveCompare: otherString];
}

- (of_unichar_t)characterAtIndex: (size_t)index
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super characterAtIndex: index];
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super indexOfFirstOccurrenceOfString: string];
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)string
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super indexOfLastOccurrenceOfString: string];
}

- (BOOL)containsString: (OFString*)string
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super containsString: string];
}

- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super substringFromIndex: start
				 toIndex: end];
}

- (OFString*)substringWithRange: (of_range_t)range
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super substringWithRange: range];
}

- (OFString*)stringByAppendingString: (OFString*)string
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super stringByAppendingString: string];
}

- (OFString*)stringByPrependingString: (OFString*)string
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super stringByPrependingString: string];
}

- (OFString*)uppercaseString
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super uppercaseString];
}

- (OFString*)lowercaseString
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super lowercaseString];
}

- (OFString*)stringByDeletingLeadingWhitespaces
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super stringByDeletingLeadingWhitespaces];
}

- (OFString*)stringByDeletingTrailingWhitespaces
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super stringByDeletingTrailingWhitespaces];
}

- (OFString*)stringByDeletingLeadingAndTrailingWhitespaces
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super stringByDeletingLeadingAndTrailingWhitespaces];
}

- (BOOL)hasPrefix: (OFString*)prefix
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super hasPrefix: prefix];
}

- (BOOL)hasSuffix: (OFString*)suffix
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super hasSuffix: suffix];
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super componentsSeparatedByString: delimiter];
}

- (OFArray*)pathComponents
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super pathComponents];
}

- (OFString*)lastPathComponent
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super lastPathComponent];
}

- (OFString*)stringByDeletingLastPathComponent
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super stringByDeletingLastPathComponent];
}

- (intmax_t)decimalValue
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super decimalValue];
}

- (uintmax_t)hexadecimalValue
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super hexadecimalValue];
}

- (float)floatValue
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super floatValue];
}

- (double)doubleValue
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super doubleValue];
}

- (of_unichar_t*)unicodeString
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super unicodeString];
}

- (void)writeToFile: (OFString*)path
{
	if (initialized != SIZE_MAX)
		[self completeInitialization];

	return [super writeToFile: path];
}
@end

Modified src/OFList.m from [422b33ae63] to [ba2a81b1ec].

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
	*listObject = (*listObject)->next;
	return 1;
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFListEnumerator alloc]
	        initWithList: self
	    mutationsPointer: &mutations] autorelease];
}
@end

@implementation OFListEnumerator
-     initWithList: (OFList*)list_
  mutationsPointer: (unsigned long*)mutationsPtr_







|







411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
	*listObject = (*listObject)->next;
	return 1;
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFListEnumerator alloc]
		initWithList: self
	    mutationsPointer: &mutations] autorelease];
}
@end

@implementation OFListEnumerator
-     initWithList: (OFList*)list_
  mutationsPointer: (unsigned long*)mutationsPtr_

Modified src/OFMutableString.m from [9305a9d8ab] to [17605d93c9].

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
79
80
81
82
83
{
	of_unichar_t c;
	of_unichar_t *unicodeString;
	size_t unicodeLen, newLength, cLen;
	size_t i, j, d;
	char *newString;

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

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

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

		return;
	}

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

	i = j = 0;
	newLength = 0;

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


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

		if (c >> 8 < tableSize) {







|


|


|













|
|
>







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
	of_unichar_t c;
	of_unichar_t *unicodeString;
	size_t unicodeLen, newLength, cLen;
	size_t i, j, d;
	char *newString;

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

		uint8_t *p = (uint8_t*)s->string + s->length;
		uint8_t t;

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

		return;
	}

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

	i = j = 0;
	newLength = 0;

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

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

		if (c >> 8 < tableSize) {
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

405
406
407
408
409
410
411
412
413
414
415
416
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
451
452
453
454
455
456
457
458
459
460
461
462

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
		j += d;
	}

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

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

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

	[self freeMemory: string];

	length_ = strlen(string_);

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

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

		@throw [OFInvalidEncodingException newWithClass: isa];
	}

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

- (void)appendCString: (const char*)string_
{
	size_t length_ = strlen(string_);

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

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

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

- (void)appendCString: (const char*)string_
	   withLength: (size_t)length_
{
	if (length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) {
		string_ += 3;
		length_ -= 3;
	}

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

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

- (void)appendCString: (const char*)string_
	 withEncoding: (of_string_encoding_t)encoding
	       length: (size_t)length_
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		[self appendCString: string_
			 withLength: length_];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[self appendString: [OFString stringWithCString: string_
						       encoding: encoding
							 length: length_]];
		[pool release];
	}
}

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

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

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

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

	[self appendCString: [string_ cString]];
}

- (void)appendFormat: (OFConstantString*)format, ...
{
	va_list arguments;

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

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

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

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

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

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

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

	madvise(string, length, MADV_SEQUENTIAL);

	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	if (!isUTF8) {
		madvise(string, length, MADV_NORMAL);
		return;
	}

	for (i = 0; i < length; i++) {
		/* ASCII */
		if (OF_LIKELY(!(string[i] & 0x80)))
			continue;

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(string[i] & 0x40)) {
			madvise(string, length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Next byte must not be ASCII */

		if (OF_UNLIKELY(length < i + 1 || !(string[i + 1] & 0x80))) {
			madvise(string, length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Next byte is the start byte */
		if (OF_LIKELY(string[i + 1] & 0x40)) {
			string[i] ^= string[i + 1];
			string[i + 1] ^= string[i];
			string[i] ^= string[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */

		if (OF_UNLIKELY(length < i + 2 || !(string[i + 2] & 0x80))) {
			madvise(string, length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Second next byte is the start byte */
		if (OF_LIKELY(string[i + 2] & 0x40)) {
			string[i] ^= string[i + 2];
			string[i + 2] ^= string[i];
			string[i] ^= string[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */

		if (OF_UNLIKELY(length < i + 3 || !(string[i + 3] & 0x80))) {
			madvise(string, length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Third next byte is the start byte */
		if (OF_LIKELY(string[i + 3] & 0x40)) {
			string[i] ^= string[i + 3];
			string[i + 3] ^= string[i];
			string[i] ^= string[i + 3];

			string[i + 1] ^= string[i + 2];
			string[i + 2] ^= string[i + 1];
			string[i + 1] ^= string[i + 2];

			i += 3;
			continue;
		}

		/* UTF-8 does not allow more than 4 bytes per character */
		madvise(string, length, MADV_NORMAL);
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	madvise(string, length, MADV_NORMAL);
}

- (void)upper
{
	[self _applyTable: of_unicode_upper_table
		 withSize: OF_UNICODE_UPPER_TABLE_SIZE];
}

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

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

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


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

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

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

	length = newLength;
}

- (void)deleteCharactersFromIndex: (size_t)start
			  toIndex: (size_t)end
{
	if (isUTF8) {
		start = of_string_index_to_position(string, start, length);

		end = of_string_index_to_position(string, end, length);
	}

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

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

	memmove(string + start, string + end, length - end);
	length -= end - start;
	string[length] = 0;

	@try {
		string = [self resizeMemory: string
				     toSize: length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)deleteCharactersInRange: (of_range_t)range
{
	[self deleteCharactersFromIndex: range.start
				toIndex: range.start + range.length];
}

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

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

		end = of_string_index_to_position(string, end, length);
	}

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

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

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

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

	length = newLength;
}

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

- (void)replaceOccurrencesOfString: (OFString*)string_
			withString: (OFString*)replacement
{
	const char *cString = [string_ cString];
	const char *replacementCString = [replacement cString];
	size_t stringLen = [string_ cStringLength];
	size_t replacementLen = [replacement cStringLength];
	size_t i, last, newLength;
	char *newString;

	if (stringLen > length)
		return;

	newString = NULL;
	newLength = 0;

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

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

	@try {
		newString = [self resizeMemory: newString
					toSize: newLength + length - last + 1];

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

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

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < length; i++)
		if (string[i] != ' '  && string[i] != '\t' &&
		    string[i] != '\n' && string[i] != '\r')
			break;

	length -= i;
	memmove(string, string + i, length);
	string[length] = '\0';

	@try {
		string = [self resizeMemory: string
				     toSize: length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)deleteTrailingWhitespaces
{
	size_t d;
	char *p;

	d = 0;
	for (p = string + length - 1; p >= string; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
			break;

		*p = '\0';
		d++;
	}

	length -= d;

	@try {
		string = [self resizeMemory: string
				     toSize: length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)deleteLeadingAndTrailingWhitespaces
{
	size_t d, i;
	char *p;

	d = 0;
	for (p = string + length - 1; p >= string; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
			break;

		*p = '\0';
		d++;
	}

	length -= d;

	for (i = 0; i < length; i++)
		if (string[i] != ' '  && string[i] != '\t' &&
		    string[i] != '\n' && string[i] != '\r')
			break;

	length -= i;
	memmove(string, string + i, length);
	string[length] = '\0';

	@try {
		string = [self resizeMemory: string
				     toSize: length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- copy
{
	return [[OFString alloc] initWithString: self];
}
@end







|
|
|


|

|

|

|

|
|
|


|

|


|


|
|
|




|
|
|


|

|

|
|
|


|

|





|
|
|
|


|
|

|
|
|


|

|





|
|
|
|
|


|

|


|
|


|

|




|

|

|
|
|
|
|


|
|

|
|
|
|
|


|

|



|
















|





|




|





|

|





|

|


|
|
|
|


|
|



|

|



|
|




>
|
|




|
|
|
|






>
|
|




|
|
|
|






>
|
|




|
|
|
|

|
|
|






|



|














|




|
|
>

|


|
|
|

|
|
|
|

|





|
|
>
|






|


|
|
|


|
|


















|
|
>
|






|


|
|
|

|
|
|

|

|










|


|

|




|





|
|










|









|
>




|
|


|
|
|






|
|
|


|
|
|


|
|












|







|


|
|












|







|

|
|
|


|
|
|


|
|











124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
		j += d;
	}

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

	[self freeMemory: s->string];
	s->string = newString;
	s->length = newLength;
}

- (void)setToCString: (const char*)string
{
	size_t length;

	[self freeMemory: s->string];

	length = strlen(string);

	if (length >= 3 && !memcmp(string, "\xEF\xBB\xBF", 3)) {
		string += 3;
		length -= 3;
	}

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

		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	s->length = length;
	s->string = [self allocMemoryWithSize: length + 1];
	memcpy(s->string, string, length + 1);
}

- (void)appendCString: (const char*)string
{
	size_t length = strlen(string);

	if (length >= 3 && !memcmp(string, "\xEF\xBB\xBF", 3)) {
		string += 3;
		length -= 3;
	}

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

	s->string = [self resizeMemory: s->string
				toSize: s->length + length + 1];
	memcpy(s->string + s->length, string, length + 1);
	s->length += length;
}

- (void)appendCString: (const char*)string
	   withLength: (size_t)length
{
	if (length >= 3 && !memcmp(string, "\xEF\xBB\xBF", 3)) {
		string += 3;
		length -= 3;
	}

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

	s->string = [self resizeMemory: s->string
				toSize: s->length + length + 1];
	memcpy(s->string + s->length, string, length);
	s->length += length;
	s->string[s->length] = 0;
}

- (void)appendCString: (const char*)string
	 withEncoding: (of_string_encoding_t)encoding
	       length: (size_t)length
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		[self appendCString: string
			 withLength: length];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[self appendString: [OFString stringWithCString: string
						       encoding: encoding
							 length: length]];
		[pool release];
	}
}

- (void)appendCStringWithoutUTF8Checking: (const char*)string
{
	size_t length;

	length = strlen(string);
	s->string = [self resizeMemory: s->string
				toSize: s->length + length + 1];
	memcpy(s->string + s->length, string, length + 1);
	s->length += length;
}

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

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

	[self appendCString: [string cString]];
}

- (void)appendFormat: (OFConstantString*)format, ...
{
	va_list arguments;

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

- (void)appendFormat: (OFConstantString*)format
       withArguments: (va_list)arguments
{
	char *t;
	int length;

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

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

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

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

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

	madvise(s->string, s->length, MADV_SEQUENTIAL);

	/* We reverse all bytes and restore UTF-8 later, if necessary */
	for (i = 0, j = s->length - 1; i < length; i++, j--) {
		s->string[i] ^= s->string[j];
		s->string[j] ^= s->string[i];
		s->string[i] ^= s->string[j];
	}

	if (!s->isUTF8) {
		madvise(s->string, s->length, MADV_NORMAL);
		return;
	}

	for (i = 0; i < s->length; i++) {
		/* ASCII */
		if (OF_LIKELY(!(s->string[i] & 0x80)))
			continue;

		/* A start byte can't happen first as we reversed everything */
		if (OF_UNLIKELY(s->string[i] & 0x40)) {
			madvise(s->string, s->length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Next byte must not be ASCII */
		if (OF_UNLIKELY(s->length < i + 1 ||
		    !(s->string[i + 1] & 0x80))) {
			madvise(s->string, s->length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Next byte is the start byte */
		if (OF_LIKELY(s->string[i + 1] & 0x40)) {
			s->string[i] ^= s->string[i + 1];
			s->string[i + 1] ^= s->string[i];
			s->string[i] ^= s->string[i + 1];

			i++;
			continue;
		}

		/* Second next byte must not be ASCII */
		if (OF_UNLIKELY(s->length < i + 2 ||
		    !(s->string[i + 2] & 0x80))) {
			madvise(s->string, s->length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Second next byte is the start byte */
		if (OF_LIKELY(s->string[i + 2] & 0x40)) {
			s->string[i] ^= s->string[i + 2];
			s->string[i + 2] ^= s->string[i];
			s->string[i] ^= s->string[i + 2];

			i += 2;
			continue;
		}

		/* Third next byte must not be ASCII */
		if (OF_UNLIKELY(s->length < i + 3 ||
		    !(s->string[i + 3] & 0x80))) {
			madvise(s->string, s->length, MADV_NORMAL);
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		/* Third next byte is the start byte */
		if (OF_LIKELY(s->string[i + 3] & 0x40)) {
			s->string[i] ^= s->string[i + 3];
			s->string[i + 3] ^= s->string[i];
			s->string[i] ^= s->string[i + 3];

			s->string[i + 1] ^= s->string[i + 2];
			s->string[i + 2] ^= s->string[i + 1];
			s->string[i + 1] ^= s->string[i + 2];

			i += 3;
			continue;
		}

		/* UTF-8 does not allow more than 4 bytes per character */
		madvise(s->string, s->length, MADV_NORMAL);
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	madvise(s->string, s->length, MADV_NORMAL);
}

- (void)upper
{
	[self _applyTable: of_unicode_upper_table
		 withSize: OF_UNICODE_UPPER_TABLE_SIZE];
}

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

- (void)insertString: (OFString*)string
	     atIndex: (size_t)index
{
	size_t newLength;

	if (s->isUTF8)
		index = of_string_index_to_position(s->string, index,
		    s->length);

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

	newLength = s->length + [string cStringLength];
	s->string = [self resizeMemory: s->string
				toSize: newLength + 1];

	memmove(s->string + index + [string cStringLength], s->string + index,
	    s->length - index);
	memcpy(s->string + index, [string cString], [string cStringLength]);
	s->string[newLength] = '\0';

	s->length = newLength;
}

- (void)deleteCharactersFromIndex: (size_t)start
			  toIndex: (size_t)end
{
	if (s->isUTF8) {
		start = of_string_index_to_position(s->string, start,
		    s->length);
		end = of_string_index_to_position(s->string, end, s->length);
	}

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

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

	memmove(s->string + start, s->string + end, s->length - end);
	s->length -= end - start;
	s->string[s->length] = 0;

	@try {
		s->string = [self resizeMemory: s->string
					toSize: s->length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)deleteCharactersInRange: (of_range_t)range
{
	[self deleteCharactersFromIndex: range.start
				toIndex: range.start + range.length];
}

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

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

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

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

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

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

	s->length = newLength;
}

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

- (void)replaceOccurrencesOfString: (OFString*)string
			withString: (OFString*)replacement
{
	const char *cString = [string cString];
	const char *replacementCString = [replacement cString];
	size_t stringLen = [string cStringLength];
	size_t replacementLen = [replacement cStringLength];
	size_t i, last, newLength;
	char *newString;

	if (stringLen > s->length)
		return;

	newString = NULL;
	newLength = 0;

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

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

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

	[self freeMemory: s->string];
	s->string = newString;
	s->length = newLength;
}

- (void)deleteLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < s->length; i++)
		if (s->string[i] != ' '  && s->string[i] != '\t' &&
		    s->string[i] != '\n' && s->string[i] != '\r')
			break;

	s->length -= i;
	memmove(s->string, s->string + i, s->length);
	s->string[s->length] = '\0';

	@try {
		s->string = [self resizeMemory: s->string
					toSize: s->length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)deleteTrailingWhitespaces
{
	size_t d;
	char *p;

	d = 0;
	for (p = s->string + s->length - 1; p >= s->string; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
			break;

		*p = '\0';
		d++;
	}

	s->length -= d;

	@try {
		s->string = [self resizeMemory: s->string
					toSize: s->length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)deleteLeadingAndTrailingWhitespaces
{
	size_t d, i;
	char *p;

	d = 0;
	for (p = s->string + s->length - 1; p >= s->string; p--) {
		if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
			break;

		*p = '\0';
		d++;
	}

	s->length -= d;

	for (i = 0; i < s->length; i++)
		if (s->string[i] != ' '  && s->string[i] != '\t' &&
		    s->string[i] != '\n' && s->string[i] != '\r')
			break;

	s->length -= i;
	memmove(s->string, s->string + i, s->length);
	s->string[s->length] = '\0';

	@try {
		s->string = [self resizeMemory: s->string
					toSize: s->length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- copy
{
	return [[OFString alloc] initWithString: self];
}
@end

Modified src/OFObject.m from [de19156e6d] to [1a556c5a33].

888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
+ (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
}

+ (void*)allocMemoryForNItems: (size_t)nItems
                     withSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
}

+ (void*)resizeMemory: (void*)pointer
	       toSize: (size_t)size







|







888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
+ (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
}

+ (void*)allocMemoryForNItems: (size_t)nItems
		     withSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
}

+ (void*)resizeMemory: (void*)pointer
	       toSize: (size_t)size

Modified src/OFString+Hashing.m from [afd3c22e1b] to [9a2cc09f1a].

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char ret[OF_MD5_DIGEST_SIZE * 2];
	size_t i;

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

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

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







|
|







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char ret[OF_MD5_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: [self cString]
			length: [self cStringLength]];
	digest = [hash digest];

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

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char ret[OF_SHA1_DIGEST_SIZE * 2];
	size_t i;

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

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

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







|
|







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char ret[OF_SHA1_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: [self cString]
			length: [self cStringLength]];
	digest = [hash digest];

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

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

Modified src/OFString+URLEncoding.m from [756001522a] to [93d8d384ea].

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

/* Reference for static linking */
int _OFString_URLEncoding_reference;

@implementation OFString (URLEncoding)
- (OFString*)stringByURLEncoding
{
	const char *string_ = string;
	char *retCString;
	size_t i;
	OFString *ret;

	/*
	 * Worst case: 3 times longer than before.
	 * Oh, and we can't use [self allocWithSize:] here as self might be a
	 * @"" literal.
	 */
	if ((retCString = malloc((length * 3) + 1)) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa

					      requestedSize: (length * 3) + 1];

	for (i = 0; *string_ != '\0'; string_++) {
		if (isalnum((int)*string_) || *string_ == '-' ||
		    *string_ == '_' || *string_ == '.' || *string_ == '~')
			retCString[i++] = *string_;
		else {
			uint8_t high, low;

			high = *string_ >> 4;
			low = *string_ & 0x0F;

			retCString[i++] = '%';
			retCString[i++] =
			    (high > 9 ? high - 10 + 'A' : high + '0');
			retCString[i++] =
			    (low  > 9 ? low  - 10 + 'A' : low  + '0');
		}







|









|
|
>
|

|
|
|
|



|
|







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

/* Reference for static linking */
int _OFString_URLEncoding_reference;

@implementation OFString (URLEncoding)
- (OFString*)stringByURLEncoding
{
	const char *string = [self cString];
	char *retCString;
	size_t i;
	OFString *ret;

	/*
	 * Worst case: 3 times longer than before.
	 * Oh, and we can't use [self allocWithSize:] here as self might be a
	 * @"" literal.
	 */
	if ((retCString = malloc(([self cStringLength] * 3) + 1)) == NULL)
		@throw [OFOutOfMemoryException
		    newWithClass: isa
		   requestedSize: ([self cStringLength] * 3) + 1];

	for (i = 0; *string != '\0'; string++) {
		if (isalnum((int)*string) || *string == '-' || *string == '_' ||
		    *string == '.' || *string == '~')
			retCString[i++] = *string;
		else {
			uint8_t high, low;

			high = *string >> 4;
			low = *string & 0x0F;

			retCString[i++] = '%';
			retCString[i++] =
			    (high > 9 ? high - 10 + 'A' : high + '0');
			retCString[i++] =
			    (low  > 9 ? low  - 10 + 'A' : low  + '0');
		}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

	return ret;
}

- (OFString*)stringByURLDecoding
{
	OFString *ret;
	const char *string_ = string;
	char *retCString;
	char byte = 0;
	int state = 0;
	size_t i;

	if ((retCString = malloc(length + 1)) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa

					      requestedSize: length + 1];

	for (i = 0; *string_; string_++) {
		switch (state) {
		case 0:
			if (*string_ == '%')
				state = 1;
			else if (*string_ == '+')
				retCString[i++] = ' ';
			else
				retCString[i++] = *string_;
			break;
		case 1:
		case 2:;
			uint8_t shift = (state == 1 ? 4  : 0);

			if (*string_ >= '0' && *string_ <= '9')
				byte += (*string_ - '0') << shift;
			else if (*string_ >= 'A' && *string_ <= 'F')
				byte += (*string_ - 'A' + 10) << shift;
			else if (*string_ >= 'a' && *string_ <= 'f')
				byte += (*string_ - 'a' + 10) << shift;
			else {
				free(retCString);
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			if (++state == 3) {







|





|
|
>
|

|


|

|


|





|
|
|
|
|
|







73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

	return ret;
}

- (OFString*)stringByURLDecoding
{
	OFString *ret;
	const char *string = [self cString];
	char *retCString;
	char byte = 0;
	int state = 0;
	size_t i;

	if ((retCString = malloc([self cStringLength] + 1)) == NULL)
		@throw [OFOutOfMemoryException
		    newWithClass: isa
		   requestedSize: [self cStringLength] + 1];

	for (i = 0; *string; string++) {
		switch (state) {
		case 0:
			if (*string == '%')
				state = 1;
			else if (*string == '+')
				retCString[i++] = ' ';
			else
				retCString[i++] = *string;
			break;
		case 1:
		case 2:;
			uint8_t shift = (state == 1 ? 4  : 0);

			if (*string >= '0' && *string <= '9')
				byte += (*string - '0') << shift;
			else if (*string >= 'A' && *string <= 'F')
				byte += (*string - 'A' + 10) << shift;
			else if (*string >= 'a' && *string <= 'f')
				byte += (*string - 'a' + 10) << shift;
			else {
				free(retCString);
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			if (++state == 3) {

Modified src/OFString+XMLEscaping.m from [bd18e10f31] to [b9a4b4f8b8].

26
27
28
29
30
31
32
33
34
35
36
37



38
39
40
41
42
43
44

int _OFString_XMLEscaping_reference;

@implementation OFString (XMLEscaping)
- (OFString*)stringByXMLEscaping
{
	char *retCString;
	const char *append;
	size_t retLength, appendLen;
	size_t i, j;
	OFString *ret;




	j = 0;
	retLength = length;

	/*
	 * We can't use allocMemoryWithSize: here as it might be a @"" literal
	 */
	if ((retCString = malloc(retLength)) == NULL)







|
|



>
>
>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

int _OFString_XMLEscaping_reference;

@implementation OFString (XMLEscaping)
- (OFString*)stringByXMLEscaping
{
	char *retCString;
	const char *string, *append;
	size_t length, retLength, appendLen;
	size_t i, j;
	OFString *ret;

	string = [self cString];
	length = [self cStringLength];

	j = 0;
	retLength = length;

	/*
	 * We can't use allocMemoryWithSize: here as it might be a @"" literal
	 */
	if ((retCString = malloc(retLength)) == NULL)

Modified src/OFString+XMLUnescaping.m from [53b4061156] to [55d286875d].

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
{
	return [self stringByXMLUnescapingWithDelegate: nil];
}

- (OFString*)stringByXMLUnescapingWithDelegate:
    (id <OFStringXMLUnescapingDelegate>)delegate
{

	size_t i, last;
	BOOL inEntity;
	OFMutableString *ret;













	last = 0;
	inEntity = NO;
	ret = [OFMutableString string];
	((OFString*)ret)->isUTF8 = [self isUTF8];

	for (i = 0; i < length; i++) {
		if (!inEntity && string[i] == '&') {
			[ret appendCStringWithoutUTF8Checking: string + last
						       length: i - last];

			last = i + 1;
			inEntity = YES;
		} else if (inEntity && string[i] == ';') {
			char *entity = string + last;
			size_t entityLength = i - last;

			if (entityLength == 2 && !memcmp(entity, "lt", 2))
				[ret appendCStringWithoutUTF8Checking: "<"
							       length: 1];
			else if (entityLength == 2 && !memcmp(entity, "gt", 2))
				[ret appendCStringWithoutUTF8Checking: ">"







>
|



>
>
>
>
>
>
>
>
>
>
>
>


<
<









|







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
{
	return [self stringByXMLUnescapingWithDelegate: nil];
}

- (OFString*)stringByXMLUnescapingWithDelegate:
    (id <OFStringXMLUnescapingDelegate>)delegate
{
	const char *string;
	size_t i, last, length;
	BOOL inEntity;
	OFMutableString *ret;

	string = [self cString];
	length = [self cStringLength];

	ret = [OFMutableString string];

	/*
	 * This is safe as we already called -[cString] on self and thus can be
	 * sure the string is correctly initialized, even if it is a constant
	 * string.
	 */
	((OFString*)ret)->s->isUTF8 = s->isUTF8;

	last = 0;
	inEntity = NO;



	for (i = 0; i < length; i++) {
		if (!inEntity && string[i] == '&') {
			[ret appendCStringWithoutUTF8Checking: string + last
						       length: i - last];

			last = i + 1;
			inEntity = YES;
		} else if (inEntity && string[i] == ';') {
			const char *entity = string + last;
			size_t entityLength = i - last;

			if (entityLength == 2 && !memcmp(entity, "lt", 2))
				[ret appendCStringWithoutUTF8Checking: "<"
							       length: 1];
			else if (entityLength == 2 && !memcmp(entity, "gt", 2))
				[ret appendCStringWithoutUTF8Checking: ">"
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
	return ret;
}

#ifdef OF_HAVE_BLOCKS
- (OFString*)stringByXMLUnescapingWithBlock:
    (of_string_xml_unescaping_block_t)block
{

	size_t i, last;
	BOOL inEntity;
	OFMutableString *ret;













	last = 0;
	inEntity = NO;
	ret = [OFMutableString string];
	((OFString*)ret)->isUTF8 = [self isUTF8];

	for (i = 0; i < length; i++) {
		if (!inEntity && string[i] == '&') {
			[ret appendCStringWithoutUTF8Checking: string + last
						       length: i - last];

			last = i + 1;
			inEntity = YES;
		} else if (inEntity && string[i] == ';') {
			char *entity = string + last;
			size_t entityLength = i - last;

			if (entityLength == 2 && !memcmp(entity, "lt", 2))
				[ret appendCStringWithoutUTF8Checking: "<"
							       length: 1];
			else if (entityLength == 2 && !memcmp(entity, "gt", 2))
				[ret appendCStringWithoutUTF8Checking: ">"







>
|



>
>
>
>
>
>
>
>
>
>
>
>


<
<









|







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

#ifdef OF_HAVE_BLOCKS
- (OFString*)stringByXMLUnescapingWithBlock:
    (of_string_xml_unescaping_block_t)block
{
	const char *string;
	size_t i, last, length;
	BOOL inEntity;
	OFMutableString *ret;

	string = [self cString];
	length = [self cStringLength];

	ret = [OFMutableString string];

	/*
	 * This is safe as we already called -[cString] on self and thus can be
	 * sure the string is correctly initialized, even if it is a constant
	 * string.
	 */
	((OFString*)ret)->s->isUTF8 = s->isUTF8;

	last = 0;
	inEntity = NO;



	for (i = 0; i < length; i++) {
		if (!inEntity && string[i] == '&') {
			[ret appendCStringWithoutUTF8Checking: string + last
						       length: i - last];

			last = i + 1;
			inEntity = YES;
		} else if (inEntity && string[i] == ';') {
			const char *entity = string + last;
			size_t entityLength = i - last;

			if (entityLength == 2 && !memcmp(entity, "lt", 2))
				[ret appendCStringWithoutUTF8Checking: "<"
							       length: 1];
			else if (entityLength == 2 && !memcmp(entity, "gt", 2))
				[ret appendCStringWithoutUTF8Checking: ">"

Modified src/OFString.h from [21d7d8692e] to [78f3465255].

51
52
53
54
55
56
57






58
59
60
61










62
63
64






65
66
67
68
69
70
71
#endif

@class OFArray;
@class OFURL;

/**
 * \brief A class for handling strings.






 */
@interface OFString: OFObject <OFCopying, OFMutableCopying, OFComparing,
    OFSerialization>
{










	char   *string;
	size_t length;
	BOOL   isUTF8;






}

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








>
>
>
>
>
>




>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#endif

@class OFArray;
@class OFURL;

/**
 * \brief A class for handling strings.
 *
 * <b>Warning:</b> If you add methods to OFString using a category, you are not
 * allowed to access the ivars directly, as these might be still uninitialized
 * for a constant string and get initialized on the first message! Therefore,
 * you should use the corresponding methods to get the ivars, which ensures the
 * constant string is initialized.
 */
@interface OFString: OFObject <OFCopying, OFMutableCopying, OFComparing,
    OFSerialization>
{
	/*
	 * The ivars have to be like this because OFConstantString bases on
	 * OFString.
	 *
	 * The compiler generates an instance with a const char* and a size_t
	 * for each constant string. We change the const char* to point to our
	 * struct on the first call to a constant string so we can have more
	 * than those two ivars.
	 */
	struct of_string_ivars {
		char   *string;
		size_t length;
		BOOL   isUTF8;
	} *restrict s;
	/*
	 * Unused in OFString, however, OFConstantString sets this to SIZE_MAX
	 * once it allocated and initialized the struct.
	 */
	size_t initialized;
}

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

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
- (size_t)length;

/**
 * \return The length of the string which cString would return
 */
- (size_t)cStringLength;

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

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







<
<
<
<







527
528
529
530
531
532
533




534
535
536
537
538
539
540
- (size_t)length;

/**
 * \return The length of the string which cString would return
 */
- (size_t)cStringLength;





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

Modified src/OFString.m from [8f7c85175f] to [9224bd11b0].

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
451
452
453
454
455
456
457
458
459
460
461
462



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
}

- init
{
	self = [super init];

	@try {



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

	return self;
}

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

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

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

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

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




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

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

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

			return self;
		}

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

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

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

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

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

				memcpy(string + j, buf, bytes);
				j += bytes;
			}

			string[length] = 0;

			return self;
		}

		switch (encoding) {
		case OF_STRING_ENCODING_ISO_8859_15:
			table = of_iso_8859_15;
			break;
		case OF_STRING_ENCODING_WINDOWS_1252:
			table = of_windows_1252;
			break;
		default:
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

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

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

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

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

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

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

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

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

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

	return self;
}

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

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

	@try {

		/* We have no -[dealloc], so this is ok */


		string = (char*)[string_ cString];

		length = [string_ cStringLength];

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

		if ((string = strdup(string)) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: length + 1];

		@try {
			[self addMemoryToPool: string];
		} @catch (id e) {
			free(string);
			@throw e;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithUnicodeString: (of_unichar_t*)string_
{
	return [self initWithUnicodeString: string_
				 byteOrder: OF_ENDIANESS_NATIVE
				    length: of_unicode_string_length(string_)];
}

- initWithUnicodeString: (of_unichar_t*)string_
	      byteOrder: (of_endianess_t)byteOrder
{
	return [self initWithUnicodeString: string_
				 byteOrder: byteOrder
				    length: of_unicode_string_length(string_)];
}

- initWithUnicodeString: (of_unichar_t*)string_
		 length: (size_t)length_
{
	return [self initWithUnicodeString: string_
				 byteOrder: OF_ENDIANESS_NATIVE
				    length: length_];
}

- initWithUnicodeString: (of_unichar_t*)string_
	      byteOrder: (of_endianess_t)byteOrder
		 length: (size_t)length_
{
	self = [super init];

	@try {
		char buffer[4];
		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_ENDIANESS_NATIVE)
			swap = YES;




		length = length_;
		string = [self allocMemoryWithSize: (length * 4) + 1];

		for (i = 0; i < length_; i++) {

			size_t characterLen = of_string_unicode_to_utf8(
			    (swap ? of_bswap32(string_[i]) : string_[i]),
			    buffer);

			switch (characterLen) {
			case 1:
				string[j++] = buffer[0];
				break;
			case 2:
				isUTF8 = YES;
				length++;

				memcpy(string + j, buffer, 2);
				j += 2;

				break;
			case 3:
				isUTF8 = YES;
				length += 2;

				memcpy(string + j, buffer, 3);
				j += 3;

				break;
			case 4:
				isUTF8 = YES;
				length += 3;

				memcpy(string + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}

		string[j] = '\0';

		@try {
			string = [self resizeMemory: string
					     toSize: length + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
			[e release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithUTF16String: (uint16_t*)string_
{
	return [self initWithUTF16String: string_
			       byteOrder: OF_ENDIANESS_NATIVE
				  length: of_utf16_string_length(string_)];
}

- initWithUTF16String: (uint16_t*)string_
	    byteOrder: (of_endianess_t)byteOrder
{
	return [self initWithUTF16String: string_
			       byteOrder: byteOrder
				  length: of_utf16_string_length(string_)];
}

- initWithUTF16String: (uint16_t*)string_
	       length: (size_t)length_
{
	return [self initWithUTF16String: string_
			       byteOrder: OF_ENDIANESS_NATIVE
				  length: length_];
}

- initWithUTF16String: (uint16_t*)string_
	    byteOrder: (of_endianess_t)byteOrder
	       length: (size_t)length_
{
	self = [super init];

	@try {
		char buffer[4];
		size_t i, j = 0;
		BOOL swap = NO;

		if (length_ > 0 && *string_ == 0xFEFF) {
			string_++;
			length_--;
		} else if (length_ > 0 && *string_ == 0xFFFE) {
			swap = YES;
			string_++;
			length_--;
		} else if (byteOrder != OF_ENDIANESS_NATIVE)
			swap = YES;




		length = length_;
		string = [self allocMemoryWithSize: (length * 4) + 1];

		for (i = 0; i < length_; i++) {

			of_unichar_t character =
			    (swap ? of_bswap16(string_[i]) : string_[i]);
			size_t characterLen;

			/* Missing high surrogate */
			if ((character & 0xFC00) == 0xDC00)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			if ((character & 0xFC00) == 0xD800) {
				uint16_t nextCharacter;

				if (length_ <= i + 1)
					@throw [OFInvalidEncodingException
						newWithClass: isa];

				nextCharacter = (swap
				    ? of_bswap16(string_[i + 1])
				    : string_[i + 1]);
				character = (((character & 0x3FF) << 10) |
				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;
			}

			characterLen = of_string_unicode_to_utf8(
			    character, buffer);

			switch (characterLen) {
			case 1:
				string[j++] = buffer[0];
				break;
			case 2:
				isUTF8 = YES;
				length++;

				memcpy(string + j, buffer, 2);
				j += 2;

				break;
			case 3:
				isUTF8 = YES;
				length += 2;

				memcpy(string + j, buffer, 3);
				j += 3;

				break;
			case 4:
				isUTF8 = YES;
				length += 3;

				memcpy(string + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}

		string[j] = '\0';

		@try {
			string = [self resizeMemory: string
					     toSize: length + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
			[e release];
		}
	} @catch (id e) {
		[self release];
		@throw e;







>
>
>
|
|








|

|

|


|


|

|


|

|








|
|
|


>
>
>
|
|


|

|






|
|





|
|


|
|



|

|





|
|
|

|



|















|
|



|
|



|





|

|





|
|
|

|



|








|
|

|

|


|




>
|
>
>
|
>
|

|

|





|


|


|

|










|

|

|


|


|

|


|
|

|

|


|

|




<



|
|
|
|

|
|



>
>
>
|
|

|
>

|




|


|
|

|




|
|

|




|
|

|









|


|
|












|

|

|


|


|

|


|
|

|

|


|

|




<



|
|
|
|

|
|



>
>
>
|
|

|
>

|










|




|
|











|


|
|

|




|
|

|




|
|

|









|


|
|







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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
}

- init
{
	self = [super init];

	@try {
		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

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

	return self;
}

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

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

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

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

		if (encoding == OF_STRING_ENCODING_UTF_8 &&
		    length >= 3 && !memcmp(string, "\xEF\xBB\xBF", 3)) {
			string += 3;
			length -= 3;
		}

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->string = [self allocMemoryWithSize: length + 1];
		s->length = length;

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

			memcpy(s->string, string, length);
			s->string[length] = 0;

			return self;
		}

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

				if (!(string[i] & 0x80)) {
					s->string[j++] = string[i];
					continue;
				}

				s->isUTF8 = YES;
				bytes = of_string_unicode_to_utf8(
				    (uint8_t)string[i], buffer);

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

				s->length += bytes - 1;
				s->string = [self resizeMemory: s->string
							toSize: s->length + 1];

				memcpy(s->string + j, buffer, bytes);
				j += bytes;
			}

			s->string[s->length] = 0;

			return self;
		}

		switch (encoding) {
		case OF_STRING_ENCODING_ISO_8859_15:
			table = of_iso_8859_15;
			break;
		case OF_STRING_ENCODING_WINDOWS_1252:
			table = of_windows_1252;
			break;
		default:
			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		for (i = j = 0; i < length; i++) {
			char buffer[4];
			of_unichar_t character;
			size_t characterBytes;

			if (!(string[i] & 0x80)) {
				s->string[j++] = string[i];
				continue;
			}

			character = table[(uint8_t)string[i]];

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

			s->isUTF8 = YES;
			characterBytes = of_string_unicode_to_utf8(character,
			    buffer);

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

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

			memcpy(s->string + j, buffer, characterBytes);
			j += characterBytes;
		}

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

	return self;
}

- initWithCString: (const char*)string
	   length: (size_t)length
{
	return [self initWithCString: string
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: length];
}

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

	@try {
		const char *cString;

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		cString = [string cString];
		s->length = [string cStringLength];

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

		if ((s->string = strdup(cString)) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: s->length + 1];

		@try {
			[self addMemoryToPool: s->string];
		} @catch (id e) {
			free(s->string);
			@throw e;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithUnicodeString: (of_unichar_t*)string
{
	return [self initWithUnicodeString: string
				 byteOrder: OF_ENDIANESS_NATIVE
				    length: of_unicode_string_length(string)];
}

- initWithUnicodeString: (of_unichar_t*)string
	      byteOrder: (of_endianess_t)byteOrder
{
	return [self initWithUnicodeString: string
				 byteOrder: byteOrder
				    length: of_unicode_string_length(string)];
}

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

- initWithUnicodeString: (of_unichar_t*)string
	      byteOrder: (of_endianess_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_ENDIANESS_NATIVE)
			swap = YES;

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

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

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

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

				memcpy(s->string + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->isUTF8 = YES;
				s->length += 2;

				memcpy(s->string + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->isUTF8 = YES;
				s->length += 3;

				memcpy(s->string + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}

		s->string[j] = '\0';

		@try {
			s->string = [self resizeMemory: s->string
						toSize: s->length + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
			[e release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithUTF16String: (uint16_t*)string
{
	return [self initWithUTF16String: string
			       byteOrder: OF_ENDIANESS_NATIVE
				  length: of_utf16_string_length(string)];
}

- initWithUTF16String: (uint16_t*)string
	    byteOrder: (of_endianess_t)byteOrder
{
	return [self initWithUTF16String: string
			       byteOrder: byteOrder
				  length: of_utf16_string_length(string)];
}

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

- initWithUTF16String: (uint16_t*)string
	    byteOrder: (of_endianess_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 == 0xFFFE) {
			swap = YES;
			string++;
			length--;
		} else if (byteOrder != OF_ENDIANESS_NATIVE)
			swap = YES;

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

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

		for (i = 0; i < length; i++) {
			char buffer[4];
			of_unichar_t character =
			    (swap ? of_bswap16(string[i]) : string[i]);
			size_t characterLen;

			/* Missing high surrogate */
			if ((character & 0xFC00) == 0xDC00)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			if ((character & 0xFC00) == 0xD800) {
				uint16_t nextCharacter;

				if (length <= i + 1)
					@throw [OFInvalidEncodingException
						newWithClass: isa];

				nextCharacter = (swap
				    ? of_bswap16(string[i + 1])
				    : string[i + 1]);
				character = (((character & 0x3FF) << 10) |
				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;
			}

			characterLen = of_string_unicode_to_utf8(
			    character, buffer);

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

				memcpy(s->string + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->isUTF8 = YES;
				s->length += 2;

				memcpy(s->string + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->isUTF8 = YES;
				s->length += 3;

				memcpy(s->string + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}

		s->string[j] = '\0';

		@try {
			s->string = [self resizeMemory: s->string
						toSize: s->length + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
			[e release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
851
852
853
854
855
856
857
858
859
860
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

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

	@try {
		int len;

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




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

		@try {
			length = len;


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

			[self addMemoryToPool: string];
		} @catch (id e) {
			free(string);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;







|





>
>
>
|



<
|

>
|

|






|

|







867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886

887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908

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

	@try {
		int length;

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

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		if ((length = of_vasprintf(&s->string, [format cString],
		    arguments)) == -1)
			@throw [OFInvalidFormatException newWithClass: isa];


		s->length = length;

		@try {
			switch (of_string_check_utf8(s->string, length)) {
			case 1:
				s->isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			[self addMemoryToPool: s->string];
		} @catch (id e) {
			free(s->string);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
905
906
907
908
909
910
911
912
913
914



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
- initWithPath: (OFString*)firstComponent
     arguments: (va_list)arguments
{
	self = [super init];

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




		length = [firstComponent cStringLength];

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

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

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

		string = [self allocMemoryWithSize: length + 1];

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

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

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

	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	return [self initWithContentsOfFile: path
				   encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfFile: (OFString*)path
		encoding: (of_string_encoding_t)encoding
{
	char *tmp;
	struct stat s;

	self = [super init];

	@try {
		OFFile *file;

		if (stat([path cString], &s) == -1)
			@throw [OFOpenFileFailedException newWithClass: isa
								  path: path
								  mode: @"rb"];

		if (s.st_size > SIZE_MAX)
			@throw [OFOutOfRangeException newWithClass: isa];

		file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];

		@try {
			tmp = [self allocMemoryWithSize: (size_t)s.st_size];

			[file readExactlyNBytes: (size_t)s.st_size
				     intoBuffer: tmp];
		} @finally {
			[file release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithCString: tmp
			    encoding: encoding
			      length: (size_t)s.st_size];
	[self freeMemory: tmp];

	return self;
}

- initWithContentsOfURL: (OFURL*)URL
{







|


>
>
>
|


|

|








|
|


|

|







|

|
|
|


|
|
|
|


|


















|
<
<




|




|






|

|











|







924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997


998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
- initWithPath: (OFString*)firstComponent
     arguments: (va_list)arguments
{
	self = [super init];

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

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->length = [firstComponent cStringLength];

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

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

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

		s->string = [self allocMemoryWithSize: s->length + 1];

		length = [firstComponent cStringLength];
		memcpy(s->string, [firstComponent cString], length);
		i = length;

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

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

	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	return [self initWithContentsOfFile: path
				   encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfFile: (OFString*)path
		encoding: (of_string_encoding_t)encoding
{
	char *tmp;
	struct stat st;



	@try {
		OFFile *file;

		if (stat([path cString], &st) == -1)
			@throw [OFOpenFileFailedException newWithClass: isa
								  path: path
								  mode: @"rb"];

		if (st.st_size > SIZE_MAX)
			@throw [OFOutOfRangeException newWithClass: isa];

		file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];

		@try {
			tmp = [self allocMemoryWithSize: (size_t)st.st_size];

			[file readExactlyNBytes: (size_t)st.st_size
				     intoBuffer: tmp];
		} @finally {
			[file release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithCString: tmp
			    encoding: encoding
			      length: (size_t)st.st_size];
	[self freeMemory: tmp];

	return self;
}

- initWithContentsOfURL: (OFURL*)URL
{
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
	}

	return self;
}

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

- (size_t)length
{
	/* FIXME: Maybe cache this in an ivar? */

	if (![self isUTF8])
		return length;

	return of_string_position_to_index(string, length);
}

- (size_t)cStringLength
{
	return length;
}

- (BOOL)isUTF8
{
	return isUTF8;
}

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

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

	otherString = object;

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

	return YES;
}

- copy
{







|






|
|

|




|
<
<
<
<
<




<
<



<
|
<







1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137





1138
1139
1140
1141


1142
1143
1144

1145

1146
1147
1148
1149
1150
1151
1152
	}

	return self;
}

- (const char*)cString
{
	return s->string;
}

- (size_t)length
{
	/* FIXME: Maybe cache this in an ivar? */

	if (!s->isUTF8)
		return s->length;

	return of_string_position_to_index(s->string, s->length);
}

- (size_t)cStringLength
{
	return s->length;





}

- (BOOL)isEqual: (id)object
{


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


	if (strcmp(s->string, [object cString]))

		return NO;

	return YES;
}

- copy
{
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174

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

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

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

	if (cmp > 0)
		return OF_ORDERED_DESCENDING;
	else







|

|
|

|







1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185

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

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

	if ((cmp = memcmp(s->string, [otherString cString], minLen)) == 0) {
		if (s->length > otherLen)
			return OF_ORDERED_DESCENDING;
		if (s->length < otherLen)
			return OF_ORDERED_ASCENDING;
		return OF_ORDERED_SAME;
	}

	if (cmp > 0)
		return OF_ORDERED_DESCENDING;
	else
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
	if (![otherString isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	otherCString = [otherString cString];
	otherLen = [otherString cStringLength];

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

		if ((cmp = memcasecmp(string, otherCString, minLen)) == 0) {
			if (length > otherLen)
				return OF_ORDERED_DESCENDING;
			if (length < otherLen)
				return OF_ORDERED_ASCENDING;
			return OF_ORDERED_SAME;
		}

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

	i = j = 0;

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

		l1 = of_string_utf8_to_unicode(string + i, length - i, &c1);

		l2 = of_string_utf8_to_unicode(otherCString + j, otherLen - j,
		    &c2);

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

		if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {







|
|

|
|

|












|



|
>







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
	if (![otherString isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	otherCString = [otherString cString];
	otherLen = [otherString cStringLength];

	if (!s->isUTF8) {
		minLen = (s->length > otherLen ? otherLen : s->length);

		if ((cmp = memcasecmp(s->string, otherCString, minLen)) == 0) {
			if (s->length > otherLen)
				return OF_ORDERED_DESCENDING;
			if (s->length < otherLen)
				return OF_ORDERED_ASCENDING;
			return OF_ORDERED_SAME;
		}

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

	i = j = 0;

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

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

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

		if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) {
1239
1240
1241
1242
1243
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
		if (c1 < c2)
			return OF_ORDERED_ASCENDING;

		i += l1;
		j += l2;
	}

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

	return OF_ORDERED_SAME;
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

	OF_HASH_INIT(hash);
	for (i = 0; i < length; i++)
		OF_HASH_ADD(hash, string[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}

- (OFString*)description
{







|

|











|
|







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
		if (c1 < c2)
			return OF_ORDERED_ASCENDING;

		i += l1;
		j += l2;
	}

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

	return OF_ORDERED_SAME;
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

	OF_HASH_INIT(hash);
	for (i = 0; i < s->length; i++)
		OF_HASH_ADD(hash, s->string[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}

- (OFString*)description
{
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
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383

1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
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
	return element;
}

- (of_unichar_t)characterAtIndex: (size_t)index
{
	of_unichar_t c;

	if (![self isUTF8]) {
		if (index >= length)
			@throw [OFOutOfRangeException newWithClass: isa];

		return string[index];
	}

	index = of_string_index_to_position(string, index, length);

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

	if (!of_string_utf8_to_unicode(string + index, length - index, &c))

		@throw [OFInvalidEncodingException newWithClass: isa];

	return c;
}

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

	if (stringLen == 0)
		return 0;

	if (stringLen > length)
		return OF_INVALID_INDEX;

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

	return OF_INVALID_INDEX;
}

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

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

	if (stringLen > length)
		return OF_INVALID_INDEX;

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

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

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

	if (stringLen == 0)
		return YES;

	if (stringLen > length)
		return NO;

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

	return NO;
}

- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end
{
	if ([self isUTF8]) {
		start = of_string_index_to_position(string, start, length);

		end = of_string_index_to_position(string, end, length);
	}

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

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

	return [OFString stringWithCString: string + start
				    length: end - start];
}

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

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

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

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

- (OFString*)stringByPrependingString: (OFString*)string_
{
	OFMutableString *new;

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

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */







|
|


|


|

|


|
>





|

|
|





|


|
|
|




|

|
|



|

|


|
|
|







|

|
|





|


|
|








|
|
>
|






|


|









|




|










|



|







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
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
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
	return element;
}

- (of_unichar_t)characterAtIndex: (size_t)index
{
	of_unichar_t c;

	if (!s->isUTF8) {
		if (index >= s->length)
			@throw [OFOutOfRangeException newWithClass: isa];

		return s->string[index];
	}

	index = of_string_index_to_position(s->string, index, s->length);

	if (index >= s->length)
		@throw [OFOutOfRangeException newWithClass: isa];

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

	return c;
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string
{
	const char *cString = [string cString];
	size_t stringLen = [string cStringLength];
	size_t i;

	if (stringLen == 0)
		return 0;

	if (stringLen > s->length)
		return OF_INVALID_INDEX;

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

	return OF_INVALID_INDEX;
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)string
{
	const char *cString = [string cString];
	size_t stringLen = [string cStringLength];
	size_t i;

	if (stringLen == 0)
		return of_string_position_to_index(s->string, s->length);

	if (stringLen > s->length)
		return OF_INVALID_INDEX;

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

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

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

	if (stringLen == 0)
		return YES;

	if (stringLen > s->length)
		return NO;

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

	return NO;
}

- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end
{
	if (s->isUTF8) {
		start = of_string_index_to_position(s->string, start,
		    s->length);
		end = of_string_index_to_position(s->string, end, s->length);
	}

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

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

	return [OFString stringWithCString: s->string + start
				    length: end - start];
}

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

- (OFString*)stringByAppendingString: (OFString*)string
{
	OFMutableString *new;

	new = [OFMutableString stringWithString: self];
	[new appendString: string];

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

- (OFString*)stringByPrependingString: (OFString*)string
{
	OFMutableString *new;

	new = [OFMutableString stringWithString: string];
	[new appendString: self];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
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
1591
1592
1593
1594
1595
1596
1597
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
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
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
1727
1728
1729
1730
1731
1732
1733
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
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
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
	 */
	new->isa = [OFString class];
	return new;
}

- (BOOL)hasPrefix: (OFString*)prefix
{
	size_t length_ = [prefix cStringLength];

	if (length_ > length)
		return NO;

	return !memcmp(string, [prefix cString], length_);
}

- (BOOL)hasSuffix: (OFString*)suffix
{
	size_t length_ = [suffix cStringLength];

	if (length_ > length)
		return NO;

	return !memcmp(string + (length - length_), [suffix cString], length_);

}

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

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

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

		return array;
	}

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

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

	[pool release];

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

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

	ret = [OFMutableArray array];

	if (pathLen == 0)
		return ret;

	pool = [[OFAutoreleasePool alloc] init];

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

	for (i = 0; i < pathLen; i++) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			[ret addObject:
			    [OFString stringWithCString: string + last
						 length: i - last]];
			last = i + 1;
		}
	}

	[ret addObject: [OFString stringWithCString: string + last
					     length: i - last]];

	[pool release];

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

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

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

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

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

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

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

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

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

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

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

	for (i = pathLen - 1; i >= 1; i--)
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIM)
#else
		if (string[i] == '/' || string[i] == '\\')
#endif
			return [OFString stringWithCString: string
						    length: i];

#ifndef _WIN32
	if (string[0] == OF_PATH_DELIM)
#else
	if (string[i] == '/' || string[i] == '\\')
#endif
		return [OFString stringWithCString: string
					    length: 1];

	return @".";
}

- (intmax_t)decimalValue
{
	const char *string_ = string;
	size_t length_ = length;
	int i = 0;
	intmax_t num = 0;
	BOOL expectWhitespace = NO;

	while (*string_ == ' ' || *string_ == '\t' || *string_ == '\n' ||
	    *string_ == '\r') {
		string_++;
		length_--;
	}

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

	for (; i < length_; i++) {
		if (expectWhitespace) {
			if (string_[i] != ' ' && string_[i] != '\t' &&
			    string_[i] != '\n' && string_[i] != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
			continue;
		}

		if (string_[i] >= '0' && string_[i] <= '9') {
			if (INTMAX_MAX / 10 < num ||
			    INTMAX_MAX - num * 10 < string_[i] - '0')
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			num = (num * 10) + (string_[i] - '0');
		} else if (string_[i] == ' ' || string_[i] == '\t' ||
		    string_[i] == '\n' || string_[i] == '\r')
			expectWhitespace = YES;
		else
			@throw [OFInvalidFormatException newWithClass: isa];
	}

	if (string_[0] == '-')
		num *= -1;

	return num;
}

- (uintmax_t)hexadecimalValue
{
	const char *string_ = string;
	size_t length_ = length;
	int i = 0;
	uintmax_t num = 0;
	BOOL expectWhitespace = NO, gotNumber = NO;

	while (*string_ == ' ' || *string_ == '\t' || *string_ == '\n' ||
	    *string_ == '\r') {
		string_++;
		length_--;
	}

	if (length_ == 0)
		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 newNum;

		if (expectWhitespace) {
			if (string_[i] != ' ' && string_[i] != '\t' &&
			    string_[i] != '\n' && string_[i] != '\r')
				@throw [OFInvalidFormatException
				    newWithClass: isa];
			continue;
		}

		if (string_[i] >= '0' && string_[i] <= '9') {
			newNum = (num << 4) | (string_[i] - '0');
			gotNumber = YES;
		} else if (string_[i] >= 'A' && string_[i] <= 'F') {
			newNum = (num << 4) | (string_[i] - 'A' + 10);
			gotNumber = YES;
		} else if (string_[i] >= 'a' && string_[i] <= 'f') {
			newNum = (num << 4) | (string_[i] - 'a' + 10);
			gotNumber = YES;
		} else if (string_[i] == 'h' || string_[i] == ' ' ||
		    string_[i] == '\t' || string_[i] == '\n' ||
		    string_[i] == '\r') {
			expectWhitespace = YES;
			continue;
		} else
			@throw [OFInvalidFormatException newWithClass: isa];

		if (newNum < num)
			@throw [OFOutOfRangeException newWithClass: isa];

		num = newNum;
	}

	if (!gotNumber)
		@throw [OFInvalidFormatException newWithClass: isa];

	return num;
}

- (float)floatValue
{
	const char *string_ = string;
	char *endPointer = NULL;
	float value;

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

	value = strtof(string_, &endPointer);

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

	return value;
}

- (double)doubleValue
{
	const char *string_ = string;
	char *endPointer = NULL;
	double value;

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

	value = strtod(string_, &endPointer);

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







|

|


|




|

|


|
>













|






|
|


|




|
















|









|

|





|

|


|





|















|






|

|





|

|













|





|





|

|




|




|

|

|



|

|

|







|
|




|
|
|
|


|


|

|
|





|

|



|
|
|





|







|
|




|
|
|
|


|


|

|


|



|
|





|
|

|
|

|
|

|
|
|



















|



|
|
|

|














|



|
|
|

|







1525
1526
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
1591
1592
1593
1594
1595
1596
1597
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
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
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
1727
1728
1729
1730
1731
1732
1733
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
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
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
	 */
	new->isa = [OFString class];
	return new;
}

- (BOOL)hasPrefix: (OFString*)prefix
{
	size_t length = [prefix cStringLength];

	if (length > s->length)
		return NO;

	return !memcmp(s->string, [prefix cString], length);
}

- (BOOL)hasSuffix: (OFString*)suffix
{
	size_t length = [suffix cStringLength];

	if (length > s->length)
		return NO;

	return !memcmp(s->string + (s->length - length), [suffix cString],
	    length);
}

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

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

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

		return array;
	}

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

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

	[pool release];

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

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

	ret = [OFMutableArray array];

	if (pathLen == 0)
		return ret;

	pool = [[OFAutoreleasePool alloc] init];

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

	for (i = 0; i < pathLen; i++) {
#ifndef _WIN32
		if (s->string[i] == OF_PATH_DELIM) {
#else
		if (s->string[i] == '/' || s->string[i] == '\\') {
#endif
			[ret addObject:
			    [OFString stringWithCString: s->string + last
						 length: i - last]];
			last = i + 1;
		}
	}

	[ret addObject: [OFString stringWithCString: s->string + last
					     length: i - last]];

	[pool release];

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

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

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

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

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

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

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

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

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

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

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

	for (i = pathLen - 1; i >= 1; i--)
#ifndef _WIN32
		if (s->string[i] == OF_PATH_DELIM)
#else
		if (s->string[i] == '/' || s->string[i] == '\\')
#endif
			return [OFString stringWithCString: s->string
						    length: i];

#ifndef _WIN32
	if (s->string[0] == OF_PATH_DELIM)
#else
	if (s->string[i] == '/' || s->string[i] == '\\')
#endif
		return [OFString stringWithCString: s->string
					    length: 1];

	return @".";
}

- (intmax_t)decimalValue
{
	const char *string = s->string;
	size_t length = s->length;
	int i = 0;
	intmax_t num = 0;
	BOOL expectWhitespace = NO;

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

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

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

		if (string[i] >= '0' && string[i] <= '9') {
			if (INTMAX_MAX / 10 < num ||
			    INTMAX_MAX - num * 10 < string[i] - '0')
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			num = (num * 10) + (string[i] - '0');
		} else if (string[i] == ' ' || string[i] == '\t' ||
		    string[i] == '\n' || string[i] == '\r')
			expectWhitespace = YES;
		else
			@throw [OFInvalidFormatException newWithClass: isa];
	}

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

	return num;
}

- (uintmax_t)hexadecimalValue
{
	const char *string = s->string;
	size_t length = s->length;
	int i = 0;
	uintmax_t num = 0;
	BOOL expectWhitespace = NO, gotNumber = NO;

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

	if (length == 0)
		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 newNum;

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

		if (string[i] >= '0' && string[i] <= '9') {
			newNum = (num << 4) | (string[i] - '0');
			gotNumber = YES;
		} else if (string[i] >= 'A' && string[i] <= 'F') {
			newNum = (num << 4) | (string[i] - 'A' + 10);
			gotNumber = YES;
		} else if (string[i] >= 'a' && string[i] <= 'f') {
			newNum = (num << 4) | (string[i] - 'a' + 10);
			gotNumber = YES;
		} else if (string[i] == 'h' || string[i] == ' ' ||
		    string[i] == '\t' || string[i] == '\n' ||
		    string[i] == '\r') {
			expectWhitespace = YES;
			continue;
		} else
			@throw [OFInvalidFormatException newWithClass: isa];

		if (newNum < num)
			@throw [OFOutOfRangeException newWithClass: isa];

		num = newNum;
	}

	if (!gotNumber)
		@throw [OFInvalidFormatException newWithClass: isa];

	return num;
}

- (float)floatValue
{
	const char *string = s->string;
	char *endPointer = NULL;
	float value;

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

	value = strtof(string, &endPointer);

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

	return value;
}

- (double)doubleValue
{
	const char *string = s->string;
	char *endPointer = NULL;
	double value;

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

	value = strtod(string, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r')
				@throw [OFInvalidFormatException
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870

1871
1872
1873
1874
1875
1876
1877
				  withSize: sizeof(of_unichar_t)];

	i = 0;
	j = 0;

	ret[j++] = 0xFEFF;

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

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


		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException newWithClass: isa];

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







|



|
>







1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
				  withSize: sizeof(of_unichar_t)];

	i = 0;
	j = 0;

	ret[j++] = 0xFEFF;

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

		cLen = of_string_utf8_to_unicode(s->string + i, s->length - i,
		    &c);

		if (cLen == 0 || c > 0x10FFFF)
			@throw [OFInvalidEncodingException newWithClass: isa];

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