gnoga_2.1.2_5f127c56/deps/PragmARC/pragmarc-b_strings.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- PragmAda Reusable Component (PragmARC)
-- Copyright (C) 2020 by PragmAda Software Engineering.  All rights reserved.
-- **************************************************************************
--
-- Bounded, variable-length strings that are hopefully more usable than
-- Ada.Strings.Bounded provides
-- MOdified from an idea by Robert Duff presented on comp.lang.ada, which works in Ada 12 but not in earlier versions
--
-- History:
-- 2020 Jun 01     J. Carter          V1.4--Use existing Too_Short exception rather than a local exception
-- 2016 Jul 01     J. Carter          V1.3--Made type B_String tagged and non-limited
-- 2016 Mar 15     J. Carter          V1.2--Default discriminant doesn't work as Duff claimed, at least with GNAT
-- 2016 Feb 15     J. carter          V1.1--Forgot "+" for To_B_String
-- 2015 Nov 15     J. Carter          V1.0--Initial release
--
with Ada.Strings;

package PragmARC.B_Strings is
   type B_String (Max_Length : Positive) is tagged private;
   -- Default initial value is Null_B_String

   Null_B_String : constant B_String; -- A string of zero characters

   function To_String (Source : B_String) return String;
   function "+" (Source : B_String) return String renames To_String;
   -- Lower bound of result is 1; upper bound is Length (Source)

   function To_B_String (Source : String) return B_String;
   function "+" (Source : String) return B_String renames To_B_String;
   -- Result's Max_Length will be Max (Source'Length, 1)

   function Length (Source : B_String) return Natural;

   procedure Assign (To : in out B_String; From : in B_String; Drop : in Ada.Strings.Truncation := Ada.Strings.Error);
   -- Gives To the same value as From
   -- If Drop = Error and Length (From) > To.Max_Length, raises Too_Short
   -- To is unchanged if Too_Short is raised
   -- Default assignment (":=") only works for objects with the same value of Max_Length
   -- Assign works for any 2 B_Strings

   procedure Assign (To : in out B_String; From : in String; Drop : in Ada.Strings.Truncation := Ada.Strings.Error);
   -- Same as Assign (To => To, From => +From, Drop => Drop);

   function "="  (Left : B_String; Right : B_String) return Boolean;
   function "<"  (Left : B_String; Right : B_String) return Boolean;
   function "<=" (Left : B_String; Right : B_String) return Boolean;
   function ">"  (Left : B_String; Right : B_String) return Boolean;
   function ">=" (Left : B_String; Right : B_String) return Boolean;
private -- PragmARC.B_Strings
   type B_String (Max_Length : Positive) is tagged record
      Len   : Natural := 0;
      Value : String (1 .. Max_Length) := (1 .. Max_Length => ' ');
   end record;

   Null_B_String : constant B_String := (Max_Length => 1, others => <>);
end PragmARC.B_Strings;
--
-- This is free software; you can redistribute it and/or modify it under
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 2, or (at your option) any later version.
-- This software is distributed in the hope that it will be useful, but WITH
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-- for more details. Free Software Foundation, 59 Temple Place - Suite
-- 330, Boston, MA 02111-1307, USA.
--
-- As a special exception, if other files instantiate generics from this
-- unit, or you link this unit with other files to produce an executable,
-- this unit does not by itself cause the resulting executable to be
-- covered by the GNU General Public License. This exception does not
-- however invalidate any other reasons why the executable file might be
-- covered by the GNU Public License.