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-2010, 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;
202 First : out Positive;
206 for J in From .. Source'Last loop
207 if Belongs (Source (J), Set, Test) then
210 for K in J + 1 .. Source'Last loop
211 if not Belongs (Source (K), Set, Test) then
217 -- Here if J indexes first char of token, and all chars after J
225 -- Here if no token found
233 Set : Maps.Character_Set;
235 First : out Positive;
239 for J in Source'Range loop
240 if Belongs (Source (J), Set, Test) then
243 for K in J + 1 .. Source'Last loop
244 if not Belongs (Source (K), Set, Test) then
250 -- Here if J indexes first char of token, and all chars after J
258 -- Here if no token found
260 First := Source'First;
271 Going : Direction := Forward;
272 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
274 PL1 : constant Integer := Pattern'Length - 1;
278 -- Index for start of match check. This can be negative if the pattern
279 -- length is greater than the string length, which is why this variable
280 -- is Integer instead of Natural. In this case, the search loops do not
281 -- execute at all, so this Ind value is never used.
290 if Going = Forward then
293 -- Unmapped forward case
295 if Mapping'Address = Maps.Identity'Address then
296 for J in 1 .. Source'Length - PL1 loop
297 if Pattern = Source (Ind .. Ind + PL1) then
304 -- Mapped forward case
307 for J in 1 .. Source'Length - PL1 loop
310 for K in Pattern'Range loop
311 if Pattern (K) /= Value (Mapping, Source (Cur)) then
328 -- Unmapped backward case
330 Ind := Source'Last - PL1;
332 if Mapping'Address = Maps.Identity'Address then
333 for J in reverse 1 .. Source'Length - PL1 loop
334 if Pattern = Source (Ind .. Ind + PL1) then
341 -- Mapped backward case
344 for J in reverse 1 .. Source'Length - PL1 loop
347 for K in Pattern'Range loop
348 if Pattern (K) /= Value (Mapping, Source (Cur)) then
363 -- Fall through if no match found. Note that the loops are skipped
364 -- completely in the case of the pattern being longer than the source.
372 Going : Direction := Forward;
373 Mapping : Maps.Character_Mapping_Function) return Natural
375 PL1 : constant Integer := Pattern'Length - 1;
384 -- Check for null pointer in case checks are off
386 if Mapping = null then
387 raise Constraint_Error;
390 -- If Pattern longer than Source it can't be found
392 if Pattern'Length > Source'Length then
398 if Going = Forward then
400 for J in 1 .. Source'Length - PL1 loop
403 for K in Pattern'Range loop
404 if Pattern (K) /= Mapping.all (Source (Cur)) then
420 Ind := Source'Last - PL1;
421 for J in reverse 1 .. Source'Length - PL1 loop
424 for K in Pattern'Range loop
425 if Pattern (K) /= Mapping.all (Source (Cur)) then
439 -- Fall through if no match found. Note that the loops are skipped
440 -- completely in the case of the pattern being longer than the source.
447 Set : Maps.Character_Set;
448 Test : Membership := Inside;
449 Going : Direction := Forward) return Natural
454 if Going = Forward then
455 for J in Source'Range loop
456 if Belongs (Source (J), Set, Test) then
464 for J in reverse Source'Range loop
465 if Belongs (Source (J), Set, Test) then
471 -- Fall through if no match
480 Going : Direction := Forward;
481 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
484 if Going = Forward then
485 if From < Source'First then
490 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
493 if From > Source'Last then
498 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
506 Going : Direction := Forward;
507 Mapping : Maps.Character_Mapping_Function) return Natural
510 if Going = Forward then
511 if From < Source'First then
516 (Source (From .. Source'Last), Pattern, Forward, Mapping);
519 if From > Source'Last then
524 (Source (Source'First .. From), Pattern, Backward, Mapping);
530 Set : Maps.Character_Set;
532 Test : Membership := Inside;
533 Going : Direction := Forward) return Natural
536 if Going = Forward then
537 if From < Source'First then
542 Index (Source (From .. Source'Last), Set, Test, Forward);
545 if From > Source'Last then
550 Index (Source (Source'First .. From), Set, Test, Backward);
554 ---------------------
555 -- Index_Non_Blank --
556 ---------------------
558 function Index_Non_Blank
560 Going : Direction := Forward) return Natural
563 if Going = Forward then
564 for J in Source'Range loop
565 if Source (J) /= ' ' then
570 else -- Going = Backward
571 for J in reverse Source'Range loop
572 if Source (J) /= ' ' then
578 -- Fall through if no match
583 function Index_Non_Blank
586 Going : Direction := Forward) return Natural
589 if Going = Forward then
590 if From < Source'First then
595 Index_Non_Blank (Source (From .. Source'Last), Forward);
598 if From > Source'Last then
603 Index_Non_Blank (Source (Source'First .. From), Backward);
607 end Ada.Strings.Search;