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;
34 package body Ada.Strings.Wide_Wide_Search is
36 -----------------------
37 -- Local Subprograms --
38 -----------------------
41 (Element : Wide_Wide_Character;
42 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
43 Test : Membership) return Boolean;
44 pragma Inline (Belongs);
45 -- Determines if the given element is in (Test = Inside) or not in
46 -- (Test = Outside) the given character set.
53 (Element : Wide_Wide_Character;
54 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
55 Test : Membership) return Boolean
59 return Is_In (Element, Set);
61 return not Is_In (Element, Set);
70 (Source : Wide_Wide_String;
71 Pattern : Wide_Wide_String;
72 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
73 Wide_Wide_Maps.Identity)
84 -- Handle the case of non-identity mappings by creating a mapped
85 -- string and making a recursive call using the identity mapping
86 -- on this mapped string.
88 if Mapping /= Wide_Wide_Maps.Identity then
90 Mapped_Source : Wide_Wide_String (Source'Range);
93 for J in Source'Range loop
94 Mapped_Source (J) := Value (Mapping, Source (J));
97 return Count (Mapped_Source, Pattern);
104 while J <= Source'Last - (Pattern'Length - 1) loop
105 if Source (J .. J + (Pattern'Length - 1)) = Pattern then
107 J := J + Pattern'Length;
117 (Source : Wide_Wide_String;
118 Pattern : Wide_Wide_String;
119 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
122 Mapped_Source : Wide_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_Wide_String;
134 Set : Wide_Wide_Maps.Wide_Wide_Character_Set) return Natural
139 for J in Source'Range loop
140 if Is_In (Source (J), Set) then
153 (Source : Wide_Wide_String;
154 Set : Wide_Wide_Maps.Wide_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_Wide_String;
191 Pattern : Wide_Wide_String;
192 Going : Direction := Forward;
193 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
194 Wide_Wide_Maps.Identity)
202 -- Handle the case of non-identity mappings by creating a mapped
203 -- string and making a recursive call using the identity mapping
204 -- on this mapped string.
206 if Mapping /= Identity then
208 Mapped_Source : Wide_Wide_String (Source'Range);
211 for J in Source'Range loop
212 Mapped_Source (J) := Value (Mapping, Source (J));
215 return Index (Mapped_Source, Pattern, Going);
219 if Going = Forward then
220 for J in Source'First .. Source'Last - Pattern'Length + 1 loop
221 if Pattern = Source (J .. J + Pattern'Length - 1) then
226 else -- Going = Backward
227 for J in reverse Source'First .. Source'Last - Pattern'Length + 1 loop
228 if Pattern = Source (J .. J + Pattern'Length - 1) then
234 -- Fall through if no match found. Note that the loops are skipped
235 -- completely in the case of the pattern being longer than the source.
241 (Source : Wide_Wide_String;
242 Pattern : Wide_Wide_String;
243 Going : Direction := Forward;
244 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
247 Mapped_Source : Wide_Wide_String (Source'Range);
250 for J in Source'Range loop
251 Mapped_Source (J) := Mapping (Source (J));
254 return Index (Mapped_Source, Pattern, Going);
258 (Source : Wide_Wide_String;
259 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
260 Test : Membership := Inside;
261 Going : Direction := Forward) return Natural
264 if Going = Forward then
265 for J in Source'Range loop
266 if Belongs (Source (J), Set, Test) then
271 else -- Going = Backward
272 for J in reverse Source'Range loop
273 if Belongs (Source (J), Set, Test) then
279 -- Fall through if no match
285 (Source : Wide_Wide_String;
286 Pattern : Wide_Wide_String;
288 Going : Direction := Forward;
289 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping :=
290 Wide_Wide_Maps.Identity)
294 if Going = Forward then
295 if From < Source'First then
300 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
303 if From > Source'Last then
308 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
313 (Source : Wide_Wide_String;
314 Pattern : Wide_Wide_String;
316 Going : Direction := Forward;
317 Mapping : Wide_Wide_Maps.Wide_Wide_Character_Mapping_Function)
321 if Going = Forward then
322 if From < Source'First then
327 (Source (From .. Source'Last), Pattern, Forward, Mapping);
330 if From > Source'Last then
335 (Source (Source'First .. From), Pattern, Backward, Mapping);
340 (Source : Wide_Wide_String;
341 Set : Wide_Wide_Maps.Wide_Wide_Character_Set;
343 Test : Membership := Inside;
344 Going : Direction := Forward) return Natural
347 if Going = Forward then
348 if From < Source'First then
353 Index (Source (From .. Source'Last), Set, Test, Forward);
356 if From > Source'Last then
361 Index (Source (Source'First .. From), Set, Test, Backward);
365 ---------------------
366 -- Index_Non_Blank --
367 ---------------------
369 function Index_Non_Blank
370 (Source : Wide_Wide_String;
371 Going : Direction := Forward) return Natural
374 if Going = Forward then
375 for J in Source'Range loop
376 if Source (J) /= Wide_Wide_Space then
381 else -- Going = Backward
382 for J in reverse Source'Range loop
383 if Source (J) /= Wide_Wide_Space then
389 -- Fall through if no match
394 function Index_Non_Blank
395 (Source : Wide_Wide_String;
397 Going : Direction := Forward) return Natural
400 if Going = Forward then
401 if From < Source'First then
406 Index_Non_Blank (Source (From .. Source'Last), Forward);
409 if From > Source'Last then
414 Index_Non_Blank (Source (Source'First .. From), Backward);
418 end Ada.Strings.Wide_Wide_Search;