ObjFW  Check-in [e5b31cf72f]

Overview
Comment:Rename -[appendUTF8String:withLength:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e5b31cf72fa6428b4458cab36dd2814eb4d628576f26bde14a082d056447a12f
User & Date: js on 2012-10-13 18:51:00
Other Links: manifest | tags
Context
2012-10-13
19:30
of_endianess_t -> of_byte_order_t. check-in: 255fd75828 user: js tags: trunk
18:51
Rename -[appendUTF8String:withLength:]. check-in: e5b31cf72f user: js tags: trunk
2012-10-12
17:59
Add __attribute__((sentinel)). check-in: c1b2e3e604 user: js tags: trunk
Changes

Modified src/OFMutableString.h from [7a52b50b3a] to [86f17a9e49].

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
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







-
+








-
+










-
+







 * \brief Appends a UTF-8 encoded C string with the specified length to the
 *	  OFMutableString.
 *
 * \param UTF8String A UTF-8 encoded C string to append
 * \param UTF8StringLength The length of the UTF-8 encoded C string
 */
- (void)appendUTF8String: (const char*)UTF8String
	      withLength: (size_t)UTF8StringLength;
		  length: (size_t)UTF8StringLength;

/**
 * \brief Appends a C string with the specified encoding to the OFMutableString.
 *
 * \param cString A C string to append
 * \param encoding The encoding of the C string
 */
- (void)appendCString: (const char*)cString
	 withEncoding: (of_string_encoding_t)encoding;
	     encoding: (of_string_encoding_t)encoding;

/**
 * \brief Appends a C string with the specified encoding and length to the
 *	  OFMutableString.
 *
 * \param cString A C string to append
 * \param encoding The encoding of the C string
 * \param cStringLength The length of the UTF-8 encoded C string
 */
- (void)appendCString: (const char*)cString
	 withEncoding: (of_string_encoding_t)encoding
	     encoding: (of_string_encoding_t)encoding
	       length: (size_t)cStringLength;

/**
 * \brief Appends another OFString to the OFMutableString.
 *
 * \param string An OFString to append
 */
90
91
92
93
94
95
96
97

98
99
100
101
102
103
104
90
91
92
93
94
95
96

97
98
99
100
101
102
103
104







-
+







 * See printf for the format syntax. As an addition, %@ is available as format
 * specifier for objects.
 *
 * \param format A format string which generates the string to append
 * \param arguments The arguments used in the format string
 */
- (void)appendFormat: (OFConstantString*)format
       withArguments: (va_list)arguments;
	   arguments: (va_list)arguments;

/**
 * \brief Prepends another OFString to the OFMutableString.
 *
 * \param string An OFString to prepend
 */
- (void)prependString: (OFString*)string;

Modified src/OFMutableString.m from [869c2383b0] to [7230c7c71f].

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
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







-
+










-
+










-
+








	[self appendString: [OFString stringWithUTF8String: UTF8String]];

	objc_autoreleasePoolPop(pool);
}

- (void)appendUTF8String: (const char*)UTF8String
	      withLength: (size_t)UTF8StringLength
		  length: (size_t)UTF8StringLength
{
	void *pool = objc_autoreleasePoolPush();

	[self appendString: [OFString stringWithUTF8String: UTF8String
						    length: UTF8StringLength]];

	objc_autoreleasePoolPop(pool);
}

- (void)appendCString: (const char*)cString
	 withEncoding: (of_string_encoding_t)encoding
	     encoding: (of_string_encoding_t)encoding
{
	void *pool = objc_autoreleasePoolPush();

	[self appendString: [OFString stringWithCString: cString
					       encoding: encoding]];

	objc_autoreleasePoolPop(pool);
}

- (void)appendCString: (const char*)cString
	 withEncoding: (of_string_encoding_t)encoding
	     encoding: (of_string_encoding_t)encoding
	       length: (size_t)cStringLength
{
	void *pool = objc_autoreleasePoolPush();

	[self appendString: [OFString stringWithCString: cString
					       encoding: encoding
						 length: cStringLength]];
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
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







-
+




-
+
















-
+








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

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

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

	if (format == nil)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	if ((UTF8StringLength = of_vasprintf(&UTF8String, [format UTF8String],
	    arguments)) == -1)
		@throw [OFInvalidFormatException
		    exceptionWithClass: [self class]];

	@try {
		[self appendUTF8String: UTF8String
			    withLength: UTF8StringLength];
				length: UTF8StringLength];
	} @finally {
		free(UTF8String);
	}
}

