Starting with Ada String Packages

Ehud Lamm

Ada provides several packages to help you manipulate strings. This is a very short and incomplete introduction to using them.

 

Overview

Ada provides three types of strings: fixed, bounded and unbounded. Fixed strings are strings that have a known fixed length, exactly the built-in String type. Bounded strings, resemble the Pascal strings that have a maximum length, but can change the length up to this bound. Unbounded strings, can change their length so far a memory permits.

For example consider reading words from a file.

You can decide that all words will be of length 10 and declare a string type

Subtype Word is String(1..10);

However doing so means that a word of less than 10 letters, will only occupy part of the string. This means that when you will operate on this word you will have to explicitly remember the length. Think what will happen when you will concatenate "Ehud " and "Lamm " - the result will include the balnks!

Using a bounded string, you can declare the maximum length of a word to be 10, and the exact length will be stored with the string value.

Now consider a program that must be able to read strings of unknown length. What to do? Right: use the unbounded strings package.

 

What Packages Are There?

We are going to discuss several packages. They are linked below, so you can browse their specs.

 Ada.Character.Handling (this package support basic character handling operations like Is_Lower)

 Ada.Strings (this is the root strings package. The other packages are child units)

 Ada.Strings.Fixed

 Ada.Strings.Bounded

 Ada.Strings.Unbounded

 Ada.Strings.Maps

 

Simple Example

The following is an ultra-simple example code, that you can cut&paste to try out.

with Ada.Strings; use Ada.Strings;

with Ada.Strings.Fixed; use Ada.Strings.Fixed;

with Ada.Strings.Maps; use Ada.Strings.Maps;

with Ada.Characters.Handling; use Ada.Characters.Handling;

 

type Freq is digits 10 range 0.0..1.0;

type Distribution is array(character) of Freq;

 

Distr:Distribution;

Line:String(1..80); -- Lines are of the fromat <letter> <freq> for example A 0.003

 

Distr(To_upper(Line(1))):=Freq'Value(Line(

Index(Line,To_Set("0123456789"),Going=>Forward)..

Index(Line,To_Set("0123456789"),Going=>Backward)));

 

The Index function is used to find the positions inside Line, which mark the start and ending of the frequency.

 

A Few Useful Operations

Some string and character operations, which you may find useful:

1. Is_Lower, Is_Upper, To_Lower, To_Upper

 

These come from the Ada.Character.Handling package

2. The & operator

 

This operator, which is sometimes forgotten, is used to concatenate one dimensional arrays, such as strings.

Name := First_Name & " " &Last_Name;

put("X= " & Integer'Image(X));

 

3. Index and Index_Non_Blank

 

These are the most basic "parsing" tools. You may also enjoy using Find_Token, which can save you the trouble of ding two Index operations like the example above. It gives you the start and end indices together.

 

4. To_Set

Many of the routines use a parameter of type Character_Set, to define the class of characters to look for. In the example above, Index is given a set containing the digit characters. To transform a string value to a Character_Set type, you use the To_Set function.

 

 

This Looks So Complicated

Actually it is pretty simple. Just look at the example above.

To make life easier - start by using only the fixed length string package. "With" all the packages shown in the example, and try them out.

The only complicated thing to remember is that the functions that need criteria to decide which character should be processed use the Character_Set type.

 

Exercises

1. A string contains the following markings <toupper>text</toupper>.

Write the code to translate the text to upper case. You can assume that the tags appear exactly once inside the string.

2. A time value is given by a string like "1245" write code to change such a value to "12:45"

Hint: Use the Translate routine.

3. Write code to count the number of time the string "string" appears inside a string.

4. Now write the code to store this entire web page as a String, and invoke your code from the last exercise on it.

 

What Next?

Basically we are done here.

If you want to learn some more about the Ada string packages, I strongly suggest you read the relevant part of the "Ada 95 Rationale" and browse the package mentioned above.

 

Enjoy!