ObjFW  Check-in [3659c87c63]

Overview
Comment:Add -[OFString initWithUTF8String:freeWhenDone:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3659c87c63a4e236cc17df6c32097f44a56080f2c18dd421778b28612b0cfef0
User & Date: js on 2012-09-06 19:13:50
Original User & Date: js on 2012-09-06 19:13:51
Other Links: manifest | tags
Context
2012-09-06
19:13
Fix a forgotten rename. check-in: 5216366469 user: js tags: trunk
19:13
Add -[OFString initWithUTF8String:freeWhenDone:]. check-in: 3659c87c63 user: js tags: trunk
19:13
Makefile: Nicer way to generate a tarball. check-in: fc37b75c50 user: js tags: trunk
Changes

Modified src/OFApplication.m from [77883af432] to [93776e4552].

164
165
166
167
168
169
170
171



172
173
174

175



176
177
178

179



180
181
182

183



184
185
186

187



188
189
190

191
192
193
194
195
196
197
		 * iOS does not provide environ and Apple does not allow using
		 * _NSGetEnviron on iOS. Therefore, we just get a few common
		 * variables from the environment which applications might
		 * expect.
		 */
		pool = objc_autoreleasePoolPush();

		if ((env = getenv("HOME")) != NULL)



			[environment
			    setObject: [OFString stringWithUTF8String: env]
			       forKey: @"HOME"];

		if ((env = getenv("PATH")) != NULL)



			[environment
			    setObject: [OFString stringWithUTF8String: env]
			       forKey: @"PATH"];

		if ((env = getenv("SHELL")) != NULL)



			[environment
			    setObject: [OFString stringWithUTF8String: env]
			       forKey: @"SHELL"];

		if ((env = getenv("TMPDIR")) != NULL)



			[environment
			    setObject: [OFString stringWithUTF8String: env]
			       forKey: @"TMPDIR"];

		if ((env = getenv("USER")) != NULL)



			[environment
			    setObject: [OFString stringWithUTF8String: env]
			       forKey: @"USER"];


		objc_autoreleasePoolPop(pool);
#endif

		[environment makeImmutable];
	} @catch (id e) {
		[self release];







|
>
>
>
|
<
|
>
|
>
>
>
|
<
|
>
|
>
>
>
|
<
|
>
|
>
>
>
|
<
|
>
|
>
>
>
|
<
|
>







164
165
166
167
168
169
170
171
172
173
174
175

176
177
178
179
180
181
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
		 * iOS does not provide environ and Apple does not allow using
		 * _NSGetEnviron on iOS. Therefore, we just get a few common
		 * variables from the environment which applications might
		 * expect.
		 */
		pool = objc_autoreleasePoolPush();

		if ((env = getenv("HOME")) != NULL) {
			OFString *home = [[[OFString alloc]
			    initWithUTF8StringNoCopy: env
					freeWhenDone: NO] autorelease];
			[environment setObject: home

					forKey: @"HOME"];
		}
		if ((env = getenv("PATH")) != NULL) {
			OFString *path = [[[OFString alloc]
			    initWithUTF8StringNoCopy: env
					freeWhenDone: NO] autorelease];
			[environment setObject: path

					forKey: @"PATH"];
		}
		if ((env = getenv("SHELL")) != NULL) {
			OFString *shell = [[[OFString alloc]
			    initWithUTF8StringNoCopy: env
					freeWhenDone: NO] autorelease];
			[environment setObject: shell

					forKey: @"SHELL"];
		}
		if ((env = getenv("TMPDIR")) != NULL) {
			OFString *tmpdir = [[[OFString alloc]
			    initWithUTF8StringNoCopy: env
					freeWhenDone: NO] autorelease];
			[environment setObject: tmpdir

					forKey: @"TMPDIR"];
		}
		if ((env = getenv("USER")) != NULL) {
			OFString *user = [[[OFString alloc]
			    initWithUTF8StringNoCopy: env
					freeWhenDone: NO] autorelease];
			[environment setObject: user

					forKey: @"USER"];
		}

		objc_autoreleasePoolPop(pool);
#endif

		[environment makeImmutable];
	} @catch (id e) {
		[self release];

Modified src/OFConstantString.m from [634c81749f] to [e519860738].

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#endif
}

