1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ M A P S --
9 -- Copyright (C) 1992-2002 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 with Unchecked_Deallocation
;
36 package body Ada
.Strings
.Wide_Maps
is
43 (Left
, Right
: in Wide_Character_Set
)
44 return Wide_Character_Set
46 LS
: constant Wide_Character_Ranges_Access
:= Left
.Set
;
47 RS
: constant Wide_Character_Ranges_Access
:= Right
.Set
;
49 Result
: Wide_Character_Ranges
(1 .. LS
'Last + RS
'Last);
50 -- Each range on the right can generate at least one more range in
51 -- the result, by splitting one of the left operand ranges.
57 Left_Low
: Wide_Character;
58 -- Left_Low is lowest character of the L'th range not yet dealt with
61 if LS
'Last = 0 or else RS
'Last = 0 then
65 Left_Low
:= LS
(L
).Low
;
66 while R
<= RS
'Last loop
68 -- If next right range is below current left range, skip it
70 if RS
(R
).High
< Left_Low
then
73 -- If next right range above current left range, copy remainder
74 -- of the left range to the result
76 elsif RS
(R
).Low
> LS
(L
).High
then
78 Result
(N
).Low
:= Left_Low
;
79 Result
(N
).High
:= LS
(L
).High
;
81 exit when L
> LS
'Last;
82 Left_Low
:= LS
(L
).Low
;
85 -- Next right range overlaps bottom of left range
87 if RS
(R
).Low
<= Left_Low
then
89 -- Case of right range complete overlaps left range
91 if RS
(R
).High
>= LS
(L
).High
then
93 exit when L
> LS
'Last;
94 Left_Low
:= LS
(L
).Low
;
96 -- Case of right range eats lower part of left range
99 Left_Low
:= Wide_Character'Succ (RS
(R
).High
);
103 -- Next right range overlaps some of left range, but not bottom
107 Result
(N
).Low
:= Left_Low
;
108 Result
(N
).High
:= Wide_Character'Pred (RS
(R
).Low
);
110 -- Case of right range splits left range
112 if RS
(R
).High
< LS
(L
).High
then
113 Left_Low
:= Wide_Character'Succ (RS
(R
).High
);
116 -- Case of right range overlaps top of left range
120 exit when L
> LS
'Last;
121 Left_Low
:= LS
(L
).Low
;
127 -- Copy remainder of left ranges to result
131 Result
(N
).Low
:= Left_Low
;
132 Result
(N
).High
:= LS
(L
).High
;
136 exit when L
> LS
'Last;
138 Result
(N
) := LS
(L
);
142 return (AF
.Controlled
with
143 Set
=> new Wide_Character_Ranges
'(Result (1 .. N)));
150 -- The sorted, discontiguous form is canonical, so equality can be used
152 function "=" (Left, Right : in Wide_Character_Set) return Boolean is
154 return Left.Set.all = Right.Set.all;
162 (Left, Right : in Wide_Character_Set)
163 return Wide_Character_Set
165 LS : constant Wide_Character_Ranges_Access := Left.Set;
166 RS : constant Wide_Character_Ranges_Access := Right.Set;
168 Result : Wide_Character_Ranges (1 .. LS'Last + RS'Last);
173 -- Loop to search for overlapping character ranges
175 while L <= LS'Last and then R <= RS'Last loop
177 if LS (L).High < RS (R).Low then
180 elsif RS (R).High < LS (L).Low then
183 -- Here we have LS (L).High >= RS (R).Low
184 -- and RS (R).High >= LS (L).Low
185 -- so we have an overlapping range
189 Result (N).Low := Wide_Character'Max (LS (L).Low, RS (R).Low);
191 Wide_Character'Min (LS (L).High, RS (R).High);
193 if RS (R).High = LS (L).High then
196 elsif RS (R).High < LS (L).High then
204 return (AF.Controlled with
205 Set => new Wide_Character_Ranges'(Result
(1 .. N
)));
213 (Right
: in Wide_Character_Set
)
214 return Wide_Character_Set
216 RS
: constant Wide_Character_Ranges_Access
:= Right
.Set
;
218 Result
: Wide_Character_Ranges
(1 .. RS
'Last + 1);
224 Result
(1) := (Low
=> Wide_Character'First,
225 High
=> Wide_Character'Last);
228 if RS
(1).Low
/= Wide_Character'First then
230 Result
(N
).Low
:= Wide_Character'First;
231 Result
(N
).High
:= Wide_Character'Pred (RS
(1).Low
);
234 for K
in 1 .. RS
'Last - 1 loop
236 Result
(N
).Low
:= Wide_Character'Succ (RS
(K
).High
);
237 Result
(N
).High
:= Wide_Character'Pred (RS
(K
+ 1).Low
);
240 if RS
(RS
'Last).High
/= Wide_Character'Last then
242 Result
(N
).Low
:= Wide_Character'Succ (RS
(RS
'Last).High
);
243 Result
(N
).High
:= Wide_Character'Last;
247 return (AF
.Controlled
with
248 Set
=> new Wide_Character_Ranges
'(Result (1 .. N)));
256 (Left, Right : in Wide_Character_Set)
257 return Wide_Character_Set
259 LS : constant Wide_Character_Ranges_Access := Left.Set;
260 RS : constant Wide_Character_Ranges_Access := Right.Set;
262 Result : Wide_Character_Ranges (1 .. LS'Last + RS'Last);
271 -- Loop through ranges in output file
274 -- If no left ranges left, copy next right range
277 exit when R > RS'Last;
279 Result (N) := RS (R);
282 -- If no right ranges left, copy next left range
284 elsif R > RS'Last then
286 Result (N) := LS (L);
290 -- We have two ranges, choose lower one
294 if LS (L).Low <= RS (R).Low then
295 Result (N) := LS (L);
298 Result (N) := RS (R);
302 -- Loop to collapse ranges into last range
305 -- Collapse next length range into current result range
309 and then LS (L).Low <= Wide_Character'Succ (Result (N).High)
312 Wide_Character'Max (Result (N).High, LS (L).High);
315 -- Collapse next right range into current result range
319 and then RS (R).Low <=
320 Wide_Character'Succ (Result (N).High)
323 Wide_Character'Max (Result (N).High, RS (R).High);
326 -- If neither range collapses, then done with this range
335 return (AF.Controlled with
336 Set => new Wide_Character_Ranges'(Result
(1 .. N
)));
344 (Left
, Right
: in Wide_Character_Set
)
345 return Wide_Character_Set
348 return (Left
or Right
) - (Left
and Right
);
355 procedure Adjust
(Object
: in out Wide_Character_Mapping
) is
357 Object
.Map
:= new Wide_Character_Mapping_Values
'(Object.Map.all);
360 procedure Adjust (Object : in out Wide_Character_Set) is
362 Object.Set := new Wide_Character_Ranges'(Object
.Set
.all);
369 procedure Finalize
(Object
: in out Wide_Character_Mapping
) is
371 procedure Free
is new Unchecked_Deallocation
372 (Wide_Character_Mapping_Values
,
373 Wide_Character_Mapping_Values_Access
);
376 if Object
.Map
/= Null_Map
'Unrestricted_Access then
381 procedure Finalize
(Object
: in out Wide_Character_Set
) is
383 procedure Free
is new Unchecked_Deallocation
384 (Wide_Character_Ranges
,
385 Wide_Character_Ranges_Access
);
388 if Object
.Set
/= Null_Range
'Unrestricted_Access then
397 procedure Initialize
(Object
: in out Wide_Character_Mapping
) is
402 procedure Initialize
(Object
: in out Wide_Character_Set
) is
412 (Element
: in Wide_Character;
413 Set
: in Wide_Character_Set
)
417 SS
: constant Wide_Character_Ranges_Access
:= Set
.Set
;
423 -- Binary search loop. The invariant is that if Element is in any of
424 -- of the constituent ranges it is in one between Set (L) and Set (R).
433 if Element
> SS
(M
).High
then
435 elsif Element
< SS
(M
).Low
then
449 (Elements
: in Wide_Character_Set
;
450 Set
: in Wide_Character_Set
)
453 ES
: constant Wide_Character_Ranges_Access
:= Elements
.Set
;
454 SS
: constant Wide_Character_Ranges_Access
:= Set
.Set
;
461 -- If no more element ranges, done, and result is true
466 -- If more element ranges, but no more set ranges, result is false
468 elsif S
> SS
'Last then
471 -- Remove irrelevant set range
473 elsif SS
(S
).High
< ES
(E
).Low
then
476 -- Get rid of element range that is properly covered by set
478 elsif SS
(S
).Low
<= ES
(E
).Low
479 and then ES
(E
).High
<= SS
(S
).High
483 -- Otherwise we have a non-covered element range, result is false
496 (Map
: in Wide_Character_Mapping
)
497 return Wide_Character_Sequence
500 return Map
.Map
.Domain
;
508 (From
, To
: in Wide_Character_Sequence
)
509 return Wide_Character_Mapping
511 Domain
: Wide_Character_Sequence
(1 .. From
'Length);
512 Rangev
: Wide_Character_Sequence
(1 .. To
'Length);
516 if From
'Length /= To
'Length then
517 raise Translation_Error
;
520 pragma Warnings
(Off
); -- apparent uninit use of Domain
522 for J
in From
'Range loop
524 if From
(J
) = Domain
(M
) then
525 raise Translation_Error
;
526 elsif From
(J
) < Domain
(M
) then
527 Domain
(M
+ 1 .. N
+ 1) := Domain
(M
.. N
);
528 Rangev
(M
+ 1 .. N
+ 1) := Rangev
(M
.. N
);
529 Domain
(M
) := From
(J
);
530 Rangev
(M
) := To
(J
);
535 Domain
(N
+ 1) := From
(J
);
536 Rangev
(N
+ 1) := To
(J
);
542 pragma Warnings
(On
);
544 return (AF
.Controlled
with
545 Map
=> new Wide_Character_Mapping_Values
'(
547 Domain => Domain (1 .. N),
548 Rangev => Rangev (1 .. N)));
557 (Map : in Wide_Character_Mapping)
558 return Wide_Character_Sequence
561 return Map.Map.Rangev;
569 (Set : in Wide_Character_Set)
570 return Wide_Character_Ranges
581 (Set : in Wide_Character_Set)
582 return Wide_Character_Sequence
584 SS : constant Wide_Character_Ranges_Access := Set.Set;
586 Result : Wide_String (Positive range 1 .. 2 ** 16);
590 for J in SS'Range loop
591 for K in SS (J).Low .. SS (J).High loop
597 return Result (1 .. N);
604 -- Case of multiple range input
607 (Ranges : in Wide_Character_Ranges)
608 return Wide_Character_Set
610 Result : Wide_Character_Ranges (Ranges'Range);
615 -- The output of To_Set is required to be sorted by increasing Low
616 -- values, and discontiguous, so first we sort them as we enter them,
617 -- using a simple insertion sort.
619 pragma Warnings (Off);
620 -- Kill bogus warning on Result being uninitialized
622 for J in Ranges'Range loop
624 if Ranges (J).Low < Result (K).Low then
625 Result (K + 1 .. N + 1) := Result (K .. N);
626 Result (K) := Ranges (J);
631 Result (N + 1) := Ranges (J);
637 pragma Warnings (On);
639 -- Now collapse any contiguous or overlapping ranges
643 if Result (J).High < Result (J).Low then
645 Result (J .. N) := Result (J + 1 .. N + 1);
647 elsif Wide_Character'Succ (Result (J).High) >= Result (J + 1).Low then
649 Wide_Character'Max (Result (J).High, Result (J + 1).High);
652 Result (J + 1 .. N) := Result (J + 2 .. N + 1);
659 if Result (N).High < Result (N).Low then
663 return (AF.Controlled with
664 Set => new Wide_Character_Ranges'(Result
(1 .. N
)));
667 -- Case of single range input
670 (Span
: in Wide_Character_Range
)
671 return Wide_Character_Set
674 if Span
.Low
> Span
.High
then
676 -- This is safe, because there is no procedure with parameter
677 -- Wide_Character_Set of mode "out" or "in out".
680 return (AF
.Controlled
with
681 Set
=> new Wide_Character_Ranges
'(1 => Span));
685 -- Case of wide string input
688 (Sequence : in Wide_Character_Sequence)
689 return Wide_Character_Set
691 R : Wide_Character_Ranges (1 .. Sequence'Length);
694 for J in R'Range loop
695 R (J) := (Sequence (J), Sequence (J));
701 -- Case of single wide character input
704 (Singleton : in Wide_Character)
705 return Wide_Character_Set
710 Set => new Wide_Character_Ranges'(1 => (Singleton
, Singleton
)));
718 (Map
: in Wide_Character_Mapping
;
719 Element
: in Wide_Character)
720 return Wide_Character
724 MV
: constant Wide_Character_Mapping_Values_Access
:= Map
.Map
;
730 -- Binary search loop
733 -- If not found, identity
738 -- Otherwise do binary divide
743 if Element
< MV
.Domain
(M
) then
746 elsif Element
> MV
.Domain
(M
) then
749 else -- Element = MV.Domain (M) then
750 return MV
.Rangev
(M
);
756 end Ada
.Strings
.Wide_Maps
;