1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . M A P S --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- Note: parts of this code are derived from the ADAR.CSH public domain
36 -- Ada 83 versions of the Appendix C string handling packages. The main
37 -- differences are that we avoid the use of the minimize function which
38 -- is bit-by-bit or character-by-character and therefore rather slow.
39 -- Generally for character sets we favor the full 32-byte representation.
41 package body Ada.Strings.Maps is
43 use Ada.Characters.Latin_1;
49 function "-" (Left, Right : Character_Set) return Character_Set is
51 return Left and not Right;
58 function "=" (Left, Right : in Character_Set) return Boolean is
60 return Character_Set_Internal (Left) = Character_Set_Internal (Right);
67 function "and" (Left, Right : in Character_Set) return Character_Set is
70 (Character_Set_Internal (Left) and Character_Set_Internal (Right));
77 function "not" (Right : in Character_Set) return Character_Set is
79 return Character_Set (not Character_Set_Internal (Right));
86 function "or" (Left, Right : in Character_Set) return Character_Set is
89 (Character_Set_Internal (Left) or Character_Set_Internal (Right));
96 function "xor" (Left, Right : in Character_Set) return Character_Set is
99 (Character_Set_Internal (Left) xor Character_Set_Internal (Right));
107 (Element : Character;
112 return Set (Element);
120 (Elements : Character_Set;
125 return (Elements and Set) = Elements;
132 function To_Domain (Map : in Character_Mapping) return Character_Sequence
134 Result : String (1 .. Map'Length);
139 for C in Map'Range loop
146 return Result (1 .. J);
154 (From, To : in Character_Sequence)
155 return Character_Mapping
157 Result : Character_Mapping;
158 Inserted : Character_Set := Null_Set;
159 From_Len : constant Natural := From'Length;
160 To_Len : constant Natural := To'Length;
163 if From_Len /= To_Len then
164 raise Strings.Translation_Error;
167 for Char in Character loop
168 Result (Char) := Char;
171 for J in From'Range loop
172 if Inserted (From (J)) then
173 raise Strings.Translation_Error;
176 Result (From (J)) := To (J - From'First + To'First);
177 Inserted (From (J)) := True;
187 function To_Range (Map : in Character_Mapping) return Character_Sequence
189 Result : String (1 .. Map'Length);
194 for C in Map'Range loop
197 Result (J) := Map (C);
201 return Result (1 .. J);
208 function To_Ranges (Set : in Character_Set) return Character_Ranges is
209 Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
214 C := Character'First;
218 -- Skip gap between subsets.
220 while not Set (C) loop
221 exit when C = Character'Last;
222 C := Character'Succ (C);
225 exit when not Set (C);
227 Range_Num := Range_Num + 1;
228 Max_Ranges (Range_Num).Low := C;
233 exit when not Set (C) or else C = Character'Last;
234 C := Character' Succ (C);
238 Max_Ranges (Range_Num). High := C;
241 Max_Ranges (Range_Num). High := Character'Pred (C);
245 return Max_Ranges (1 .. Range_Num);
253 (Set : Character_Set)
254 return Character_Sequence
256 Result : String (1 .. Character'Pos (Character'Last) + 1);
257 Count : Natural := 0;
260 for Char in Set'Range loop
263 Result (Count) := Char;
267 return Result (1 .. Count);
274 function To_Set (Ranges : in Character_Ranges) return Character_Set is
275 Result : Character_Set;
278 for C in Result'Range loop
282 for R in Ranges'Range loop
283 for C in Ranges (R).Low .. Ranges (R).High loop
291 function To_Set (Span : in Character_Range) return Character_Set is
292 Result : Character_Set;
295 for C in Result'Range loop
299 for C in Span.Low .. Span.High loop
306 function To_Set (Sequence : Character_Sequence) return Character_Set is
307 Result : Character_Set := Null_Set;
310 for J in Sequence'Range loop
311 Result (Sequence (J)) := True;
317 function To_Set (Singleton : Character) return Character_Set is
318 Result : Character_Set := Null_Set;
321 Result (Singleton) := True;
329 function Value (Map : in Character_Mapping; Element : in Character)
333 return Map (Element);
336 end Ada.Strings.Maps;