From dfc4c6600d72aa0148683affa010b1716a7bf79b Mon Sep 17 00:00:00 2001 From: Dylan Marinho Date: Tue, 23 Jan 2024 12:50:39 +0100 Subject: [PATCH] Fix function for string of arrays without empty strings 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 --- src/lib/OCamlUtilities.ml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/OCamlUtilities.ml b/src/lib/OCamlUtilities.ml index d0ec1fcc..06191711 100644 --- a/src/lib/OCamlUtilities.ml +++ b/src/lib/OCamlUtilities.ml @@ -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