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-2005, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- Note: This code is derived from the ADAR.CSH public domain Ada 83
35 -- versions of the Appendix C string handling packages (code extracted
36 -- from Ada.Strings.Fixed). A significant change is that we optimize the
37 -- case of identity mappings for Count and Index, and also Index_Non_Blank
38 -- is specialized (rather than using the general Index routine).
40 with Ada
.Strings
.Maps
; use Ada
.Strings
.Maps
;
42 package body Ada
.Strings
.Search
is
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
50 Set
: Maps
.Character_Set
;
51 Test
: Membership
) return Boolean;
52 pragma Inline
(Belongs
);
53 -- Determines if the given element is in (Test = Inside) or not in
54 -- (Test = Outside) the given character set.
62 Set
: Maps
.Character_Set
;
63 Test
: Membership
) return Boolean
67 return Is_In
(Element
, Set
);
69 return not Is_In
(Element
, Set
);
80 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
85 Mapped_Source
: String (Source
'Range);
88 for J
in Source
'Range loop
89 Mapped_Source
(J
) := Value
(Mapping
, Source
(J
));
99 while J
<= Source
'Last - (Pattern
'Length - 1) loop
100 if Mapped_Source
(J
.. J
+ (Pattern
'Length - 1)) = Pattern
then
102 J
:= J
+ Pattern
'Length;
114 Mapping
: Maps
.Character_Mapping_Function
) return Natural
116 Mapped_Source
: String (Source
'Range);
125 -- We make sure Access_Check is unsuppressed so that the Mapping.all
126 -- call will generate a friendly Constraint_Error if the value for
127 -- Mapping is uninitialized (and hence null).
130 pragma Unsuppress
(Access_Check
);
133 for J
in Source
'Range loop
134 Mapped_Source
(J
) := Mapping
.all (Source
(J
));
141 while J
<= Source
'Last - (Pattern
'Length - 1) loop
142 if Mapped_Source
(J
.. J
+ (Pattern
'Length - 1)) = Pattern
then
144 J
:= J
+ Pattern
'Length;
155 Set
: Maps
.Character_Set
) return Natural
160 for J
in Source
'Range loop
161 if Is_In
(Source
(J
), Set
) then
175 Set
: Maps
.Character_Set
;
177 First
: out Positive;
181 for J
in Source
'Range loop
182 if Belongs
(Source
(J
), Set
, Test
) then
185 for K
in J
+ 1 .. Source
'Last loop
186 if not Belongs
(Source
(K
), Set
, Test
) then
192 -- Here if J indexes 1st char of token, and all chars
193 -- after J are in the token
200 -- Here if no token found
202 First
:= Source
'First;
213 Going
: Direction
:= Forward
;
214 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
217 Mapped_Source
: String (Source
'Range);
224 for J
in Source
'Range loop
225 Mapped_Source
(J
) := Value
(Mapping
, Source
(J
));
230 if Going
= Forward
then
231 for J
in 1 .. Source
'Length - Pattern
'Length + 1 loop
232 Cur_Index
:= Source
'First + J
- 1;
234 if Pattern
= Mapped_Source
235 (Cur_Index
.. Cur_Index
+ Pattern
'Length - 1)
244 for J
in reverse 1 .. Source
'Length - Pattern
'Length + 1 loop
245 Cur_Index
:= Source
'First + J
- 1;
247 if Pattern
= Mapped_Source
248 (Cur_Index
.. Cur_Index
+ Pattern
'Length - 1)
255 -- Fall through if no match found. Note that the loops are skipped
256 -- completely in the case of the pattern being longer than the source.
264 Going
: Direction
:= Forward
;
265 Mapping
: Maps
.Character_Mapping_Function
) return Natural
267 Mapped_Source
: String (Source
'Range);
275 -- We make sure Access_Check is unsuppressed so that the Mapping.all
276 -- call will generate a friendly Constraint_Error if the value for
277 -- Mapping is uninitialized (and hence null).
280 pragma Unsuppress
(Access_Check
);
282 for J
in Source
'Range loop
283 Mapped_Source
(J
) := Mapping
.all (Source
(J
));
289 if Going
= Forward
then
290 for J
in 1 .. Source
'Length - Pattern
'Length + 1 loop
291 Cur_Index
:= Source
'First + J
- 1;
293 if Pattern
= Mapped_Source
294 (Cur_Index
.. Cur_Index
+ Pattern
'Length - 1)
303 for J
in reverse 1 .. Source
'Length - Pattern
'Length + 1 loop
304 Cur_Index
:= Source
'First + J
- 1;
306 if Pattern
= Mapped_Source
307 (Cur_Index
.. Cur_Index
+ Pattern
'Length - 1)
319 Set
: Maps
.Character_Set
;
320 Test
: Membership
:= Inside
;
321 Going
: Direction
:= Forward
) return Natural
326 if Going
= Forward
then
327 for J
in Source
'Range loop
328 if Belongs
(Source
(J
), Set
, Test
) then
336 for J
in reverse Source
'Range loop
337 if Belongs
(Source
(J
), Set
, Test
) then
343 -- Fall through if no match
352 Going
: Direction
:= Forward
;
353 Mapping
: Maps
.Character_Mapping
:= Maps
.Identity
) return Natural
356 if Going
= Forward
then
357 if From
< Source
'First then
362 Index
(Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
365 if From
> Source
'Last then
370 Index
(Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
378 Going
: Direction
:= Forward
;
379 Mapping
: Maps
.Character_Mapping_Function
) return Natural
382 if Going
= Forward
then
383 if From
< Source
'First then
388 (Source
(From
.. Source
'Last), Pattern
, Forward
, Mapping
);
391 if From
> Source
'Last then
396 (Source
(Source
'First .. From
), Pattern
, Backward
, Mapping
);
402 Set
: Maps
.Character_Set
;
404 Test
: Membership
:= Inside
;
405 Going
: Direction
:= Forward
) return Natural
408 if Going
= Forward
then
409 if From
< Source
'First then
414 Index
(Source
(From
.. Source
'Last), Set
, Test
, Forward
);
417 if From
> Source
'Last then
422 Index
(Source
(Source
'First .. From
), Set
, Test
, Backward
);
426 ---------------------
427 -- Index_Non_Blank --
428 ---------------------
430 function Index_Non_Blank
432 Going
: Direction
:= Forward
) return Natural
435 if Going
= Forward
then
436 for J
in Source
'Range loop
437 if Source
(J
) /= ' ' then
442 else -- Going = Backward
443 for J
in reverse Source
'Range loop
444 if Source
(J
) /= ' ' then
450 -- Fall through if no match
455 function Index_Non_Blank
458 Going
: Direction
:= Forward
) return Natural
461 if Going
= Forward
then
462 if From
< Source
'First then
467 Index_Non_Blank
(Source
(From
.. Source
'Last), Forward
);
470 if From
> Source
'Last then
475 Index_Non_Blank
(Source
(Source
'First .. From
), Backward
);
479 end Ada
.Strings
.Search
;