1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . M A P S --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 -- Note: parts of this code are derived from the ADAR.CSH public domain
37 -- Ada 83 versions of the Appendix C string handling packages. The main
38 -- differences are that we avoid the use of the minimize function which
39 -- is bit-by-bit or character-by-character and therefore rather slow.
40 -- Generally for character sets we favor the full 32-byte representation.
42 package body Ada.Strings.Maps is
44 use Ada.Characters.Latin_1;
50 function "-" (Left, Right : Character_Set) return Character_Set is
52 return Left and not Right;
59 function "=" (Left, Right : in Character_Set) return Boolean is
61 return Character_Set_Internal (Left) = Character_Set_Internal (Right);
68 function "and" (Left, Right : in Character_Set) return Character_Set is
71 (Character_Set_Internal (Left) and Character_Set_Internal (Right));
78 function "not" (Right : in Character_Set) return Character_Set is
80 return Character_Set (not Character_Set_Internal (Right));
87 function "or" (Left, Right : in Character_Set) return Character_Set is
90 (Character_Set_Internal (Left) or Character_Set_Internal (Right));
97 function "xor" (Left, Right : in Character_Set) return Character_Set is
100 (Character_Set_Internal (Left) xor Character_Set_Internal (Right));
108 (Element : Character;
113 return Set (Element);
121 (Elements : Character_Set;
126 return (Elements and Set) = Elements;
133 function To_Domain (Map : in Character_Mapping) return Character_Sequence
135 Result : String (1 .. Map'Length);
140 for C in Map'Range loop
147 return Result (1 .. J);
155 (From, To : in Character_Sequence)
156 return Character_Mapping
158 Result : Character_Mapping;
159 Inserted : Character_Set := Null_Set;
160 From_Len : constant Natural := From'Length;
161 To_Len : constant Natural := To'Length;
164 if From_Len /= To_Len then
165 raise Strings.Translation_Error;
168 for Char in Character loop
169 Result (Char) := Char;
172 for J in From'Range loop
173 if Inserted (From (J)) then
174 raise Strings.Translation_Error;
177 Result (From (J)) := To (J - From'First + To'First);
178 Inserted (From (J)) := True;
188 function To_Range (Map : in Character_Mapping) return Character_Sequence
190 Result : String (1 .. Map'Length);
195 for C in Map'Range loop
198 Result (J) := Map (C);
202 return Result (1 .. J);
209 function To_Ranges (Set : in Character_Set) return Character_Ranges is
210 Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
215 C := Character'First;
219 -- Skip gap between subsets.
221 while not Set (C) loop
222 exit when C = Character'Last;
223 C := Character'Succ (C);
226 exit when not Set (C);
228 Range_Num := Range_Num + 1;
229 Max_Ranges (Range_Num).Low := C;
234 exit when not Set (C) or else C = Character'Last;
235 C := Character' Succ (C);
239 Max_Ranges (Range_Num). High := C;
242 Max_Ranges (Range_Num). High := Character'Pred (C);
246 return Max_Ranges (1 .. Range_Num);
254 (Set : Character_Set)
255 return Character_Sequence
257 Result : String (1 .. Character'Pos (Character'Last) + 1);
258 Count : Natural := 0;
261 for Char in Set'Range loop
264 Result (Count) := Char;
268 return Result (1 .. Count);
275 function To_Set (Ranges : in Character_Ranges) return Character_Set is
276 Result : Character_Set;
279 for C in Result'Range loop
283 for R in Ranges'Range loop
284 for C in Ranges (R).Low .. Ranges (R).High loop
292 function To_Set (Span : in Character_Range) return Character_Set is
293 Result : Character_Set;
296 for C in Result'Range loop
300 for C in Span.Low .. Span.High loop
307 function To_Set (Sequence : Character_Sequence) return Character_Set is
308 Result : Character_Set := Null_Set;
311 for J in Sequence'Range loop
312 Result (Sequence (J)) := True;
318 function To_Set (Singleton : Character) return Character_Set is
319 Result : Character_Set := Null_Set;
322 Result (Singleton) := True;
330 function Value (Map : in Character_Mapping; Element : in Character)
334 return Map (Element);
337 end Ada.Strings.Maps;