1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . M A P S --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
48 function "-" (Left, Right : Character_Set) return Character_Set is
50 return Left and not Right;
57 function "=" (Left, Right : in Character_Set) return Boolean is
59 return Character_Set_Internal (Left) = Character_Set_Internal (Right);
66 function "and" (Left, Right : in Character_Set) return Character_Set is
69 (Character_Set_Internal (Left) and Character_Set_Internal (Right));
76 function "not" (Right : in Character_Set) return Character_Set is
78 return Character_Set (not Character_Set_Internal (Right));
85 function "or" (Left, Right : in Character_Set) return Character_Set is
88 (Character_Set_Internal (Left) or Character_Set_Internal (Right));
95 function "xor" (Left, Right : in Character_Set) return Character_Set is
98 (Character_Set_Internal (Left) xor Character_Set_Internal (Right));
106 (Element : Character;
111 return Set (Element);
119 (Elements : Character_Set;
124 return (Elements and Set) = Elements;
131 function To_Domain (Map : in Character_Mapping) return Character_Sequence
133 Result : String (1 .. Map'Length);
138 for C in Map'Range loop
145 return Result (1 .. J);
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;
162 if From_Len /= To_Len then
163 raise Strings.Translation_Error;
166 for Char in Character loop
167 Result (Char) := Char;
170 for J in From'Range loop
171 if Inserted (From (J)) then
172 raise Strings.Translation_Error;
175 Result (From (J)) := To (J - From'First + To'First);
176 Inserted (From (J)) := True;
186 function To_Range (Map : in Character_Mapping) return Character_Sequence
188 Result : String (1 .. Map'Length);
193 for C in Map'Range loop
196 Result (J) := Map (C);
200 return Result (1 .. J);
207 function To_Ranges (Set : in Character_Set) return Character_Ranges is
208 Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
213 C := Character'First;
217 -- Skip gap between subsets.
219 while not Set (C) loop
220 exit when C = Character'Last;
221 C := Character'Succ (C);
224 exit when not Set (C);
226 Range_Num := Range_Num + 1;
227 Max_Ranges (Range_Num).Low := C;
232 exit when not Set (C) or else C = Character'Last;
233 C := Character' Succ (C);
237 Max_Ranges (Range_Num). High := C;
240 Max_Ranges (Range_Num). High := Character'Pred (C);
244 return Max_Ranges (1 .. Range_Num);
252 (Set : Character_Set)
253 return Character_Sequence
255 Result : String (1 .. Character'Pos (Character'Last) + 1);
256 Count : Natural := 0;
259 for Char in Set'Range loop
262 Result (Count) := Char;
266 return Result (1 .. Count);
273 function To_Set (Ranges : in Character_Ranges) return Character_Set is
274 Result : Character_Set;
277 for C in Result'Range loop
281 for R in Ranges'Range loop
282 for C in Ranges (R).Low .. Ranges (R).High loop
290 function To_Set (Span : in Character_Range) return Character_Set is
291 Result : Character_Set;
294 for C in Result'Range loop
298 for C in Span.Low .. Span.High loop
305 function To_Set (Sequence : Character_Sequence) return Character_Set is
306 Result : Character_Set := Null_Set;
309 for J in Sequence'Range loop
310 Result (Sequence (J)) := True;
316 function To_Set (Singleton : Character) return Character_Set is
317 Result : Character_Set := Null_Set;
320 Result (Singleton) := True;
328 function Value (Map : in Character_Mapping; Element : in Character)
332 return Map (Element);
335 end Ada.Strings.Maps;