OSDN Git Service

2007-04-20 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-strmap.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
33
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.
39
40 package body Ada.Strings.Maps is
41
42    use Ada.Characters.Latin_1;
43
44    ---------
45    -- "-" --
46    ---------
47
48    function "-" (Left, Right : Character_Set) return Character_Set is
49    begin
50       return Left and not Right;
51    end "-";
52
53    ---------
54    -- "=" --
55    ---------
56
57    function "=" (Left, Right : Character_Set) return Boolean is
58    begin
59       return Character_Set_Internal (Left) = Character_Set_Internal (Right);
60    end "=";
61
62    -----------
63    -- "and" --
64    -----------
65
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";
71
72    -----------
73    -- "not" --
74    -----------
75
76    function "not" (Right : Character_Set) return Character_Set is
77    begin
78       return Character_Set (not Character_Set_Internal (Right));
79    end "not";
80
81    ----------
82    -- "or" --
83    ----------
84
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";
90
91    -----------
92    -- "xor" --
93    -----------
94
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";
100
101    -----------
102    -- Is_In --
103    -----------
104
105    function Is_In
106      (Element : Character;
107       Set     : Character_Set) return Boolean
108    is
109    begin
110       return Set (Element);
111    end Is_In;
112
113    ---------------
114    -- Is_Subset --
115    ---------------
116
117    function Is_Subset
118      (Elements : Character_Set;
119       Set      : Character_Set) return Boolean
120    is
121    begin
122       return (Elements and Set) = Elements;
123    end Is_Subset;
124
125    ---------------
126    -- To_Domain --
127    ---------------
128
129    function To_Domain (Map : Character_Mapping) return Character_Sequence
130    is
131       Result : String (1 .. Map'Length);
132       J      : Natural;
133
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;
142
143       return Result (1 .. J);
144    end To_Domain;
145
146    ----------------
147    -- To_Mapping --
148    ----------------
149
150    function To_Mapping
151      (From, To : Character_Sequence) return Character_Mapping
152    is
153       Result   : Character_Mapping;
154       Inserted : Character_Set := Null_Set;
155       From_Len : constant Natural := From'Length;
156       To_Len   : constant Natural := To'Length;
157
158    begin
159       if From_Len /= To_Len then
160          raise Strings.Translation_Error;
161       end if;
162
163       for Char in Character loop
164          Result (Char) := Char;
165       end loop;
166
167       for J in From'Range loop
168          if Inserted (From (J)) then
169             raise Strings.Translation_Error;
170          end if;
171
172          Result   (From (J)) := To (J - From'First + To'First);
173          Inserted (From (J)) := True;
174       end loop;
175
176       return Result;
177    end To_Mapping;
178
179    --------------
180    -- To_Range --
181    --------------
182
183    function To_Range (Map : Character_Mapping) return Character_Sequence
184    is
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;
195
196       return Result (1 .. J);
197    end To_Range;
198
199    ---------------
200    -- To_Ranges --
201    ---------------
202
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;
207
208    begin
209       C := Character'First;
210       Range_Num := 0;
211
212       loop
213          --  Skip gap between subsets
214
215          while not Set (C) loop
216             exit when C = Character'Last;
217             C := Character'Succ (C);
218          end loop;
219
220          exit when not Set (C);
221
222          Range_Num := Range_Num + 1;
223          Max_Ranges (Range_Num).Low := C;
224
225          --  Span a subset
226
227          loop
228             exit when not Set (C) or else C = Character'Last;
229             C := Character'Succ (C);
230          end loop;
231
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;
239
240       return Max_Ranges (1 .. Range_Num);
241    end To_Ranges;
242
243    -----------------
244    -- To_Sequence --
245    -----------------
246
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;
257
258       return Result (1 .. Count);
259    end To_Sequence;
260
261    ------------
262    -- To_Set --
263    ------------
264
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;
271
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;
277
278       return Result;
279    end To_Set;
280
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;
287
288       for C in Span.Low .. Span.High loop
289          Result (C) := True;
290       end loop;
291
292       return Result;
293    end To_Set;
294
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;
301
302       return Result;
303    end To_Set;
304
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;
311
312    -----------
313    -- Value --
314    -----------
315
316    function Value
317      (Map     : Character_Mapping;
318       Element : Character) return Character
319    is
320    begin
321       return Map (Element);
322    end Value;
323
324 end Ada.Strings.Maps;