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-2005, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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
: Character_Set
) return Boolean is
59 return Character_Set_Internal
(Left
) = Character_Set_Internal
(Right
);
66 function "and" (Left
, Right
: Character_Set
) return Character_Set
is
69 (Character_Set_Internal
(Left
) and Character_Set_Internal
(Right
));
76 function "not" (Right
: Character_Set
) return Character_Set
is
78 return Character_Set
(not Character_Set_Internal
(Right
));
85 function "or" (Left
, Right
: Character_Set
) return Character_Set
is
88 (Character_Set_Internal
(Left
) or Character_Set_Internal
(Right
));
95 function "xor" (Left
, Right
: Character_Set
) return Character_Set
is
98 (Character_Set_Internal
(Left
) xor Character_Set_Internal
(Right
));
106 (Element
: Character;
107 Set
: Character_Set
) return Boolean
110 return Set
(Element
);
118 (Elements
: Character_Set
;
119 Set
: Character_Set
) return Boolean
122 return (Elements
and Set
) = Elements
;
129 function To_Domain
(Map
: Character_Mapping
) return Character_Sequence
131 Result
: String (1 .. Map
'Length);
136 for C
in Map
'Range loop
143 return Result
(1 .. J
);
151 (From
, To
: Character_Sequence
) return Character_Mapping
153 Result
: Character_Mapping
;
154 Inserted
: Character_Set
:= Null_Set
;
155 From_Len
: constant Natural := From
'Length;
156 To_Len
: constant Natural := To
'Length;
159 if From_Len
/= To_Len
then
160 raise Strings
.Translation_Error
;
163 for Char
in Character loop
164 Result
(Char
) := Char
;
167 for J
in From
'Range loop
168 if Inserted
(From
(J
)) then
169 raise Strings
.Translation_Error
;
172 Result
(From
(J
)) := To
(J
- From
'First + To
'First);
173 Inserted
(From
(J
)) := True;
183 function To_Range
(Map
: Character_Mapping
) return Character_Sequence
185 Result
: String (1 .. Map
'Length);
189 for C
in Map
'Range loop
192 Result
(J
) := Map
(C
);
196 return Result
(1 .. J
);
203 function To_Ranges
(Set
: Character_Set
) return Character_Ranges
is
204 Max_Ranges
: Character_Ranges
(1 .. Set
'Length / 2 + 1);
209 C
:= Character'First;
213 -- Skip gap between subsets
215 while not Set
(C
) loop
216 exit when C
= Character'Last;
217 C
:= Character'Succ (C
);
220 exit when not Set
(C
);
222 Range_Num
:= Range_Num
+ 1;
223 Max_Ranges
(Range_Num
).Low
:= C
;
228 exit when not Set
(C
) or else C
= Character'Last;
229 C
:= Character'Succ (C
);
233 Max_Ranges
(Range_Num
). High
:= C
;
236 Max_Ranges
(Range_Num
). High
:= Character'Pred (C
);
240 return Max_Ranges
(1 .. Range_Num
);
247 function To_Sequence
(Set
: Character_Set
) return Character_Sequence
is
248 Result
: String (1 .. Character'Pos (Character'Last) + 1);
249 Count
: Natural := 0;
251 for Char
in Set
'Range loop
254 Result
(Count
) := Char
;
258 return Result
(1 .. Count
);
265 function To_Set
(Ranges
: Character_Ranges
) return Character_Set
is
266 Result
: Character_Set
;
268 for C
in Result
'Range loop
272 for R
in Ranges
'Range loop
273 for C
in Ranges
(R
).Low
.. Ranges
(R
).High
loop
281 function To_Set
(Span
: Character_Range
) return Character_Set
is
282 Result
: Character_Set
;
284 for C
in Result
'Range loop
288 for C
in Span
.Low
.. Span
.High
loop
295 function To_Set
(Sequence
: Character_Sequence
) return Character_Set
is
296 Result
: Character_Set
:= Null_Set
;
298 for J
in Sequence
'Range loop
299 Result
(Sequence
(J
)) := True;
305 function To_Set
(Singleton
: Character) return Character_Set
is
306 Result
: Character_Set
:= Null_Set
;
308 Result
(Singleton
) := True;
317 (Map
: Character_Mapping
;
318 Element
: Character) return Character
321 return Map
(Element
);
324 end Ada
.Strings
.Maps
;