- (void)finishInitialization
{
	struct of_string_utf8_ivars *ivars;

	if ((ivars = malloc(sizeof(*ivars))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: [self class]
			 requestedSize: sizeof(*ivars)];
	memset(ivars, 0, sizeof(*ivars));

	ivars->cString = cString;
	ivars->cStringLength = cStringLength;

	switch (of_string_check_utf8(ivars->cString, ivars->cStringLength,
	    &ivars->length)) {
	case 1:







|



<







143
144
145
146
147
148
149
150
151
152
153

154
155
156
157
158
159
160
#endif
}

- (void)finishInitialization
{
	struct of_string_utf8_ivars *ivars;

	if ((ivars = calloc(1, sizeof(*ivars))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: [self class]
			 requestedSize: sizeof(*ivars)];


	ivars->cString = cString;
	ivars->cStringLength = cStringLength;

	switch (of_string_check_utf8(ivars->cString, ivars->cStringLength,
	    &ivars->length)) {
	case 1:

Modified src/OFMutableString_UTF8.m from [9ad6fdf9a8] to [4461cd0c4e].

39
40
41
42
43
44
45






46
47
48
49
50
51
52

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







>
>
>
>
>
>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

@implementation OFMutableString_UTF8
+ (void)initialize
{
	if (self == [OFMutableString_UTF8 class])
		[self inheritMethodsFromClass: [OFString_UTF8 class]];
}

- initWithUTF8StringNoCopy: (const char*)UTF8String
	      freeWhenDone: (BOOL)freeWhenDone
{
	return [self initWithUTF8String: UTF8String];
}

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

Modified src/OFString.h from [b5ebcdbbc4] to [6666093c89].

309
310
311
312
313
314
315














316
317
318
319
320
321
322
 * \param UTF8String A UTF-8 encoded C string to initialize the OFString with
 * \param UTF8StringLength The length of the UTF-8 encoded C string
 * \return An initialized OFString
 */
- initWithUTF8String: (const char*)UTF8String
	      length: (size_t)UTF8StringLength;















/**
 * \brief Initializes an already allocated OFString from a C string with the
 *	  specified encoding.
 *
 * \param cString A C string to initialize the OFString with
 * \param encoding The encoding of the C string
 * \return An initialized OFString







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







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
 * \param UTF8String A UTF-8 encoded C string to initialize the OFString with
 * \param UTF8StringLength The length of the UTF-8 encoded C string
 * \return An initialized OFString
 */
- initWithUTF8String: (const char*)UTF8String
	      length: (size_t)UTF8StringLength;

/**
 * \brief Initializes an already allocated OFString from an UTF-8 encoded C
 *	  string without copying it, if possible.
 *
 * \note Mutable versions always create a copy!
 *
 * \param UTF8String A UTF-8 encoded C string to initialize the OFString with
 * \param freeWhenDone Whether to free the C string when it is not needed
 *		       anymore
 * \return An initialized OFString
 */
- initWithUTF8StringNoCopy: (const char*)UTF8String
	      freeWhenDone: (BOOL)freeWhenDone;

/**
 * \brief Initializes an already allocated OFString from a C string with the
 *	  specified encoding.
 *
 * \param cString A C string to initialize the OFString with
 * \param encoding The encoding of the C string
 * \return An initialized OFString

Modified src/OFString.m from [bc6ced3df8] to [0e4634027e].

292
293
294
295
296
297
298








299
300
301
302
303
304
305
	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;







>
>
>
>
>
>
>
>







292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
	string = of_alloc_object([OFString_UTF8 class],
	    UTF8StringLength + 1, 1, &storage);

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

- initWithUTF8StringNoCopy: (const char*)UTF8String
	      freeWhenDone: (BOOL)freeWhenDone
{
	return (id)[[OFString_UTF8 alloc]
	    initWithUTF8StringNoCopy: UTF8String
			freeWhenDone: freeWhenDone];
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
{
	if (encoding == OF_STRING_ENCODING_UTF_8) {
		id string;
		size_t length;
673
674
675
676
677
678
679






680
681
682
683
684
685
686
- initWithUTF8String: (const char*)UTF8String
	      length: (size_t)UTF8StringLength
{
	return [self initWithCString: UTF8String
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: UTF8StringLength];
}







- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
{
	return [self initWithCString: cString
			    encoding: encoding
			      length: strlen(cString)];







>
>
>
>
>
>







681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
- initWithUTF8String: (const char*)UTF8String
	      length: (size_t)UTF8StringLength
{
	return [self initWithCString: UTF8String
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: UTF8StringLength];
}

- initWithUTF8StringNoCopy: (const char*)UTF8String
	      freeWhenDone: (BOOL)freeWhenDone
{
	return [self initWithUTF8String: UTF8String];
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
{
	return [self initWithCString: cString
			    encoding: encoding
			      length: strlen(cString)];

Modified src/OFString_UTF8.h from [716218f422] to [c51ed68707].

29
30
31
32
33
34
35

36
37
38
39
40
41
42
43
	struct of_string_utf8_ivars {
		char	 *cString;
		size_t	 cStringLength;
		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







>








29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	struct of_string_utf8_ivars {
		char	 *cString;
		size_t	 cStringLength;
		BOOL	 UTF8;
		size_t	 length;
		BOOL	 hashed;
		uint32_t hash;
		char	 *freeWhenDone;
	} *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 [b0a0f37ea8] to [743848703f].

240
241
242
243
244
245
246







































247
248
249
250
251
252
253
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}








































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

	@try {
		s = &s_store;







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







240
241
242
243
244
245
246
247
248
249
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
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithUTF8StringNoCopy: (const char*)UTF8String
	      freeWhenDone: (BOOL)freeWhenDone
{
	self = [super init];

	@try {
		size_t UTF8StringLength = strlen(UTF8String);

		if (freeWhenDone)
			s->freeWhenDone = (char*)UTF8String;

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

		s = &s_store;

		s->cString = (char*)UTF8String;
		s->cStringLength = UTF8StringLength;

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

	return self;
}

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

	@try {
		s = &s_store;
567
568
569
570
571
572
573








574
575
576
577
578
579
580
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}









- (const char*)UTF8String
{
	return s->cString;
}

- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding







>
>
>
>
>
>
>
>







606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	if (s != NULL && s->freeWhenDone != NULL)
		free(s->freeWhenDone);

	[super dealloc];
}

- (const char*)UTF8String
{
	return s->cString;
}

- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding