1. Strings

Introduction

----------------------------------------------------------------------------
---- Charater are enumeration values and use single quotes for delimiters
-- except for non-printing characters, which have their own labels
c : character := LF;
-- a newline (line feed) character
----------------------------------------------------------------------------
---- Strings are fixed length arrays of characters with no NULL terminator
s1 : string(1..1) := (1 => LF);   -- A string containing only a line feed
s2 : string := "Jon " & '"' &
               "Maddog" & '"' & "Orwant"; -- a string containing double quotes
Put_Line("Jon 'Maddog' Orwant"); -- a string containing single quotes
------------------------------------------------------------------------------
s : string := "This is a multiline string declaration "
            & "using the concatenation operator "
            & "to append separate parts.";
------------------------------------------------------------------------------

Accessing Substrings

------------------------------------------------------------------------------
-- accessing substrings
s1 : string := data(start..ending);
-- "end" is a reserved word in Ada
s2 : string := data(start..data'Last); -- substrings accessed through array slices
-----------------------------------------------------------------------------
-- different string types
-- Bounded strings have a fixed maximum length and may vary in size up to the
-- maximum length.
package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length(80);
s : Bounded.Bounded_String := Bounded.To_Bounded_String("Hello World!");
-- Unbounded strings have no maximum size and may vary in size and content
use Ada.Strings.Unbounded;
s : Unbounded_String := To_Unbounded_String("Hello World!");
------------------------------------------------------------------------------
-- No pack/unpack equivalents exist in standard Ada libraries.
-- Examples here use a custom implementation to split an original string into
-- chunks of specified length.

-- TO BE DONE

------------------------------------------------------------------------------
-- Aggregates repeating a basic pattern s :
string := 5 * "Wow! ";
-- result: "Wow! Wow! Wow! Wow! Wow! "
------------------------------------------------------------------------------
data : string := "This is what you have";
-- forward        123456789012345678901
-- Any data element can be accessed directly through its index.
-- Substrings can be accessed directly through array slices.
first : character := data(1);                    -- 'T'
start : string := data(6..7);                    -- "is"
rest  : string := data(14..data'Last);           -- "you have"
last  : character := data(data'Last);            -- 'e'
final : string := data(data'Last - 3..data'Last);-- "have"
piece : string := data(14..16);                  -- "you"
------------------------------------------------------------------------------
-- String contents can be rearranged as long as the length of the string does
-- not change. If you want to change the length then use either Bounded or
-- Unbounded strings.
data : string := "This is what you have";
data(18..21) := "were";  -- "This is what you were"
data(1..7) := "Is this"; -- "Is this what you were"
------------------------------------------------------------------------------
use GNAT.Regpat;

-- roughly the same set of regular expressions as are available in Perl or
-- Python.

Match_Position : Natural;

Match_Position := Match(Compile("pattern"),data(data'last - 9..data'last));
if Match_Position > 0 then
   Put_Line("Pattern matches in last 10 characters");
end if;
-- substitute "at" for "is", restricted to the first five characters
declare
   Matches : Match_Array(0..4);
   Regexp  : string := "is";
   Data    : string := "This is what you have";
begin
   Match(Regexp, Data(1..5), Matches);
   if Matches(0).First > 0 then
      Data(Matches(0).First .. Matches(0).Last) := "at";
   end if;
end;
-- exchange the first and last letters in a string
data : string := "make a hat";
temp : character := data(1);
data(1) := data(data'Last);
data(data'Last) := temp; -- "take a ham"
-- extract a column with array slicing
data : string := "to be or not to be";
a1   : string := data(7..12); -- "or not"
-- forward 6, grab 2, backward 5, grab 2
a2   : string := data(7..8) & data(4..5); -- "orbe"
------------------------------------------------------------------------------

Establishing a Default Value

------------------------------------------------------------------------------
-- boolean values
-- use b if b is true, else use c
b, c : boolean;
-- [..]
a : boolean := b or else c; -- lazy evaluation

-- set x to y unless x is already true
x, y : boolean;
-- [..]
x := x or else y; -- Ada has no ternary operator
-------------------------------------
-- find the user name on Unix systems
with Gnat.Os_Lib; -- Gnat.Os_Lib is a Gnat extension

username : string := Gnat.Os_Lib.Getenv("USER");
logname  : string := Gnat.Os_Lib.Getenv("LOGNAME");
------------------------------------------------------------------------------

Exchanging Values Without Using Temporary Variables

------------------------------------------------------------------------------
-- Ada always requires temporary variables to swap values
------------------------------------------------------------------------------

Converting Between ASCII Characters and Values

------------------------------------------------------------------------------
num : integer := character'Pos(char);
char : character := character'Val(num);
------------------------------------------------------------------------------

Processing a String One Character at a Time

------------------------------------------------------------------------------
s : string := "Some value"; c : character;
for index in S'Range loop
   c := s(index);
end loop;
------------------------------------------------------------------------------

Reversing a String by Word or Character

Expanding and Compressing Tabs

Expanding Variables in User Input

Controlling Case

Interpolating Functions and Expressions Within Strings

Indenting Here Documents

Reformatting Paragraphs

Escaping Characters

Trimming Blanks from the Ends of a String

Parsing Comma-Separated Data

Soundex Matching

Program: fixstyle

Program: psgrep