1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ S E A R C H --
9 -- Copyright (C) 1992-2006, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Strings.Wide_Maps; use Ada.Strings.Wide_Maps;
36 package body Ada.Strings.Wide_Search is
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
43 (Element : Wide_Character;
44 Set : Wide_Maps.Wide_Character_Set;
45 Test : Membership) return Boolean;
46 pragma Inline (Belongs);
47 -- Determines if the given element is in (Test = Inside) or not in
48 -- (Test = Outside) the given character set.
55 (Element : Wide_Character;
56 Set : Wide_Maps.Wide_Character_Set;
57 Test : Membership) return Boolean
61 return Is_In (Element, Set);
63 return not Is_In (Element, Set);
72 (Source : Wide_String;
73 Pattern : Wide_String;
74 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
85 -- Handle the case of non-identity mappings by creating a mapped
86 -- string and making a recursive call using the identity mapping
87 -- on this mapped string.
89 if Mapping /= Wide_Maps.Identity then
91 Mapped_Source : Wide_String (Source'Range);
94 for J in Source'Range loop
95 Mapped_Source (J) := Value (Mapping, Source (J));
98 return Count (Mapped_Source, Pattern);
105 while J <= Source'Last - (Pattern'Length - 1) loop
106 if Source (J .. J + (Pattern'Length - 1)) = Pattern then
108 J := J + Pattern'Length;
118 (Source : Wide_String;
119 Pattern : Wide_String;
120 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
122 Mapped_Source : Wide_String (Source'Range);
125 for J in Source'Range loop
126 Mapped_Source (J) := Mapping (Source (J));
129 return Count (Mapped_Source, Pattern);
133 (Source : Wide_String;
134 Set : Wide_Maps.Wide_Character_Set) return Natural
139 for J in Source'Range loop
140 if Is_In (Source (J), Set) then
153 (Source : Wide_String;
154 Set : Wide_Maps.Wide_Character_Set;
156 First : out Positive;
160 for J in Source'Range loop
161 if Belongs (Source (J), Set, Test) then
164 for K in J + 1 .. Source'Last loop
165 if not Belongs (Source (K), Set, Test) then
171 -- Here if J indexes 1st char of token, and all chars
172 -- after J are in the token
179 -- Here if no token found
181 First := Source'First;
190 (Source : Wide_String;
191 Pattern : Wide_String;
192 Going : Direction := Forward;
193 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
201 -- Handle the case of non-identity mappings by creating a mapped
202 -- string and making a recursive call using the identity mapping
203 -- on this mapped string.
205 if Mapping /= Identity then
207 Mapped_Source : Wide_String (Source'Range);
210 for J in Source'Range loop
211 Mapped_Source (J) := Value (Mapping, Source (J));
214 return Index (Mapped_Source, Pattern, Going);
218 if Going = Forward then
219 for J in Source'First .. Source'Last - Pattern'Length + 1 loop
220 if Pattern = Source (J .. J + Pattern'Length - 1) then
225 else -- Going = Backward
226 for J in reverse Source'First .. Source'Last - Pattern'Length + 1 loop
227 if Pattern = Source (J .. J + Pattern'Length - 1) then
233 -- Fall through if no match found. Note that the loops are skipped
234 -- completely in the case of the pattern being longer than the source.
240 (Source : Wide_String;
241 Pattern : Wide_String;
242 Going : Direction := Forward;
243 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
245 Mapped_Source : Wide_String (Source'Range);
248 for J in Source'Range loop
249 Mapped_Source (J) := Mapping (Source (J));
252 return Index (Mapped_Source, Pattern, Going);
256 (Source : Wide_String;
257 Set : Wide_Maps.Wide_Character_Set;
258 Test : Membership := Inside;
259 Going : Direction := Forward) return Natural
262 if Going = Forward then
263 for J in Source'Range loop
264 if Belongs (Source (J), Set, Test) then
269 else -- Going = Backward
270 for J in reverse Source'Range loop
271 if Belongs (Source (J), Set, Test) then
277 -- Fall through if no match
283 (Source : Wide_String;
284 Pattern : Wide_String;
286 Going : Direction := Forward;
287 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
291 if Going = Forward then
292 if From < Source'First then
297 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
300 if From > Source'Last then
305 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
310 (Source : Wide_String;
311 Pattern : Wide_String;
313 Going : Direction := Forward;
314 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
317 if Going = Forward then
318 if From < Source'First then
323 (Source (From .. Source'Last), Pattern, Forward, Mapping);
326 if From > Source'Last then
331 (Source (Source'First .. From), Pattern, Backward, Mapping);
336 (Source : Wide_String;
337 Set : Wide_Maps.Wide_Character_Set;
339 Test : Membership := Inside;
340 Going : Direction := Forward) return Natural
343 if Going = Forward then
344 if From < Source'First then
349 Index (Source (From .. Source'Last), Set, Test, Forward);
352 if From > Source'Last then
357 Index (Source (Source'First .. From), Set, Test, Backward);
361 ---------------------
362 -- Index_Non_Blank --
363 ---------------------
365 function Index_Non_Blank
366 (Source : Wide_String;
367 Going : Direction := Forward) return Natural
370 if Going = Forward then
371 for J in Source'Range loop
372 if Source (J) /= Wide_Space then
377 else -- Going = Backward
378 for J in reverse Source'Range loop
379 if Source (J) /= Wide_Space then
385 -- Fall through if no match
390 function Index_Non_Blank
391 (Source : Wide_String;
393 Going : Direction := Forward) return Natural
396 if Going = Forward then
397 if From < Source'First then
402 Index_Non_Blank (Source (From .. Source'Last), Forward);
405 if From > Source'Last then
410 Index_Non_Blank (Source (Source'First .. From), Backward);
414 end Ada.Strings.Wide_Search;