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-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 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;
199 First : out Positive;
203 for J in From .. Source'Last loop
204 if Belongs (Source (J), Set, Test) then
207 for K in J + 1 .. Source'Last loop
208 if not Belongs (Source (K), Set, Test) then
214 -- Here if J indexes first char of token, and all chars after J
222 -- Here if no token found
229 (Source : Wide_Wide_String;
230 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
232 First : out Positive;
236 for J in Source'Range loop
237 if Belongs (Source (J), Set, Test) then
240 for K in J + 1 .. Source'Last loop
241 if not Belongs (Source (K), Set, Test) then
247 -- Here if J indexes first char of token, and all chars after J
255 -- Here if no token found
257 First := Source'First;
266 (Source : Wide_Wide_String;
267 Pattern : Wide_Wide_String;
268 Going : Direction := Forward;
269 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
270 Wide_Wide_Maps.Identity)
273 PL1 : constant Integer := Pattern'Length - 1;
277 -- Index for start of match check. This can be negative if the pattern
278 -- length is greater than the string length, which is why this variable
279 -- is Integer instead of Natural. In this case, the search loops do not
280 -- execute at all, so this Ind value is never used.
289 if Going = Forward then
292 -- Unmapped forward case
294 if Mapping'Address = Wide_Wide_Maps.Identity'Address then
295 for J in 1 .. Source'Length - PL1 loop
296 if Pattern = Source (Ind .. Ind + PL1) then
303 -- Mapped forward case
306 for J in 1 .. Source'Length - PL1 loop
309 for K in Pattern'Range loop
310 if Pattern (K) /= Value (Mapping, Source (Cur)) then
327 -- Unmapped backward case
329 Ind := Source'Last - PL1;
331 if Mapping'Address = Wide_Wide_Maps.Identity'Address then
332 for J in reverse 1 .. Source'Length - PL1 loop
333 if Pattern = Source (Ind .. Ind + PL1) then
340 -- Mapped backward case
343 for J in reverse 1 .. Source'Length - PL1 loop
346 for K in Pattern'Range loop
347 if Pattern (K) /= Value (Mapping, Source (Cur)) then
362 -- Fall through if no match found. Note that the loops are skipped
363 -- completely in the case of the pattern being longer than the source.
369 (Source : Wide_Wide_String;
370 Pattern : Wide_Wide_String;
371 Going : Direction := Forward;
372 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
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.
446 (Source : Wide_Wide_String;
447 Set : Wide_Wide_Maps.Wide_Wide_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
477 (Source : Wide_Wide_String;
478 Pattern : Wide_Wide_String;
480 Going : Direction := Forward;
481 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
482 Wide_Wide_Maps.Identity)
486 if Going = Forward then
487 if From < Source'First then
492 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
495 if From > Source'Last then
500 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
505 (Source : Wide_Wide_String;
506 Pattern : Wide_Wide_String;
508 Going : Direction := Forward;
509 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
513 if Going = Forward then
514 if From < Source'First then
519 (Source (From .. Source'Last), Pattern, Forward, Mapping);
522 if From > Source'Last then
527 (Source (Source'First .. From), Pattern, Backward, Mapping);
532 (Source : Wide_Wide_String;
533 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
535 Test : Membership := Inside;
536 Going : Direction := Forward) return Natural
539 if Going = Forward then
540 if From < Source'First then
545 Index (Source (From .. Source'Last), Set, Test, Forward);
548 if From > Source'Last then
553 Index (Source (Source'First .. From), Set, Test, Backward);
557 ---------------------
558 -- Index_Non_Blank --
559 ---------------------
561 function Index_Non_Blank
562 (Source : Wide_Wide_String;
563 Going : Direction := Forward) return Natural
566 if Going = Forward then
567 for J in Source'Range loop
568 if Source (J) /= Wide_Wide_Space then
573 else -- Going = Backward
574 for J in reverse Source'Range loop
575 if Source (J) /= Wide_Wide_Space then
581 -- Fall through if no match
586 function Index_Non_Blank
587 (Source : Wide_Wide_String;
589 Going : Direction := Forward) return Natural
592 if Going = Forward then
593 if From < Source'First then
598 Index_Non_Blank (Source (From .. Source'Last), Forward);
601 if From > Source'Last then
606 Index_Non_Blank (Source (Source'First .. From), Backward);
610 end Ada.Strings.Wide_Wide_Search;