ObjFW  Check-in [9a2430abfc]

Overview
Comment:Add -[OFString capitalizedString].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9a2430abfcc9da58352d7a83f2b49fb893bbc739dfe0f68c476ff061cf468205
User & Date: js on 2012-07-06 15:00:06
Other Links: manifest | tags
Context
2012-07-07
11:10
Remove useless variable. check-in: a7d93acf17 user: js tags: trunk
2012-07-06
15:00
Add -[OFString capitalizedString]. check-in: 9a2430abfc user: js tags: trunk
2012-07-04
22:50
Rename -[{upper,lower}] to -[{upper,lower}case]. check-in: 8e995a13e7 user: js tags: trunk
Changes

Modified src/OFMutableString.h from [5224ad3044] to [7a52b50b3a].

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
 * \brief Prepends another OFString to the OFMutableString.
 *
 * \param string An OFString to prepend
 */
- (void)prependString: (OFString*)string;

/**
 * \brief Reverses the OFMutableString.
 */
- (void)reverse;

/**
 * \brief Converts the OFMutableString to uppercase.
 */
- (void)uppercase;

/**
 * \brief Converts the OFMutableString to lowercase.
 */
- (void)lowercase;










/**
 * \brief Inserts a string at the specified index.
 *
 * \param string The string to insert
 * \param index The index
 */
- (void)insertString: (OFString*)string







|




|




|



>
>
>
>
>
>
>
>
>







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
 * \brief Prepends another OFString to the OFMutableString.
 *
 * \param string An OFString to prepend
 */
- (void)prependString: (OFString*)string;

/**
 * \brief Reverses the string.
 */
- (void)reverse;

/**
 * \brief Converts the string to uppercase.
 */
- (void)uppercase;

/**
 * \brief Converts the string to lowercase.
 */
- (void)lowercase;

/**
 * \brief Capitalizes the string.
 *
 * \note This only considers spaces, tabs and newlines to be word delimiters!
 *	 Also note that this might change in the future to all word delimiters
 *	 specified by Unicode!
 */
- (void)capitalize;

/**
 * \brief Inserts a string at the specified index.
 *
 * \param string The string to insert
 * \param index The index
 */
- (void)insertString: (OFString*)string

Modified src/OFMutableString.m from [9288f5195e] to [a950dfa678].

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
{
	if (self == [OFMutableString class])
		return (id)&placeholder;

	return [super alloc];
}


- (void)_applyTable: (const of_unichar_t* const[])table
	   withSize: (size_t)tableSize

{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	const of_unichar_t *string = [self unicodeString];
	size_t i, length = [self length];


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


		of_unichar_t c = string[i];









		if (c >> 8 < tableSize && table[c >> 8][c & 0xFF])
			[self setCharacter: table[c >> 8][c & 0xFF]
				   atIndex: i];












	}

	[pool release];
}

- (void)setCharacter: (of_unichar_t)character
	     atIndex: (size_t)index







>
|
|
>




>


>
>

>
>
>
>
>
>
>
>




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







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
{
	if (self == [OFMutableString class])
		return (id)&placeholder;

	return [super alloc];
}

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

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

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

		if (c >> 8 < tableSize && table[c >> 8][c & 0xFF])
			[self setCharacter: table[c >> 8][c & 0xFF]
				   atIndex: i];

		switch (c) {
		case ' ':
		case '\t':
		case '\n':
		case '\r':
			isStart = YES;
			break;
		default:
			isStart = NO;
			break;
		}
	}

	[pool release];
}

- (void)setCharacter: (of_unichar_t)character
	     atIndex: (size_t)index
378
379
380
381
382
383
384

385

386
387
388
389
390

391

392








393
394
395
396
397
398
399
		[self setCharacter: tmp
			   atIndex: i];
	}
}

- (void)uppercase
{

	[self _applyTable: of_unicode_uppercase_table

		 withSize: OF_UNICODE_UPPERCASE_TABLE_SIZE];
}

- (void)lowercase
{

	[self _applyTable: of_unicode_lowercase_table

		 withSize: OF_UNICODE_LOWERCASE_TABLE_SIZE];








}

- (void)insertString: (OFString*)string
	     atIndex: (size_t)index
{
	@throw [OFNotImplementedException exceptionWithClass: isa
						    selector: _cmd];







>
|
>
|




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







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
		[self setCharacter: tmp
			   atIndex: i];
	}
}

- (void)uppercase
{
	[self _convertWithWordStartTable: of_unicode_uppercase_table
			 wordMiddleTable: of_unicode_uppercase_table
		      wordStartTableSize: OF_UNICODE_UPPERCASE_TABLE_SIZE
		     wordMiddleTableSize: OF_UNICODE_UPPERCASE_TABLE_SIZE];
}

- (void)lowercase
{
	[self _convertWithWordStartTable: of_unicode_lowercase_table
			 wordMiddleTable: of_unicode_lowercase_table
		      wordStartTableSize: OF_UNICODE_LOWERCASE_TABLE_SIZE
		     wordMiddleTableSize: OF_UNICODE_LOWERCASE_TABLE_SIZE];
}

- (void)capitalize
{
	[self _convertWithWordStartTable: of_unicode_titlecase_table
			 wordMiddleTable: of_unicode_lowercase_table
		      wordStartTableSize: OF_UNICODE_TITLECASE_TABLE_SIZE
		     wordMiddleTableSize: OF_UNICODE_LOWERCASE_TABLE_SIZE];
}

