Skip to content

Commit

Permalink
Merge pull request #195 from evanphx/b-string-compare-unicode
Browse files Browse the repository at this point in the history
Compare strings after decoding them to handle unicode correctly. Fixes #172
  • Loading branch information
evanphx authored Jan 12, 2024
2 parents f72a464 + a002ca6 commit c32ee8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 21 additions & 1 deletion v5/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,27 @@ func (n *lazyNode) equal(o *lazyNode) bool {
return false
}

return bytes.Equal(n.compact(), o.compact())
nc := n.compact()
oc := o.compact()

if nc[0] == '"' && oc[0] == '"' {
// ok, 2 strings

var ns, os string

err := json.Unmarshal(nc, &ns)
if err != nil {
return false
}
err = json.Unmarshal(oc, &os)
if err != nil {
return false
}

return ns == os
}

return bytes.Equal(nc, oc)
}
}

Expand Down
6 changes: 6 additions & 0 deletions v5/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,12 @@ var EqualityCases = []EqualityCase{
`null`,
false,
},
{
"Unicode",
`{"name": "λJohn"}`,
`{"name": "\u03BBJohn"}`,
true,
},
}

func TestEquality(t *testing.T) {
Expand Down

0 comments on commit c32ee8f

Please sign in to comment.