The Ada Program: bounds.adb

  1 -- bounds.adb:  upper and lower bounds of strings
  2 
  3 with Ada.Text_IO, Ada.Integer_Text_IO;
  4 use Ada;
  5 
  6 procedure Bounds is
  7 
  8    procedure Upper_And_Lower_Bounds (S: String) is
  9    begin
 10       Text_IO.Put ("The lower bound is ");
 11       Integer_Text_IO.Put (S'First, Width=>0);
 12       Text_IO.Put (" and the upper bound is ");
 13       Integer_Text_IO.Put (S'Last, Width=>0);
 14       Text_IO.Put (".  The length is ");
 15       Integer_Text_IO.Put (S'Length, Width=>0);
 16       Text_IO.New_Line;
 17    end Upper_And_Lower_Bounds;
 18 
 19    -- Recall that in package Standard
 20    --   type String is array (Positive range <>) of Character
 21 
 22    Alpha: String := "abcdefghijklmnopqrstuvwxyz";
 23 
 24 begin
 25 
 26    -- non-null ranges
 27    Upper_And_Lower_Bounds (Alpha);                      --  1..26
 28    Upper_And_Lower_Bounds (Alpha(3..5));                --  3..5
 29    Upper_And_Lower_Bounds ("cde");                      --  1..3
 30    Upper_And_Lower_Bounds (Alpha&Alpha);                --  1..52
 31    Upper_And_Lower_Bounds (Alpha(17..22)&Alpha(3..5));  -- 17..25
 32 
 33    -- null ranges; 'Length=0
 34    Upper_And_Lower_Bounds ("");             --  1..0
 35    Upper_And_Lower_Bounds (Alpha(1..0));    --  1..0
 36    Upper_And_Lower_Bounds (Alpha(5..3));    --  5..3
 37    Upper_And_Lower_Bounds (Alpha(5..-4));   --  5..-4
 38    Upper_And_Lower_Bounds (Alpha(34..-9));  -- 34..-9
 39 
 40    -- illegal ranges
 41    Upper_And_Lower_Bounds (Alpha(-4..9));   -- CONSTRAINT_ERROR
 42    Upper_And_Lower_Bounds (Alpha( 0..1));   -- CONSTRAINT_ERROR
 43 
 44 end Bounds;