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;
241 Ind : Integer; -- can be negative if Pattern'Length > Source'Length
251 if Going = Forward then
254 -- Unmapped forward case
256 if Mapping'Address = Maps.Identity'Address then
257 for J in 1 .. Source'Length - PL1 loop
258 if Pattern = Source (Ind .. Ind + PL1) then
265 -- Mapped forward case
268 for J in 1 .. Source'Length - PL1 loop
271 for K in Pattern'Range loop
272 if Pattern (K) /= Value (Mapping, Source (Cur)) then
289 -- Unmapped backward case
291 Ind := Source'Last - PL1;
293 if Mapping'Address = Maps.Identity'Address then
294 for J in reverse 1 .. Source'Length - PL1 loop
295 if Pattern = Source (Ind .. Ind + PL1) then
302 -- Mapped backward case
305 for J in reverse 1 .. Source'Length - PL1 loop
308 for K in Pattern'Range loop
309 if Pattern (K) /= Value (Mapping, Source (Cur)) then
324 -- Fall through if no match found. Note that the loops are skipped
325 -- completely in the case of the pattern being longer than the source.
333 Going : Direction := Forward;
334 Mapping : Maps.Character_Mapping_Function) return Natural
336 PL1 : constant Integer := Pattern'Length - 1;
345 -- Check for null pointer in case checks are off
347 if Mapping = null then
348 raise Constraint_Error;
351 -- If Pattern longer than Source it can't be found
353 if Pattern'Length > Source'Length then
359 if Going = Forward then
361 for J in 1 .. Source'Length - PL1 loop
364 for K in Pattern'Range loop
365 if Pattern (K) /= Mapping.all (Source (Cur)) then
381 Ind := Source'Last - PL1;
382 for J in reverse 1 .. Source'Length - PL1 loop
385 for K in Pattern'Range loop
386 if Pattern (K) /= Mapping.all (Source (Cur)) then
400 -- Fall through if no match found. Note that the loops are skipped
401 -- completely in the case of the pattern being longer than the source.
408 Set : Maps.Character_Set;
409 Test : Membership := Inside;
410 Going : Direction := Forward) return Natural
415 if Going = Forward then
416 for J in Source'Range loop
417 if Belongs (Source (J), Set, Test) then
425 for J in reverse Source'Range loop
426 if Belongs (Source (J), Set, Test) then
432 -- Fall through if no match
441 Going : Direction := Forward;
442 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
445 if Going = Forward then
446 if From < Source'First then
451 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
454 if From > Source'Last then
459 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
467 Going : Direction := Forward;
468 Mapping : Maps.Character_Mapping_Function) return Natural
471 if Going = Forward then
472 if From < Source'First then
477 (Source (From .. Source'Last), Pattern, Forward, Mapping);
480 if From > Source'Last then
485 (Source (Source'First .. From), Pattern, Backward, Mapping);
491 Set : Maps.Character_Set;
493 Test : Membership := Inside;
494 Going : Direction := Forward) return Natural
497 if Going = Forward then
498 if From < Source'First then
503 Index (Source (From .. Source'Last), Set, Test, Forward);
506 if From > Source'Last then
511 Index (Source (Source'First .. From), Set, Test, Backward);
515 ---------------------
516 -- Index_Non_Blank --
517 ---------------------
519 function Index_Non_Blank
521 Going : Direction := Forward) return Natural
524 if Going = Forward then
525 for J in Source'Range loop
526 if Source (J) /= ' ' then
531 else -- Going = Backward
532 for J in reverse Source'Range loop
533 if Source (J) /= ' ' then
539 -- Fall through if no match
544 function Index_Non_Blank
547 Going : Direction := Forward) return Natural
550 if Going = Forward then
551 if From < Source'First then
556 Index_Non_Blank (Source (From .. Source'Last), Forward);
559 if From > Source'Last then
564 Index_Non_Blank (Source (Source'First .. From), Backward);
568 end Ada.Strings.Search;