-- Unmapped case
if Mapping'Address = Maps.Identity'Address then
- Ind := Source'First;
- while Ind <= Source'Length - PL1 loop
+ while Ind <= Source'Last - PL1 loop
if Pattern = Source (Ind .. Ind + PL1) then
Num := Num + 1;
Ind := Ind + Pattern'Length;
-- Mapped case
else
- Ind := Source'First;
- while Ind <= Source'Length - PL1 loop
+ while Ind <= Source'Last - PL1 loop
Cur := Ind;
for K in Pattern'Range loop
if Pattern (K) /= Value (Mapping, Source (Cur)) then
Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
is
PL1 : constant Integer := Pattern'Length - 1;
- Ind : Natural;
Cur : Natural;
+ Ind : Integer;
+ -- Index for start of match check. This can be negative if the pattern
+ -- length is greater than the string length, which is why this variable
+ -- is Integer instead of Natural. In this case, the search loops do not
+ -- execute at all, so this Ind value is never used.
+
begin
if Pattern = "" then
raise Pattern_Error;
raise Constraint_Error;
end if;
+ -- If Pattern longer than Source it can't be found
+
+ if Pattern'Length > Source'Length then
+ return 0;
+ end if;
+
-- Forwards case
if Going = Forward then