Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / gcc / ada / a-strmap.adb
blob0e9974eec05380f894e586f43631e1ffe724c4de
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- A D A . S T R I N G S . M A P S --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- --
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. --
22 -- --
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. --
29 -- --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- --
33 ------------------------------------------------------------------------------
35 -- Note: parts of this code are derived from the ADAR.CSH public domain
36 -- Ada 83 versions of the Appendix C string handling packages. The main
37 -- differences are that we avoid the use of the minimize function which
38 -- is bit-by-bit or character-by-character and therefore rather slow.
39 -- Generally for character sets we favor the full 32-byte representation.
41 package body Ada.Strings.Maps is
43 use Ada.Characters.Latin_1;
45 ---------
46 -- "-" --
47 ---------
49 function "-" (Left, Right : Character_Set) return Character_Set is
50 begin
51 return Left and not Right;
52 end "-";
54 ---------
55 -- "=" --
56 ---------
58 function "=" (Left, Right : in Character_Set) return Boolean is
59 begin
60 return Character_Set_Internal (Left) = Character_Set_Internal (Right);
61 end "=";
63 -----------
64 -- "and" --
65 -----------
67 function "and" (Left, Right : in Character_Set) return Character_Set is
68 begin
69 return Character_Set
70 (Character_Set_Internal (Left) and Character_Set_Internal (Right));
71 end "and";
73 -----------
74 -- "not" --
75 -----------
77 function "not" (Right : in Character_Set) return Character_Set is
78 begin
79 return Character_Set (not Character_Set_Internal (Right));
80 end "not";
82 ----------
83 -- "or" --
84 ----------
86 function "or" (Left, Right : in Character_Set) return Character_Set is
87 begin
88 return Character_Set
89 (Character_Set_Internal (Left) or Character_Set_Internal (Right));
90 end "or";
92 -----------
93 -- "xor" --
94 -----------
96 function "xor" (Left, Right : in Character_Set) return Character_Set is
97 begin
98 return Character_Set
99 (Character_Set_Internal (Left) xor Character_Set_Internal (Right));
100 end "xor";
102 -----------
103 -- Is_In --
104 -----------
106 function Is_In
107 (Element : Character;
108 Set : Character_Set)
109 return Boolean
111 begin
112 return Set (Element);
113 end Is_In;
115 ---------------
116 -- Is_Subset --
117 ---------------
119 function Is_Subset
120 (Elements : Character_Set;
121 Set : Character_Set)
122 return Boolean
124 begin
125 return (Elements and Set) = Elements;
126 end Is_Subset;
128 ---------------
129 -- To_Domain --
130 ---------------
132 function To_Domain (Map : in Character_Mapping) return Character_Sequence
134 Result : String (1 .. Map'Length);
135 J : Natural;
137 begin
138 J := 0;
139 for C in Map'Range loop
140 if Map (C) /= C then
141 J := J + 1;
142 Result (J) := C;
143 end if;
144 end loop;
146 return Result (1 .. J);
147 end To_Domain;
149 ----------------
150 -- To_Mapping --
151 ----------------
153 function To_Mapping
154 (From, To : in Character_Sequence)
155 return Character_Mapping
157 Result : Character_Mapping;
158 Inserted : Character_Set := Null_Set;
159 From_Len : constant Natural := From'Length;
160 To_Len : constant Natural := To'Length;
162 begin
163 if From_Len /= To_Len then
164 raise Strings.Translation_Error;
165 end if;
167 for Char in Character loop
168 Result (Char) := Char;
169 end loop;
171 for J in From'Range loop
172 if Inserted (From (J)) then
173 raise Strings.Translation_Error;
174 end if;
176 Result (From (J)) := To (J - From'First + To'First);
177 Inserted (From (J)) := True;
178 end loop;
180 return Result;
181 end To_Mapping;
183 --------------
184 -- To_Range --
185 --------------
187 function To_Range (Map : in Character_Mapping) return Character_Sequence
189 Result : String (1 .. Map'Length);
190 J : Natural;
192 begin
193 J := 0;
194 for C in Map'Range loop
195 if Map (C) /= C then
196 J := J + 1;
197 Result (J) := Map (C);
198 end if;
199 end loop;
201 return Result (1 .. J);
202 end To_Range;
204 ---------------
205 -- To_Ranges --
206 ---------------
208 function To_Ranges (Set : in Character_Set) return Character_Ranges is
209 Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
210 Range_Num : Natural;
211 C : Character;
213 begin
214 C := Character'First;
215 Range_Num := 0;
217 loop
218 -- Skip gap between subsets.
220 while not Set (C) loop
221 exit when C = Character'Last;
222 C := Character'Succ (C);
223 end loop;
225 exit when not Set (C);
227 Range_Num := Range_Num + 1;
228 Max_Ranges (Range_Num).Low := C;
230 -- Span a subset.
232 loop
233 exit when not Set (C) or else C = Character'Last;
234 C := Character' Succ (C);
235 end loop;
237 if Set (C) then
238 Max_Ranges (Range_Num). High := C;
239 exit;
240 else
241 Max_Ranges (Range_Num). High := Character'Pred (C);
242 end if;
243 end loop;
245 return Max_Ranges (1 .. Range_Num);
246 end To_Ranges;
248 -----------------
249 -- To_Sequence --
250 -----------------
252 function To_Sequence
253 (Set : Character_Set)
254 return Character_Sequence
256 Result : String (1 .. Character'Pos (Character'Last) + 1);
257 Count : Natural := 0;
259 begin
260 for Char in Set'Range loop
261 if Set (Char) then
262 Count := Count + 1;
263 Result (Count) := Char;
264 end if;
265 end loop;
267 return Result (1 .. Count);
268 end To_Sequence;
270 ------------
271 -- To_Set --
272 ------------
274 function To_Set (Ranges : in Character_Ranges) return Character_Set is
275 Result : Character_Set;
277 begin
278 for C in Result'Range loop
279 Result (C) := False;
280 end loop;
282 for R in Ranges'Range loop
283 for C in Ranges (R).Low .. Ranges (R).High loop
284 Result (C) := True;
285 end loop;
286 end loop;
288 return Result;
289 end To_Set;
291 function To_Set (Span : in Character_Range) return Character_Set is
292 Result : Character_Set;
294 begin
295 for C in Result'Range loop
296 Result (C) := False;
297 end loop;
299 for C in Span.Low .. Span.High loop
300 Result (C) := True;
301 end loop;
303 return Result;
304 end To_Set;
306 function To_Set (Sequence : Character_Sequence) return Character_Set is
307 Result : Character_Set := Null_Set;
309 begin
310 for J in Sequence'Range loop
311 Result (Sequence (J)) := True;
312 end loop;
314 return Result;
315 end To_Set;
317 function To_Set (Singleton : Character) return Character_Set is
318 Result : Character_Set := Null_Set;
320 begin
321 Result (Singleton) := True;
322 return Result;
323 end To_Set;
325 -----------
326 -- Value --
327 -----------
329 function Value (Map : in Character_Mapping; Element : in Character)
330 return Character is
332 begin
333 return Map (Element);
334 end Value;
336 end Ada.Strings.Maps;