1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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).
41 with Ada.Strings.Maps; use Ada.Strings.Maps;
43 package body Ada.Strings.Search is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
51 Set : Maps.Character_Set;
52 Test : Membership) return Boolean;
53 pragma Inline (Belongs);
54 -- Determines if the given element is in (Test = Inside) or not in
55 -- (Test = Outside) the given character set.
63 Set : Maps.Character_Set;
64 Test : Membership) return Boolean
68 return Is_In (Element, Set);
70 return not Is_In (Element, Set);
81 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
86 Mapped_Source : String (Source'Range);
89 for J in Source'Range loop
90 Mapped_Source (J) := Value (Mapping, Source (J));
100 while J <= Source'Last - (Pattern'Length - 1) loop
101 if Mapped_Source (J .. J + (Pattern'Length - 1)) = Pattern then
103 J := J + Pattern'Length;
115 Mapping : Maps.Character_Mapping_Function) return Natural
117 Mapped_Source : String (Source'Range);
126 -- We make sure Access_Check is unsuppressed so that the Mapping.all
127 -- call will generate a friendly Constraint_Error if the value for
128 -- Mapping is uninitialized (and hence null).
131 pragma Unsuppress (Access_Check);
134 for J in Source'Range loop
135 Mapped_Source (J) := Mapping.all (Source (J));
142 while J <= Source'Last - (Pattern'Length - 1) loop
143 if Mapped_Source (J .. J + (Pattern'Length - 1)) = Pattern then
145 J := J + Pattern'Length;
156 Set : Maps.Character_Set) return Natural
161 for J in Source'Range loop
162 if Is_In (Source (J), Set) then
176 Set : Maps.Character_Set;
178 First : out Positive;
182 for J in Source'Range loop
183 if Belongs (Source (J), Set, Test) then
186 for K in J + 1 .. Source'Last loop
187 if not Belongs (Source (K), Set, Test) then
193 -- Here if J indexes 1st char of token, and all chars
194 -- after J are in the token
201 -- Here if no token found
203 First := Source'First;
214 Going : Direction := Forward;
215 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
218 Mapped_Source : String (Source'Range);
226 for J in Source'Range loop
227 Mapped_Source (J) := Value (Mapping, Source (J));
232 if Going = Forward then
233 for J in 1 .. Source'Length - Pattern'Length + 1 loop
234 Cur_Index := Source'First + J - 1;
236 if Pattern = Mapped_Source
237 (Cur_Index .. Cur_Index + Pattern'Length - 1)
246 for J in reverse 1 .. Source'Length - Pattern'Length + 1 loop
247 Cur_Index := Source'First + J - 1;
249 if Pattern = Mapped_Source
250 (Cur_Index .. Cur_Index + Pattern'Length - 1)
257 -- Fall through if no match found. Note that the loops are skipped
258 -- completely in the case of the pattern being longer than the source.
266 Going : Direction := Forward;
267 Mapping : Maps.Character_Mapping_Function) return Natural
269 Mapped_Source : String (Source'Range);
277 -- We make sure Access_Check is unsuppressed so that the Mapping.all
278 -- call will generate a friendly Constraint_Error if the value for
279 -- Mapping is uninitialized (and hence null).
282 pragma Unsuppress (Access_Check);
284 for J in Source'Range loop
285 Mapped_Source (J) := Mapping.all (Source (J));
291 if Going = Forward then
292 for J in 1 .. Source'Length - Pattern'Length + 1 loop
293 Cur_Index := Source'First + J - 1;
295 if Pattern = Mapped_Source
296 (Cur_Index .. Cur_Index + Pattern'Length - 1)
305 for J in reverse 1 .. Source'Length - Pattern'Length + 1 loop
306 Cur_Index := Source'First + J - 1;
308 if Pattern = Mapped_Source
309 (Cur_Index .. Cur_Index + Pattern'Length - 1)
321 Set : Maps.Character_Set;
322 Test : Membership := Inside;
323 Going : Direction := Forward) return Natural
328 if Going = Forward then
329 for J in Source'Range loop
330 if Belongs (Source (J), Set, Test) then
338 for J in reverse Source'Range loop
339 if Belongs (Source (J), Set, Test) then
345 -- Fall through if no match
354 Going : Direction := Forward;
355 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
358 if Going = Forward then
359 if From < Source'First then
364 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
367 if From > Source'Last then
372 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
380 Going : Direction := Forward;
381 Mapping : Maps.Character_Mapping_Function) return Natural
384 if Going = Forward then
385 if From < Source'First then
390 (Source (From .. Source'Last), Pattern, Forward, Mapping);
393 if From > Source'Last then
398 (Source (Source'First .. From), Pattern, Backward, Mapping);
404 Set : Maps.Character_Set;
406 Test : Membership := Inside;
407 Going : Direction := Forward) return Natural
410 if Going = Forward then
411 if From < Source'First then
416 Index (Source (From .. Source'Last), Set, Test, Forward);
419 if From > Source'Last then
424 Index (Source (Source'First .. From), Set, Test, Backward);
428 ---------------------
429 -- Index_Non_Blank --
430 ---------------------
432 function Index_Non_Blank
434 Going : Direction := Forward) return Natural
437 if Going = Forward then
438 for J in Source'Range loop
439 if Source (J) /= ' ' then
444 else -- Going = Backward
445 for J in reverse Source'Range loop
446 if Source (J) /= ' ' then
452 -- Fall through if no match
457 function Index_Non_Blank
460 Going : Direction := Forward) return Natural
463 if Going = Forward then
464 if From < Source'First then
469 Index_Non_Blank (Source (From .. Source'Last), Forward);
472 if From > Source'Last then
477 Index_Non_Blank (Source (Source'First .. From), Backward);
481 end Ada.Strings.Search;