diff --git a/source/text/implementation/vss-string_vectors.adb b/source/text/implementation/vss-string_vectors.adb index ea7c3231..f1777828 100644 --- a/source/text/implementation/vss-string_vectors.adb +++ b/source/text/implementation/vss-string_vectors.adb @@ -268,6 +268,32 @@ package body VSS.String_Vectors is end return; end Join; + ---------- + -- Join -- + ---------- + + function Join + (Self : Virtual_String_Vector'Class; + Separator : VSS.Strings.Virtual_String) + return VSS.Strings.Virtual_String + is + First_Segment : Boolean := True; + + begin + return Result : VSS.Strings.Virtual_String do + for Item of Self loop + if First_Segment then + First_Segment := False; + + else + Result.Append (Separator); + end if; + + Result.Append (Item); + end loop; + end return; + end Join; + ---------------- -- Join_Lines -- ---------------- diff --git a/source/text/vss-string_vectors.ads b/source/text/vss-string_vectors.ads index ecd798f3..57beb39d 100644 --- a/source/text/vss-string_vectors.ads +++ b/source/text/vss-string_vectors.ads @@ -119,6 +119,10 @@ package VSS.String_Vectors is (Self : Virtual_String_Vector'Class; Separator : VSS.Characters.Virtual_Character) return VSS.Strings.Virtual_String; + function Join + (Self : Virtual_String_Vector'Class; + Separator : VSS.Strings.Virtual_String) + return VSS.Strings.Virtual_String; -- Join all strings in the string vector into single string with each -- element separated by the given separator. diff --git a/testsuite/text/test_string_vector.adb b/testsuite/text/test_string_vector.adb index fc61bf07..9159aeba 100644 --- a/testsuite/text/test_string_vector.adb +++ b/testsuite/text/test_string_vector.adb @@ -30,6 +30,7 @@ procedure Test_String_Vector is -- Testcases procedure Test_Legacy_Tests; + procedure Test_Join_String; procedure Test_Join_Lines; procedure Test_Is_Empty; procedure Test_Append_Vector; @@ -305,6 +306,41 @@ procedure Test_String_Vector is end; end Test_Join_Lines; + ---------------------- + -- Test_Join_String -- + ---------------------- + + procedure Test_Join_String is + begin + -- Usual case + + declare + V : constant VSS.String_Vectors.Virtual_String_Vector := + ["ABC", "DEF", "GHI"]; + + begin + Test_Support.Assert (V.Join ("") = "ABCDEFGHI"); + Test_Support.Assert (V.Join (",") = "ABC,DEF,GHI"); + Test_Support.Assert (V.Join ("=>") = "ABC=>DEF=>GHI"); + end; + + -- Empty vector + + declare + VE : VSS.String_Vectors.Virtual_String_Vector; + + begin + Test_Support.Assert (VE.Join ("").Is_Empty); + Test_Support.Assert (VE.Join ("").Is_Null); + + Test_Support.Assert (VE.Join (",").Is_Empty); + Test_Support.Assert (VE.Join (",").Is_Null); + + Test_Support.Assert (VE.Join ("=>").Is_Empty); + Test_Support.Assert (VE.Join ("=>").Is_Null); + end; + end Test_Join_String; + ----------------------- -- Test_Legacy_Tests -- ----------------------- @@ -436,6 +472,8 @@ procedure Test_String_Vector is begin Test_Support.Run_Testcase (Test_Legacy_Tests'Access, "Various legacy tests"); + Test_Support.Run_Testcase + (Test_Join_String'Access, "Join String Separator"); Test_Support.Run_Testcase (Test_Join_Lines'Access, "Join_Lines"); Test_Support.Run_Testcase (Test_Is_Empty'Access, "Is_Empty"); Test_Support.Run_Testcase (Test_Append_Vector'Access, "Append (Vector)");