1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ 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 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 with Unchecked_Deallocation
;
37 package body Ada
.Strings
.Wide_Maps
is
44 (Left
, Right
: in Wide_Character_Set
)
45 return Wide_Character_Set
47 LS
: constant Wide_Character_Ranges_Access
:= Left
.Set
;
48 RS
: constant Wide_Character_Ranges_Access
:= Right
.Set
;
50 Result
: Wide_Character_Ranges
(1 .. LS
'Last + RS
'Last);
51 -- Each range on the right can generate at least one more range in
52 -- the result, by splitting one of the left operand ranges.
58 Left_Low
: Wide_Character;
59 -- Left_Low is lowest character of the L'th range not yet dealt with
62 if LS
'Last = 0 or else RS
'Last = 0 then
66 Left_Low
:= LS
(L
).Low
;
67 while R
<= RS
'Last loop
69 -- If next right range is below current left range, skip it
71 if RS
(R
).High
< Left_Low
then
74 -- If next right range above current left range, copy remainder
75 -- of the left range to the result
77 elsif RS
(R
).Low
> LS
(L
).High
then
79 Result
(N
).Low
:= Left_Low
;
80 Result
(N
).High
:= LS
(L
).High
;
82 exit when L
> LS
'Last;
83 Left_Low
:= LS
(L
).Low
;
86 -- Next right range overlaps bottom of left range
88 if RS
(R
).Low
<= Left_Low
then
90 -- Case of right range complete overlaps left range
92 if RS
(R
).High
>= LS
(L
).High
then
94 exit when L
> LS
'Last;
95 Left_Low
:= LS
(L
).Low
;
97 -- Case of right range eats lower part of left range
100 Left_Low
:= Wide_Character'Succ (RS
(R
).High
);
104 -- Next right range overlaps some of left range, but not bottom
108 Result
(N
).Low
:= Left_Low
;
109 Result
(N
).High
:= Wide_Character'Pred (RS
(R
).Low
);
111 -- Case of right range splits left range
113 if RS
(R
).High
< LS
(L
).High
then
114 Left_Low
:= Wide_Character'Succ (RS
(R
).High
);
117 -- Case of right range overlaps top of left range
121 exit when L
> LS
'Last;
122 Left_Low
:= LS
(L
).Low
;
128 -- Copy remainder of left ranges to result
132 Result
(N
).Low
:= Left_Low
;
133 Result
(N
).High
:= LS
(L
).High
;
137 exit when L
> LS
'Last;
139 Result
(N
) := LS
(L
);
143 return (AF
.Controlled
with
144 Set
=> new Wide_Character_Ranges
'(Result (1 .. N)));
151 -- The sorted, discontiguous form is canonical, so equality can be used
153 function "=" (Left, Right : in Wide_Character_Set) return Boolean is
155 return Left.Set.all = Right.Set.all;
163 (Left, Right : in Wide_Character_Set)
164 return Wide_Character_Set
166 LS : constant Wide_Character_Ranges_Access := Left.Set;
167 RS : constant Wide_Character_Ranges_Access := Right.Set;
169 Result : Wide_Character_Ranges (1 .. LS'Last + RS'Last);
174 -- Loop to search for overlapping character ranges
176 while L <= LS'Last and then R <= RS'Last loop
178 if LS (L).High < RS (R).Low then
181 elsif RS (R).High < LS (L).Low then
184 -- Here we have LS (L).High >= RS (R).Low
185 -- and RS (R).High >= LS (L).Low
186 -- so we have an overlapping range
190 Result (N).Low := Wide_Character'Max (LS (L).Low, RS (R).Low);
192 Wide_Character'Min (LS (L).High, RS (R).High);
194 if RS (R).High = LS (L).High then
197 elsif RS (R).High < LS (L).High then
205 return (AF.Controlled with
206 Set => new Wide_Character_Ranges'(Result
(1 .. N
)));
214 (Right
: in Wide_Character_Set
)
215 return Wide_Character_Set
217 RS
: constant Wide_Character_Ranges_Access
:= Right
.Set
;
219 Result
: Wide_Character_Ranges
(1 .. RS
'Last + 1);
225 Result
(1) := (Low
=> Wide_Character'First,
226 High
=> Wide_Character'Last);
229 if RS
(1).Low
/= Wide_Character'First then
231 Result
(N
).Low
:= Wide_Character'First;
232 Result
(N
).High
:= Wide_Character'Pred (RS
(1).Low
);
235 for K
in 1 .. RS
'Last - 1 loop
237 Result
(N
).Low
:= Wide_Character'Succ (RS
(K
).High
);
238 Result
(N
).High
:= Wide_Character'Pred (RS
(K
+ 1).Low
);
241 if RS
(RS
'Last).High
/= Wide_Character'Last then
243 Result
(N
).Low
:= Wide_Character'Succ (RS
(RS
'Last).High
);
244 Result
(N
).High
:= Wide_Character'Last;
248 return (AF
.Controlled
with
249 Set
=> new Wide_Character_Ranges
'(Result (1 .. N)));
257 (Left, Right : in Wide_Character_Set)
258 return Wide_Character_Set
260 LS : constant Wide_Character_Ranges_Access := Left.Set;
261 RS : constant Wide_Character_Ranges_Access := Right.Set;
263 Result : Wide_Character_Ranges (1 .. LS'Last + RS'Last);
272 -- Loop through ranges in output file
275 -- If no left ranges left, copy next right range
278 exit when R > RS'Last;
280 Result (N) := RS (R);
283 -- If no right ranges left, copy next left range
285 elsif R > RS'Last then
287 Result (N) := LS (L);
291 -- We have two ranges, choose lower one
295 if LS (L).Low <= RS (R).Low then
296 Result (N) := LS (L);
299 Result (N) := RS (R);
303 -- Loop to collapse ranges into last range
306 -- Collapse next length range into current result range
310 and then LS (L).Low <= Wide_Character'Succ (Result (N).High)
313 Wide_Character'Max (Result (N).High, LS (L).High);
316 -- Collapse next right range into current result range
320 and then RS (R).Low <=
321 Wide_Character'Succ (Result (N).High)
324 Wide_Character'Max (Result (N).High, RS (R).High);
327 -- If neither range collapses, then done with this range
336 return (AF.Controlled with
337 Set => new Wide_Character_Ranges'(Result
(1 .. N
)));
345 (Left
, Right
: in Wide_Character_Set
)
346 return Wide_Character_Set
349 return (Left
or Right
) - (Left
and Right
);
356 procedure Adjust
(Object
: in out Wide_Character_Mapping
) is
358 Object
.Map
:= new Wide_Character_Mapping_Values
'(Object.Map.all);
361 procedure Adjust (Object : in out Wide_Character_Set) is
363 Object.Set := new Wide_Character_Ranges'(Object
.Set
.all);
370 procedure Finalize
(Object
: in out Wide_Character_Mapping
) is
372 procedure Free
is new Unchecked_Deallocation
373 (Wide_Character_Mapping_Values
,
374 Wide_Character_Mapping_Values_Access
);
377 if Object
.Map
/= Null_Map
'Unrestricted_Access then
382 procedure Finalize
(Object
: in out Wide_Character_Set
) is
384 procedure Free
is new Unchecked_Deallocation
385 (Wide_Character_Ranges
,
386 Wide_Character_Ranges_Access
);
389 if Object
.Set
/= Null_Range
'Unrestricted_Access then
398 procedure Initialize
(Object
: in out Wide_Character_Mapping
) is
403 procedure Initialize
(Object
: in out Wide_Character_Set
) is
413 (Element
: in Wide_Character;
414 Set
: in Wide_Character_Set
)
418 SS
: constant Wide_Character_Ranges_Access
:= Set
.Set
;
424 -- Binary search loop. The invariant is that if Element is in any of
425 -- of the constituent ranges it is in one between Set (L) and Set (R).
434 if Element
> SS
(M
).High
then
436 elsif Element
< SS
(M
).Low
then
450 (Elements
: in Wide_Character_Set
;
451 Set
: in Wide_Character_Set
)
454 ES
: constant Wide_Character_Ranges_Access
:= Elements
.Set
;
455 SS
: constant Wide_Character_Ranges_Access
:= Set
.Set
;
462 -- If no more element ranges, done, and result is true
467 -- If more element ranges, but no more set ranges, result is false
469 elsif S
> SS
'Last then
472 -- Remove irrelevant set range
474 elsif SS
(S
).High
< ES
(E
).Low
then
477 -- Get rid of element range that is properly covered by set
479 elsif SS
(S
).Low
<= ES
(E
).Low
480 and then ES
(E
).High
<= SS
(S
).High
484 -- Otherwise we have a non-covered element range, result is false
497 (Map
: in Wide_Character_Mapping
)
498 return Wide_Character_Sequence
501 return Map
.Map
.Domain
;
509 (From
, To
: in Wide_Character_Sequence
)
510 return Wide_Character_Mapping
512 Domain
: Wide_Character_Sequence
(1 .. From
'Length);
513 Rangev
: Wide_Character_Sequence
(1 .. To
'Length);
517 if From
'Length /= To
'Length then
518 raise Translation_Error
;
521 pragma Warnings
(Off
); -- apparent uninit use of Domain
523 for J
in From
'Range loop
525 if From
(J
) = Domain
(M
) then
526 raise Translation_Error
;
527 elsif From
(J
) < Domain
(M
) then
528 Domain
(M
+ 1 .. N
+ 1) := Domain
(M
.. N
);
529 Rangev
(M
+ 1 .. N
+ 1) := Rangev
(M
.. N
);
530 Domain
(M
) := From
(J
);
531 Rangev
(M
) := To
(J
);
536 Domain
(N
+ 1) := From
(J
);
537 Rangev
(N
+ 1) := To
(J
);
543 pragma Warnings
(On
);
545 return (AF
.Controlled
with
546 Map
=> new Wide_Character_Mapping_Values
'(
548 Domain => Domain (1 .. N),
549 Rangev => Rangev (1 .. N)));
558 (Map : in Wide_Character_Mapping)
559 return Wide_Character_Sequence
562 return Map.Map.Rangev;
570 (Set : in Wide_Character_Set)
571 return Wide_Character_Ranges
582 (Set : in Wide_Character_Set)
583 return Wide_Character_Sequence
585 SS : constant Wide_Character_Ranges_Access := Set.Set;
587 Result : Wide_String (Positive range 1 .. 2 ** 16);
591 for J in SS'Range loop
592 for K in SS (J).Low .. SS (J).High loop
598 return Result (1 .. N);
605 -- Case of multiple range input
608 (Ranges : in Wide_Character_Ranges)
609 return Wide_Character_Set
611 Result : Wide_Character_Ranges (Ranges'Range);
616 -- The output of To_Set is required to be sorted by increasing Low
617 -- values, and discontiguous, so first we sort them as we enter them,
618 -- using a simple insertion sort.
620 pragma Warnings (Off);
621 -- Kill bogus warning on Result being uninitialized
623 for J in Ranges'Range loop
625 if Ranges (J).Low < Result (K).Low then
626 Result (K + 1 .. N + 1) := Result (K .. N);
627 Result (K) := Ranges (J);
632 Result (N + 1) := Ranges (J);
638 pragma Warnings (On);
640 -- Now collapse any contiguous or overlapping ranges
644 if Result (J).High < Result (J).Low then
646 Result (J .. N) := Result (J + 1 .. N + 1);
648 elsif Wide_Character'Succ (Result (J).High) >= Result (J + 1).Low then
650 Wide_Character'Max (Result (J).High, Result (J + 1).High);
653 Result (J + 1 .. N) := Result (J + 2 .. N + 1);
660 if Result (N).High < Result (N).Low then
664 return (AF.Controlled with
665 Set => new Wide_Character_Ranges'(Result
(1 .. N
)));
668 -- Case of single range input
671 (Span
: in Wide_Character_Range
)
672 return Wide_Character_Set
675 if Span
.Low
> Span
.High
then
677 -- This is safe, because there is no procedure with parameter
678 -- Wide_Character_Set of mode "out" or "in out".
681 return (AF
.Controlled
with
682 Set
=> new Wide_Character_Ranges
'(1 => Span));
686 -- Case of wide string input
689 (Sequence : in Wide_Character_Sequence)
690 return Wide_Character_Set
692 R : Wide_Character_Ranges (1 .. Sequence'Length);
695 for J in R'Range loop
696 R (J) := (Sequence (J), Sequence (J));
702 -- Case of single wide character input
705 (Singleton : in Wide_Character)
706 return Wide_Character_Set
711 Set => new Wide_Character_Ranges' (1 => (Singleton
, Singleton
)));
719 (Map
: in Wide_Character_Mapping
;
720 Element
: in Wide_Character)
721 return Wide_Character
725 MV
: constant Wide_Character_Mapping_Values_Access
:= Map
.Map
;
731 -- Binary search loop
734 -- If not found, identity
739 -- Otherwise do binary divide
744 if Element
< MV
.Domain
(M
) then
747 elsif Element
> MV
.Domain
(M
) then
750 else -- Element = MV.Domain (M) then
751 return MV
.Rangev
(M
);
757 end Ada
.Strings
.Wide_Maps
;