1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ W I D E _ 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 with Ada.Strings.Wide_Wide_Maps; use Ada.Strings.Wide_Wide_Maps;
33 with System; use System;
35 package body Ada.Strings.Wide_Wide_Search is
37 -----------------------
38 -- Local Subprograms --
39 -----------------------
42 (Element : Wide_Wide_Character;
43 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
44 Test : Membership) return Boolean;
45 pragma Inline (Belongs);
46 -- Determines if the given element is in (Test = Inside) or not in
47 -- (Test = Outside) the given character set.
54 (Element : Wide_Wide_Character;
55 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
56 Test : Membership) return Boolean
60 return Is_In (Element, Set);
62 return not Is_In (Element, Set);
71 (Source : Wide_Wide_String;
72 Pattern : Wide_Wide_String;
73 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
74 Wide_Wide_Maps.Identity)
77 PL1 : constant Integer := Pattern'Length - 1;
92 if Mapping'Address = Wide_Wide_Maps.Identity'Address then
93 while Ind <= Source'Last - PL1 loop
94 if Pattern = Source (Ind .. Ind + PL1) then
96 Ind := Ind + Pattern'Length;
105 while Ind <= Source'Last - PL1 loop
107 for K in Pattern'Range loop
108 if Pattern (K) /= Value (Mapping, Source (Cur)) then
117 Ind := Ind + Pattern'Length;
130 (Source : Wide_Wide_String;
131 Pattern : Wide_Wide_String;
132 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
135 PL1 : constant Integer := Pattern'Length - 1;
145 -- Check for null pointer in case checks are off
147 if Mapping = null then
148 raise Constraint_Error;
153 while Ind <= Source'Last - PL1 loop
155 for K in Pattern'Range loop
156 if Pattern (K) /= Mapping (Source (Cur)) then
165 Ind := Ind + Pattern'Length;
175 (Source : Wide_Wide_String;
176 Set : Wide_Wide_Maps.Wide_Wide_Character_Set) return Natural
181 for J in Source'Range loop
182 if Is_In (Source (J), Set) then
195 (Source : Wide_Wide_String;
196 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
198 First : out Positive;
202 for J in Source'Range loop
203 if Belongs (Source (J), Set, Test) then
206 for K in J + 1 .. Source'Last loop
207 if not Belongs (Source (K), Set, Test) then
213 -- Here if J indexes first char of token, and all chars after J
221 -- Here if no token found
223 First := Source'First;
232 (Source : Wide_Wide_String;
233 Pattern : Wide_Wide_String;
234 Going : Direction := Forward;
235 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
236 Wide_Wide_Maps.Identity)
239 PL1 : constant Integer := Pattern'Length - 1;
248 -- If Pattern longer than Source it can't be found
250 if Pattern'Length > Source'Length then
256 if Going = Forward then
259 -- Unmapped forward case
261 if Mapping'Address = Wide_Wide_Maps.Identity'Address then
262 for J in 1 .. Source'Length - PL1 loop
263 if Pattern = Source (Ind .. Ind + PL1) then
270 -- Mapped forward case
273 for J in 1 .. Source'Length - PL1 loop
276 for K in Pattern'Range loop
277 if Pattern (K) /= Value (Mapping, Source (Cur)) then
294 -- Unmapped backward case
296 Ind := Source'Last - PL1;
298 if Mapping'Address = Wide_Wide_Maps.Identity'Address then
299 for J in reverse 1 .. Source'Length - PL1 loop
300 if Pattern = Source (Ind .. Ind + PL1) then
307 -- Mapped backward case
310 for J in reverse 1 .. Source'Length - PL1 loop
313 for K in Pattern'Range loop
314 if Pattern (K) /= Value (Mapping, Source (Cur)) then
329 -- Fall through if no match found. Note that the loops are skipped
330 -- completely in the case of the pattern being longer than the source.
336 (Source : Wide_Wide_String;
337 Pattern : Wide_Wide_String;
338 Going : Direction := Forward;
339 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
342 PL1 : constant Integer := Pattern'Length - 1;
351 -- Check for null pointer in case checks are off
353 if Mapping = null then
354 raise Constraint_Error;
357 -- If Pattern longer than Source it can't be found
359 if Pattern'Length > Source'Length then
365 if Going = Forward then
367 for J in 1 .. Source'Length - PL1 loop
370 for K in Pattern'Range loop
371 if Pattern (K) /= Mapping.all (Source (Cur)) then
387 Ind := Source'Last - PL1;
388 for J in reverse 1 .. Source'Length - PL1 loop
391 for K in Pattern'Range loop
392 if Pattern (K) /= Mapping.all (Source (Cur)) then
406 -- Fall through if no match found. Note that the loops are skipped
407 -- completely in the case of the pattern being longer than the source.
413 (Source : Wide_Wide_String;
414 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
415 Test : Membership := Inside;
416 Going : Direction := Forward) return Natural
421 if Going = Forward then
422 for J in Source'Range loop
423 if Belongs (Source (J), Set, Test) then
431 for J in reverse Source'Range loop
432 if Belongs (Source (J), Set, Test) then
438 -- Fall through if no match
444 (Source : Wide_Wide_String;
445 Pattern : Wide_Wide_String;
447 Going : Direction := Forward;
448 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
449 Wide_Wide_Maps.Identity)
453 if Going = Forward then
454 if From < Source'First then
459 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
462 if From > Source'Last then
467 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
472 (Source : Wide_Wide_String;
473 Pattern : Wide_Wide_String;
475 Going : Direction := Forward;
476 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
480 if Going = Forward then
481 if From < Source'First then
486 (Source (From .. Source'Last), Pattern, Forward, Mapping);
489 if From > Source'Last then
494 (Source (Source'First .. From), Pattern, Backward, Mapping);
499 (Source : Wide_Wide_String;
500 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
502 Test : Membership := Inside;
503 Going : Direction := Forward) return Natural
506 if Going = Forward then
507 if From < Source'First then
512 Index (Source (From .. Source'Last), Set, Test, Forward);
515 if From > Source'Last then
520 Index (Source (Source'First .. From), Set, Test, Backward);
524 ---------------------
525 -- Index_Non_Blank --
526 ---------------------
528 function Index_Non_Blank
529 (Source : Wide_Wide_String;
530 Going : Direction := Forward) return Natural
533 if Going = Forward then
534 for J in Source'Range loop
535 if Source (J) /= Wide_Wide_Space then
540 else -- Going = Backward
541 for J in reverse Source'Range loop
542 if Source (J) /= Wide_Wide_Space then
548 -- Fall through if no match
553 function Index_Non_Blank
554 (Source : Wide_Wide_String;
556 Going : Direction := Forward) return Natural
559 if Going = Forward then
560 if From < Source'First then
565 Index_Non_Blank (Source (From .. Source'Last), Forward);
568 if From > Source'Last then
573 Index_Non_Blank (Source (Source'First .. From), Backward);
577 end Ada.Strings.Wide_Wide_Search;