ObjFW  Check-in [48190e2a91]

Overview
Comment:Add +[stringWithPath:] to OFString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 48190e2a917224c1ffceda1acd944d09f278506d2fc1ecf5c5256232489c57a3
User & Date: js on 2010-04-10 23:52:54
Other Links: manifest | tags
Context
2010-04-11
02:49
Add +[stringWithContentsOfFile:] to OFString. check-in: c7630df319 user: js tags: trunk
2010-04-10
23:52
Add +[stringWithPath:] to OFString. check-in: 48190e2a91 user: js tags: trunk
23:10
Prevent starting a thread twice. check-in: 8135be3b0f user: js tags: trunk
Changes

Modified src/OFMutableString.m from [7f7b9ed83b] to [e8c7a3fc62].

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	string[length] = 0;

	return self;
}

- appendString: (OFString*)str
{
	[self appendCStringWithoutUTF8Checking: [str cString]];

	if (str->is_utf8)
		is_utf8 = YES;

	return self;
}

- appendFormat: (OFString*)fmt, ...
{
	id ret;







|
<
<
<







223
224
225
226
227
228
229
230



231
232
233
234
235
236
237
	string[length] = 0;

	return self;
}

- appendString: (OFString*)str
{
	[self appendCString: [str cString]];




	return self;
}

- appendFormat: (OFString*)fmt, ...
{
	id ret;

Modified src/OFString.h from [f68ed3b0a1] to [7aecf0bec8].

96
97
98
99
100
101
102








103
104
105
106
107
108
109
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \return A new autoreleased OFString
 */
+ stringWithFormat: (OFString*)fmt, ...;









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







>
>
>
>
>
>
>
>







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \return A new autoreleased OFString
 */
+ stringWithFormat: (OFString*)fmt, ...;

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

/**
 * Creates a new OFString from another string.
 *
 * \param str A string to initialize the OFString with
 * \return A new autoreleased OFString
 */
+ stringWithString: (OFString*)str;
174
175
176
177
178
179
180




















181
182
183
184
185
186
187
 * \param fmt A string used as format to initialize the OFString
 * \param args The arguments used in the format string
 * \return An initialized OFString
 */
- initWithFormat: (OFString*)fmt
       arguments: (va_list)args;





















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







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







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
 * \param fmt A string used as format to initialize the OFString
 * \param args The arguments used in the format string
 * \return An initialized OFString
 */
- initWithFormat: (OFString*)fmt
       arguments: (va_list)args;

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

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

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

Modified src/OFString.m from [bdc5753e61] to [4ec189c3b9].

27
28
29
30
31
32
33






34
35
36
37
38
39
40
#import "OFArray.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"
#import "unicode.h"







extern const uint16_t of_iso_8859_15[256];
extern const uint16_t of_windows_1252[256];

/* References for static linking */
void _references_to_categories_of_OFString()
{







>
>
>
>
>
>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#import "OFArray.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"
#import "unicode.h"

#ifndef _WIN32
# define PATH_DELIM '/'
#else
# define PATH_DELIM '\\'
#endif

extern const uint16_t of_iso_8859_15[256];
extern const uint16_t of_windows_1252[256];

/* References for static linking */
void _references_to_categories_of_OFString()
{
249
250
251
252
253
254
255













256
257
258
259
260
261
262
	va_start(args, fmt);
	ret = [[[self alloc] initWithFormat: fmt
				  arguments: args] autorelease];
	va_end(args);

	return ret;
}














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

- init







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







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
	va_start(args, fmt);
	ret = [[[self alloc] initWithFormat: fmt
				  arguments: args] autorelease];
	va_end(args);

	return ret;
}

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

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

	return ret;
}

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

- init
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
			      length: strlen(str)];
}

