diff --git a/source/text/implementation/vss-strings-formatters-generic_modulars.adb b/source/text/implementation/vss-strings-formatters-generic_modulars.adb new file mode 100644 index 00000000..34f4f123 --- /dev/null +++ b/source/text/implementation/vss-strings-formatters-generic_modulars.adb @@ -0,0 +1,62 @@ +-- +-- Copyright (C) 2023, AdaCore +-- +-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +-- + +package body VSS.Strings.Formatters.Generic_Modulars is + + ------------ + -- Format -- + ------------ + + overriding function Format + (Self : Formatter; + Format : VSS.Strings.Formatters.Format_Information) + return VSS.Strings.Virtual_String + is + Buffer : constant Wide_Wide_String := + Modular_Type'Wide_Wide_Image (Self.Value); + + begin + if Buffer (Buffer'First) = ' ' then + return + VSS.Strings.To_Virtual_String + (Buffer (Buffer'First + 1 .. Buffer'Last)); + + else + return VSS.Strings.To_Virtual_String (Buffer); + end if; + end Format; + + ----------- + -- Image -- + ----------- + + function Image (Item : Modular_Type) return Formatter is + begin + return (Name => <>, Value => Item); + end Image; + + ----------- + -- Image -- + ----------- + + function Image + (Name : VSS.Strings.Virtual_String; + Item : Modular_Type) return Formatter is + begin + return (Name => Name, Value => Item); + end Image; + + ---------- + -- Name -- + ---------- + + overriding function Name + (Self : Formatter) return VSS.Strings.Virtual_String is + begin + return Self.Name; + end Name; + +end VSS.Strings.Formatters.Generic_Modulars; diff --git a/source/text/implementation/vss-strings.adb b/source/text/implementation/vss-strings.adb index 50ff1218..32e727f6 100644 --- a/source/text/implementation/vss-strings.adb +++ b/source/text/implementation/vss-strings.adb @@ -40,6 +40,23 @@ package body VSS.Strings is end return; end "&"; + --------- + -- "*" -- + --------- + + function "*" + (Left : Character_Count; + Right : VSS.Characters.Virtual_Character) return Virtual_String is + begin + return Result : Virtual_String do + -- Result.Set_Capacity (Left); + + for J in 1 .. Left loop + Result.Append (Right); + end loop; + end return; + end "*"; + --------- -- "<" -- --------- diff --git a/source/text/vss-strings-formatters-generic_modulars.ads b/source/text/vss-strings-formatters-generic_modulars.ads new file mode 100644 index 00000000..d9fe3b12 --- /dev/null +++ b/source/text/vss-strings-formatters-generic_modulars.ads @@ -0,0 +1,56 @@ +-- +-- Copyright (C) 2023, AdaCore +-- +-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +-- + +-- This package provides formatter for integer types. +-- +-- Integer_Formatter supports following formatting options: +-- [0][1-9[0-9]*][#[1-9][0-9]+] +-- +-- 0 - fill leading zeros +-- +-- By default, leading zeros is not filled. +-- +-- 1-9[0-9]* - number of digits (not include sign) +-- +-- By default, shortest number of digits is used. +-- +-- #[1-9][0-9]* - base +-- +-- By default, base is 10. + +generic + type Modular_Type is mod <>; + +package VSS.Strings.Formatters.Generic_Modulars is + + pragma Preelaborate; + + type Formatter is + new VSS.Strings.Formatters.Abstract_Formatter with private; + + function Image (Item : Modular_Type) return Formatter; + + function Image + (Name : VSS.Strings.Virtual_String; + Item : Modular_Type) return Formatter; + +private + + type Formatter is + new VSS.Strings.Formatters.Abstract_Formatter with record + Name : VSS.Strings.Virtual_String; + Value : Modular_Type; + end record; + + overriding function Name + (Self : Formatter) return VSS.Strings.Virtual_String; + + overriding function Format + (Self : Formatter; + Format : VSS.Strings.Formatters.Format_Information) + return VSS.Strings.Virtual_String; + +end VSS.Strings.Formatters.Generic_Modulars; diff --git a/source/text/vss-strings.ads b/source/text/vss-strings.ads index 64be6810..a1fda10e 100644 --- a/source/text/vss-strings.ads +++ b/source/text/vss-strings.ads @@ -221,6 +221,11 @@ package VSS.Strings is -- Right : VSS.Characters.Virtual_Character) return Virtual_String; -- -- Concatenation operator for virtual string and virtual characters. + function "*" + (Left : Character_Count; + Right : VSS.Characters.Virtual_Character) return Virtual_String; + -- Create string by repeating given number of given character. + procedure Clear (Self : in out Virtual_String'Class); -- Remove all data. diff --git a/testsuite/text/test_string-test_asterisk_character.adb b/testsuite/text/test_string-test_asterisk_character.adb new file mode 100644 index 00000000..a7627b66 --- /dev/null +++ b/testsuite/text/test_string-test_asterisk_character.adb @@ -0,0 +1,29 @@ +-- +-- Copyright (C) 2020-2023, AdaCore +-- +-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +-- + +separate (Test_String) +procedure Test_Asterisk_Character is + + use type VSS.Strings.Virtual_String; + +begin + declare + R : constant VSS.Strings.Virtual_String := 0 * 'A'; + + begin + Test_Support.Assert (R.Is_Empty); + -- Test_Support.Assert (not R.Is_Null); + -- XXX Should it be non-null string? + end; + + declare + R : constant VSS.Strings.Virtual_String := 5 * 'B'; + + begin + Test_Support.Assert (not R.Is_Empty); + Test_Support.Assert (R = "BBBBB"); + end; +end Test_Asterisk_Character; diff --git a/testsuite/text/test_string.adb b/testsuite/text/test_string.adb index 796220d2..1bbe6c90 100644 --- a/testsuite/text/test_string.adb +++ b/testsuite/text/test_string.adb @@ -11,6 +11,7 @@ with Test_Support; procedure Test_String is + procedure Test_Asterisk_Character; procedure Test_Ends_With; procedure Test_Prepend; procedure Test_Replace; @@ -22,6 +23,8 @@ procedure Test_String is -- -- This test requires valgrind. + procedure Test_Asterisk_Character is separate; + procedure Test_Ends_With is separate; ------------------ @@ -198,6 +201,7 @@ procedure Test_String is end Test_V705_011; begin + Test_Asterisk_Character; Test_Ends_With; Test_Prepend; Test_Replace;