ObjFW  Check-in [0dafdc4730]

Overview
Comment:One malloc less for creating an immutable UTF-8 string.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0dafdc4730b8620694351120731fb8abc20fa961cd89daa7f4ff441014d00ee9
User & Date: js on 2012-03-17 15:48:08
Other Links: manifest | tags
Context
2012-03-17
21:36
Update buildsys. check-in: cc91153554 user: js tags: trunk
15:48
One malloc less for creating an immutable UTF-8 string. check-in: 0dafdc4730 user: js tags: trunk
15:42
Update to Unicode 6.2. check-in: 1ef92da32f user: js tags: trunk
Changes

Modified src/OFString.m from [f2bf50723c] to [bcea8a4ed5].

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
- init
{
	return (id)[[OFString_UTF8 alloc] init];
}

- initWithUTF8String: (const char*)UTF8String
{








	return (id)[[OFString_UTF8 alloc] initWithUTF8String: UTF8String];


}

- initWithUTF8String: (const char*)UTF8String
	      length: (size_t)UTF8StringLength
{






	return (id)[[OFString_UTF8 alloc] initWithUTF8String: UTF8String
						      length: UTF8StringLength];

}

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














	return (id)[[OFString_UTF8 alloc] initWithCString: cString
						 encoding: encoding];
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)cStringLength
{












	return (id)[[OFString_UTF8 alloc] initWithCString: cString
						 encoding: encoding
						   length: cStringLength];
}

- initWithString: (OFString*)string
{







>
>
>
>
>
>
>
>
|
>
>





>
>
>
>
>
>
|
|
>





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








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







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
- init
{
	return (id)[[OFString_UTF8 alloc] init];
}

- initWithUTF8String: (const char*)UTF8String
{
	id string;
	size_t length;
	void *storage;

	length = strlen(UTF8String);
	string = of_alloc_object([OFString_UTF8 class],
	    length + 1, 1, &storage);

	return (id)[string _initWithUTF8String: UTF8String
					length: length
				       storage: storage];
}

- initWithUTF8String: (const char*)UTF8String
	      length: (size_t)UTF8StringLength
{
	id string;
	void *storage;

	string = of_alloc_object([OFString_UTF8 class],
	    UTF8StringLength + 1, 1, &storage);

	return (id)[string _initWithUTF8String: UTF8String
					length: UTF8StringLength
				       storage: storage];
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
{
	if (encoding == OF_STRING_ENCODING_UTF_8) {
		id string;
		size_t length;
		void *storage;

		length = strlen(cString);
		string = of_alloc_object([OFString_UTF8 class],
		    length + 1, 1, &storage);

		return (id)[string _initWithUTF8String: cString
						length: length
					       storage: storage];
	}

	return (id)[[OFString_UTF8 alloc] initWithCString: cString
						 encoding: encoding];
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)cStringLength
{
	if (encoding == OF_STRING_ENCODING_UTF_8) {
		id string;
		void *storage;

		string = of_alloc_object([OFString_UTF8 class],
		    cStringLength + 1, 1, &storage);

		return (id)[string _initWithUTF8String: cString
						length: cStringLength
					       storage: storage];
	}

	return (id)[[OFString_UTF8 alloc] initWithCString: cString
						 encoding: encoding
						   length: cStringLength];
}

- initWithString: (OFString*)string
{

Modified src/OFString_UTF8.h from [b1a5c898cd] to [f6988d5f2e].

32
33
34
35
36
37
38




39
		BOOL	 UTF8;
		size_t	 length;
		BOOL	 hashed;
		uint32_t hash;
	} *restrict s;
	struct of_string_utf8_ivars s_store;
}




@end







>
>
>
>

32
33
34
35
36
37
38
39
40
41
42
43
		BOOL	 UTF8;
		size_t	 length;
		BOOL	 hashed;
		uint32_t hash;
	} *restrict s;
	struct of_string_utf8_ivars s_store;
}

- _initWithUTF8String: (const char*)UTF8String
	       length: (size_t)UTF8StringLength
	      storage: (char*)storage;
@end

Modified src/OFString_UTF8.m from [2441b116ae] to [03beefc292].

68
69
70
71
72
73
74






































75
76
77
78
79
80
81

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







































	return self;
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)cStringLength







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







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

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

	return self;
}

- _initWithUTF8String: (const char*)UTF8String
	       length: (size_t)UTF8StringLength
	      storage: (char*)storage
{
	self = [super init];

	@try {
		if (UTF8StringLength >= 3 &&
		    !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
			UTF8String += 3;
			UTF8StringLength -= 3;
		}

		s = &s_store;

		s->cString = storage;
		s->cStringLength = UTF8StringLength;

		switch (of_string_check_utf8(UTF8String, UTF8StringLength,
		    &s->length)) {
		case 1:
			s->UTF8 = YES;
			break;
		case -1:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];
		}

		memcpy(s->cString, UTF8String, UTF8StringLength);
		s->cString[UTF8StringLength] = 0;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)cStringLength