Well, here's a possible solution:
main.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure Main is
A : constant String := "John Johnson";
B : constant String := "son";
begin
if Tail (A, B'Length) = B then
Put_Line ("Yay!");
end if;
end Main;
output
$ ./main
Yay!
UPDATE (2)
Another update (thanks @Brian Drummond for the comment; comment disappeared though), again using Tail
. This is now almost identical to @Zerte's answer, except for the dependency on Ada.Strings.Fixed
:
main.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Assertions; use Ada.Assertions;
procedure Main is
function Ends_With (Source, Pattern : String) return Boolean is
begin
return Source'Length >= Pattern'Length and then
Tail (Source, Pattern'Length) = Pattern;
end Ends_With;
begin
Assert (Ends_With ("John Johnson", "son") = True);
Assert (Ends_With ("hi", "longer than hi") = False);
Assert (Ends_With ("" , "" ) = True);
Assert (Ends_With (" " , "" ) = True);
Assert (Ends_With ("" , " " ) = False);
Assert (Ends_With (" " , " " ) = True);
Assert (Ends_With ("n ", "n ") = True);
Assert (Ends_With (" n", "n" ) = True);
Assert (Ends_With ("n" , " n") = False);
Assert (Ends_With (" n", " n") = True);
Put_Line ("All OK.");
end Main;
output
$ ./main
All OK.
Ends_With
. WhileAda.Strings.Fixed.Tail
works in the expected cases, we need to also know whatEnds_With
should do in the less common cases (A'Length < B'Length or B = ""
). – Jeffrey R. Carter May 29 '20 at 10:52