ObjFW  Check-in [ef02147e8b]

Overview
Comment:Better format for serialization of floating point OFNumbers.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ef02147e8b054d52881102f34d28fb381373476a115bd42f1aa85bbc8e997679
User & Date: js on 2011-09-17 18:58:01
Other Links: manifest | tags
Context
2011-09-17
19:18
Make hashes independant of endianess in OF{Float,Double}{Vector,Matrix}. check-in: f3db074478 user: js tags: trunk
18:58
Better format for serialization of floating point OFNumbers. check-in: ef02147e8b user: js tags: trunk
15:20
Change OFDate to use and export doubles. check-in: 4bef853a72 user: js tags: trunk
Changes

Modified src/OFNumber.m from [d6b1e87e08] to [687e9eb918].

741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
			 */
			type = OF_NUMBER_UINTMAX;
			value.uintmax = [element decimalValue];
		} else if ([typeString isEqual: @"signed"]) {
			type = OF_NUMBER_INTMAX;
			value.intmax = [element decimalValue];
		} else if ([typeString isEqual: @"float"]) {
			union {
				float f;
				uint32_t i;
			} f;

			type = OF_NUMBER_FLOAT;
			f.i = (uint32_t)[[element stringValue]
			    hexadecimalValue];
			value.float_ = f.f;
		} else if ([typeString isEqual: @"double"]) {
			union {
				double d;
				uint64_t i;
			} d;

			type = OF_NUMBER_DOUBLE;
			d.i = (uint64_t)[[element stringValue]
			    hexadecimalValue];
			value.double_ = d.d;
		} else
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

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







<
<
<
<
<

<
<
|

<
<
<
<
<

<
<
|







741
742
743
744
745
746
747





748


749
750





751


752
753
754
755
756
757
758
759
			 */
			type = OF_NUMBER_UINTMAX;
			value.uintmax = [element decimalValue];
		} else if ([typeString isEqual: @"signed"]) {
			type = OF_NUMBER_INTMAX;
			value.intmax = [element decimalValue];
		} else if ([typeString isEqual: @"float"]) {





			type = OF_NUMBER_FLOAT;


			value.float_ = [element floatValue];
		} else if ([typeString isEqual: @"double"]) {





			type = OF_NUMBER_DOUBLE;


			value.double_ = [element doubleValue];
		} else
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		[pool release];
	} @catch (id e) {
		[self release];
954
955
956
957
958
959
960

961
962
963
964
965
966
967
968
969
970
971


972

973
974
975
976
977
978
979
980
981
982
983
984


985

986
987
988
989
990
991
992
		return NO;
	}
}

- (uint32_t)hash
{
	uint32_t hash;


	switch (type) {
	case OF_NUMBER_FLOAT:;
		union {
			float f;
			uint32_t i;
		} f;

		f.f = of_bswap_float_if_le(value.float_);

		OF_HASH_INIT(hash);


		OF_HASH_ADD_INT32(hash, f.i);

		OF_HASH_FINALIZE(hash);

		return hash;
	case OF_NUMBER_DOUBLE:;
		union {
			double d;
			uint64_t i;
		} d;

		d.d = of_bswap_double_if_le(value.double_);

		OF_HASH_INIT(hash);


		OF_HASH_ADD_INT64(hash, d.i);

		OF_HASH_FINALIZE(hash);

		return hash;
	default:
		return [self uInt32Value];
	}
}







>





|





>
>
|
>






|





>
>
|
>







940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
		return NO;
	}
}

- (uint32_t)hash
{
	uint32_t hash;
	uint8_t i;

	switch (type) {
	case OF_NUMBER_FLOAT:;
		union {
			float f;
			uint8_t b[sizeof(float)];
		} f;

		f.f = of_bswap_float_if_le(value.float_);

		OF_HASH_INIT(hash);

		for (i = 0; i < sizeof(float); i++)
			OF_HASH_ADD(hash, f.b[i]);

		OF_HASH_FINALIZE(hash);

		return hash;
	case OF_NUMBER_DOUBLE:;
		union {
			double d;
			uint8_t b[sizeof(double)];
		} d;

		d.d = of_bswap_double_if_le(value.double_);

		OF_HASH_INIT(hash);

		for (i = 0; i < sizeof(double); i++)
			OF_HASH_ADD(hash, d.b[i]);

		OF_HASH_FINALIZE(hash);

		return hash;
	default:
		return [self uInt32Value];
	}
}
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
	case OF_NUMBER_INTMAX:
	case OF_NUMBER_PTRDIFF:
	case OF_NUMBER_INTPTR:;
		[element addAttributeWithName: @"type"
				  stringValue: @"signed"];
		break;
	case OF_NUMBER_FLOAT:;
		union {
			float f;
			uint32_t i;
		} f;

		f.f = value.float_;

		[element addAttributeWithName: @"type"
				  stringValue: @"float"];
		[element setStringValue:
		    [OFString stringWithFormat: @"%" PRIX32, f.i]];

		break;
	case OF_NUMBER_DOUBLE:;
		union {
			double d;
			uint64_t i;
		} d;

		d.d = value.double_;

		[element addAttributeWithName: @"type"
				  stringValue: @"double"];
		[element setStringValue:
		    [OFString stringWithFormat: @"%" PRIX64, d.i]];

		break;
	default:
		@throw [OFInvalidFormatException newWithClass: isa];
	}

	[element retain];
	[pool release];
	[element autorelease];

	return element;
}
@end







