Skip to content

Commit 48beb79

Browse files
Jonny Stotenericmj
Jonny Stoten
authored andcommitted
Run the code formatter on Regex (#6770)
1 parent 2c2d033 commit 48beb79

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

lib/elixir/lib/regex.ex

+49-32
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ defmodule Regex do
155155
case :re.compile(source, opts) do
156156
{:ok, re_pattern} ->
157157
{:ok, %Regex{re_pattern: re_pattern, re_version: version, source: source, opts: doc_opts}}
158+
158159
error ->
159160
error
160161
end
@@ -185,6 +186,7 @@ defmodule Regex do
185186
case Map.get(regex, :re_version, :error) do
186187
^version ->
187188
{:ok, regex}
189+
188190
_ ->
189191
%{source: source, opts: opts} = regex
190192
compile(source, opts, version)
@@ -226,7 +228,7 @@ defmodule Regex do
226228
false
227229
228230
"""
229-
@spec match?(t, String.t) :: boolean
231+
@spec match?(t, String.t()) :: boolean
230232
def match?(%Regex{re_pattern: compiled}, string) when is_binary(string) do
231233
:re.run(string, compiled, [{:capture, :none}]) == :match
232234
end
@@ -275,12 +277,12 @@ defmodule Regex do
275277
def run(regex, string, options \\ [])
276278

277279
def run(%Regex{re_pattern: compiled}, string, options) when is_binary(string) do
278-
return = Keyword.get(options, :return, :binary)
280+
return = Keyword.get(options, :return, :binary)
279281
captures = Keyword.get(options, :capture, :all)
280282

281283
case :re.run(string, compiled, [{:capture, captures, return}]) do
282284
:nomatch -> nil
283-
:match -> []
285+
:match -> []
284286
{:match, results} -> results
285287
end
286288
end
@@ -302,7 +304,7 @@ defmodule Regex do
302304
nil
303305
304306
"""
305-
@spec named_captures(t, String.t, [term]) :: map | nil
307+
@spec named_captures(t, String.t(), [term]) :: map | nil
306308
def named_captures(regex, string, options \\ []) when is_binary(string) do
307309
names = names(regex)
308310
options = Keyword.put(options, :capture, names)
@@ -327,7 +329,7 @@ defmodule Regex do
327329
"foo"
328330
329331
"""
330-
@spec source(t) :: String.t
332+
@spec source(t) :: String.t()
331333
def source(%Regex{source: source}) do
332334
source
333335
end
@@ -341,7 +343,7 @@ defmodule Regex do
341343
"m"
342344
343345
"""
344-
@spec opts(t) :: String.t
346+
@spec opts(t) :: String.t()
345347
def opts(%Regex{opts: opts}) do
346348
opts
347349
end
@@ -355,7 +357,7 @@ defmodule Regex do
355357
["foo"]
356358
357359
"""
358-
@spec names(t) :: [String.t]
360+
@spec names(t) :: [String.t()]
359361
def names(%Regex{re_pattern: re_pattern}) do
360362
{:namelist, names} = :re.inspect(re_pattern, :namelist)
361363
names
@@ -389,13 +391,13 @@ defmodule Regex do
389391
[["$"], ["£"], ["€"]]
390392
391393
"""
392-
@spec scan(t, String.t, [term]) :: [[String.t]]
394+
@spec scan(t, String.t(), [term]) :: [[String.t()]]
393395
def scan(regex, string, options \\ [])
394396

395397
def scan(%Regex{re_pattern: compiled}, string, options) when is_binary(string) do
396-
return = Keyword.get(options, :return, :binary)
398+
return = Keyword.get(options, :return, :binary)
397399
captures = Keyword.get(options, :capture, :all)
398-
options = [{:capture, captures, return}, :global]
400+
options = [{:capture, captures, return}, :global]
399401

400402
case :re.run(string, compiled, options) do
401403
:match -> []
@@ -452,7 +454,7 @@ defmodule Regex do
452454
["a", "b", "c"]
453455
454456
"""
455-
@spec split(t, String.t, [term]) :: [String.t]
457+
@spec split(t, String.t(), [term]) :: [String.t()]
456458
def split(regex, string, options \\ [])
457459

458460
def split(%Regex{}, "", opts) do
@@ -465,33 +467,40 @@ defmodule Regex do
465467

466468
def split(%Regex{re_pattern: compiled}, string, opts) when is_binary(string) and is_list(opts) do
467469
on = Keyword.get(opts, :on, :first)
470+
468471
case :re.run(string, compiled, [:global, capture: on]) do
469472
{:match, matches} ->
470-
do_split(matches, string, 0,
471-
parts_to_index(Keyword.get(opts, :parts, :infinity)),
472-
Keyword.get(opts, :trim, false),
473-
Keyword.get(opts, :include_captures, false))
473+
index = parts_to_index(Keyword.get(opts, :parts, :infinity))
474+
trim = Keyword.get(opts, :trim, false)
475+
include_captures = Keyword.get(opts, :include_captures, false)
476+
do_split(matches, string, 0, index, trim, include_captures)
477+
474478
:match ->
475479
[string]
480+
476481
:nomatch ->
477482
[string]
478483
end
479484
end
480485

481-
defp parts_to_index(:infinity), do: 0
486+
defp parts_to_index(:infinity), do: 0
482487
defp parts_to_index(n) when is_integer(n) and n > 0, do: n
483488

484-
defp do_split(_, string, offset, _counter, true, _with_captures) when byte_size(string) <= offset,
485-
do: []
489+
defp do_split(_, string, offset, _counter, true, _with_captures)
490+
when byte_size(string) <= offset do
491+
[]
492+
end
486493

487494
defp do_split(_, string, offset, 1, _trim, _with_captures),
488495
do: [binary_part(string, offset, byte_size(string) - offset)]
489496

490497
defp do_split([], string, offset, _counter, _trim, _with_captures),
491498
do: [binary_part(string, offset, byte_size(string) - offset)]
492499

493-
defp do_split([[{pos, _} | h] | t], string, offset, counter, trim, with_captures) when pos - offset < 0,
494-
do: do_split([h | t], string, offset, counter, trim, with_captures)
500+
defp do_split([[{pos, _} | h] | t], string, offset, counter, trim, with_captures)
501+
when pos - offset < 0 do
502+
do_split([h | t], string, offset, counter, trim, with_captures)
503+
end
495504

496505
defp do_split([[] | t], string, offset, counter, trim, with_captures),
497506
do: do_split(t, string, offset, counter, trim, with_captures)
@@ -503,7 +512,8 @@ defmodule Regex do
503512
if keep == 0 and length == 0 do
504513
do_split([h | t], string, new_offset, counter, trim, true)
505514
else
506-
<<_::binary-size(offset), part::binary-size(keep), match::binary-size(length), _::binary>> = string
515+
<<_::binary-size(offset), part::binary-size(keep), match::binary-size(length), _::binary>> =
516+
string
507517

508518
if keep == 0 and (length == 0 or trim) do
509519
[match | do_split([h | t], string, new_offset, counter - 1, trim, true)]
@@ -570,7 +580,7 @@ defmodule Regex do
570580
"Abcadc"
571581
572582
"""
573-
@spec replace(t, String.t, String.t | (... -> String.t), [term]) :: String.t
583+
@spec replace(t, String.t(), String.t() | (... -> String.t()), [term]) :: String.t()
574584
def replace(regex, string, replacement, options \\ [])
575585

576586
def replace(regex, string, replacement, options)
@@ -579,7 +589,7 @@ defmodule Regex do
579589
end
580590

581591
def replace(regex, string, replacement, options)
582-
when is_binary(string) and is_function(replacement) and is_list(options) do
592+
when is_binary(string) and is_function(replacement) and is_list(options) do
583593
{:arity, arity} = :erlang.fun_info(replacement, :arity)
584594
do_replace(regex, string, {replacement, arity}, options)
585595
end
@@ -591,15 +601,16 @@ defmodule Regex do
591601
case :re.run(string, compiled, opts) do
592602
:nomatch ->
593603
string
604+
594605
{:match, [mlist | t]} when is_list(mlist) ->
595-
apply_list(string, replacement, [mlist | t]) |> IO.iodata_to_binary
606+
apply_list(string, replacement, [mlist | t]) |> IO.iodata_to_binary()
607+
596608
{:match, slist} ->
597-
apply_list(string, replacement, [slist]) |> IO.iodata_to_binary
609+
apply_list(string, replacement, [slist]) |> IO.iodata_to_binary()
598610
end
599611
end
600612

601-
defp precompile_replacement(""),
602-
do: []
613+
defp precompile_replacement(""), do: []
603614

604615
defp precompile_replacement(<<?\\, ?g, ?{, rest::binary>>) when byte_size(rest) > 0 do
605616
{ns, <<?}, rest::binary>>} = pick_int(rest)
@@ -619,6 +630,7 @@ defmodule Regex do
619630
case precompile_replacement(rest) do
620631
[head | t] when is_binary(head) ->
621632
[<<x, head::binary>> | t]
633+
622634
other ->
623635
[<<x>> | other]
624636
end
@@ -672,8 +684,10 @@ defmodule Regex do
672684
cond do
673685
is_binary(part) ->
674686
part
687+
675688
part >= tuple_size(indexes) ->
676689
""
690+
677691
true ->
678692
get_index(string, elem(indexes, part))
679693
end
@@ -713,11 +727,11 @@ defmodule Regex do
713727
"\\\\what\\ if"
714728
715729
"""
716-
@spec escape(String.t) :: String.t
730+
@spec escape(String.t()) :: String.t()
717731
def escape(string) when is_binary(string) do
718732
string
719733
|> escape(_length = 0, string)
720-
|> IO.iodata_to_binary
734+
|> IO.iodata_to_binary()
721735
end
722736

723737
@escapable '.^$*+?()[]{}|#-\\\t\n\v\f\r\s'
@@ -752,7 +766,7 @@ defmodule Regex do
752766
def unescape_map(?t), do: ?\t
753767
def unescape_map(?v), do: ?\v
754768
def unescape_map(?a), do: ?\a
755-
def unescape_map(_), do: false
769+
def unescape_map(_), do: false
756770

757771
# Private Helpers
758772

@@ -761,12 +775,15 @@ defmodule Regex do
761775
defp translate_options(<<?x, t::binary>>, acc), do: translate_options(t, [:extended | acc])
762776
defp translate_options(<<?f, t::binary>>, acc), do: translate_options(t, [:firstline | acc])
763777
defp translate_options(<<?U, t::binary>>, acc), do: translate_options(t, [:ungreedy | acc])
764-
defp translate_options(<<?s, t::binary>>, acc), do: translate_options(t, [:dotall, {:newline, :anycrlf} | acc])
778+
779+
defp translate_options(<<?s, t::binary>>, acc),
780+
do: translate_options(t, [:dotall, {:newline, :anycrlf} | acc])
781+
765782
defp translate_options(<<?m, t::binary>>, acc), do: translate_options(t, [:multiline | acc])
766783

767784
# TODO: Remove on 2.0
768785
defp translate_options(<<?r, t::binary>>, acc) do
769-
IO.warn "the /r modifier in regular expressions is deprecated, please use /U instead"
786+
IO.warn("the /r modifier in regular expressions is deprecated, please use /U instead")
770787
translate_options(t, [:ungreedy | acc])
771788
end
772789

0 commit comments

Comments
 (0)