- (void)insertString: (OFString*)string
	     atIndex: (size_t)index
{
	@throw [OFNotImplementedException exceptionWithClass: isa
						    selector: _cmd];

Modified src/OFMutableString_UTF8.m from [ccd236b694] to [19cda638bc].

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
@implementation OFMutableString_UTF8
+ (void)initialize
{
	if (self == [OFMutableString_UTF8 class])
		[self inheritMethodsFromClass: [OFString_UTF8 class]];
}


- (void)_applyTable: (const of_unichar_t* const[])table
	   withSize: (size_t)tableSize

{
	of_unichar_t c;
	of_unichar_t *unicodeString;
	size_t unicodeLen, newCStringLength, cLen;
	size_t i, j, d;
	char *newCString;


	if (!s->UTF8) {


		assert(tableSize >= 1);


		uint8_t *p = (uint8_t*)s->cString + s->cStringLength;

		uint8_t t;

















		s->hashed = NO;


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


		return;
	}

	unicodeLen = [self length];
	unicodeString = [self allocMemoryWithSize: sizeof(of_unichar_t)
					    count: unicodeLen];

	i = j = 0;
	newCStringLength = 0;

	while (i < s->cStringLength) {













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

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













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

			if (tc)
				c = tc;
		}







>
|
|
>

<

|
|

>


>
>
|

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












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








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







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
@implementation OFMutableString_UTF8
+ (void)initialize
{
	if (self == [OFMutableString_UTF8 class])
		[self inheritMethodsFromClass: [OFString_UTF8 class]];
}

- (void)_convertWithWordStartTable: (const of_unichar_t *const[])startTable
		   wordMiddleTable: (const of_unichar_t *const[])middleTable
		wordStartTableSize: (size_t)startTableSize
	       wordMiddleTableSize: (size_t)middleTableSize
{

	of_unichar_t *unicodeString;
	size_t unicodeLen, newCStringLength;
	size_t i, j;
	char *newCString;
	BOOL isStart = YES;

	if (!s->UTF8) {
		uint8_t t;
		const of_unichar_t *const *table;
		size_t tableSize;

		assert(startTableSize >= 1 && middleTableSize >= 1);

		s->hashed = NO;

		for (i = 0; i < s->cStringLength; i++) {
			if (isStart) {
				table = startTable;
				tableSize = middleTableSize;
			} else {
				table = middleTable;
				tableSize = middleTableSize;
			}

			switch (s->cString[i]) {
			case ' ':
			case '\t':
			case '\n':
			case '\r':
				isStart = YES;
				break;
			default:
				isStart = NO;
				break;
			}

			if ((t = table[0][(uint8_t)s->cString[i]]) != 0)
				s->cString[i] = t;
		}

		return;
	}

	unicodeLen = [self length];
	unicodeString = [self allocMemoryWithSize: sizeof(of_unichar_t)
					    count: unicodeLen];

	i = j = 0;
	newCStringLength = 0;

	while (i < s->cStringLength) {
		const of_unichar_t *const *table;
		size_t tableSize;
		of_unichar_t c;
		size_t cLen;

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

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

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

		switch (c) {
		case ' ':
		case '\t':
		case '\n':
		case '\r':
			isStart = YES;
			break;
		default:
			isStart = NO;
			break;
		}

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

			if (tc)
				c = tc;
		}
116
117
118
119
120
121
122


123
124
125
126
127
128
129
		[self freeMemory: unicodeString];
		@throw e;
	}

	j = 0;

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


		if ((d = of_string_unicode_to_utf8(unicodeString[i],
		    newCString + j)) == 0) {
			[self freeMemory: unicodeString];
			[self freeMemory: newCString];
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];
		}







>
>







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
		[self freeMemory: unicodeString];
		@throw e;
	}

	j = 0;

	for (i = 0; i < unicodeLen; i++) {
		size_t d;

		if ((d = of_string_unicode_to_utf8(unicodeString[i],
		    newCString + j)) == 0) {
			[self freeMemory: unicodeString];
			[self freeMemory: newCString];
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];
		}

Modified src/OFString.h from [56e5646eb4] to [1edda5da97].

688
689
690
691
692
693
694











695
696
697
698
699
700
701
/**
 * \brief Returns the string in lowercase.
 *
 * \return The string in lowercase
 */
- (OFString*)lowercaseString;












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








>
>
>
>
>
>
>
>
>
>
>







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
/**
 * \brief Returns the string in lowercase.
 *
 * \return The string in lowercase
 */
- (OFString*)lowercaseString;

/**
 * \brief Returns the string capitalized.
 *
 * \note This only considers spaces, tab and newlines to be word delimiters!
 *	 Also note that this might change in the future to all word delimiters
 *	 specified by Unicode!
 *
 * \return The capitalized string
 */
- (OFString*)capitalizedString;

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

Modified src/OFString.m from [7e10de457a] to [b81efdf820].

1474
1475
1476
1477
1478
1479
1480











1481
1482
1483
1484
1485
1486
1487
}

- (OFString*)lowercaseString
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new lowercase];












	[new makeImmutable];

	return new;
}

- (OFString*)stringByDeletingLeadingWhitespaces







>
>
>
>
>
>
>
>
>
>
>







1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
}

- (OFString*)lowercaseString
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new lowercase];

	[new makeImmutable];

	return new;
}

- (OFString*)capitalizedString
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new capitalize];

	[new makeImmutable];

	return new;
}

- (OFString*)stringByDeletingLeadingWhitespaces