Skip to content

Commit 0a0f7d5

Browse files
authored
Merge pull request #12 from corka149/11-performance-improvements-of-diff
11 performance improvements of diff
2 parents 958549f + 4f229df commit 0a0f7d5

5 files changed

Lines changed: 28 additions & 7 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.13.1
2+
- Make Jsonpatch faster by (un)escaping conditional
3+
14
# 0.13.0
25
- Allow usage of atoms for keys via `keys` option
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The package can be installed by adding `jsonpatch` to your list of dependencies
3232
```elixir
3333
def deps do
3434
[
35-
{:jsonpatch, "~> 0.12.1"}
35+
{:jsonpatch, "~> 0.13.1"}
3636
]
3737
end
3838
```

lib/jsonpatch.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,21 @@ defmodule Jsonpatch do
216216
Map.get(source, key)
217217
end
218218

219-
# Escape `/` to `~1 and `~` to `~`.
219+
# Escape `/` to `~1 and `~` to `~0`.
220220
defp escape(subpath) when is_bitstring(subpath) do
221221
subpath
222-
|> String.replace("~", "~0")
223-
|> String.replace("/", "~1")
222+
|> do_escape("~", "~0")
223+
|> do_escape("/", "~1")
224224
end
225225

226226
defp escape(subpath) do
227227
subpath
228228
end
229+
230+
defp do_escape(subpath, pattern, replacement) do
231+
case String.contains?(subpath, pattern) do
232+
true -> String.replace(subpath, pattern, replacement)
233+
false -> subpath
234+
end
235+
end
229236
end

lib/jsonpatch/path_util.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ defmodule Jsonpatch.PathUtil do
7575
"""
7676
def unescape(fragment) when is_bitstring(fragment) do
7777
fragment
78-
|> String.replace("~1", "/")
79-
|> String.replace("~0", "~")
78+
|> do_unescape("~1", "/")
79+
|> do_unescape("~0", "~")
8080
end
8181

8282
def unescape(fragment) do
@@ -194,4 +194,15 @@ defmodule Jsonpatch.PathUtil do
194194
defp is_number?(_term) do
195195
false
196196
end
197+
198+
defp do_unescape(fragment, pattern, replacement) when is_binary(fragment) do
199+
case String.contains?(fragment, pattern) do
200+
true -> String.replace(fragment, pattern, replacement)
201+
false -> fragment
202+
end
203+
end
204+
205+
defp do_unescape(fragment, _pattern, _replacement) do
206+
fragment
207+
end
197208
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Jsonpatch.MixProject do
66
app: :jsonpatch,
77
name: "Jsonpatch",
88
description: "Implementation of RFC 6902 in pure Elixir",
9-
version: "0.13.0",
9+
version: "0.13.1",
1010
elixir: "~> 1.10",
1111
start_permanent: Mix.env() == :prod,
1212
deps: deps(),

0 commit comments

Comments
 (0)