1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . S E A R C H --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- Note: This code is derived from the ADAR.CSH public domain Ada 83
33 -- versions of the Appendix C string handling packages (code extracted
34 -- from Ada.Strings.Fixed). A significant change is that we optimize the
35 -- case of identity mappings for Count and Index, and also Index_Non_Blank
36 -- is specialized (rather than using the general Index routine).
38 with Ada
.Strings
.Maps
; use Ada
.Strings
.Maps
;
39 with System
; use System
;
41 package body Ada
.Strings
.Search
is
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
49 Set
: Maps
.Character_Set
;
50 Test
: Membership
) return Boolean;
51 pragma Inline
(Belongs
);
52 -- Determines if the given element is in (Test = Inside) or not in
53 -- (Test = Outside) the given character set.
61 Set
: Maps
.Character_Set
;
62 Test
: Membership
) return Boolean
66 return Is_In
(Element
, Set
);
68 return not Is_In
(Element
, Set
);
79 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
81 PL1
: constant Integer := Pattern
'Length - 1;
96 if Mapping
'Address = Maps
.Identity
'Address then
97 while Ind
<= Source
'Last - PL1
loop
98 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
100 Ind
:= Ind
+ Pattern
'Length;
109 while Ind
<= Source
'Last - PL1
loop
111 for K
in Pattern
'Range loop
112 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
121 Ind
:= Ind
+ Pattern
'Length;
136 Mapping
: Maps
.Character_Mapping_Function
) return Natural
138 PL1
: constant Integer := Pattern
'Length - 1;
148 -- Check for null pointer in case checks are off
150 if Mapping
= null then
151 raise Constraint_Error
;
156 while Ind
<= Source
'Last - PL1
loop
158 for K
in Pattern
'Range loop
159 if Pattern
(K
) /= Mapping
(Source
(Cur
)) then
168 Ind
:= Ind
+ Pattern
'Length;
179 Set
: Maps
.Character_Set
) return Natural
184 for J
in Source
'Range loop
185 if Is_In
(Source
(J
), Set
) then
199 Set
: Maps
.Character_Set
;
202 First
: out Positive;
206 -- AI05-031: Raise Index error if Source non-empty and From not in range
208 if Source
'Length /= 0 and then From
not in Source
'Range then
212 -- If Source is the empty string, From may still be out of its
213 -- range. The following ensures that in all cases there is no
214 -- possible erroneous access to a non-existing character.
216 for J
in Integer'Max (From
, Source
'First) .. Source
'Last loop
217 if Belongs
(Source
(J
), Set
, Test
) then
220 for K
in J
+ 1 .. Source
'Last loop
221 if not Belongs
(Source
(K
), Set
, Test
) then
227 -- Here if J indexes first char of token, and all chars after J
235 -- Here if no token found
243 Set
: Maps
.Character_Set
;
245 First
: out Positive;
249 for J
in Source
'Range loop
250 if Belongs
(Source
(J
), Set
, Test
) then
253 for K
in J
+ 1 .. Source
'Last loop
254 if not Belongs
(Source
(K
), Set
, Test
) then
260 -- Here if J indexes first char of token, and all chars after J
268 -- Here if no token found
270 -- RM 2005 A.4.3 (68/1) specifies that an exception must be raised if
271 -- Source'First is not positive and is assigned to First. Formulation
272 -- is slightly different in RM 2012, but the intent seems similar, so
273 -- we check explicitly for that condition.
275 if Source
'First not in Positive then
276 raise Constraint_Error
;
279 First
:= Source
'First;
291 Going
: Direction
:= Forward
;
292 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
294 PL1
: constant Integer := Pattern
'Length - 1;
298 -- Index for start of match check. This can be negative if the pattern
299 -- length is greater than the string length, which is why this variable
300 -- is Integer instead of Natural. In this case, the search loops do not
301 -- execute at all, so this Ind value is never used.
310 if Going
= Forward
then
313 -- Unmapped forward case
315 if Mapping
'Address = Maps
.Identity
'Address then
316 for J
in 1 .. Source
'Length - PL1
loop
317 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
324 -- Mapped forward case
327 for J
in 1 .. Source
'Length - PL1
loop
330 for K
in Pattern
'Range loop
331 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
348 -- Unmapped backward case
350 Ind
:= Source
'Last - PL1
;
352 if Mapping
'Address = Maps
.Identity
'Address then
353 for J
in reverse 1 .. Source
'Length - PL1
loop
354 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
361 -- Mapped backward case
364 for J
in reverse 1 .. Source
'Length - PL1
loop
367 for K
in Pattern
'Range loop
368 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
383 -- Fall through if no match found. Note that the loops are skipped
384 -- completely in the case of the pattern being longer than the source.
392 Going
: Direction
:= Forward
;
393 Mapping
: Maps
.Character_Mapping_Function
) return Natural
395 PL1
: constant Integer := Pattern
'Length - 1;
404 -- Check for null pointer in case checks are off
406 if Mapping
= null then
407 raise Constraint_Error
;
410 -- If Pattern longer than Source it can't be found
412 if Pattern
'Length > Source
'Length then
418 if Going
= Forward
then
420 for J
in 1 .. Source
'Length - PL1
loop
423 for K
in Pattern
'Range loop
424 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
440 Ind
:= Source
'Last - PL1
;
441 for J
in reverse 1 .. Source
'Length - PL1
loop
444 for K
in Pattern
'Range loop
445 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
459 -- Fall through if no match found. Note that the loops are skipped
460 -- completely in the case of the pattern being longer than the source.
467 Set
: Maps
.Character_Set
;
468 Test
: Membership
:= Inside
;
469 Going
: Direction
:= Forward
) return Natural
474 if Going
= Forward
then
475 for J
in Source
'Range loop
476 if Belongs
(Source
(J
), Set
, Test
) then
484 for J
in reverse Source
'Range loop
485 if Belongs
(Source
(J
), Set
, Test
) then
491 -- Fall through if no match
500 Going
: Direction
:= Forward
;
501 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
505 -- AI05-056: If source is empty result is always zero
507 if Source
'Length = 0 then
510 elsif Going
= Forward
then
511 if From
< Source
'First then
516 Index
(Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
519 if From
> Source
'Last then
524 Index
(Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
532 Going
: Direction
:= Forward
;
533 Mapping
: Maps
.Character_Mapping_Function
) return Natural
537 -- AI05-056: If source is empty result is always zero
539 if Source
'Length = 0 then
542 elsif Going
= Forward
then
543 if From
< Source
'First then
548 (Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
551 if From
> Source
'Last then
556 (Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
562 Set
: Maps
.Character_Set
;
564 Test
: Membership
:= Inside
;
565 Going
: Direction
:= Forward
) return Natural
569 -- AI05-056 : if source is empty result is always 0.
571 if Source
'Length = 0 then
574 elsif Going
= Forward
then
575 if From
< Source
'First then
580 Index
(Source
(From
.. Source
'Last), Set
, Test
, Forward
);
583 if From
> Source
'Last then
588 Index
(Source
(Source
'First .. From
), Set
, Test
, Backward
);
592 ---------------------
593 -- Index_Non_Blank --
594 ---------------------
596 function Index_Non_Blank
598 Going
: Direction
:= Forward
) return Natural
601 if Going
= Forward
then
602 for J
in Source
'Range loop
603 if Source
(J
) /= ' ' then
608 else -- Going = Backward
609 for J
in reverse Source
'Range loop
610 if Source
(J
) /= ' ' then
616 -- Fall through if no match
621 function Index_Non_Blank
624 Going
: Direction
:= Forward
) return Natural
627 if Going
= Forward
then
628 if From
< Source
'First then
633 Index_Non_Blank
(Source
(From
.. Source
'Last), Forward
);
636 if From
> Source
'Last then
641 Index_Non_Blank
(Source
(Source
'First .. From
), Backward
);
645 end Ada
.Strings
.Search
;