Skip to content

Commit

Permalink
Fix function for string of arrays without empty strings
Browse files Browse the repository at this point in the history
Previous length was computed with empty elements and cause error when the list is composed (eg.) of one non-empty element and one empty element
  • Loading branch information
DylanMarinho committed Jan 23, 2024
1 parent 5275203 commit dfc4c66
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lib/OCamlUtilities.ml
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,18 @@ let string_of_array_of_string_with_sep sep a =
!the_string ^ a.(length - 1)
)

(* Compute the number of non-empty elements in an array *)
let length_non_empty a =
let classic_length = Array.length a in
let number_non_empty_elements = ref 0 in
for i = 0 to classic_length - 1 do
if a.(i) <> "" then number_non_empty_elements := (!number_non_empty_elements) + 1
done;
!number_non_empty_elements

(* Convert an array of string into a string with separators removing empty strings *)
let string_of_array_of_string_with_sep_without_empty_strings sep a =
let length = Array.length a in
let length = length_non_empty a in
if length = 0 then "" else (
let the_string = ref "" in
for i = 0 to length - 2 do
Expand Down

0 comments on commit dfc4c66

Please sign in to comment.