ObjFW  Check-in [8389241a05]

Overview
Comment:OFMutableString's -[upper] and -[lower]: Get rid of code duplication.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8389241a055d3fecfc8f6b4354abaf2e647a4fab972820521fb9aae03b1c851b
User & Date: js on 2009-10-16 08:44:46
Other Links: manifest | tags
Context
2009-10-16
10:46
Implement -[hash] for OFArray. check-in: deea33a4d9 user: js tags: trunk
08:44
OFMutableString's -[upper] and -[lower]: Get rid of code duplication. check-in: 8389241a05 user: js tags: trunk
2009-10-15
20:38
Full Unicode support for OFMutableString's -[upper] and -[lower]. check-in: 0480a27d5e user: js tags: trunk
Changes

Modified src/Makefile from [58a18bd0f1] to [2b51a81e8b].

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	    objfw.h			\
	    threading.h

SRCS += ${OBJC_SYNC_M}		\
	${ASPRINTF_C}		\
	iso_8859_15.c		\
	windows_1252.c		\
	unicode_lower.m		\
	unicode_upper.m		\
	unicode_titlecase.m

include ../buildsys.mk

CPPFLAGS += -I..
CFLAGS += ${LIB_CFLAGS}
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}







<

|







36
37
38
39
40
41
42

43
44
45
46
47
48
49
50
51
	    objfw.h			\
	    threading.h

SRCS += ${OBJC_SYNC_M}		\
	${ASPRINTF_C}		\
	iso_8859_15.c		\
	windows_1252.c		\

	unicode_upper.m		\
	unicode_lower.m

include ../buildsys.mk

CPPFLAGS += -I..
CFLAGS += ${LIB_CFLAGS}
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}

Modified src/OFMutableString.m from [faf789896c] to [feeaa349e9].

28
29
30
31
32
33
34































































































35
36
37
38
39
40
41
#import "OFExceptions.h"
#import "OFMacros.h"

#import "asprintf.h"

extern const of_unichar_t* const of_unicode_upper_table[0x1100];
extern const of_unichar_t* const of_unicode_lower_table[0x1100];
































































































@implementation OFMutableString
- setToCString: (const char*)str
{
	size_t len;

	if (string != NULL)







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







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
#import "OFExceptions.h"
#import "OFMacros.h"

#import "asprintf.h"

extern const of_unichar_t* const of_unicode_upper_table[0x1100];
extern const of_unichar_t* const of_unicode_lower_table[0x1100];

static void
apply_table(id self, Class isa, char **string, unsigned int *length,
    BOOL is_utf8, const of_unichar_t* const table[])
{
	of_unichar_t c, tc;
	of_unichar_t *ustr;
	size_t ulen, nlen;
	size_t i, j, d;
	char *nstr;

	if (!is_utf8) {
		uint8_t *p = (uint8_t*)*string + *length;
		uint8_t t;

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

		return;
	}

	ulen = [self length];
	ustr = [self allocMemoryForNItems: [self length]
				 withSize: ulen];

	j = 0;
	nlen = 0;

	for (i = 0; i < *length; i++) {
		c = of_string_utf8_to_unicode(*string + i, *length - i);

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

		if ((tc = table[c >> 8][c & 0xFF]) == 0)
			tc = c;
		ustr[j++] = tc;

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

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

	@try {
		nstr = [self allocMemoryWithSize: nlen + 1];
	} @catch (OFException *e) {
		[self freeMemory: ustr];
		@throw e;
	}

	j = 0;

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

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

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

@implementation OFMutableString
- setToCString: (const char*)str
{
	size_t len;

	if (string != NULL)
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
	madvise(string, len, MADV_NORMAL);

	return self;
}

- upper
{
	of_unichar_t c, uc;
	of_unichar_t *ustr;
	size_t ulen, nlen;
	size_t i, j, d;
	char *nstr;

	if (!is_utf8) {
		uint8_t *p = (uint8_t*)string + length;
		uint8_t t;

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

		return self;
	}

	ulen = [self length];
	ustr = [self allocMemoryForNItems: [self length]
				 withSize: ulen];

	j = 0;
	nlen = 0;

	for (i = 0; i < length; i++) {
		c = of_string_utf8_to_unicode(string + i, length - i);

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

		uc = of_unicode_upper_table[c >> 8][c & 0xFF];
		if (uc == 0)
			uc = c;
		ustr[j++] = uc;

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

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

	@try {
		nstr = [self allocMemoryWithSize: nlen + 1];
	} @catch (OFException *e) {
		[self freeMemory: ustr];
		@throw e;
	}

	j = 0;

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

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

	[self freeMemory: string];
	string = nstr;
	length = nlen;

	return self;
}

- lower
{
	of_unichar_t c, lc;
	of_unichar_t *ustr;
	size_t ulen, nlen;
	size_t i, j, d;
	char *nstr;

	if (!is_utf8) {
		uint8_t *p = (uint8_t*)string + length;
		uint8_t t;

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

		return self;
	}

	ulen = [self length];
	ustr = [self allocMemoryForNItems: [self length]
				 withSize: ulen];

	j = 0;
	nlen = 0;

	for (i = 0; i < length; i++) {
		c = of_string_utf8_to_unicode(string + i, length - i);

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

		lc = of_unicode_lower_table[c >> 8][c & 0xFF];
		if (lc == 0)
			lc = c;
		ustr[j++] = lc;

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

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

	@try {
		nstr = [self allocMemoryWithSize: nlen + 1];
	} @catch (OFException *e) {
		[self freeMemory: ustr];
		@throw e;
	}

	j = 0;

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

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

	[self freeMemory: string];
	string = nstr;
	length = nlen;

	return self;
}

- removeCharactersFromIndex: (size_t)start
		    toIndex: (size_t)end
{







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






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







370
371
372
373
374
375
376





377





378














































































379
380
381
382
383
384





385





386














































































387
388
389
390
391
392
393
	madvise(string, len, MADV_NORMAL);

	return self;
}

- upper
{





	apply_table(self, isa, &string, &length, is_utf8,





	    of_unicode_upper_table);















































































	return self;
}

- lower
{





	apply_table(self, isa, &string, &length, is_utf8,





	    of_unicode_lower_table);















































































	return self;
}

- removeCharactersFromIndex: (size_t)start
		    toIndex: (size_t)end
{

Modified src/OFString.h from [0ac3d63fc6] to [72b442269e].

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

/**
 * A class for managing strings.
 */
@interface OFString: OFObject <OFCopying, OFMutableCopying>
{
	char	     *string;
#ifdef __objc_INCLUDE_GNU
	unsigned int length;
#else
	int	     length;
#if __LP64__
	int	     _unused;
#endif
#endif
	BOOL	     is_utf8;
}

/**
 * \return A new autoreleased OFString
 */







<

<
<
|

<







34
35
36
37
38
39
40

41


42
43

44
45
46
47
48
49
50

/**
 * A class for managing strings.
 */
@interface OFString: OFObject <OFCopying, OFMutableCopying>
{
	char	     *string;

	unsigned int length;


#if !defined(__objc_INCLUDE_GNU) && __LP64__
	int	     _unused;

#endif
	BOOL	     is_utf8;
}

/**
 * \return A new autoreleased OFString
 */