- (void)prependString: (OFString*)string
{

Modified src/OFString+XMLUnescaping.m from [c2d8e053db] to [a0597bf098].

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
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







-
+









-
+



-
+




-
+




-
+



-
+








	last = 0;
	inEntity = NO;

	for (i = 0; i < length; i++) {
		if (!inEntity && string[i] == '&') {
			[ret appendUTF8String: string + last
				   withLength: i - 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 appendCString: "<"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 2 && !memcmp(entity, "gt", 2))
				[ret appendCString: ">"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 4 &&
			    !memcmp(entity, "quot", 4))
				[ret appendCString: "\""
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 4 &&
			    !memcmp(entity, "apos", 4))
				[ret appendCString: "'"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 3 && !memcmp(entity, "amp", 3))
				[ret appendCString: "&"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entity[0] == '#') {
				void *pool;
				OFString *tmp;

				pool = objc_autoreleasePoolPush();
				tmp = parse_numeric_entity(entity,
172
173
174
175
176
177
178
179

180
181
182
183
184
185
186
172
173
174
175
176
177
178

179
180
181
182
183
184
185
186







-
+







	}

	if (inEntity)
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];

	[ret appendUTF8String: string + last
		   withLength: i - last];
		       length: i - last];

	[ret makeImmutable];

	return ret;
}

#ifdef OF_HAVE_BLOCKS
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
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







-
+









-
+



-
+




-
+




-
+



-
+








	last = 0;
	inEntity = NO;

	for (i = 0; i < length; i++) {
		if (!inEntity && string[i] == '&') {
			[ret appendUTF8String: string + last
				   withLength: i - 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 appendCString: "<"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 2 && !memcmp(entity, "gt", 2))
				[ret appendCString: ">"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 4 &&
			    !memcmp(entity, "quot", 4))
				[ret appendCString: "\""
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 4 &&
			    !memcmp(entity, "apos", 4))
				[ret appendCString: "'"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entityLength == 3 && !memcmp(entity, "amp", 3))
				[ret appendCString: "&"
				      withEncoding: OF_STRING_ENCODING_ASCII
					  encoding: OF_STRING_ENCODING_ASCII
					    length: 1];
			else if (entity[0] == '#') {
				void *pool;
				OFString *tmp;

				pool = objc_autoreleasePoolPush();
				tmp = parse_numeric_entity(entity,
272
273
274
275
276
277
278
279

280
281
282
283
284
285
286
272
273
274
275
276
277
278

279
280
281
282
283
284
285
286







-
+







	}

	if (inEntity)
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];

	[ret appendUTF8String: string + last
		   withLength: i - last];
		       length: i - last];

	[ret makeImmutable];

	return ret;
}
#endif
@end

Modified src/base64.m from [8d7fe753db] to [9c1fb17538].

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
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







-
+










-
+












-
+








		tb[0] = of_base64_encode_table[(sb & 0xFC0000) >> 18];
		tb[1] = of_base64_encode_table[(sb & 0x03F000) >> 12];
		tb[2] = of_base64_encode_table[(sb & 0x000FC0) >> 6];
		tb[3] = of_base64_encode_table[sb & 0x00003F];

		[ret appendCString: tb
		      withEncoding: OF_STRING_ENCODING_ASCII
			  encoding: OF_STRING_ENCODING_ASCII
			    length: 4];
	}

	switch (rest) {
	case 1:
		tb[0] = of_base64_encode_table[buffer[i] >> 2];
		tb[1] = of_base64_encode_table[(buffer[i] & 3) << 4];
		tb[2] = tb[3] = '=';

		[ret appendCString: tb
		      withEncoding: OF_STRING_ENCODING_ASCII
			  encoding: OF_STRING_ENCODING_ASCII
			    length: 4];

		break;
	case 2:
		sb = (buffer[i] << 16) | (buffer[i + 1] << 8);

		tb[0] = of_base64_encode_table[(sb & 0xFC0000) >> 18];
		tb[1] = of_base64_encode_table[(sb & 0x03F000) >> 12];
		tb[2] = of_base64_encode_table[(sb & 0x000FC0) >> 6];
		tb[3] = '=';

		[ret appendCString: tb
		      withEncoding: OF_STRING_ENCODING_ASCII
			  encoding: OF_STRING_ENCODING_ASCII
			    length: 4];

		break;
	}

	[ret makeImmutable];

Modified tests/OFStringTests.m from [ea0a5564ab] to [129c465da1].

176
177
178
179
180
181
182
183

184
185

186
187
188
189
190
191
192
176
177
178
179
180
181
182

183
184

185
186
187
188
189
190
191
192







-
+

-
+








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

	TEST(@"-[appendUTFString:withLength:]",
	TEST(@"-[appendUTFString:length:]",
	    R([s[0] appendUTF8String: "foo\xEF\xBB\xBF" "barqux" + 3
			  withLength: 6]) && [s[0] isEqual: @"foobar"])
			      length: 6]) && [s[0] isEqual: @"foobar"])

	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1",
	    OFInvalidEncodingException,
	    [OFString stringWithUTF8String: "\xE0\x80"])
	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #2",
	    OFInvalidEncodingException,
	    [OFString stringWithUTF8String: "\xF0\x80\x80\xC0"])