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-2014, 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 for J
in From
.. Source
'Last loop
213 if Belongs
(Source
(J
), Set
, Test
) then
216 for K
in J
+ 1 .. Source
'Last loop
217 if not Belongs
(Source
(K
), Set
, Test
) then
223 -- Here if J indexes first char of token, and all chars after J
231 -- Here if no token found
239 Set
: Maps
.Character_Set
;
241 First
: out Positive;
245 for J
in Source
'Range loop
246 if Belongs
(Source
(J
), Set
, Test
) then
249 for K
in J
+ 1 .. Source
'Last loop
250 if not Belongs
(Source
(K
), Set
, Test
) then
256 -- Here if J indexes first char of token, and all chars after J
264 -- Here if no token found
266 -- RM 2005 A.4.3 (68/1)) specifies that an exception must be raised if
267 -- Source'First is not positive and is assigned to First. Formulation
268 -- is slightly different in RM 2012, but the intent seems similar, so
269 -- we check explicitly for that condition.
271 if Source
'First not in Positive then
272 raise Constraint_Error
;
275 First
:= Source
'First;
287 Going
: Direction
:= Forward
;
288 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
290 PL1
: constant Integer := Pattern
'Length - 1;
294 -- Index for start of match check. This can be negative if the pattern
295 -- length is greater than the string length, which is why this variable
296 -- is Integer instead of Natural. In this case, the search loops do not
297 -- execute at all, so this Ind value is never used.
306 if Going
= Forward
then
309 -- Unmapped forward case
311 if Mapping
'Address = Maps
.Identity
'Address then
312 for J
in 1 .. Source
'Length - PL1
loop
313 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
320 -- Mapped forward case
323 for J
in 1 .. Source
'Length - PL1
loop
326 for K
in Pattern
'Range loop
327 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
344 -- Unmapped backward case
346 Ind
:= Source
'Last - PL1
;
348 if Mapping
'Address = Maps
.Identity
'Address then
349 for J
in reverse 1 .. Source
'Length - PL1
loop
350 if Pattern
= Source
(Ind
.. Ind
+ PL1
) then
357 -- Mapped backward case
360 for J
in reverse 1 .. Source
'Length - PL1
loop
363 for K
in Pattern
'Range loop
364 if Pattern
(K
) /= Value
(Mapping
, Source
(Cur
)) then
379 -- Fall through if no match found. Note that the loops are skipped
380 -- completely in the case of the pattern being longer than the source.
388 Going
: Direction
:= Forward
;
389 Mapping
: Maps
.Character_Mapping_Function
) return Natural
391 PL1
: constant Integer := Pattern
'Length - 1;
400 -- Check for null pointer in case checks are off
402 if Mapping
= null then
403 raise Constraint_Error
;
406 -- If Pattern longer than Source it can't be found
408 if Pattern
'Length > Source
'Length then
414 if Going
= Forward
then
416 for J
in 1 .. Source
'Length - PL1
loop
419 for K
in Pattern
'Range loop
420 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
436 Ind
:= Source
'Last - PL1
;
437 for J
in reverse 1 .. Source
'Length - PL1
loop
440 for K
in Pattern
'Range loop
441 if Pattern
(K
) /= Mapping
.all (Source
(Cur
)) then
455 -- Fall through if no match found. Note that the loops are skipped
456 -- completely in the case of the pattern being longer than the source.
463 Set
: Maps
.Character_Set
;
464 Test
: Membership
:= Inside
;
465 Going
: Direction
:= Forward
) return Natural
470 if Going
= Forward
then
471 for J
in Source
'Range loop
472 if Belongs
(Source
(J
), Set
, Test
) then
480 for J
in reverse Source
'Range loop
481 if Belongs
(Source
(J
), Set
, Test
) then
487 -- Fall through if no match
496 Going
: Direction
:= Forward
;
497 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
501 -- AI05-056: If source is empty result is always zero
503 if Source
'Length = 0 then
506 elsif Going
= Forward
then
507 if From
< Source
'First then
512 Index
(Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
515 if From
> Source
'Last then
520 Index
(Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
528 Going
: Direction
:= Forward
;
529 Mapping
: Maps
.Character_Mapping_Function
) return Natural
533 -- AI05-056: If source is empty result is always zero
535 if Source
'Length = 0 then
538 elsif Going
= Forward
then
539 if From
< Source
'First then
544 (Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
547 if From
> Source
'Last then
552 (Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
558 Set
: Maps
.Character_Set
;
560 Test
: Membership
:= Inside
;
561 Going
: Direction
:= Forward
) return Natural
565 -- AI05-056 : if source is empty result is always 0.
567 if Source
'Length = 0 then
570 elsif Going
= Forward
then
571 if From
< Source
'First then
576 Index
(Source
(From
.. Source
'Last), Set
, Test
, Forward
);
579 if From
> Source
'Last then
584 Index
(Source
(Source
'First .. From
), Set
, Test
, Backward
);
588 ---------------------
589 -- Index_Non_Blank --
590 ---------------------
592 function Index_Non_Blank
594 Going
: Direction
:= Forward
) return Natural
597 if Going
= Forward
then
598 for J
in Source
'Range loop
599 if Source
(J
) /= ' ' then
604 else -- Going = Backward
605 for J
in reverse Source
'Range loop
606 if Source
(J
) /= ' ' then
612 -- Fall through if no match
617 function Index_Non_Blank
620 Going
: Direction
:= Forward
) return Natural
623 if Going
= Forward
then
624 if From
< Source
'First then
629 Index_Non_Blank
(Source
(From
.. Source
'Last), Forward
);
632 if From
> Source
'Last then
637 Index_Non_Blank
(Source
(Source
'First .. From
), Backward
);
641 end Ada
.Strings
.Search
;