8. Text Strings - JulTob/Ada GitHub Wiki

Fixed Strings

Strings are first-class types in Ada.

They are defined as an array of characters.

The length is fixed when built. Specific characters might change.

Not the best option to process text, but the simplest solution for embedded machines.

Definition

Strings are internally defined as such:

Type String is array (Positive range <>) of Character;
Type      wide_string is array(positive range <>) of      wide_character;
Type wide_wide_string is array(positive range <>) of wide_wide_character;

<> indicates length on runtime. As the variable of the array is CREATED, declared, it sets it's size. And never changes size again.

Initialization

-- Implicit length initiation
Str: constant String:= "Hello World";
  --  Constant makes the string unmutable.

c: Character;
s1: String:= “DarthVader”;         
  --  Implicit len=10 Initiation.
s2: String:= “Yoda”;         
  --  Implicit len=4 Initiation.
s3: string(1..4); 
  -- Explicit len=4  Declaration.
s4: string(1..4):= "Solo"
  -- Explicit len=4  Initiation.
s4:= "Chewaca"; 
  -- Ilegal, too long
s5 : String; 
  -- Implicit Declaration.
  -- Ilegal


-- Can be Initializaed
s3:= "Luke"

--  Compared (compares content, not address  -- <3
if s2 = s4 then null;

-- Sliced (and compared
if s1(1...5) = "Darth" then null;

-- They have attributes
r : string = "DarthVader";

put_line(r);        --> "DarthVader"
put_line(r(1..5));  --> "Darth"
put_line(r(6..10))  --> "Vader"
put_line(r(r'first));  --> "D"
put_line(r(r'last));   --> "r"
put     (r'length);    --> 10


c:= s1(2); -- Returns the character at 2 to c
s1(3):= c; -- Changes the character at 3 to c

StrBits: constant Natural := Str'Size;
StrChars: constant Natural := Str'Length;



Latin_1_Str    : String           := "Hello World";
UCS_16_Str     : Wide_String      := "Hello World";
Unicode_Str    : Wide_Wide_String := "Hello World";
Latin_1_Length : constant Natural := Latin_1_Str'Length;
UCS_16_Length  : constant Natural := UCS_16_Str'Length;
Unicode_Length : constant Natural := Unicode_Str'Length;
with Ada.Strings;
with Ada.Strings.fixed;

with Ada.Text_IO;

procedure show_strings is
  fixed : String := "Hello";
  fixed2: String := "Folks";
  bound : String(1..250);
begin 
 Ada.strings.fixed.
  Move(fixed, fixed2);
  -- Moves the smaller into the bigest

  fixed2 := fixed;
  -- Same size, works!

  Ada.Text_IO.Put_Line(Fixed);

  Ada.strings.fixed.
   Move(fixed, bound);

Quantified expressions

Bool := (for all Char of Str => Char /= 'e');          

Bool := (for some Char of Str => Char = 'e');

Count iterations

To count iterations of substrings:

Ada.Strings.Fixed.
   Count (Source  => "the three truths",
          Pattern => "th"));
   ---> 3

For other needs, we have two other string types:
Bounded Length Strings and Unbounded Length Strings

Embeded Sequences

Unlike C++ and Java, Ada does not offer escape sequences such as \n. Instead, explicit values from the ASCII package must be concatenated (via the concatenation operator, &).

End of Line

My_String : String:= "This is a line with an end of line" & ASCII.LF;

Text IO: Bounded IO

La entrada/salida de ristras de tamaño limitado se realiza usando la librería Ada.Text_IO.Bounded_IO. Es una librería genérica con un parámetro llamado Bounded que representa una instancia del paquete Ada.Strings.Bounded.Generic_Bounded_Length que especifica el tamaño máximo de las ristras.

with Ada.Strings.Bounded, Ada.Text_IO.Bounded_IO;
...
package String20 is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
package String20_IO is new Ada.Text_IO.Bounded_IO (String20);

-- function Get_Line[(File : in File_Type)] return Bounded.Bounded_String;
-- procedure Get_Line ([File : in File_Type; ]Item : out Bounded.Bounded_String);

with Ada.Strings.Bounded, Ada.Text_IO.Bounded_IO;
...
package String20 is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
package String20_IO is new Ada.Text_IO.Bounded_IO (String20);
...
S1, S2: String20.Bounded_String;
...
S1 := String20_IO.Get_Line;
String20_IO.Get_Line (S2);

------
with Ada.Strings.Bounded, Ada.Text_IO, Ada.Text_IO.Bounded_IO;
...
package String20 is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
package String20_IO is new Ada.Text_IO.Bounded_IO (String20);
...
S1, S2: String20.Bounded_String;
F     : Text_IO.File_Type;
...
S1 := String20_IO.Get_Line (F);  
String20_IO.Get_Line (F, S2);
---------
-- procedure Put([File : in File_Type;] Item : in Bounded.Bounded_String);
with Ada.Strings.Bounded, Ada.Text_IO.Bounded_IO;
...
package String20 is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
package String20_IO is new Ada.Text_IO.Bounded_IO (String20);
...
S1: String20.Bounded_String;
...
String20_IO.Put (S1);
----
with Ada.Strings.Bounded, Ada.Text_IO, Ada.Text_IO.Bounded_IO;
...
package String20 is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
package String20_IO is new Ada.Text_IO.Bounded_IO (String20);
...
S1: String20.Bounded_String;
F     : Text_IO.File_Type;
...
String20_IO.Put (F, S1);
---------------------------------
-- procedure Put_Line ([File : in File_Type; ]Item : in Bounded.Bounded_String);

with Ada.Strings.Bounded, Ada.Text_IO.Bounded_IO;
...
package String20 is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
package String20_IO is new Ada.Text_IO.Bounded_IO (String20);
...
S1: String20.Bounded_String;
...
String20_IO.Put_Line (S1);
------
with Ada.Strings.Bounded, Ada.Text_IO, Ada.Text_IO.Bounded_IO;
...
package String20 is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
package String20_IO is new Ada.Text_IO.Bounded_IO (String20);
...
S1: String20.Bounded_String;
F     : Text_IO.File_Type;
...
String20_IO.Put_Line (F, S1);

Unbounded Strings

with Ada.Strings.Bounded;   --  Librería genérica de ristras de tamaño limitado
...
package Str10 is new Ada.Strings.Bounded.Generic_Bounded_Length(10); use Str10;
...
SL1, SL2: Str10.Bounded_String;
-- procedure Replace_Element (Source : in out Ristra;
--                           Index  : in Positive;
--                           By     : in Character)

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
...

Din1 : Unbounded_String;
...

Din1 := To_Unbounded_String ("pedro");
Replace_Element (Din1, 1, 'P');  --  Din1 = "Pedro" (P mayúscula)
-- function Replace_Slice (Source : in Bounded_String;
--                        Low    : in Positive;
--                        High   : in Natural;
--                        By     : in String;
--                        Drop   : in Truncation
--                        return Bounded_String;

-- procedure Replace_Slice (Source  : in out Bounded_String;
--                         Low     : in Positive;
--                         High    : in Natural;
--                         By      : in String;
--                         Drop    : in Truncation := Error);

with Ada.Strings;           use Ada.Strings
with Ada.Strings.Bounded;   --  Librería de ristras de tamaño limitado
...
package Str100 is new Ada.Strings.Bounded.Generic_Bounded_Length(100); use Str100;  
...
SL: Str100.Bounded_String;
...

SL := To_Bounded_String ("Pedro corre");
SL := Replace_Slice (SL , 1, 5, "Jesús");  --  SL = "Jesús corre"
Replace_Slice (SL , 1, 5, "Santiago");     --  SL = "Santiago corre"

La función Tail devuelve la ristra resultante de tomar los Count últimos caracteres de Source; si Source tiene menos de Count caracteres, Pad indica el carácter de relleno, si Count es mayor que el tamaño límite de las ristras, Drop indica qué hacer. El procedimiento realiza la acción equivalente modificando Source.

-- function Tail (Source : in Bounded_String;
--               Count  : in Natural;
--               Pad    : in Character := Space;
--               Drop   : in Truncation := Error)
--               return Bounded_String;

-- procedure Tail (Source  : in out Bounded_String;
--                Count   : in Natural;
--                Justify : in Alignment := Left;
--                Pad     : in Character := Space;
--                Drop    : in Truncation := Error);

with Ada.Strings;           use Ada.Strings
with Ada.Strings.Bounded;   --  Librería de ristras de tamaño limitado
...
package Str100 is new Ada.Strings.Bounded.Generic_Bounded_Length(100); use Str100;  
...
SL: Str100.Bounded_String;
...

SL := To_Bounded_String ("Universidad de Las Palmas de Gran Canaria");
SL := Tail (SL, 10); --  SL = "an Canaria"
SL := To_Bounded_String ("Casa");
SL := Tail (SL, 10); --  SL = "Casa      "

Head

-- function Head (Source : in Bounded_String;
--               Count  : in Natural;
--               Pad    : in Character := Space;
--               Drop   : in Truncation := Error)
--               return Bounded_String;

-- procedure Head (Source  : in out Bounded_String;
--                Count   : in Natural;
--                Justify : in Alignment := Left;
--                Pad     : in Character := Space;
--                Drop    : in Truncation := Error);

with Ada.Strings;           use Ada.Strings
with Ada.Strings.Bounded;   --  Librería de ristras de tamaño limitado
...
package Str100 is new Ada.Strings.Bounded.Generic_Bounded_Length(100); use Str100;  
...
SL: Str100.Bounded_String;
...

SL := To_Bounded_String ("Universidad de Las Palmas de Gran Canaria");
SL := Head (SL, 10); --  SL = "Universida"
SL := To_Bounded_String ("Casa");
SL := Head (SL, 10); --  SL = "Casa      "
-- function Trim (Source : in Ristra;
--               Side   : in Trim_End)
--               return Ristra;

-- procedure Trim (Source : in out Ristra;
--                Side   : in Trim_End);


-- function Trim (Source : in Ristra;
--               Left   : in Maps.Character_Set;
--               Right  : in Maps.Character_Set)
--               return Ristra;

-- procedure Trim (Source : in out Ristra;
--                Left   : in Maps.Character_Set;
--                Right  : in Maps.Character_Set);

with Ada.Strings;           use Ada.Strings
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
-- En Strings.Maps se encuentra la función To_Set() 
with Ada.Strings.Maps;      use Ada.Strings.Maps;
...

Din1 : Unbounded_String;
...

Din1 := To_Unbounded_String ("   pedro   ");
Din1 := Trim (Din1, Left);  --  Din1 = "pedro   "
Din1 := To_Unbounded_String ("   pedro   ");
Trim (Din1, Both);  --  Din1 = "pedro"
Din1 := To_Unbounded_String ("agua");
Din1 := Trim (Din1, To_Set("aeiou"),  To_Set("aeiou"));  --  Din1 = "g"