@@ -677,18 +677,23 @@ defmodule Regex do
677
677
<< _ :: binary - size ( ^ offset ) , part :: binary - size ( ^ keep ) , match :: binary - size ( ^ length ) , _ :: binary >> =
678
678
string
679
679
680
- if keep == 0 and trim do
681
- [ match | do_split ( [ h | t ] , string , new_offset , counter - 1 , trim , true ) ]
682
- else
683
- [ part , match | do_split ( [ h | t ] , string , new_offset , counter - 1 , trim , true ) ]
680
+ cond do
681
+ keep == 0 and ( offset != 0 and length == 0 ) ->
682
+ do_split ( [ h | t ] , string , new_offset , counter - 1 , trim , true )
683
+
684
+ keep == 0 and trim ->
685
+ [ match | do_split ( [ h | t ] , string , new_offset , counter - 1 , trim , true ) ]
686
+
687
+ true ->
688
+ [ part , match | do_split ( [ h | t ] , string , new_offset , counter - 1 , trim , true ) ]
684
689
end
685
690
end
686
691
687
692
defp do_split ( [ [ { pos , length } | h ] | t ] , string , offset , counter , trim , false ) do
688
693
new_offset = pos + length
689
694
keep = pos - offset
690
695
691
- if keep == 0 and trim do
696
+ if keep == 0 and ( trim or ( offset != 0 and length == 0 ) ) do
692
697
do_split ( [ h | t ] , string , new_offset , counter , trim , false )
693
698
else
694
699
<< _ :: binary - size ( ^ offset ) , part :: binary - size ( ^ keep ) , _ :: binary >> = string
0 commit comments