-- $Date: 2004/01/02 03:46:21 $
-- $Revision: 1.12 $
-- $Author: jcrocholl $

package body Number_Strings is

   -- Convert a string to a number.
   function To_Number
     (Value : in String-- The string to convert.
     return Number       -- The integer number.
   is
      Index    : Positive := Value'First;
      Result   : Number := 0;
      Negative : Boolean := Value(Index) = '-';
   begin
      if Negative then Index := Index + 1; end if;
      loop
         exit when Index > Value'Last;
         exit when not (Value(Index) in '0' .. '9');
         Result := Result * 10
           + Character'Pos(Value(Index))
           - Character'Pos('0');
         Index := Index + 1;
      end loop;
      if Negative then Result := -Result; end if;
      return Result;
   end To_Number;

   -- Add leading characters up to a minimum length.
   function Fill_String
     (Value : in String;    -- The text to fill.
      Fill  : in Positive;  -- Minimum length.
      Char  : in Character-- Fill character.
     return String          -- The filled text.
   is
      Prefix_Length : Natural := Integer'Max(0, Fill - Value'Length);
      Prefix        : String(1 .. Prefix_Length) := (others => Char);
   begin
      return Prefix & Value;
   end Fill_String;

   -- Convert a number to a string.
   function To_String
     (Value : in Number;           -- The number to convert.
      Fill  : in Natural;          -- Minimum length.
      Char  : in Character := '0') -- Fill character.
     return String is              -- The decimal string representation.
   begin
      if Fill /= 0 then
         declare
            Result : String := To_String(Value);
         begin
            return Fill_String(Result, Fill, Char);
         end;
      elsif Value < 0 then
         return "-" & To_String(-Value);
      elsif Value >= 10 then
         return To_String(Value / 10) &
           Character'Val(Character'Pos('0') + Value mod 10);
      else
         return "" & Character'Val(Character'Pos('0') + Value mod 10);
      end if;
   end To_String;

   -- Convert a number to a string.
   function To_String
     (Value : in Number-- The number to convert.
     return String is    -- The decimal string representation.
   begin
      return To_String(Value, 0);
   end To_String;

end Number_Strings;