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-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_Maps; use Ada.Strings.Wide_Maps;
34 package body Ada.Strings.Wide_Search is
36 -----------------------
37 -- Local Subprograms --
38 -----------------------
41 (Element : Wide_Character;
42 Set : Wide_Maps.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_Character;
54 Set : Wide_Maps.Wide_Character_Set;
55 Test : Membership) return Boolean
59 return Is_In (Element, Set);
61 return not Is_In (Element, Set);
70 (Source : Wide_String;
71 Pattern : Wide_String;
72 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
83 -- Handle the case of non-identity mappings by creating a mapped
84 -- string and making a recursive call using the identity mapping
85 -- on this mapped string.
87 if Mapping /= Wide_Maps.Identity then
89 Mapped_Source : Wide_String (Source'Range);
92 for J in Source'Range loop
93 Mapped_Source (J) := Value (Mapping, Source (J));
96 return Count (Mapped_Source, Pattern);
103 while J <= Source'Last - (Pattern'Length - 1) loop
104 if Source (J .. J + (Pattern'Length - 1)) = Pattern then
106 J := J + Pattern'Length;
116 (Source : Wide_String;
117 Pattern : Wide_String;
118 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
120 Mapped_Source : Wide_String (Source'Range);
123 for J in Source'Range loop
124 Mapped_Source (J) := Mapping (Source (J));
127 return Count (Mapped_Source, Pattern);
131 (Source : Wide_String;
132 Set : Wide_Maps.Wide_Character_Set) return Natural
137 for J in Source'Range loop
138 if Is_In (Source (J), Set) then
151 (Source : Wide_String;
152 Set : Wide_Maps.Wide_Character_Set;
154 First : out Positive;
158 for J in Source'Range loop
159 if Belongs (Source (J), Set, Test) then
162 for K in J + 1 .. Source'Last loop
163 if not Belongs (Source (K), Set, Test) then
169 -- Here if J indexes 1st char of token, and all chars
170 -- after J are in the token
177 -- Here if no token found
179 First := Source'First;
188 (Source : Wide_String;
189 Pattern : Wide_String;
190 Going : Direction := Forward;
191 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
199 -- Handle the case of non-identity mappings by creating a mapped
200 -- string and making a recursive call using the identity mapping
201 -- on this mapped string.
203 if Mapping /= Identity then
205 Mapped_Source : Wide_String (Source'Range);
208 for J in Source'Range loop
209 Mapped_Source (J) := Value (Mapping, Source (J));
212 return Index (Mapped_Source, Pattern, Going);
216 if Going = Forward then
217 for J in Source'First .. Source'Last - Pattern'Length + 1 loop
218 if Pattern = Source (J .. J + Pattern'Length - 1) then
223 else -- Going = Backward
224 for J in reverse Source'First .. Source'Last - Pattern'Length + 1 loop
225 if Pattern = Source (J .. J + Pattern'Length - 1) then
231 -- Fall through if no match found. Note that the loops are skipped
232 -- completely in the case of the pattern being longer than the source.
238 (Source : Wide_String;
239 Pattern : Wide_String;
240 Going : Direction := Forward;
241 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
243 Mapped_Source : Wide_String (Source'Range);
246 for J in Source'Range loop
247 Mapped_Source (J) := Mapping (Source (J));
250 return Index (Mapped_Source, Pattern, Going);
254 (Source : Wide_String;
255 Set : Wide_Maps.Wide_Character_Set;
256 Test : Membership := Inside;
257 Going : Direction := Forward) return Natural
260 if Going = Forward then
261 for J in Source'Range loop
262 if Belongs (Source (J), Set, Test) then
267 else -- Going = Backward
268 for J in reverse Source'Range loop
269 if Belongs (Source (J), Set, Test) then
275 -- Fall through if no match
281 (Source : Wide_String;
282 Pattern : Wide_String;
284 Going : Direction := Forward;
285 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
289 if Going = Forward then
290 if From < Source'First then
295 Index (Source (From .. Source'Last), Pattern, Forward, Mapping);
298 if From > Source'Last then
303 Index (Source (Source'First .. From), Pattern, Backward, Mapping);
308 (Source : Wide_String;
309 Pattern : Wide_String;
311 Going : Direction := Forward;
312 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
315 if Going = Forward then
316 if From < Source'First then
321 (Source (From .. Source'Last), Pattern, Forward, Mapping);
324 if From > Source'Last then
329 (Source (Source'First .. From), Pattern, Backward, Mapping);
334 (Source : Wide_String;
335 Set : Wide_Maps.Wide_Character_Set;
337 Test : Membership := Inside;
338 Going : Direction := Forward) return Natural
341 if Going = Forward then
342 if From < Source'First then
347 Index (Source (From .. Source'Last), Set, Test, Forward);
350 if From > Source'Last then
355 Index (Source (Source'First .. From), Set, Test, Backward);
359 ---------------------
360 -- Index_Non_Blank --
361 ---------------------
363 function Index_Non_Blank
364 (Source : Wide_String;
365 Going : Direction := Forward) return Natural
368 if Going = Forward then
369 for J in Source'Range loop
370 if Source (J) /= Wide_Space then
375 else -- Going = Backward
376 for J in reverse Source'Range loop
377 if Source (J) /= Wide_Space then
383 -- Fall through if no match
388 function Index_Non_Blank
389 (Source : Wide_String;
391 Going : Direction := Forward) return Natural
394 if Going = Forward then
395 if From < Source'First then
400 Index_Non_Blank (Source (From .. Source'Last), Forward);
403 if From > Source'Last then
408 Index_Non_Blank (Source (Source'First .. From), Backward);
412 end Ada.Strings.Wide_Search;