Merge from the pain train
[official-gcc.git] / gcc / ada / a-strmap.adb
blobc7201cfc985b1b6850f41b8a460641b57d6bddfc
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-2005 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 : 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 : 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 : 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 : 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 : 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) return Boolean
109 begin
110 return Set (Element);
111 end Is_In;
113 ---------------
114 -- Is_Subset --
115 ---------------
117 function Is_Subset
118 (Elements : Character_Set;
119 Set : Character_Set) return Boolean
121 begin
122 return (Elements and Set) = Elements;
123 end Is_Subset;
125 ---------------
126 -- To_Domain --
127 ---------------
129 function To_Domain (Map : Character_Mapping) return Character_Sequence
131 Result : String (1 .. Map'Length);
132 J : Natural;
134 begin
135 J := 0;
136 for C in Map'Range loop
137 if Map (C) /= C then
138 J := J + 1;
139 Result (J) := C;
140 end if;
141 end loop;
143 return Result (1 .. J);
144 end To_Domain;
146 ----------------
147 -- To_Mapping --
148 ----------------
150 function To_Mapping
151 (From, To : Character_Sequence) return Character_Mapping
153 Result : Character_Mapping;
154 Inserted : Character_Set := Null_Set;
155 From_Len : constant Natural := From'Length;
156 To_Len : constant Natural := To'Length;
158 begin
159 if From_Len /= To_Len then
160 raise Strings.Translation_Error;
161 end if;
163 for Char in Character loop
164 Result (Char) := Char;
165 end loop;
167 for J in From'Range loop
168 if Inserted (From (J)) then
169 raise Strings.Translation_Error;
170 end if;
172 Result (From (J)) := To (J - From'First + To'First);
173 Inserted (From (J)) := True;
174 end loop;
176 return Result;
177 end To_Mapping;
179 --------------
180 -- To_Range --
181 --------------
183 function To_Range (Map : Character_Mapping) return Character_Sequence
185 Result : String (1 .. Map'Length);
186 J : Natural;
187 begin
188 J := 0;
189 for C in Map'Range loop
190 if Map (C) /= C then
191 J := J + 1;
192 Result (J) := Map (C);
193 end if;
194 end loop;
196 return Result (1 .. J);
197 end To_Range;
199 ---------------
200 -- To_Ranges --
201 ---------------
203 function To_Ranges (Set : Character_Set) return Character_Ranges is
204 Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
205 Range_Num : Natural;
206 C : Character;
208 begin
209 C := Character'First;
210 Range_Num := 0;
212 loop
213 -- Skip gap between subsets
215 while not Set (C) loop
216 exit when C = Character'Last;
217 C := Character'Succ (C);
218 end loop;
220 exit when not Set (C);
222 Range_Num := Range_Num + 1;
223 Max_Ranges (Range_Num).Low := C;
225 -- Span a subset
227 loop
228 exit when not Set (C) or else C = Character'Last;
229 C := Character'Succ (C);
230 end loop;
232 if Set (C) then
233 Max_Ranges (Range_Num). High := C;
234 exit;
235 else
236 Max_Ranges (Range_Num). High := Character'Pred (C);
237 end if;
238 end loop;
240 return Max_Ranges (1 .. Range_Num);
241 end To_Ranges;
243 -----------------
244 -- To_Sequence --
245 -----------------
247 function To_Sequence (Set : Character_Set) return Character_Sequence is
248 Result : String (1 .. Character'Pos (Character'Last) + 1);
249 Count : Natural := 0;
250 begin
251 for Char in Set'Range loop
252 if Set (Char) then
253 Count := Count + 1;
254 Result (Count) := Char;
255 end if;
256 end loop;
258 return Result (1 .. Count);
259 end To_Sequence;
261 ------------
262 -- To_Set --
263 ------------
265 function To_Set (Ranges : Character_Ranges) return Character_Set is
266 Result : Character_Set;
267 begin
268 for C in Result'Range loop
269 Result (C) := False;
270 end loop;
272 for R in Ranges'Range loop
273 for C in Ranges (R).Low .. Ranges (R).High loop
274 Result (C) := True;
275 end loop;
276 end loop;
278 return Result;
279 end To_Set;
281 function To_Set (Span : Character_Range) return Character_Set is
282 Result : Character_Set;
283 begin
284 for C in Result'Range loop
285 Result (C) := False;
286 end loop;
288 for C in Span.Low .. Span.High loop
289 Result (C) := True;
290 end loop;
292 return Result;
293 end To_Set;
295 function To_Set (Sequence : Character_Sequence) return Character_Set is
296 Result : Character_Set := Null_Set;
297 begin
298 for J in Sequence'Range loop
299 Result (Sequence (J)) := True;
300 end loop;
302 return Result;
303 end To_Set;
305 function To_Set (Singleton : Character) return Character_Set is
306 Result : Character_Set := Null_Set;
307 begin
308 Result (Singleton) := True;
309 return Result;
310 end To_Set;
312 -----------
313 -- Value --
314 -----------
316 function Value
317 (Map : Character_Mapping;
318 Element : Character) return Character
320 begin
321 return Map (Element);
322 end Value;
324 end Ada.Strings.Maps;