Skip to content

Commit

Permalink
Merge pull request #582 from Simperium/issue/580-emojis-crash-mark-2
Browse files Browse the repository at this point in the history
Bugfix: DiffMatchPatch Surrogate Pairs Support
  • Loading branch information
jleandroperez authored Nov 14, 2019
2 parents 0003ed4 + c402580 commit feab201
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
19 changes: 19 additions & 0 deletions External/diffmatchpatch/DiffMatchPatch.m
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,26 @@ - (NSString *)diff_text2:(NSMutableArray *)diffs;
- (NSString *)diff_toDelta:(NSMutableArray *)diffs;
{
NSMutableString *delta = [NSMutableString string];
unichar lastEnd = 0;

for (Diff *aDiff in diffs) {

unichar thisTop = [aDiff.text characterAtIndex:0];
unichar thisEnd = [aDiff.text characterAtIndex:(aDiff.text.length - 1)];

if (CFStringIsSurrogateHighCharacter(thisEnd)) {
aDiff.text = [aDiff.text substringToIndex:(aDiff.text.length - 1)];
}

if (lastEnd != 0 && CFStringIsSurrogateHighCharacter(lastEnd) && CFStringIsSurrogateLowCharacter(thisTop)) {
aDiff.text = [NSString stringWithFormat:@"%C%@", lastEnd, aDiff.text];
}

lastEnd = thisEnd;
if (0 == [aDiff.text length]) {
continue;
}

switch (aDiff.operation) {
case DIFF_INSERT:
[delta appendFormat:@"+%@\t", [[aDiff.text diff_stringByAddingPercentEscapesForEncodeUriCompatibility]
Expand Down
2 changes: 1 addition & 1 deletion Simperium/SPEnvironment.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#endif

// TODO: Update this automatically via a script that looks at current git tag
NSString* const SPLibraryVersion = @"0.8.23";
NSString* const SPLibraryVersion = @"0.8.24";

/// SSL Pinning
///
Expand Down
27 changes: 27 additions & 0 deletions SimperiumTests/DiffMatchPatchTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ - (void)testDiffCommonPrefixTest {
XCTAssertEqual((NSUInteger)4, [dmp diff_commonPrefixOfFirstString:@"1234" andSecondString:@"1234xyz"], @"Common suffix whole case failed.");
}

- (void)testDiffToDeltaDoesNotProduceNullsWhenDealingWithEmojis {
DiffMatchPatch *dmp = [DiffMatchPatch new];

NSString *pristine = @"☺️🖖🏿";
NSString *edited = @"☺️😃🖖🏿";
NSString *expected = @"=2\t+%F0%9F%98%83\t=4";

NSMutableArray *diffs = [dmp diff_mainOfOldString:pristine andNewString:edited];
NSString *delta = [dmp diff_toDelta:diffs];

XCTAssertEqualObjects(delta, expected, @"Delta should match the expected string");
}

- (void)testDiffToDeltaWithEmojisCanBeProperlyAppliedToOriginalString {
DiffMatchPatch *dmp = [DiffMatchPatch new];

NSString *pristine = @"☺️🖖🏿";
NSString *edited = @"☺️😃🖖🏿";

NSMutableArray *diffs = [dmp diff_mainOfOldString:pristine andNewString:edited];
NSArray *patches = [dmp patch_makeFromDiffs:diffs];
NSArray *result = [dmp patch_apply:patches toString:pristine];

NSString *output = [result firstObject];
XCTAssertEqualObjects(edited, output, @"Output String should match the Edited one!");
}

- (void)testDiffCommonSuffixTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];

Expand Down

0 comments on commit feab201

Please sign in to comment.