Skip to content

Commit 6569821

Browse files
committed
better debugging
1 parent 7b98aec commit 6569821

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

lib/dmp/debug_utils.ex

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,33 @@ defmodule Dmp.DebugUtils do
66
use Bitwise, only_operators: true
77

88
@doc """
9-
Prints the `alphabet` bitarray on IO, showing binary values.
9+
Formats the `alphabet` bitarray into a list of lines, showing binary values.
10+
11+
## Examples
12+
13+
iex> DebugUtils.debug_alphabet("aba", %{?a => 5, ?b => 2})
14+
[
15+
" alphabet: a b a",
16+
" a 5: 1 0 1",
17+
" b 2: 0 1 0"
18+
]
19+
1020
"""
11-
@spec debug_alphabet(String.t(), Dmp.Match.alpha()) :: nil
21+
@spec debug_alphabet(String.t(), Dmp.Match.alpha()) :: [String.t()]
1222
def debug_alphabet(pattern, s) do
13-
alphabet_header(pattern) |> IO.puts()
1423
pattern_length = String.length(pattern)
1524

16-
String.codepoints(pattern)
17-
|> Enum.sort()
18-
|> Enum.dedup()
19-
|> Enum.map(fn ch -> alphabet_line(ch, s, pattern_length) |> IO.puts() end)
25+
data =
26+
String.codepoints(pattern)
27+
|> Enum.sort()
28+
|> Enum.dedup()
29+
|> Enum.map(fn ch -> alphabet_line(ch, s, pattern_length) end)
2030

21-
nil
31+
[alphabet_header(pattern) | data]
2232
end
2333

2434
defp alphabet_header(pattern) do
25-
line = ["\n alphabet:" | String.codepoints(pattern)]
35+
line = [" alphabet:" | String.codepoints(pattern)]
2636
Enum.join(line, " ")
2737
end
2838

@@ -36,11 +46,24 @@ defmodule Dmp.DebugUtils do
3646
end
3747

3848
@doc """
39-
Prints the `rd` bitarray on IO, showing binary values.
49+
Formats the `rd` bitarray into a list of lines, showing binary values.
4050
4151
* `d` - Error level for the bitarray.
4252
* `start` - Lowest index that has been calculated.
4353
* `best_loc` - Index in the text where the best match has been found.
54+
55+
## Examples
56+
57+
iex> DebugUtils.debug_rd("abc", "add", 0, %{1 => 5, 2 => 7, -1 => 3}, 1, 2)
58+
[
59+
"rd_j^0 pattern: a d d",
60+
" 0* _ 0: 0 0 0",
61+
" 1 a 5: 1 0 1",
62+
" 2 b 7: 1 1 1",
63+
" 3@ c 0: 0 0 0",
64+
" 4 _ 0: 0 0 0"
65+
]
66+
4467
"""
4568
@spec debug_rd(
4669
String.t(),
@@ -49,24 +72,24 @@ defmodule Dmp.DebugUtils do
4972
Dmp.Match.bitap_array(),
5073
non_neg_integer(),
5174
integer()
52-
) :: nil
75+
) :: [String.t()]
5376
def debug_rd(text, pattern, d, rd, start \\ 0, best_loc \\ -1) do
54-
rd_size = max(String.length(text) + 2, Map.fetch!(rd, -1))
55-
rd_header(d, pattern) |> IO.puts()
5677
pattern_length = String.length(pattern)
78+
rd_size = max(String.length(text) + 2, Map.fetch!(rd, -1))
5779

58-
Enum.map(0..(rd_size - 1), fn j ->
59-
ch =
60-
if j == 0 do
61-
nil
62-
else
63-
String.at(text, j - 1)
64-
end
80+
data =
81+
Enum.map(0..(rd_size - 1), fn j ->
82+
ch =
83+
if j == 0 do
84+
nil
85+
else
86+
String.at(text, j - 1)
87+
end
6588

66-
rd_j_line(ch, j, rd, pattern_length, start, best_loc) |> IO.puts()
67-
end)
89+
rd_j_line(ch, j, rd, pattern_length, start, best_loc)
90+
end)
6891

69-
nil
92+
[rd_header(d, pattern) | data]
7093
end
7194

7295
defp rd_header(d, pattern) do

lib/dmp/match.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule Dmp.Match do
77
use Bitwise, only_operators: true
88

99
import Dmp.StringUtils
10-
# import Dmp.DebugUtils
10+
import Dmp.DebugUtils
1111

1212
alias Dmp.Options
1313

@@ -123,7 +123,8 @@ defmodule Dmp.Match do
123123
constants =
124124
{text, pattern, loc, s, matchmask, shiftmask, text_length, pattern_length, match_distance}
125125

126-
# debug_alphabet(pattern, s)
126+
# Uncomment to see the bitarray
127+
# debug_alphabet(pattern, s) |> Enum.join("\n") |> IO.puts
127128

128129
# Start with `max_distance = text_length + pattern_length`
129130
# and the $$R_j^0$$ array all zero (empty map).
@@ -243,7 +244,8 @@ defmodule Dmp.Match do
243244
# that level.
244245
d1_score = bitap_score(d + 1, loc, loc, pattern_length, match_distance)
245246

246-
# debug_rd(text, pattern, d, rd, j, best_loc)
247+
# Uncomment to see the bitarray
248+
# debug_rd(text, pattern, d, rd, j, best_loc) |> Enum.join("\n") |> IO.puts
247249

248250
if d1_score > score_threshold do
249251
# No hope for a (better) match at greater error levels.

test/other_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
defmodule OtherTest do
22
use ExUnit.Case
33

4-
alias Dmp.{Match, Options, StringUtils}
4+
alias Dmp.{DebugUtils, Match, Options, StringUtils}
55

6+
doctest Dmp.DebugUtils
67
doctest Dmp.Options
78
doctest Dmp.StringUtils
89

0 commit comments

Comments
 (0)