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;
40 package body Ada.Strings.Search is
42 -----------------------
43 -- Local Subprograms --
44 -----------------------
48 Set : Maps.Character_Set;
49 Test : Membership) return Boolean;
50 pragma Inline (Belongs);
51 -- Determines if the given element is in (Test = Inside) or not in
52 -- (Test = Outside) the given character set.
60 Set : Maps.Character_Set;
61 Test : Membership) return Boolean
65 return Is_In (Element, Set);
67 return not Is_In (Element, Set);
78 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
83 Mapped_Source : String (Source'Range);
86 for J in Source'Range loop
87 Mapped_Source (J) := Value (Mapping, Source (J));
97 while J <= Source'Last - (Pattern'Length - 1) loop
98 if Mapped_Source (J .. J + (Pattern'Length - 1)) = Pattern then
100 J := J + Pattern'Length;
112 Mapping : Maps.Character_Mapping_Function) return Natural
114 Mapped_Source : String (Source'Range);
123 -- We make sure Access_Check is unsuppressed so that the Mapping.all
124 -- call will generate a friendly Constraint_Error if the value for
125 -- Mapping is uninitialized (and hence null).
128 pragma Unsuppress (Access_Check);
131 for J in Source'Range loop
132 Mapped_Source (J) := Mapping.all (Source (J));
139 while J <= Source'Last - (Pattern'Length - 1) loop
140 if Mapped_Source (J .. J + (Pattern'Length - 1)) = Pattern then
142 J := J + Pattern'Length;
153 Set : Maps.Character_Set) return Natural
158 for J in Source'Range loop
159 if Is_In (Source (J), Set) then
173 Set : Maps.Character_Set;
175 First : out Positive;
179 for J in Source'Range loop
180 if Belongs (Source (J), Set, Test) then
183 for K in J + 1 .. Source'Last loop
184 if not Belongs (Source (K), Set, Test) then
190 -- Here if J indexes 1st char of token, and all chars
191 -- after J are in the token
198 -- Here if no token found
200 First := Source'First;
211 Going : Direction := Forward;
212 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
215 Mapped_Source : String (Source'Range);
222 for J in Source'Range loop
223 Mapped_Source (J) := Value (Mapping, Source (J));
228 if Going = Forward then
229 for J in 1 .. Source'Length - Pattern'Length + 1 loop
230 Cur_Index := Source'First + J - 1;
232 if Pattern = Mapped_Source
233 (Cur_Index .. Cur_Index + Pattern'Length - 1)
242 for J in reverse 1 .. Source'Length - Pattern'Length + 1 loop
243 Cur_Index := Source'First + J - 1;
245 if Pattern = Mapped_Source
246 (Cur_Index .. Cur_Index + Pattern'Length - 1)
253 -- Fall through if no match found. Note that the loops are skipped
254 -- completely in the case of the pattern being longer than the source.
262 Going : Direction := Forward;
263 Mapping : Maps.Character_Mapping_Function) return Natural
265 Mapped_Source : String (Source'Range);
273 -- We make sure Access_Check is unsuppressed so that the Mapping.all
274 -- call will generate a friendly Constraint_Error if the value for
275 -- Mapping is uninitialized (and hence null).
278 pragma Unsuppress (Access_Check);
280 for J in Source'Range loop
281 Mapped_Source (J) := Mapping.all (Source (J));
287 if Going = Forward then
288 for J in 1 .. Source'Length - Pattern'Length + 1 loop
289 Cur_Index := Source'First + J - 1;
291 if Pattern = Mapped_Source
292 (Cur_Index .. Cur_Index + Pattern'Length - 1)
301 for J in reverse 1 .. Source'Length - Pattern'Length + 1 loop
302 Cur_Index := Source'First + J - 1;
304 if Pattern = Mapped_Source
305 (Cur_Index .. Cur_Index + Pattern'Length - 1)
317 Set : Maps.Character_Set;
318 Test : Membership := Inside;
319 Going : Direction := Forward) return Natural
324 if Going = Forward then
325 for J in Source'Range loop
326 if Belongs (Source (J), Set, Test) then
334 for J in reverse Source'Range loop
335 if Belongs (Source (J), Set, Test) then
341 -- Fall through if no match
350 Going : Direction := Forward;
351 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
354 if Going = Forward then
355 if From < Source'First then
360 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
363 if From > Source'Last then
368 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
376 Going : Direction := Forward;
377 Mapping : Maps.Character_Mapping_Function) return Natural
380 if Going = Forward then
381 if From < Source'First then
386 (Source (From .. Source'Last), Pattern, Forward, Mapping);
389 if From > Source'Last then
394 (Source (Source'First .. From), Pattern, Backward, Mapping);
400 Set : Maps.Character_Set;
402 Test : Membership := Inside;
403 Going : Direction := Forward) return Natural
406 if Going = Forward then
407 if From < Source'First then
412 Index (Source (From .. Source'Last), Set, Test, Forward);
415 if From > Source'Last then
420 Index (Source (Source'First .. From), Set, Test, Backward);
424 ---------------------
425 -- Index_Non_Blank --
426 ---------------------
428 function Index_Non_Blank
430 Going : Direction := Forward) return Natural
433 if Going = Forward then
434 for J in Source'Range loop
435 if Source (J) /= ' ' then
440 else -- Going = Backward
441 for J in reverse Source'Range loop
442 if Source (J) /= ' ' then
448 -- Fall through if no match
453 function Index_Non_Blank
456 Going : Direction := Forward) return Natural
459 if Going = Forward then
460 if From < Source'First then
465 Index_Non_Blank (Source (From .. Source'Last), Forward);
468 if From > Source'Last then
473 Index_Non_Blank (Source (Source'First .. From), Backward);
477 end Ada.Strings.Search;