Ada 95 provides several standard packages for dealing with characters and variable-length strings; these offer a rich collection of operations for text processing.
To cover the Ada 95 string packages here in much detail would go beyond our available space. Instead, we give a summary of the capabilities, referring the reader to Appendix F, in which the specifications and explanations for the character and string facilities are reproduced verbatim from the Ada 95 RM.
In Ada 83, the type Character
is defined in terms of the
128-character ASCII code. In Ada95, Character
is given a more
international flavor; this type is defined in terms of the Latin-1 character
set, which has 256 values and allows for the additional letters used in
non-English languages, such as the French à, the German ü, and the
Scandinavian Æ. Since the first 128 characters are the same as in the
familiar ASCII, the change causes few problems for most work in English. This
was discussed in
Section
7.5, along with the package Ada.Characters.Handling
.
Ada.Strings
provides a number of interesting child packages for
dealing with text processing and encoding. We show only a very small selection
here.
Ada.Strings.Maps
provides a set of types and functions used for
creating sets of characters and translating between them. For example, if
M
is of type Ada.Strings.Maps.Character_Mapping
and
C
is of type Character
,
M := Ada.Strings.Maps.To_Mapping(From => "ABCD", To => "PQRS");returns in
M
a mapping that maps 'A'
into 'P'
,
'B'
into 'Q'
, 'C'
into 'R'
,
and 'D'
into 'S'
. All other characters--the ones not
named in the mapping--are mapped into themselves. The statement
C := Ada.Strings.Maps.Value(M, 'D');returns
'S'
to the variable C
; the statement
C := Ada.Strings.Maps.Value(M, 'G');returns
'G'
to the variable C
, that is, it makes no change.
Ada.Strings.Fixed
provides a large number of search, delete,
replace, trim, and other operations on normal Ada fixed-length strings such as
we have been studying here.
One useful function, Translate
, translates an entire string
into another string using the character mappings from
Ada.Strings.Maps
. For example, if S1
is a
ten-character string containing "ABC 123 DD"
, and S2
is a ten-character string, the statement
S2 := Ada.Strings.Fixed.Translate(Source => S1, Mapping => M);returns
"PQR 123 SS"
to S2
.
These functions make it easy to develop translators like the cryptogram program. As an example, Program 9.2 shows a modified cryptogram program that uses many of the facilities described in this section.
Program 9.2
WITH Ada.Text_IO; WITH Ada.Characters.Handling; WITH Ada.Strings.Maps; WITH Ada.Strings.Fixed; PROCEDURE Cryptogram_2 IS ------------------------------------------------------------------------ --| Program to generate a cryptogram, using Ada 95 facilities --| Author: Michael B. Feldman, The George Washington University --| Last Modified: September 1995 ------------------------------------------------------------------------ SUBTYPE Message IS String(1..60); Code : String(1..26); -- input - string of code symbols PlainText : Message; -- input - plain text message CodedText : Message; -- output - coded message CodeMap : Ada.Strings.Maps.Character_Mapping; HowLong : Natural; BEGIN -- Cryptogram_2 Ada.Text_IO.Put(Item => "Enter a code symbol under each letter."); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); Ada.Text_IO.New_Line; -- Read code string from terminal, convert to mapping Ada.Text_IO.Get(Item => Code); Ada.Text_IO.Skip_Line; CodeMap := Ada.Strings.Maps.To_Mapping (From => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", To => Code); -- Read plain text message Ada.Text_IO.Put(Item => "Enter each character of your message."); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "No more than 60 characters, please."); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "Press RETURN after your message."); Ada.Text_IO.New_Line; -- Display scale so user knows how many characters Ada.Text_IO.Put(Item => " 1 2 3" & " 4 5 6"); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "123456789012345678901234567890" & "123456789012345678901234567890"); Ada.Text_IO.New_Line; Ada.Text_IO.Get_Line (Item => PlainText, Last => HowLong); -- Encode message using Translate function CodedText(1..HowLong) := Ada.Strings.Fixed.Translate (Source => Ada.Characters.Handling.To_Upper (Item => PlainText(1..HowLong)), Mapping => CodeMap); -- Display coded message Ada.Text_IO.Put (Item => CodedText(1..HowLong)); Ada.Text_IO.New_Line; END Cryptogram_2;
Ada.Strings.Bounded
is a generic package that provides a similar
set of operations on bounded strings, which are strings with a given maximum
length whose actual length can vary. The package is generic, with a single
parameter Max
to give the maximum length of all strings created by
a given instance of the package. For example,
MaxName: CONSTANT Positive := 30; PACKAGE Names IS NEW Ada.Strings.Bounded.Generic_Bounded_Length(Max => MaxName);provides an instance so that a string variable, say,
Name: Names.Bounded_String;can be at most 30 characters long. The package keeps track of the actual length.
Finally, Ada.Strings.Unbounded
provides similar operations for
unbounded strings, that is, strings for which no maximum length is given. The
actual length of a string object such as
VeryLongString: Ada.Strings.Unbounded.Unbounded_String;can range from 0 to
Positive'Last
.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.