@@ -155,6 +155,7 @@ defmodule Regex do
155
155
case :re . compile ( source , opts ) do
156
156
{ :ok , re_pattern } ->
157
157
{ :ok , % Regex { re_pattern: re_pattern , re_version: version , source: source , opts: doc_opts } }
158
+
158
159
error ->
159
160
error
160
161
end
@@ -185,6 +186,7 @@ defmodule Regex do
185
186
case Map . get ( regex , :re_version , :error ) do
186
187
^ version ->
187
188
{ :ok , regex }
189
+
188
190
_ ->
189
191
% { source: source , opts: opts } = regex
190
192
compile ( source , opts , version )
@@ -226,7 +228,7 @@ defmodule Regex do
226
228
false
227
229
228
230
"""
229
- @ spec match? ( t , String . t ) :: boolean
231
+ @ spec match? ( t , String . t ( ) ) :: boolean
230
232
def match? ( % Regex { re_pattern: compiled } , string ) when is_binary ( string ) do
231
233
:re . run ( string , compiled , [ { :capture , :none } ] ) == :match
232
234
end
@@ -275,12 +277,12 @@ defmodule Regex do
275
277
def run ( regex , string , options \\ [ ] )
276
278
277
279
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 )
279
281
captures = Keyword . get ( options , :capture , :all )
280
282
281
283
case :re . run ( string , compiled , [ { :capture , captures , return } ] ) do
282
284
:nomatch -> nil
283
- :match -> [ ]
285
+ :match -> [ ]
284
286
{ :match , results } -> results
285
287
end
286
288
end
@@ -302,7 +304,7 @@ defmodule Regex do
302
304
nil
303
305
304
306
"""
305
- @ spec named_captures ( t , String . t , [ term ] ) :: map | nil
307
+ @ spec named_captures ( t , String . t ( ) , [ term ] ) :: map | nil
306
308
def named_captures ( regex , string , options \\ [ ] ) when is_binary ( string ) do
307
309
names = names ( regex )
308
310
options = Keyword . put ( options , :capture , names )
@@ -327,7 +329,7 @@ defmodule Regex do
327
329
"foo"
328
330
329
331
"""
330
- @ spec source ( t ) :: String . t
332
+ @ spec source ( t ) :: String . t ( )
331
333
def source ( % Regex { source: source } ) do
332
334
source
333
335
end
@@ -341,7 +343,7 @@ defmodule Regex do
341
343
"m"
342
344
343
345
"""
344
- @ spec opts ( t ) :: String . t
346
+ @ spec opts ( t ) :: String . t ( )
345
347
def opts ( % Regex { opts: opts } ) do
346
348
opts
347
349
end
@@ -355,7 +357,7 @@ defmodule Regex do
355
357
["foo"]
356
358
357
359
"""
358
- @ spec names ( t ) :: [ String . t ]
360
+ @ spec names ( t ) :: [ String . t ( ) ]
359
361
def names ( % Regex { re_pattern: re_pattern } ) do
360
362
{ :namelist , names } = :re . inspect ( re_pattern , :namelist )
361
363
names
@@ -389,13 +391,13 @@ defmodule Regex do
389
391
[["$"], ["£"], ["€"]]
390
392
391
393
"""
392
- @ spec scan ( t , String . t , [ term ] ) :: [ [ String . t ] ]
394
+ @ spec scan ( t , String . t ( ) , [ term ] ) :: [ [ String . t ( ) ] ]
393
395
def scan ( regex , string , options \\ [ ] )
394
396
395
397
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 )
397
399
captures = Keyword . get ( options , :capture , :all )
398
- options = [ { :capture , captures , return } , :global ]
400
+ options = [ { :capture , captures , return } , :global ]
399
401
400
402
case :re . run ( string , compiled , options ) do
401
403
:match -> [ ]
@@ -452,7 +454,7 @@ defmodule Regex do
452
454
["a", "b", "c"]
453
455
454
456
"""
455
- @ spec split ( t , String . t , [ term ] ) :: [ String . t ]
457
+ @ spec split ( t , String . t ( ) , [ term ] ) :: [ String . t ( ) ]
456
458
def split ( regex , string , options \\ [ ] )
457
459
458
460
def split ( % Regex { } , "" , opts ) do
@@ -465,33 +467,40 @@ defmodule Regex do
465
467
466
468
def split ( % Regex { re_pattern: compiled } , string , opts ) when is_binary ( string ) and is_list ( opts ) do
467
469
on = Keyword . get ( opts , :on , :first )
470
+
468
471
case :re . run ( string , compiled , [ :global , capture: on ] ) do
469
472
{ :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
+
474
478
:match ->
475
479
[ string ]
480
+
476
481
:nomatch ->
477
482
[ string ]
478
483
end
479
484
end
480
485
481
- defp parts_to_index ( :infinity ) , do: 0
486
+ defp parts_to_index ( :infinity ) , do: 0
482
487
defp parts_to_index ( n ) when is_integer ( n ) and n > 0 , do: n
483
488
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
486
493
487
494
defp do_split ( _ , string , offset , 1 , _trim , _with_captures ) ,
488
495
do: [ binary_part ( string , offset , byte_size ( string ) - offset ) ]
489
496
490
497
defp do_split ( [ ] , string , offset , _counter , _trim , _with_captures ) ,
491
498
do: [ binary_part ( string , offset , byte_size ( string ) - offset ) ]
492
499
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
495
504
496
505
defp do_split ( [ [ ] | t ] , string , offset , counter , trim , with_captures ) ,
497
506
do: do_split ( t , string , offset , counter , trim , with_captures )
@@ -503,7 +512,8 @@ defmodule Regex do
503
512
if keep == 0 and length == 0 do
504
513
do_split ( [ h | t ] , string , new_offset , counter , trim , true )
505
514
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
507
517
508
518
if keep == 0 and ( length == 0 or trim ) do
509
519
[ match | do_split ( [ h | t ] , string , new_offset , counter - 1 , trim , true ) ]
@@ -570,7 +580,7 @@ defmodule Regex do
570
580
"Abcadc"
571
581
572
582
"""
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 ( )
574
584
def replace ( regex , string , replacement , options \\ [ ] )
575
585
576
586
def replace ( regex , string , replacement , options )
@@ -579,7 +589,7 @@ defmodule Regex do
579
589
end
580
590
581
591
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
583
593
{ :arity , arity } = :erlang . fun_info ( replacement , :arity )
584
594
do_replace ( regex , string , { replacement , arity } , options )
585
595
end
@@ -591,15 +601,16 @@ defmodule Regex do
591
601
case :re . run ( string , compiled , opts ) do
592
602
:nomatch ->
593
603
string
604
+
594
605
{ :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
+
596
608
{ :match , slist } ->
597
- apply_list ( string , replacement , [ slist ] ) |> IO . iodata_to_binary
609
+ apply_list ( string , replacement , [ slist ] ) |> IO . iodata_to_binary ( )
598
610
end
599
611
end
600
612
601
- defp precompile_replacement ( "" ) ,
602
- do: [ ]
613
+ defp precompile_replacement ( "" ) , do: [ ]
603
614
604
615
defp precompile_replacement ( << ?\\ , ?g , ?{ , rest :: binary >> ) when byte_size ( rest ) > 0 do
605
616
{ ns , << ?} , rest :: binary >> } = pick_int ( rest )
@@ -619,6 +630,7 @@ defmodule Regex do
619
630
case precompile_replacement ( rest ) do
620
631
[ head | t ] when is_binary ( head ) ->
621
632
[ << x , head :: binary >> | t ]
633
+
622
634
other ->
623
635
[ << x >> | other ]
624
636
end
@@ -672,8 +684,10 @@ defmodule Regex do
672
684
cond do
673
685
is_binary ( part ) ->
674
686
part
687
+
675
688
part >= tuple_size ( indexes ) ->
676
689
""
690
+
677
691
true ->
678
692
get_index ( string , elem ( indexes , part ) )
679
693
end
@@ -713,11 +727,11 @@ defmodule Regex do
713
727
"\\\\what\\ if"
714
728
715
729
"""
716
- @ spec escape ( String . t ) :: String . t
730
+ @ spec escape ( String . t ( ) ) :: String . t ( )
717
731
def escape ( string ) when is_binary ( string ) do
718
732
string
719
733
|> escape ( _length = 0 , string )
720
- |> IO . iodata_to_binary
734
+ |> IO . iodata_to_binary ( )
721
735
end
722
736
723
737
@ escapable '.^$*+?()[]{}|#-\\ \t \n \v \f \r \s '
@@ -752,7 +766,7 @@ defmodule Regex do
752
766
def unescape_map ( ?t ) , do: ?\t
753
767
def unescape_map ( ?v ) , do: ?\v
754
768
def unescape_map ( ?a ) , do: ?\a
755
- def unescape_map ( _ ) , do: false
769
+ def unescape_map ( _ ) , do: false
756
770
757
771
# Private Helpers
758
772
@@ -761,12 +775,15 @@ defmodule Regex do
761
775
defp translate_options ( << ?x , t :: binary >> , acc ) , do: translate_options ( t , [ :extended | acc ] )
762
776
defp translate_options ( << ?f , t :: binary >> , acc ) , do: translate_options ( t , [ :firstline | acc ] )
763
777
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
+
765
782
defp translate_options ( << ?m , t :: binary >> , acc ) , do: translate_options ( t , [ :multiline | acc ] )
766
783
767
784
# TODO: Remove on 2.0
768
785
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" )
770
787
translate_options ( t , [ :ungreedy | acc ] )
771
788
end
772
789
0 commit comments