with Ada.Text_IO;
with Ada.Text_IO.Unbounded_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Containers.Vectors; use Ada.Containers;
procedure Quotes is
package IO renames Ada.Text_IO;
package SUIO renames Ada.Text_IO.Unbounded_IO;
package Quote_Container is new Vectors (Natural, Unbounded_String);
use Quote_Container;
Quotes : Vector;
Input : IO.File_Type;
Needle : Unbounded_String;
Pos : Cursor;
begin
IO.Open (File => Input,
Mode => IO.In_File,
Name => "quotes.txt");
while not IO.End_Of_File (File => Input) loop
Quotes.Append (New_Item => SUIO.Get_Line (File => Input));
end loop;
IO.Close (Input);
-- First search. This will fail because we have no FooBar quote.
Needle := To_Unbounded_String ("FooBar");
Pos := Quotes.Find (Item => Needle);
if Pos = No_Element then
IO.Put_Line ("No_Element");
else
IO.Put_Line (To_Index (Position => Pos)'Img);
end if;
-- Second search. This will succeed, finding the quote at index 7.
Needle := To_Unbounded_String
("Most days I wake up thinking I'm the luckiest bastard alive.");
Pos := Quotes.Find (Item => Needle);
if Pos = No_Element then
IO.Put_Line ("No_Element");
else
IO.Put_Line (To_Index (Position => Pos)'Img);
end if;
-- Third search. This will fail, because we start the search at index 8.
Needle := To_Unbounded_String
("Most days I wake up thinking I'm the luckiest bastard alive.");
Pos := Quotes.To_Cursor (Index => 8);
Pos := Quotes.Find (Item => Needle,
Position => Pos);
if Pos = No_Element then
IO.Put_Line ("No_Element");
else
IO.Put_Line (To_Index (Position => Pos)'Img);
end if;
end Quotes;
Go back