* config/rs6000/rs6000.md: Document why a pattern is not
[official-gcc.git] / gcc / ada / a-strmap.adb
blobba02086a3164618f9948ba518a1eb7d003c617e6
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 -- Copyright (C) 1992-2002 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: parts of this code are derived from the ADAR.CSH public domain
35 -- Ada 83 versions of the Appendix C string handling packages. The main
36 -- differences are that we avoid the use of the minimize function which
37 -- is bit-by-bit or character-by-character and therefore rather slow.
38 -- Generally for character sets we favor the full 32-byte representation.
40 package body Ada.Strings.Maps is
42 use Ada.Characters.Latin_1;
44 ---------
45 -- "-" --
46 ---------
48 function "-" (Left, Right : Character_Set) return Character_Set is
49 begin
50 return Left and not Right;
51 end "-";
53 ---------
54 -- "=" --
55 ---------
57 function "=" (Left, Right : in Character_Set) return Boolean is
58 begin
59 return Character_Set_Internal (Left) = Character_Set_Internal (Right);
60 end "=";
62 -----------
63 -- "and" --
64 -----------
66 function "and" (Left, Right : in Character_Set) return Character_Set is
67 begin
68 return Character_Set
69 (Character_Set_Internal (Left) and Character_Set_Internal (Right));
70 end "and";
72 -----------
73 -- "not" --
74 -----------
76 function "not" (Right : in Character_Set) return Character_Set is
77 begin
78 return Character_Set (not Character_Set_Internal (Right));
79 end "not";
81 ----------
82 -- "or" --
83 ----------
85 function "or" (Left, Right : in Character_Set) return Character_Set is
86 begin
87 return Character_Set
88 (Character_Set_Internal (Left) or Character_Set_Internal (Right));
89 end "or";
91 -----------
92 -- "xor" --
93 -----------
95 function "xor" (Left, Right : in Character_Set) return Character_Set is
96 begin
97 return Character_Set
98 (Character_Set_Internal (Left) xor Character_Set_Internal (Right));
99 end "xor";
101 -----------
102 -- Is_In --
103 -----------
105 function Is_In
106 (Element : Character;
107 Set : Character_Set)
108 return Boolean
110 begin
111 return Set (Element);
112 end Is_In;
114 ---------------
115 -- Is_Subset --
116 ---------------
118 function Is_Subset
119 (Elements : Character_Set;
120 Set : Character_Set)
121 return Boolean
123 begin
124 return (Elements and Set) = Elements;
125 end Is_Subset;
127 ---------------
128 -- To_Domain --
129 ---------------
131 function To_Domain (Map : in Character_Mapping) return Character_Sequence
133 Result : String (1 .. Map'Length);
134 J : Natural;
136 begin
137 J := 0;
138 for C in Map'Range loop
139 if Map (C) /= C then
140 J := J + 1;
141 Result (J) := C;
142 end if;
143 end loop;
145 return Result (1 .. J);
146 end To_Domain;
148 ----------------
149 -- To_Mapping --
150 ----------------
152 function To_Mapping
153 (From, To : in Character_Sequence)
154 return Character_Mapping
156 Result : Character_Mapping;
157 Inserted : Character_Set := Null_Set;
158 From_Len : constant Natural := From'Length;
159 To_Len : constant Natural := To'Length;
161 begin
162 if From_Len /= To_Len then
163 raise Strings.Translation_Error;
164 end if;
166 for Char in Character loop
167 Result (Char) := Char;
168 end loop;
170 for J in From'Range loop
171 if Inserted (From (J)) then
172 raise Strings.Translation_Error;
173 end if;
175 Result (From (J)) := To (J - From'First + To'First);
176 Inserted (From (J)) := True;
177 end loop;
179 return Result;
180 end To_Mapping;
182 --------------
183 -- To_Range --
184 --------------
186 function To_Range (Map : in Character_Mapping) return Character_Sequence
188 Result : String (1 .. Map'Length);
189 J : Natural;
191 begin
192 J := 0;
193 for C in Map'Range loop
194 if Map (C) /= C then
195 J := J + 1;
196 Result (J) := Map (C);
197 end if;
198 end loop;
200 return Result (1 .. J);
201 end To_Range;
203 ---------------
204 -- To_Ranges --
205 ---------------
207 function To_Ranges (Set : in Character_Set) return Character_Ranges is
208 Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
209 Range_Num : Natural;
210 C : Character;
212 begin
213 C := Character'First;
214 Range_Num := 0;
216 loop
217 -- Skip gap between subsets.
219 while not Set (C) loop
220 exit when C = Character'Last;
221 C := Character'Succ (C);
222 end loop;
224 exit when not Set (C);
226 Range_Num := Range_Num + 1;
227 Max_Ranges (Range_Num).Low := C;
229 -- Span a subset.
231 loop
232 exit when not Set (C) or else C = Character'Last;
233 C := Character'Succ (C);
234 end loop;
236 if Set (C) then
237 Max_Ranges (Range_Num). High := C;
238 exit;
239 else
240 Max_Ranges (Range_Num). High := Character'Pred (C);
241 end if;
242 end loop;
244 return Max_Ranges (1 .. Range_Num);
245 end To_Ranges;
247 -----------------
248 -- To_Sequence --
249 -----------------
251 function To_Sequence
252 (Set : Character_Set)
253 return Character_Sequence
255 Result : String (1 .. Character'Pos (Character'Last) + 1);
256 Count : Natural := 0;
258 begin
259 for Char in Set'Range loop
260 if Set (Char) then
261 Count := Count + 1;
262 Result (Count) := Char;
263 end if;
264 end loop;
266 return Result (1 .. Count);
267 end To_Sequence;
269 ------------
270 -- To_Set --
271 ------------
273 function To_Set (Ranges : in Character_Ranges) return Character_Set is
274 Result : Character_Set;
276 begin
277 for C in Result'Range loop
278 Result (C) := False;
279 end loop;
281 for R in Ranges'Range loop
282 for C in Ranges (R).Low .. Ranges (R).High loop
283 Result (C) := True;
284 end loop;
285 end loop;
287 return Result;
288 end To_Set;
290 function To_Set (Span : in Character_Range) return Character_Set is
291 Result : Character_Set;
293 begin
294 for C in Result'Range loop
295 Result (C) := False;
296 end loop;
298 for C in Span.Low .. Span.High loop
299 Result (C) := True;
300 end loop;
302 return Result;
303 end To_Set;
305 function To_Set (Sequence : Character_Sequence) return Character_Set is
306 Result : Character_Set := Null_Set;
308 begin
309 for J in Sequence'Range loop
310 Result (Sequence (J)) := True;
311 end loop;
313 return Result;
314 end To_Set;
316 function To_Set (Singleton : Character) return Character_Set is
317 Result : Character_Set := Null_Set;
319 begin
320 Result (Singleton) := True;
321 return Result;
322 end To_Set;
324 -----------
325 -- Value --
326 -----------
328 function Value (Map : in Character_Mapping; Element : in Character)
329 return Character is
331 begin
332 return Map (Element);
333 end Value;
335 end Ada.Strings.Maps;