1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ S E A R C H --
10 -- Copyright (C) 1992,1993,1994 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 with Ada.Strings.Wide_Maps; use Ada.Strings.Wide_Maps;
37 package body Ada.Strings.Wide_Search is
39 -----------------------
40 -- Local Subprograms --
41 -----------------------
44 (Element : Wide_Character;
45 Set : Wide_Maps.Wide_Character_Set;
48 pragma Inline (Belongs);
49 -- Determines if the given element is in (Test = Inside) or not in
50 -- (Test = Outside) the given character set.
57 (Element : Wide_Character;
58 Set : Wide_Maps.Wide_Character_Set;
64 return Is_In (Element, Set);
66 return not Is_In (Element, Set);
75 (Source : in Wide_String;
76 Pattern : in Wide_String;
77 Mapping : in Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
88 -- Handle the case of non-identity mappings by creating a mapped
89 -- string and making a recursive call using the identity mapping
90 -- on this mapped string.
92 if Mapping /= Wide_Maps.Identity then
94 Mapped_Source : Wide_String (Source'Range);
97 for J in Source'Range loop
98 Mapped_Source (J) := Value (Mapping, Source (J));
101 return Count (Mapped_Source, Pattern);
108 while J <= Source'Last - (Pattern'Length - 1) loop
109 if Source (J .. J + (Pattern'Length - 1)) = Pattern then
111 J := J + Pattern'Length;
121 (Source : in Wide_String;
122 Pattern : in Wide_String;
123 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
126 Mapped_Source : Wide_String (Source'Range);
129 for J in Source'Range loop
130 Mapped_Source (J) := Mapping (Source (J));
133 return Count (Mapped_Source, Pattern);
136 function Count (Source : in Wide_String;
137 Set : in Wide_Maps.Wide_Character_Set)
143 for J in Source'Range loop
144 if Is_In (Source (J), Set) then
157 (Source : in Wide_String;
158 Set : in Wide_Maps.Wide_Character_Set;
159 Test : in Membership;
160 First : out Positive;
164 for J in Source'Range loop
165 if Belongs (Source (J), Set, Test) then
168 for K in J + 1 .. Source'Last loop
169 if not Belongs (Source (K), Set, Test) then
175 -- Here if J indexes 1st char of token, and all chars
176 -- after J are in the token
183 -- Here if no token found
185 First := Source'First;
194 (Source : in Wide_String;
195 Pattern : in Wide_String;
196 Going : in Direction := Forward;
197 Mapping : in Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
205 -- Handle the case of non-identity mappings by creating a mapped
206 -- string and making a recursive call using the identity mapping
207 -- on this mapped string.
209 if Mapping /= Identity then
211 Mapped_Source : Wide_String (Source'Range);
214 for J in Source'Range loop
215 Mapped_Source (J) := Value (Mapping, Source (J));
218 return Index (Mapped_Source, Pattern, Going);
222 if Going = Forward then
223 for J in Source'First .. Source'Last - Pattern'Length + 1 loop
224 if Pattern = Source (J .. J + Pattern'Length - 1) then
229 else -- Going = Backward
230 for J in reverse Source'First .. Source'Last - Pattern'Length + 1 loop
231 if Pattern = Source (J .. J + Pattern'Length - 1) then
237 -- Fall through if no match found. Note that the loops are skipped
238 -- completely in the case of the pattern being longer than the source.
248 (Source : in Wide_String;
249 Pattern : in Wide_String;
250 Going : in Direction := Forward;
251 Mapping : in Wide_Maps.Wide_Character_Mapping_Function)
254 Mapped_Source : Wide_String (Source'Range);
257 for J in Source'Range loop
258 Mapped_Source (J) := Mapping (Source (J));
261 return Index (Mapped_Source, Pattern, Going);
265 (Source : in Wide_String;
266 Set : in Wide_Maps.Wide_Character_Set;
267 Test : in Membership := Inside;
268 Going : in Direction := Forward)
272 if Going = Forward then
273 for J in Source'Range loop
274 if Belongs (Source (J), Set, Test) then
279 else -- Going = Backward
280 for J in reverse Source'Range loop
281 if Belongs (Source (J), Set, Test) then
287 -- Fall through if no match
292 ---------------------
293 -- Index_Non_Blank --
294 ---------------------
296 function Index_Non_Blank
297 (Source : in Wide_String;
298 Going : in Direction := Forward)
302 if Going = Forward then
303 for J in Source'Range loop
304 if Source (J) /= Wide_Space then
309 else -- Going = Backward
310 for J in reverse Source'Range loop
311 if Source (J) /= Wide_Space then
317 -- Fall through if no match
323 end Ada.Strings.Wide_Search;