Skip to content

Commit 68c5c31

Browse files
author
José Valim
committed
Remove unnecessary regexes in IO.ANSI.Docs
1 parent dd79a3e commit 68c5c31

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

lib/elixir/lib/io/ansi/docs.ex

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule IO.ANSI.Docs do
22
@moduledoc false
33

44
@bullets [?*, ?-, ?+]
5+
@spaces [" ", "\n", "\t"]
56

67
@doc """
78
The default options used by this module.
@@ -204,7 +205,7 @@ defmodule IO.ANSI.Docs do
204205
|> Enum.join(" ")
205206
|> handle_links
206207
|> handle_inline(options)
207-
|> String.split(~r{\s})
208+
|> String.split(@spaces)
208209
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
209210

210211
unless no_wrap, do: newline_after_block()
@@ -266,8 +267,10 @@ defmodule IO.ANSI.Docs do
266267
count = Enum.map(lines, &length/1) |> Enum.max
267268
lines = Enum.map(lines, &pad_to_number_of_columns(&1, count))
268269

269-
widths = for line <- lines, do:
270-
(for {_col, length} <- line, do: length)
270+
widths =
271+
for line <- lines do
272+
for {_col, length} <- line, do: length
273+
end
271274

272275
col_widths = Enum.reduce(widths,
273276
List.duplicate(0, count),
@@ -280,15 +283,16 @@ defmodule IO.ANSI.Docs do
280283
line
281284
|> String.trim("|")
282285
|> String.trim()
283-
|> String.split(~r/\s\|\s/)
286+
|> String.split(" | ")
284287
|> Enum.map(&render_column(&1, options))
285288
end
286289

287290
defp render_column(col, options) do
288-
col = col
289-
|> String.replace(~r/\\ \|/x, "|")
290-
|> handle_links
291-
|> handle_inline(options)
291+
col =
292+
col
293+
|> String.replace("\\\|", "|")
294+
|> handle_links
295+
|> handle_inline(options)
292296
{col, length_without_escape(col, 0)}
293297
end
294298

@@ -319,8 +323,17 @@ defmodule IO.ANSI.Docs do
319323
defp render_table([], _, _),
320324
do: nil
321325

322-
defp table_header?(row), do:
323-
Enum.all?(row, fn {col, _} -> col =~ ~r/^:?-+:?$/ end)
326+
defp table_header?(row) do
327+
Enum.all?(row, fn {col, _} -> table_header_column?(col) end)
328+
end
329+
330+
defp table_header_column?(":" <> row), do: table_header_contents?(row)
331+
defp table_header_column?(row), do: table_header_contents?(row)
332+
333+
defp table_header_contents?("-" <> row), do: table_header_contents?(row)
334+
defp table_header_contents?(":"), do: true
335+
defp table_header_contents?(""), do: true
336+
defp table_header_contents?(_), do: false
324337

325338
defp draw_table_row(cols_and_widths, options, heading \\ false) do
326339
columns =
@@ -336,11 +349,7 @@ defmodule IO.ANSI.Docs do
336349
end
337350

338351
defp table_line?(line) do
339-
Regex.match?(~r'''
340-
( ^ \s{0,3} \| (?: [^|]+ \|)+ \s* $ )
341-
|
342-
(\s \| \s)
343-
'''x, line)
352+
line =~ " | "
344353
end
345354

346355
## Helpers
@@ -416,14 +425,17 @@ defmodule IO.ANSI.Docs do
416425
end
417426

418427
defp escape_underlines_in_link(text) do
419-
Regex.replace(~r{https?\S*}, text, &String.replace(&1, "_", "\\_"))
428+
~r{https?\S*}
429+
|> Regex.recompile!
430+
|> Regex.replace(text, &String.replace(&1, "_", "\\_"))
420431
end
421432

422433
defp remove_square_brackets_in_link(text) do
423-
Regex.replace(~r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
434+
~r{\[(.*?)\]\((.*?)\)}
435+
|> Regex.recompile!
436+
|> Regex.replace(text, "\\1 (\\2)")
424437
end
425438

426-
427439
# We have four entries: **, *, _ and `.
428440
#
429441
# The first three behave the same while the last one is simpler

0 commit comments

Comments
 (0)