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
98 while Ind <= Source'Length - PL1 loop
99 if Pattern = Source (Ind .. Ind + PL1) then
101 Ind := Ind + Pattern'Length;
111 while Ind <= Source'Length - PL1 loop
113 for K in Pattern'Range loop
114 if Pattern (K) /= Value (Mapping, Source (Cur)) then
123 Ind := Ind + Pattern'Length;
138 Mapping : Maps.Character_Mapping_Function) return Natural
140 PL1 : constant Integer := Pattern'Length - 1;
150 -- Check for null pointer in case checks are off
152 if Mapping = null then
153 raise Constraint_Error;
158 while Ind <= Source'Last - PL1 loop
160 for K in Pattern'Range loop
161 if Pattern (K) /= Mapping (Source (Cur)) then
170 Ind := Ind + Pattern'Length;
181 Set : Maps.Character_Set) return Natural
186 for J in Source'Range loop
187 if Is_In (Source (J), Set) then
201 Set : Maps.Character_Set;
203 First : out Positive;
207 for J in Source'Range loop
208 if Belongs (Source (J), Set, Test) then
211 for K in J + 1 .. Source'Last loop
212 if not Belongs (Source (K), Set, Test) then
218 -- Here if J indexes first char of token, and all chars after J
226 -- Here if no token found
228 First := Source'First;
239 Going : Direction := Forward;
240 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
242 PL1 : constant Integer := Pattern'Length - 1;
253 if Going = Forward then
256 -- Unmapped forward case
258 if Mapping'Address = Maps.Identity'Address then
259 for J in 1 .. Source'Length - PL1 loop
260 if Pattern = Source (Ind .. Ind + PL1) then
267 -- Mapped forward case
270 for J in 1 .. Source'Length - PL1 loop
273 for K in Pattern'Range loop
274 if Pattern (K) /= Value (Mapping, Source (Cur)) then
291 -- Unmapped backward case
293 Ind := Source'Last - PL1;
295 if Mapping'Address = Maps.Identity'Address then
296 for J in reverse 1 .. Source'Length - PL1 loop
297 if Pattern = Source (Ind .. Ind + PL1) then
304 -- Mapped backward case
307 for J in reverse 1 .. Source'Length - PL1 loop
310 for K in Pattern'Range loop
311 if Pattern (K) /= Value (Mapping, Source (Cur)) then
326 -- Fall through if no match found. Note that the loops are skipped
327 -- completely in the case of the pattern being longer than the source.
335 Going : Direction := Forward;
336 Mapping : Maps.Character_Mapping_Function) return Natural
338 PL1 : constant Integer := Pattern'Length - 1;
347 -- Check for null pointer in case checks are off
349 if Mapping = null then
350 raise Constraint_Error;
355 if Going = Forward then
357 for J in 1 .. Source'Length - PL1 loop
360 for K in Pattern'Range loop
361 if Pattern (K) /= Mapping.all (Source (Cur)) then
377 Ind := Source'Last - PL1;
378 for J in reverse 1 .. Source'Length - PL1 loop
381 for K in Pattern'Range loop
382 if Pattern (K) /= Mapping.all (Source (Cur)) then
396 -- Fall through if no match found. Note that the loops are skipped
397 -- completely in the case of the pattern being longer than the source.
404 Set : Maps.Character_Set;
405 Test : Membership := Inside;
406 Going : Direction := Forward) return Natural
411 if Going = Forward then
412 for J in Source'Range loop
413 if Belongs (Source (J), Set, Test) then
421 for J in reverse Source'Range loop
422 if Belongs (Source (J), Set, Test) then
428 -- Fall through if no match
437 Going : Direction := Forward;
438 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
441 if Going = Forward then
442 if From < Source'First then
447 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
450 if From > Source'Last then
455 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
463 Going : Direction := Forward;
464 Mapping : Maps.Character_Mapping_Function) return Natural
467 if Going = Forward then
468 if From < Source'First then
473 (Source (From .. Source'Last), Pattern, Forward, Mapping);
476 if From > Source'Last then
481 (Source (Source'First .. From), Pattern, Backward, Mapping);
487 Set : Maps.Character_Set;
489 Test : Membership := Inside;
490 Going : Direction := Forward) return Natural
493 if Going = Forward then
494 if From < Source'First then
499 Index (Source (From .. Source'Last), Set, Test, Forward);
502 if From > Source'Last then
507 Index (Source (Source'First .. From), Set, Test, Backward);
511 ---------------------
512 -- Index_Non_Blank --
513 ---------------------
515 function Index_Non_Blank
517 Going : Direction := Forward) return Natural
520 if Going = Forward then
521 for J in Source'Range loop
522 if Source (J) /= ' ' then
527 else -- Going = Backward
528 for J in reverse Source'Range loop
529 if Source (J) /= ' ' then
535 -- Fall through if no match
540 function Index_Non_Blank
543 Going : Direction := Forward) return Natural
546 if Going = Forward then
547 if From < Source'First then
552 Index_Non_Blank (Source (From .. Source'Last), Forward);
555 if From > Source'Last then
560 Index_Non_Blank (Source (Source'First .. From), Backward);
564 end Ada.Strings.Search;