1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . M A P S --
9 -- Copyright (C) 1992-2009, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- Note: parts of this code are derived from the ADAR.CSH public domain
33 -- Ada 83 versions of the Appendix C string handling packages. The main
34 -- differences are that we avoid the use of the minimize function which
35 -- is bit-by-bit or character-by-character and therefore rather slow.
36 -- Generally for character sets we favor the full 32-byte representation.
38 package body Ada.Strings.Maps is
40 use Ada.Characters.Latin_1;
46 function "-" (Left, Right : Character_Set) return Character_Set is
48 return Left and not Right;
55 function "=" (Left, Right : Character_Set) return Boolean is
57 return Character_Set_Internal (Left) = Character_Set_Internal (Right);
64 function "and" (Left, Right : Character_Set) return Character_Set is
67 (Character_Set_Internal (Left) and Character_Set_Internal (Right));
74 function "not" (Right : Character_Set) return Character_Set is
76 return Character_Set (not Character_Set_Internal (Right));
83 function "or" (Left, Right : Character_Set) return Character_Set is
86 (Character_Set_Internal (Left) or Character_Set_Internal (Right));
93 function "xor" (Left, Right : Character_Set) return Character_Set is
96 (Character_Set_Internal (Left) xor Character_Set_Internal (Right));
104 (Element : Character;
105 Set : Character_Set) return Boolean
108 return Set (Element);
116 (Elements : Character_Set;
117 Set : Character_Set) return Boolean
120 return (Elements and Set) = Elements;
127 function To_Domain (Map : Character_Mapping) return Character_Sequence
129 Result : String (1 .. Map'Length);
134 for C in Map'Range loop
141 return Result (1 .. J);
149 (From, To : Character_Sequence) return Character_Mapping
151 Result : Character_Mapping;
152 Inserted : Character_Set := Null_Set;
153 From_Len : constant Natural := From'Length;
154 To_Len : constant Natural := To'Length;
157 if From_Len /= To_Len then
158 raise Strings.Translation_Error;
161 for Char in Character loop
162 Result (Char) := Char;
165 for J in From'Range loop
166 if Inserted (From (J)) then
167 raise Strings.Translation_Error;
170 Result (From (J)) := To (J - From'First + To'First);
171 Inserted (From (J)) := True;
181 function To_Range (Map : Character_Mapping) return Character_Sequence
183 Result : String (1 .. Map'Length);
187 for C in Map'Range loop
190 Result (J) := Map (C);
194 return Result (1 .. J);
201 function To_Ranges (Set : Character_Set) return Character_Ranges is
202 Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
207 C := Character'First;
211 -- Skip gap between subsets
213 while not Set (C) loop
214 exit when C = Character'Last;
215 C := Character'Succ (C);
218 exit when not Set (C);
220 Range_Num := Range_Num + 1;
221 Max_Ranges (Range_Num).Low := C;
226 exit when not Set (C) or else C = Character'Last;
227 C := Character'Succ (C);
231 Max_Ranges (Range_Num). High := C;
234 Max_Ranges (Range_Num). High := Character'Pred (C);
238 return Max_Ranges (1 .. Range_Num);
245 function To_Sequence (Set : Character_Set) return Character_Sequence is
246 Result : String (1 .. Character'Pos (Character'Last) + 1);
247 Count : Natural := 0;
249 for Char in Set'Range loop
252 Result (Count) := Char;
256 return Result (1 .. Count);
263 function To_Set (Ranges : Character_Ranges) return Character_Set is
264 Result : Character_Set;
266 for C in Result'Range loop
270 for R in Ranges'Range loop
271 for C in Ranges (R).Low .. Ranges (R).High loop
279 function To_Set (Span : Character_Range) return Character_Set is
280 Result : Character_Set;
282 for C in Result'Range loop
286 for C in Span.Low .. Span.High loop
293 function To_Set (Sequence : Character_Sequence) return Character_Set is
294 Result : Character_Set := Null_Set;
296 for J in Sequence'Range loop
297 Result (Sequence (J)) := True;
303 function To_Set (Singleton : Character) return Character_Set is
304 Result : Character_Set := Null_Set;
306 Result (Singleton) := True;
315 (Map : Character_Mapping;
316 Element : Character) return Character
319 return Map (Element);
322 end Ada.Strings.Maps;