Skip to content

Commit 4fae9ab

Browse files
author
Patrick Thomson
committed
Removed dependencies on isKindOfClass:.
1 parent cabb816 commit 4fae9ab

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/YKEmitter.m

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ - (id)init
1919
yaml_emitter_initialize(&emitter);
2020

2121
buffer = [NSMutableData data];
22-
// I am overjoyed that this works.
2322
// Coincidentally, the order of arguments to CFDataAppendBytes are just right
2423
// such that if I pass the buffer as the data parameter, I can just use
2524
// a pointer to CFDataAppendBytes to tell the emitter to write to the NSMutableData.
@@ -34,10 +33,10 @@ - (void)emitItem:(id)item
3433
// Create and initialize a document to hold this.
3534
yaml_document_t document;
3635
memset(&document, 0, sizeof(document));
37-
// The double usage of !usesExplicitDelimiters is to indicate that both the start and the end of the
38-
// document should be delimited, if at all.
36+
// The double usage of !usesExplicitDelimiters corresponds to explicitly
37+
// delimiting the start and end of various documents.
3938
yaml_document_initialize(&document, NULL, NULL, NULL, !usesExplicitDelimiters, !usesExplicitDelimiters);
40-
39+
// TODO: Make this into a proper private method.
4140
[self _writeItem:item toDocument:&document];
4241
yaml_emitter_dump(&emitter, &document);
4342
yaml_document_delete(&document);
@@ -46,23 +45,27 @@ - (void)emitItem:(id)item
4645
- (int)_writeItem:(id)item toDocument:(yaml_document_t *)doc;
4746
{
4847
int nodeID = 0;
49-
if([item isKindOfClass:[NSArray class]] || [item isKindOfClass:[NSSet class]]) {
50-
// emit beginning sequence
51-
nodeID = yaml_document_add_sequence(doc, (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG, YAML_ANY_SEQUENCE_STYLE);
52-
for(id subitem in item) {
53-
int newItem = [self _writeItem:subitem toDocument:doc];
54-
yaml_document_append_sequence_item(doc, nodeID, newItem);
55-
}
56-
} else if([item isKindOfClass:[NSDictionary class]]) {
57-
// emit beginning mapping
48+
// #keyEnumerator covers NSMapTable/NSHashTable/NSDictionary
49+
if([item respondsToSelector:@selector(keyEnumerator)]) {
50+
// Add a mapping node.
5851
nodeID = yaml_document_add_mapping(doc, (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG, YAML_ANY_MAPPING_STYLE);
5952
for(id key in item) {
6053
int keyID = [self _writeItem:key toDocument:doc];
6154
int valueID = [self _writeItem:[item objectForKey:key] toDocument:doc];
6255
yaml_document_append_mapping_pair(doc, nodeID, keyID, valueID);
6356
}
57+
// #objectEnumerator covers NSSet/NSArray.
58+
} else if([item respondsToSelector:@selector(objectEnumerator)]) {
59+
// emit beginning sequence
60+
nodeID = yaml_document_add_sequence(doc, (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG, YAML_ANY_SEQUENCE_STYLE);
61+
for(id subitem in item) {
62+
int newItem = [self _writeItem:subitem toDocument:doc];
63+
yaml_document_append_sequence_item(doc, nodeID, newItem);
64+
}
65+
// Everything else is a scalar.
6466
} else {
6567
// TODO: Add optional support for tagging emitted items.
68+
// TODO: Wrap long lines.
6669
nodeID = yaml_document_add_scalar(doc, (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)[[item description] UTF8String], strlen([[item description] UTF8String]), YAML_ANY_SCALAR_STYLE);
6770
}
6871
return nodeID;

0 commit comments

Comments
 (0)