Function Rename 2
MC logo
 

Function Rename 2

Ada Code Examples


<<Function Rename 1 Download Objects 1>>
--
-- Keyword and default parameters with rename.  This is like the
-- earlier example of keywork parameters, but shows that rename can be
-- used to change the default values and parameter names.
--
with Gnat.Io; use Gnat.Io;
procedure Ren2 is
   -- This type is used to provide a list of subscripts to String_Fill.
   type SubList is array ( Positive range <> ) of Integer;

   -- This procedure fills up a string under the control of various
   -- options.  It can fill with blanks by default, or any other character.
   -- It can fill the whole string or a portion thereof.  It can fill with
   -- the same character, or adjacent characters.
   -- The portion can be specified in a number of ways.
   procedure String_Fill(
          Str: out String;                -- String to be filled
          Fill_With: Character := ' ';    -- Fill with this, space by default.
          Char_Step: Integer := 0;        -- Increment the fill char each time.
          Fill_Step: Integer := 1;        -- Step by this much.
          Specific_Points: SubList := (-1, -2); -- Fill these specific points.
                                          -- (Ada won't permit default () )
          Max_Fill: Integer := -1)        -- Max number of entries to fill.
                is
      I: Integer;                       -- Scanner.
      Ch: Character := Fill_With;       -- Filling character
      Filled: Integer := 0;             -- Number of places filled.
   begin
      I := Str'First;
      while I <= Str'Last loop
         exit when Max_Fill > 0 and Filled >= Max_Fill;

         -- Fill with curr fill char.
         Str(I) := Ch;

         -- Count the filling.
         Filled := Filled + 1;

         -- Go to the next fill char.
         Ch := Character'Val(Character'Pos(Ch) + Char_Step);

         -- Next position.
         I := I + Fill_Step;
      end loop;

      -- Now fill up from the specific positions.
      for I in Specific_Points'Range loop
         exit when Max_Fill > 0 and Filled >= Max_Fill;

         -- Fill with curr fill char.
         if Specific_Points(I) in Str'Range then
            Str(Specific_Points(I)) := Ch;

            -- Count the filling.
            Filled := Filled + 1;
         end if;

         -- Go to the next fill char.
         Ch := Character'Val(Character'Pos(Ch) + Char_Step);
      end loop;
   end String_Fill;

   A: String(1..20);

   procedure Splat_Fill (
          Str: out String;                -- String to be filled
          Fill_With: Character := '!';    -- Fill with this, '!' by default.
          Char_Incr: Integer := 1;        -- Increment the fill char each time.
          Pos_Incr: Integer := 1;         -- Step by this much.
          Specific_Points: SubList := (1 => 2); -- Fill these specific points.
          Fill_Limit: Integer) renames    -- Max number of entries to fill.
   String_Fill;

begin
   A := (1..20 => ' ');
   Splat_Fill(A, Fill_Limit => 10, Pos_Incr => 2);
   Put_Line("[" & A & "]");
   Splat_Fill(A, Fill_Limit => -1, Fill_With => '*',
              Pos_Incr => 3, Char_Incr => 0);
   Put_Line("[" & A & "]");
end Ren2;
<<Function Rename 1 Objects 1>>