2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / a-strsea.adb
blob29db92a87c999248b957ec925cd847de8908c186
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- A D A . S T R I N G S . S E A R C H --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992,1993,1994,1995,1996 Free Software Foundation, Inc. --
10 -- --
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
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. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- Note: This code is derived from the ADAR.CSH public domain Ada 83
35 -- versions of the Appendix C string handling packages (code extracted
36 -- from Ada.Strings.Fixed). A significant change is that we optimize the
37 -- case of identity mappings for Count and Index, and also Index_Non_Blank
38 -- is specialized (rather than using the general Index routine).
41 with Ada.Strings.Maps; use Ada.Strings.Maps;
43 package body Ada.Strings.Search is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 function Belongs
50 (Element : Character;
51 Set : Maps.Character_Set;
52 Test : Membership)
53 return Boolean;
54 pragma Inline (Belongs);
55 -- Determines if the given element is in (Test = Inside) or not in
56 -- (Test = Outside) the given character set.
58 -------------
59 -- Belongs --
60 -------------
62 function Belongs
63 (Element : Character;
64 Set : Maps.Character_Set;
65 Test : Membership)
66 return Boolean
68 begin
69 if Test = Inside then
70 return Is_In (Element, Set);
71 else
72 return not Is_In (Element, Set);
73 end if;
74 end Belongs;
76 -----------
77 -- Count --
78 -----------
80 function Count
81 (Source : in String;
82 Pattern : in String;
83 Mapping : in Maps.Character_Mapping := Maps.Identity)
84 return Natural
86 N : Natural;
87 J : Natural;
89 Mapped_Source : String (Source'Range);
91 begin
92 for J in Source'Range loop
93 Mapped_Source (J) := Value (Mapping, Source (J));
94 end loop;
96 if Pattern = "" then
97 raise Pattern_Error;
98 end if;
100 N := 0;
101 J := Source'First;
103 while J <= Source'Last - (Pattern'Length - 1) loop
104 if Mapped_Source (J .. J + (Pattern'Length - 1)) = Pattern then
105 N := N + 1;
106 J := J + Pattern'Length;
107 else
108 J := J + 1;
109 end if;
110 end loop;
112 return N;
113 end Count;
115 function Count
116 (Source : in String;
117 Pattern : in String;
118 Mapping : in Maps.Character_Mapping_Function)
119 return Natural
121 Mapped_Source : String (Source'Range);
122 N : Natural;
123 J : Natural;
125 begin
126 if Pattern = "" then
127 raise Pattern_Error;
128 end if;
130 -- We make sure Access_Check is unsuppressed so that the Mapping.all
131 -- call will generate a friendly Constraint_Error if the value for
132 -- Mapping is uninitialized (and hence null).
134 declare
135 pragma Unsuppress (Access_Check);
137 begin
138 for J in Source'Range loop
139 Mapped_Source (J) := Mapping.all (Source (J));
140 end loop;
141 end;
143 N := 0;
144 J := Source'First;
146 while J <= Source'Last - (Pattern'Length - 1) loop
147 if Mapped_Source (J .. J + (Pattern'Length - 1)) = Pattern then
148 N := N + 1;
149 J := J + Pattern'Length;
150 else
151 J := J + 1;
152 end if;
153 end loop;
155 return N;
156 end Count;
158 function Count
159 (Source : in String;
160 Set : in Maps.Character_Set)
161 return Natural
163 N : Natural := 0;
165 begin
166 for J in Source'Range loop
167 if Is_In (Source (J), Set) then
168 N := N + 1;
169 end if;
170 end loop;
172 return N;
173 end Count;
175 ----------------
176 -- Find_Token --
177 ----------------
179 procedure Find_Token
180 (Source : in String;
181 Set : in Maps.Character_Set;
182 Test : in Membership;
183 First : out Positive;
184 Last : out Natural)
186 begin
187 for J in Source'Range loop
188 if Belongs (Source (J), Set, Test) then
189 First := J;
191 for K in J + 1 .. Source'Last loop
192 if not Belongs (Source (K), Set, Test) then
193 Last := K - 1;
194 return;
195 end if;
196 end loop;
198 -- Here if J indexes 1st char of token, and all chars
199 -- after J are in the token
201 Last := Source'Last;
202 return;
203 end if;
204 end loop;
206 -- Here if no token found
208 First := Source'First;
209 Last := 0;
210 end Find_Token;
212 -----------
213 -- Index --
214 -----------
216 function Index
217 (Source : in String;
218 Pattern : in String;
219 Going : in Direction := Forward;
220 Mapping : in Maps.Character_Mapping := Maps.Identity)
221 return Natural
223 Cur_Index : Natural;
224 Mapped_Source : String (Source'Range);
227 begin
228 if Pattern = "" then
229 raise Pattern_Error;
230 end if;
232 for J in Source'Range loop
233 Mapped_Source (J) := Value (Mapping, Source (J));
234 end loop;
236 -- Forwards case
238 if Going = Forward then
239 for J in 1 .. Source'Length - Pattern'Length + 1 loop
240 Cur_Index := Source'First + J - 1;
242 if Pattern = Mapped_Source
243 (Cur_Index .. Cur_Index + Pattern'Length - 1)
244 then
245 return Cur_Index;
246 end if;
247 end loop;
249 -- Backwards case
251 else
252 for J in reverse 1 .. Source'Length - Pattern'Length + 1 loop
253 Cur_Index := Source'First + J - 1;
255 if Pattern = Mapped_Source
256 (Cur_Index .. Cur_Index + Pattern'Length - 1)
257 then
258 return Cur_Index;
259 end if;
260 end loop;
261 end if;
263 -- Fall through if no match found. Note that the loops are skipped
264 -- completely in the case of the pattern being longer than the source.
266 return 0;
267 end Index;
269 function Index (Source : in String;
270 Pattern : in String;
271 Going : in Direction := Forward;
272 Mapping : in Maps.Character_Mapping_Function)
273 return Natural
275 Mapped_Source : String (Source'Range);
276 Cur_Index : Natural;
278 begin
279 if Pattern = "" then
280 raise Pattern_Error;
281 end if;
283 -- We make sure Access_Check is unsuppressed so that the Mapping.all
284 -- call will generate a friendly Constraint_Error if the value for
285 -- Mapping is uninitialized (and hence null).
287 declare
288 pragma Unsuppress (Access_Check);
290 begin
291 for J in Source'Range loop
292 Mapped_Source (J) := Mapping.all (Source (J));
293 end loop;
294 end;
296 -- Forwards case
298 if Going = Forward then
299 for J in 1 .. Source'Length - Pattern'Length + 1 loop
300 Cur_Index := Source'First + J - 1;
302 if Pattern = Mapped_Source
303 (Cur_Index .. Cur_Index + Pattern'Length - 1)
304 then
305 return Cur_Index;
306 end if;
307 end loop;
309 -- Backwards case
311 else
312 for J in reverse 1 .. Source'Length - Pattern'Length + 1 loop
313 Cur_Index := Source'First + J - 1;
315 if Pattern = Mapped_Source
316 (Cur_Index .. Cur_Index + Pattern'Length - 1)
317 then
318 return Cur_Index;
319 end if;
320 end loop;
321 end if;
323 return 0;
324 end Index;
326 function Index
327 (Source : in String;
328 Set : in Maps.Character_Set;
329 Test : in Membership := Inside;
330 Going : in Direction := Forward)
331 return Natural
333 begin
334 -- Forwards case
336 if Going = Forward then
337 for J in Source'Range loop
338 if Belongs (Source (J), Set, Test) then
339 return J;
340 end if;
341 end loop;
343 -- Backwards case
345 else
346 for J in reverse Source'Range loop
347 if Belongs (Source (J), Set, Test) then
348 return J;
349 end if;
350 end loop;
351 end if;
353 -- Fall through if no match
355 return 0;
356 end Index;
358 ---------------------
359 -- Index_Non_Blank --
360 ---------------------
362 function Index_Non_Blank
363 (Source : in String;
364 Going : in Direction := Forward)
365 return Natural
367 begin
368 if Going = Forward then
369 for J in Source'Range loop
370 if Source (J) /= ' ' then
371 return J;
372 end if;
373 end loop;
375 else -- Going = Backward
376 for J in reverse Source'Range loop
377 if Source (J) /= ' ' then
378 return J;
379 end if;
380 end loop;
381 end if;
383 -- Fall through if no match
385 return 0;
387 end Index_Non_Blank;
389 end Ada.Strings.Search;