<
<
<
<
<
<
<



|



<
<
<
<
<
<
<



|













1206
1207
1208
1209
1210
1211
1212







1213
1214
1215
1216
1217
1218
1219







1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
	case OF_NUMBER_INTMAX:
	case OF_NUMBER_PTRDIFF:
	case OF_NUMBER_INTPTR:;
		[element addAttributeWithName: @"type"
				  stringValue: @"signed"];
		break;
	case OF_NUMBER_FLOAT:;







		[element addAttributeWithName: @"type"
				  stringValue: @"float"];
		[element setStringValue:
		    [OFString stringWithFormat: @"%a", value.float_]];

		break;
	case OF_NUMBER_DOUBLE:;







		[element addAttributeWithName: @"type"
				  stringValue: @"double"];
		[element setStringValue:
		    [OFString stringWithFormat: @"%la", value.double_]];

		break;
	default:
		@throw [OFInvalidFormatException newWithClass: isa];
	}

	[element retain];
	[pool release];
	[element autorelease];

	return element;
}
@end

Modified tests/OFSerializationTests.m from [d1ee4a5580] to [7281d1f793].

42
43
44
45
46
47
48

49
50
51
52
53
54
55
	OFMutableArray *a = [OFMutableArray array];
	OFList *l = [OFList list];
	OFDataArray *da = [OFDataArray dataArray];
	OFString *s;

	[a addObject: @"Qu\"xbar\ntest"];
	[a addObject: [OFNumber numberWithInt: 1234]];

	[a addObject: [OFMutableString stringWithString: @"asd"]];
	[a addObject: [OFDate dateWithTimeIntervalSince1970: 1234.5678]];

	[d setObject: @"Hello"
	      forKey: a];
	[d setObject: @"B\"la"
	      forKey: @"Blub"];







>







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
	OFMutableArray *a = [OFMutableArray array];
	OFList *l = [OFList list];
	OFDataArray *da = [OFDataArray dataArray];
	OFString *s;

	[a addObject: @"Qu\"xbar\ntest"];
	[a addObject: [OFNumber numberWithInt: 1234]];
	[a addObject: [OFNumber numberWithDouble: 1234.5678]];
	[a addObject: [OFMutableString stringWithString: @"asd"]];
	[a addObject: [OFDate dateWithTimeIntervalSince1970: 1234.5678]];

	[d setObject: @"Hello"
	      forKey: a];
	[d setObject: @"B\"la"
	      forKey: @"Blub"];

Modified tests/serialization.xml from [dcac3fafcc] to [dbe29a5558].

1
2
3

























4
5
6
7
8
9
10
<?xml version='1.0' encoding='UTF-8'?>
<serialization xmlns='https://webkeks.org/objfw/serialization' version='0'>
  <OFMutableDictionary>

























    <key>
      <OFList>
        <OFString>Hello</OFString>
        <OFString>Wo&#xD;ld!
How are you?</OFString>
        <OFURL>https://webkeks.org/</OFURL>
        <OFXMLElement name='x'>



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







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version='1.0' encoding='UTF-8'?>
<serialization xmlns='https://webkeks.org/objfw/serialization' version='0'>
  <OFMutableDictionary>
    <key>
      <OFDataArray>MDEyMzQ1Njc4OTo7PEFCQ0RFRkdISklLTE1OT1BRUlNUVVZXWFla</OFDataArray>
    </key>
    <object>
      <OFString>data</OFString>
    </object>
    <key>
      <OFArray>
        <OFString>Qu&quot;xbar
test</OFString>
        <OFNumber type='signed'>1234</OFNumber>
        <OFNumber type='double'>0x1.34a456d5cfaadp+10</OFNumber>
        <OFMutableString>asd</OFMutableString>
        <OFDate>0x1.34a456d5cfaadp+10</OFDate>
      </OFArray>
    </key>
    <object>
      <OFString>Hello</OFString>
    </object>
    <key>
      <OFString>Blub</OFString>
    </key>
    <object>
      <OFString>B&quot;la</OFString>
    </object>
    <key>
      <OFList>
        <OFString>Hello</OFString>
        <OFString>Wo&#xD;ld!
How are you?</OFString>
        <OFURL>https://webkeks.org/</OFURL>
        <OFXMLElement name='x'>
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
          </object>
        </OFCountedSet>
      </OFList>
    </key>
    <object>
      <OFString>list</OFString>
    </object>
    <key>
      <OFDataArray>MDEyMzQ1Njc4OTo7PEFCQ0RFRkdISklLTE1OT1BRUlNUVVZXWFla</OFDataArray>
    </key>
    <object>
      <OFString>data</OFString>
    </object>
    <key>
      <OFArray>
        <OFString>Qu&quot;xbar
test</OFString>
        <OFNumber type='signed'>1234</OFNumber>
        <OFMutableString>asd</OFMutableString>
        <OFDate>0x1.34a456d5cfaadp+10</OFDate>
      </OFArray>
    </key>
    <object>
      <OFString>Hello</OFString>
    </object>
    <key>
      <OFString>Blub</OFString>
    </key>
    <object>
      <OFString>B&quot;la</OFString>
    </object>
  </OFMutableDictionary>
</serialization>







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


88
89
90
91
92
93
94
























95
96
          </object>
        </OFCountedSet>
      </OFList>
    </key>
    <object>
      <OFString>list</OFString>
    </object>
























  </OFMutableDictionary>
</serialization>