- initWithCString: (const char*)str
	 encoding: (enum of_string_encoding)encoding
	   length: (size_t)len
{
	Class c;
	size_t i, j;

	self = [super init];

	length = len;

	@try {







<







302
303
304
305
306
307
308

309
310
311
312
313
314
315
			      length: strlen(str)];
}

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

	size_t i, j;

	self = [super init];

	length = len;

	@try {
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		switch (of_string_check_utf8(str, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:
			/*
			 * We can't use [super dealloc] on OS X here.
			 * Compiler bug? Anyway, [self dealloc] will do here as
			 * we don't reimplement dealloc.
			 */
			c = isa;
			[self dealloc];
			@throw [OFInvalidEncodingException newWithClass: c];
		}

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








|





|







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

	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		switch (of_string_check_utf8(str, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:;
			/*
			 * We can't use [super dealloc] on OS X here.
			 * Compiler bug? Anyway, [self dealloc] will do here as
			 * we don't reimplement dealloc.
			 */
			Class c = isa;
			[self dealloc];
			@throw [OFInvalidEncodingException newWithClass: c];
		}

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

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
					break;
				case OF_STRING_ENCODING_ISO_8859_15:
					chr = of_iso_8859_15[(uint8_t)str[i]];
					break;
				case OF_STRING_ENCODING_WINDOWS_1252:
					chr = of_windows_1252[(uint8_t)str[i]];
					break;
				default:
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				if (chr == 0xFFFD) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				is_utf8 = YES;
				chr_bytes = of_string_unicode_to_utf8(chr, buf);

				if (chr_bytes == 0) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				length += chr_bytes - 1;
				@try {







|






|












|















|







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
					break;
				case OF_STRING_ENCODING_ISO_8859_15:
					chr = of_iso_8859_15[(uint8_t)str[i]];
					break;
				case OF_STRING_ENCODING_WINDOWS_1252:
					chr = of_windows_1252[(uint8_t)str[i]];
					break;
				default:;
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					Class c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				if (chr == 0xFFFD) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					Class c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				is_utf8 = YES;
				chr_bytes = of_string_unicode_to_utf8(chr, buf);

				if (chr_bytes == 0) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					Class c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				length += chr_bytes - 1;
				@try {
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
				j += chr_bytes;
			}
		}

		string[length] = 0;

		break;
	default:
		/*
		 * We can't use [super dealloc] on OS X here.
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		c = isa;
		[self dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	return self;
}








|





|







428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
				j += chr_bytes;
			}
		}

		string[length] = 0;

		break;
	default:;
		/*
		 * We can't use [super dealloc] on OS X here.
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		Class c = isa;
		[self dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	return self;
}

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

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
{
	int t;
	Class c;

	self = [super init];

	if (fmt == nil) {
		c = isa;
		[super dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	if ((t = vasprintf(&string, [fmt cString], args)) == -1) {
		c = isa;
		[super dealloc];

		/*
		 * This is only the most likely error to happen. Unfortunately,
		 * there is no good way to check what really happened.
		 */
		@throw [OFOutOfMemoryException newWithClass: c];
	}
	length = t;

	switch (of_string_check_utf8(string, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:

			free(string);
			c = isa;
			[super dealloc];
			@throw [OFInvalidEncodingException newWithClass: c];
	}

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

	return self;
}














- initWithString: (OFString*)str

{



	Class c;

	self = [super init];












	string = (char*)[str cString];


	length = [str cStringLength];


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










































	if ((string = strdup(string)) == NULL) {
		c = isa;
		[self dealloc];
		@throw [OFOutOfMemoryException newWithClass: c
						       size: length + 1];
	}

	@try {
		[self addMemoryToPool: string];







<




|






|











|
|
|
|
>
|
<
|
|












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

>
>
>




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

|







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

|







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

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


	self = [super init];

	if (fmt == nil) {
		Class c = isa;
		[super dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	if ((t = vasprintf(&string, [fmt cString], args)) == -1) {
		Class c = isa;
		[super dealloc];

		/*
		 * This is only the most likely error to happen. Unfortunately,
		 * there is no good way to check what really happened.
		 */
		@throw [OFOutOfMemoryException newWithClass: c];
	}
	length = t;

	switch (of_string_check_utf8(string, length)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:;
		Class c = isa;
		free(string);

		[super dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

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

	return self;
}

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

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

	return ret;
}

- initWithPath: (OFString*)first
     arguments: (va_list)args
{
	OFString *component;
	size_t len, i;
	va_list args2;
	Class c;

	self = [super init];

	len = [first cStringLength];
	switch (of_string_check_utf8([first cString], len)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:
		c = isa;
		[self dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}
	length += len;

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

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

	@try {
		string = [self allocMemoryWithSize: length + 1];
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

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

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

	string[i] = '\0';

	return self;
}

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

	string = (char*)[str cString];
	length = [str cStringLength];

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

	if ((string = strdup(string)) == NULL) {
		Class c = isa;
		[self dealloc];
		@throw [OFOutOfMemoryException newWithClass: c
						       size: length + 1];
	}

	@try {
		[self addMemoryToPool: string];

Modified tests/OFStringTests.m from [ac7e540eac] to [0740a8d2f7].

136
137
138
139
140
141
142










143
144
145
146
147
148
149
					  "\x9C\x9E\x9F"
				encoding: OF_STRING_ENCODING_WINDOWS_1252]
	    isEqual: @"€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"])

	TEST(@"+[stringWithFormat:]",
	    [(s[0] = [OFMutableString stringWithFormat: @"%s: %d", "test", 123])
	    isEqual: @"test: 123"])











	TEST(@"-[appendFormat:]",
	    [([s[0] appendFormat: @"%02X", 15]) isEqual: @"test: 1230F"])

	TEST(@"-[indexOfFirstOccurrenceOfString:]",
	    [@"𝄞öö" indexOfFirstOccurrenceOfString: @"öö"] == 1 &&
	    [@"𝄞öö" indexOfFirstOccurrenceOfString: @"ö"] == 1 &&







>
>
>
>
>
>
>
>
>
>







136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
					  "\x9C\x9E\x9F"
				encoding: OF_STRING_ENCODING_WINDOWS_1252]
	    isEqual: @"€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"])

	TEST(@"+[stringWithFormat:]",
	    [(s[0] = [OFMutableString stringWithFormat: @"%s: %d", "test", 123])
	    isEqual: @"test: 123"])

	TEST(@"+[stringWithPath:]",
	    (s[1] = [OFString stringWithPath: @"foo", @"bar", @"baz", nil]) &&
#ifndef _WIN32
	    [s[1] isEqual: @"foo/bar/baz"] &&
#else
	    [s[1] isEqual: @"foo/bar/baz"] &&
#endif
	    (s[1] = [OFString stringWithPath: @"foo", nil]) &&
	    [s[1] isEqual: @"foo"])

	TEST(@"-[appendFormat:]",
	    [([s[0] appendFormat: @"%02X", 15]) isEqual: @"test: 1230F"])

	TEST(@"-[indexOfFirstOccurrenceOfString:]",
	    [@"𝄞öö" indexOfFirstOccurrenceOfString: @"öö"] == 1 &&
	    [@"𝄞öö" indexOfFirstOccurrenceOfString: @"ö"] == 1 &&