Skip to content

Commit

Permalink
Compare keywords like Clojure
Browse files Browse the repository at this point in the history
Compare by ns then name.
  • Loading branch information
scramjet committed Jul 10, 2020
1 parent f41d8b2 commit f318e5a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions MPEdn/MPEdnKeyword.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
*/
- (NSString *) stringValue;

- (NSComparisonResult) compare: (MPEdnKeyword *) object;

@end

@interface NSString (MPEdn)
Expand Down
16 changes: 14 additions & 2 deletions MPEdn/MPEdnKeyword.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@implementation MPEdnKeyword
{
NSString *name;
NSString *namespace;
}

+ (void) initialize
Expand Down Expand Up @@ -82,8 +83,14 @@ - (instancetype) initWithName: (NSString *) initName
if (self = [super init])
{
name = initName;
CFIndex nsIndex = [name rangeOfString: @"/"].location;

if (nsIndex == NSNotFound)
namespace = @"";
else
namespace = [name substringToIndex: nsIndex];
}

return self;
}

Expand All @@ -109,7 +116,12 @@ - (BOOL) isEqual: (id) object

- (NSComparisonResult) compare: (MPEdnKeyword *) object
{
return [name compare: [object ednName]];
NSComparisonResult nsCompare = [self->namespace compare: object->namespace];

if (nsCompare == 0)
return [self->name compare: object->name];
else
return nsCompare;
}

- (NSString *) description
Expand Down
4 changes: 4 additions & 0 deletions MPEdnTests/MPEdnParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ - (void) testKeywords

// keywords as strings
XCTAssertEqualObjects ([@":b" ednStringToObjectNoKeywords], @"b", @"Equal keyword");

// test comparison uses namespace like Clojure does (this test doesn't really belong here...)
XCTAssert ([[@"z" ednKeyword] compare: [@"a/a" ednKeyword]] < 0, @"Compare using ns");
XCTAssert ([[@"a/z" ednKeyword] compare: [@"z/a" ednKeyword]] < 0, @"Compare using ns");
}

- (void) testSets
Expand Down

0 comments on commit f318e5a

Please sign in to comment.