Skip to content

Commit 12c0bc8

Browse files
committed
Always return results from Pcre.split in the right order
Fixes #576
1 parent 839eaa9 commit 12c0bc8

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

lib/pcre.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ let split ~rex s =
108108
in
109109
match Re.exec rex s ~pos:0 with
110110
| g ->
111-
if Group.start g 0 = 0
112-
then List.rev (split [] (Group.stop g 0))
113-
else split [ String.sub s 0 (Group.start g 0) ] (Group.stop g 0)
111+
List.rev
112+
(if Group.start g 0 = 0
113+
then split [] (Group.stop g 0)
114+
else split [ String.sub s 0 (Group.start g 0) ] (Group.stop g 0))
114115
| exception Not_found -> if s = "" then [] else [ s ]
115116
;;
116117

lib_test/expect/test_pcre_split.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ let split ~rex s = Re.Pcre.split ~rex s |> strings
44

55
let%expect_test "split" =
66
split ~rex:re_whitespace "aa bb c d ";
7-
[%expect {| ["d"; "c"; "bb"; "aa"] |}];
7+
[%expect {| ["aa"; "bb"; "c"; "d"] |}];
88
split ~rex:re_whitespace " a full_word bc ";
99
[%expect {| ["a"; "full_word"; "bc"] |}];
1010
split ~rex:re_empty "abcd";
1111
[%expect {| ["a"; "b"; "c"; "d"] |}];
1212
split ~rex:re_eol "a\nb";
13-
[%expect {| ["\nb"; "a"] |}];
13+
[%expect {| ["a"; "\nb"] |}];
1414
split ~rex:re_bow "a b";
1515
[%expect {| ["a "; "b"] |}];
1616
split ~rex:re_eow "a b";
17-
[%expect {| [" b"; "a"] |}];
17+
[%expect {| ["a"; " b"] |}];
1818
let rex = Re.Pcre.regexp "" in
1919
split ~rex "xx";
2020
[%expect {| ["x"; "x"] |}]

0 commit comments

Comments
 (0)