The Ada Program: bounded_strings.ads
1 -- bounded_strings.ads: generic package implementing bounded-length strings
2
3 with Ada.Text_IO;
4 use Ada;
5
6 generic
7
8 Max_Length : Positive; -- The maximum size of an individual Bounded_String
9
10 package Bounded_Strings is
11
12 type Bounded_String is private; -- Bounded-length string type
13
14 Null_Bounded_String: constant Bounded_String; -- A string with no characters
15
16
17 function Length (Source : in Bounded_String) return Natural;
18
19 -- Return the character in the string at the position given by Index.
20 -- Exceptions: CONSTRAINT_ERROR is raised if Index > Length(Source)
21 function Element (Source : in Bounded_String;
22 Index : in Positive) return Character;
23
24
25 ------------------------------------------------------------------------------
26 -- Relational operators
27 ------------------------------------------------------------------------------
28 function "<" (Left, Right : Bounded_String) return Boolean;
29 function "<=" (Left, Right : Bounded_String) return Boolean;
30 function ">" (Left, Right : Bounded_String) return Boolean;
31 function ">=" (Left, Right : Bounded_String) return Boolean;
32 function Equal (Left, Right : Bounded_String) return Boolean;
33
34 ------------------------------------------------------------------------------
35 -- Concatenation operators
36 -- Exceptions: CONSTRAINT_ERROR is raised if the combined length exceeds max
37 ------------------------------------------------------------------------------
38 function "&" (Left, Right : Bounded_String) return Bounded_String;
39 function "&" (Left: Bounded_String; Right: Character) return Bounded_String;
40 function "&" (Left: Character; Right: Bounded_String) return Bounded_String;
41 function "&" (Left: Bounded_String; Right: String) return Bounded_String;
42 function "&" (Left: String; Right: Bounded_String) return Bounded_String;
43
44 ------------------------------------------------------------------------------
45 -- The substring of "Source" from "Low" to "High".
46 -- Exceptions: CONSTRAINT_ERROR is raised if Low <= High or High > Length(Source)
47 function Slice (Source : Bounded_String;
48 Low : Positive;
49 High : Natural) return Bounded_String;
50
51 ------------------------------------------------------------------------------
52 -- Search for "Pattern" in "Source". If the pattern is found, "Index"
53 -- returns the smallest index I such that the slice of "Source" starting
54 -- at I matches the pattern. Otherwise, 0 is returned.
55 function Index (Source : in Bounded_String;
56 Pattern : in Bounded_String) return Natural;
57
58
59 --------------
60 -- Conversions
61 --------------
62
63 ------------------------------------------------------------------------------
64 -- This function converts a bounded-length string to a fixed-length string
65 function To_String (Source: Bounded_String) return String;
66
67 ------------------------------------------------------------------------------
68 -- This function converts a fixed-length string to a bounded-length string
69 -- Exceptions: CONSTRAINT_ERROR is raised if Source'Last > Max_Length
70 function To_Bounded_String (Source: String) return Bounded_String;
71
72 ------------------------------------------------------------------------------
73 -- Input and Output Operations
74 ------------------------------------------------------------------------------
75
76 procedure Get_Line (File: in Text_IO.File_Type := Text_IO.Current_Input;
77 Item: out Bounded_String);
78
79 procedure Put (File: in Text_IO.File_Type := Text_IO.Current_Output;
80 Item: in Bounded_String);
81
82 private
83
84 subtype Length_Range is Natural range 0..Max_Length;
85 subtype Index_Range is Length_Range range 1..Max_Length;
86
87 subtype String_Array is String (Index_Range);
88
89 type Bounded_String is
90 record
91 Length : Length_Range := 0; -- The number of characters
92 Data : String_Array; -- The characters in the string
93 end record;
94
95 Null_Bounded_String : constant Bounded_String := (0, (others => ' '));
96
97
98 end Bounded_Strings;