ObjFW  Check-in [bd879fd455]

Overview
Comment:Nicer formatting for serialization.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bd879fd4558b3e5cb245d949ef0910d275b758c3bc6ef8979d53b8642c3232b9
User & Date: js on 2011-05-08 20:35:09
Other Links: manifest | tags
Context
2011-05-08
20:44
Add support for serialization to OFList. check-in: 046abd074d user: js tags: trunk
20:35
Nicer formatting for serialization. check-in: bd879fd455 user: js tags: trunk
19:55
Add support for serialization.
No deserialization yet.
check-in: b27b3aa3e3 user: js tags: trunk
Changes

Modified src/OFArray.m from [d8d0e45d9b] to [31db88bd8e].

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
		else
			return @"<0>()";
	}

	cArray = [array cArray];
	count = [array count];
	if ([self isKindOfClass: [OFMutableArray class]])
		ret = [OFMutableString stringWithFormat: @"<mutable,%zd>(",
							 count];
	else
		ret = [OFMutableString stringWithFormat: @"<%zd>(", count];
	pool = [[OFAutoreleasePool alloc] init];
	append = [ret methodForSelector: @selector(appendString:)];

	for (i = 0; i < count - 1; i++) {
		append(ret, @selector(appendString:),
		    [cArray[i] stringBySerializing]);
		append(ret, @selector(appendString:), @", ");

		[pool releaseObjects];
	}


	[ret appendFormat: @"%@)", [cArray[i] stringBySerializing]];

	[pool release];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.







|


|






|



>
>
|







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
		else
			return @"<0>()";
	}

	cArray = [array cArray];
	count = [array count];
	if ([self isKindOfClass: [OFMutableArray class]])
		ret = [OFMutableString stringWithFormat: @"<mutable,%zd>(\n",
							 count];
	else
		ret = [OFMutableString stringWithFormat: @"<%zd>(\n", count];
	pool = [[OFAutoreleasePool alloc] init];
	append = [ret methodForSelector: @selector(appendString:)];

	for (i = 0; i < count - 1; i++) {
		append(ret, @selector(appendString:),
		    [cArray[i] stringBySerializing]);
		append(ret, @selector(appendString:), @",\n");

		[pool releaseObjects];
	}
	[ret replaceOccurrencesOfString: @"\n"
			     withString: @"\n\t"];
	[ret appendFormat: @"%@\n)", [cArray[i] stringBySerializing]];

	[pool release];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.

Modified src/OFDictionary.m from [7a1aa2d8ed] to [894cef5e29].

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797


798
799
800
801
802
803
804
805
		if ([self isKindOfClass: [OFMutableDictionary class]])
			return @"<mutable,0>{}";
		else
			return @"<0>{}";
	}

	if ([self isKindOfClass: [OFMutableDictionary class]])
		ret = [OFMutableString stringWithFormat: @"<mutable,%zd>{",
							 count];
	else
		ret = [OFMutableString stringWithFormat: @"<%zd>{", count];
	pool = [[OFAutoreleasePool alloc] init];
	keyEnumerator = [self keyEnumerator];
	objectEnumerator = [self objectEnumerator];

	i = 0;
	pool2 = [[OFAutoreleasePool alloc] init];

	while ((key = [keyEnumerator nextObject]) != nil &&
	    (object = [objectEnumerator nextObject]) != nil) {
		[ret appendString: [key stringBySerializing]];
		[ret appendString: @" = "];
		[ret appendString: [object stringBySerializing]];

		if (++i < count)
			[ret appendString: @"; "];

		[pool2 releaseObjects];
	}


	[ret appendString: @"}"];

	[pool release];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.







|


|














|



>
>
|







769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
		if ([self isKindOfClass: [OFMutableDictionary class]])
			return @"<mutable,0>{}";
		else
			return @"<0>{}";
	}

	if ([self isKindOfClass: [OFMutableDictionary class]])
		ret = [OFMutableString stringWithFormat: @"<mutable,%zd>{\n",
							 count];
	else
		ret = [OFMutableString stringWithFormat: @"<%zd>{\n", count];
	pool = [[OFAutoreleasePool alloc] init];
	keyEnumerator = [self keyEnumerator];
	objectEnumerator = [self objectEnumerator];

	i = 0;
	pool2 = [[OFAutoreleasePool alloc] init];

	while ((key = [keyEnumerator nextObject]) != nil &&
	    (object = [objectEnumerator nextObject]) != nil) {
		[ret appendString: [key stringBySerializing]];
		[ret appendString: @" = "];
		[ret appendString: [object stringBySerializing]];

		if (++i < count)
			[ret appendString: @";\n"];

		[pool2 releaseObjects];
	}
	[ret replaceOccurrencesOfString: @"\n"
			     withString: @"\n\t"];
	[ret appendString: @";\n}"];

	[pool release];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.

Modified src/OFString.m from [b90f5bed16] to [4009def51f].

1058
1059
1060
1061
1062
1063
1064






1065
1066
1067
1068
1069
1070
1071
- (OFString*)stringBySerializing
{
	OFMutableString *serialization = [[self mutableCopy] autorelease];
	[serialization replaceOccurrencesOfString: @"\\"
				       withString: @"\\\\"];
	[serialization replaceOccurrencesOfString: @"\""
				       withString: @"\\\""];







	if ([self isKindOfClass: [OFMutableString class]])
		[serialization prependString: @"<mutable>\""];
	else
		[serialization prependString: @"\""];
	[serialization appendString: @"\""];








>
>
>
>
>
>







1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
- (OFString*)stringBySerializing
{
	OFMutableString *serialization = [[self mutableCopy] autorelease];
	[serialization replaceOccurrencesOfString: @"\\"
				       withString: @"\\\\"];
	[serialization replaceOccurrencesOfString: @"\""
				       withString: @"\\\""];
	[serialization replaceOccurrencesOfString: @"\n"
				       withString: @"\\n\"\n    \""];
	[serialization replaceOccurrencesOfString: @"\r"
				       withString: @"\\r"];
	[serialization replaceOccurrencesOfString: @"\t"
				       withString: @"\\t"];

	if ([self isKindOfClass: [OFMutableString class]])
		[serialization prependString: @"<mutable>\""];
	else
		[serialization prependString: @"\""];
	[serialization appendString: @"\""];