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-2009, 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
;
201 First
: out Positive;
205 for J
in Source
'Range loop
206 if Belongs
(Source
(J
), Set
, Test
) then
209 for K
in J
+ 1 .. Source
'Last loop
210 if not Belongs
(Source
(K
), Set
, Test
) then
216 -- Here if J indexes first char of token, and all chars after J
224 -- Here if no token found
226 First
:= Source
'First;
237 Going
: Direction
:= Forward
;
238 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
240 PL1
: constant Integer := Pattern
'Length - 1;
244 -- Index for start of match check. This can be negative if the pattern
245 -- length is greater than the string length, which is why this variable
246 -- is Integer instead of Natural. In this case, the search loops do not
247 -- execute at all, so this Ind value is never used.
256 if Going
= Forward
then
259 -- Unmapped forward case
261 if Mapping
'Address = Maps
.Identity
'Address then
262 for J
in 1 .. Source
'Length - PL1
loop
263 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
270 -- Mapped forward case
273 for J
in 1 .. Source
'Length - PL1
loop
276 for K
in Pattern
'Range loop
277 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
294 -- Unmapped backward case
296 Ind
:= Source
'Last - PL1
;
298 if Mapping
'Address = Maps
.Identity
'Address then
299 for J
in reverse 1 .. Source
'Length - PL1
loop
300 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
307 -- Mapped backward case
310 for J
in reverse 1 .. Source
'Length - PL1
loop
313 for K
in Pattern
'Range loop
314 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
329 -- Fall through if no match found. Note that the loops are skipped
330 -- completely in the case of the pattern being longer than the source.
338 Going
: Direction
:= Forward
;
339 Mapping
: Maps
.Character_Mapping_Function
) return Natural
341 PL1
: constant Integer := Pattern
'Length - 1;
350 -- Check for null pointer in case checks are off
352 if Mapping
= null then
353 raise Constraint_Error
;
356 -- If Pattern longer than Source it can't be found
358 if Pattern
'Length > Source
'Length then
364 if Going
= Forward
then
366 for J
in 1 .. Source
'Length - PL1
loop
369 for K
in Pattern
'Range loop
370 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
386 Ind
:= Source
'Last - PL1
;
387 for J
in reverse 1 .. Source
'Length - PL1
loop
390 for K
in Pattern
'Range loop
391 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
405 -- Fall through if no match found. Note that the loops are skipped
406 -- completely in the case of the pattern being longer than the source.
413 Set
: Maps
.Character_Set
;
414 Test
: Membership
:= Inside
;
415 Going
: Direction
:= Forward
) return Natural
420 if Going
= Forward
then
421 for J
in Source
'Range loop
422 if Belongs
(Source
(J
), Set
, Test
) then
430 for J
in reverse Source
'Range loop
431 if Belongs
(Source
(J
), Set
, Test
) then
437 -- Fall through if no match
446 Going
: Direction
:= Forward
;
447 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
450 if Going
= Forward
then
451 if From
< Source
'First then
456 Index
(Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
459 if From
> Source
'Last then
464 Index
(Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
472 Going
: Direction
:= Forward
;
473 Mapping
: Maps
.Character_Mapping_Function
) return Natural
476 if Going
= Forward
then
477 if From
< Source
'First then
482 (Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
485 if From
> Source
'Last then
490 (Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
496 Set
: Maps
.Character_Set
;
498 Test
: Membership
:= Inside
;
499 Going
: Direction
:= Forward
) return Natural
502 if Going
= Forward
then
503 if From
< Source
'First then
508 Index
(Source
(From
.. Source
'Last), Set
, Test
, Forward
);
511 if From
> Source
'Last then
516 Index
(Source
(Source
'First .. From
), Set
, Test
, Backward
);
520 ---------------------
521 -- Index_Non_Blank --
522 ---------------------
524 function Index_Non_Blank
526 Going
: Direction
:= Forward
) return Natural
529 if Going
= Forward
then
530 for J
in Source
'Range loop
531 if Source
(J
) /= ' ' then
536 else -- Going = Backward
537 for J
in reverse Source
'Range loop
538 if Source
(J
) /= ' ' then
544 -- Fall through if no match
549 function Index_Non_Blank
552 Going
: Direction
:= Forward
) return Natural
555 if Going
= Forward
then
556 if From
< Source
'First then
561 Index_Non_Blank
(Source
(From
.. Source
'Last), Forward
);
564 if From
> Source
'Last then
569 Index_Non_Blank
(Source
(Source
'First .. From
), Backward
);
573 end Ada
.Strings
.Search
;