1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ W I D E _ M A P S --
9 -- Copyright (C) 1992-2015, 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 with Ada
.Unchecked_Deallocation
;
34 package body Ada
.Strings
.Wide_Wide_Maps
is
41 (Left
, Right
: Wide_Wide_Character_Set
) return Wide_Wide_Character_Set
43 LS
: constant Wide_Wide_Character_Ranges_Access
:= Left
.Set
;
44 RS
: constant Wide_Wide_Character_Ranges_Access
:= Right
.Set
;
46 Result
: Wide_Wide_Character_Ranges
(1 .. LS
'Last + RS
'Last);
47 -- Each range on the right can generate at least one more range in
48 -- the result, by splitting one of the left operand ranges.
54 Left_Low
: Wide_Wide_Character
;
55 -- Left_Low is lowest character of the L'th range not yet dealt with
58 if LS
'Last = 0 or else RS
'Last = 0 then
62 Left_Low
:= LS
(L
).Low
;
63 while R
<= RS
'Last loop
65 -- If next right range is below current left range, skip it
67 if RS
(R
).High
< Left_Low
then
70 -- If next right range above current left range, copy remainder of
71 -- the left range to the result
73 elsif RS
(R
).Low
> LS
(L
).High
then
75 Result
(N
).Low
:= Left_Low
;
76 Result
(N
).High
:= LS
(L
).High
;
78 exit when L
> LS
'Last;
79 Left_Low
:= LS
(L
).Low
;
82 -- Next right range overlaps bottom of left range
84 if RS
(R
).Low
<= Left_Low
then
86 -- Case of right range complete overlaps left range
88 if RS
(R
).High
>= LS
(L
).High
then
90 exit when L
> LS
'Last;
91 Left_Low
:= LS
(L
).Low
;
93 -- Case of right range eats lower part of left range
96 Left_Low
:= Wide_Wide_Character
'Succ (RS
(R
).High
);
100 -- Next right range overlaps some of left range, but not bottom
104 Result
(N
).Low
:= Left_Low
;
105 Result
(N
).High
:= Wide_Wide_Character
'Pred (RS
(R
).Low
);
107 -- Case of right range splits left range
109 if RS
(R
).High
< LS
(L
).High
then
110 Left_Low
:= Wide_Wide_Character
'Succ (RS
(R
).High
);
113 -- Case of right range overlaps top of left range
117 exit when L
> LS
'Last;
118 Left_Low
:= LS
(L
).Low
;
124 -- Copy remainder of left ranges to result
128 Result
(N
).Low
:= Left_Low
;
129 Result
(N
).High
:= LS
(L
).High
;
133 exit when L
> LS
'Last;
135 Result
(N
) := LS
(L
);
139 return (AF
.Controlled
with
140 Set
=> new Wide_Wide_Character_Ranges
'(Result (1 .. N)));
147 -- The sorted, discontiguous form is canonical, so equality can be used
149 function "=" (Left, Right : Wide_Wide_Character_Set) return Boolean is
151 return Left.Set.all = Right.Set.all;
159 (Left, Right : Wide_Wide_Character_Set) return Wide_Wide_Character_Set
161 LS : constant Wide_Wide_Character_Ranges_Access := Left.Set;
162 RS : constant Wide_Wide_Character_Ranges_Access := Right.Set;
164 Result : Wide_Wide_Character_Ranges (1 .. LS'Last + RS'Last);
169 -- Loop to search for overlapping character ranges
171 while L <= LS'Last and then R <= RS'Last loop
173 if LS (L).High < RS (R).Low then
176 elsif RS (R).High < LS (L).Low then
179 -- Here we have LS (L).High >= RS (R).Low
180 -- and RS (R).High >= LS (L).Low
181 -- so we have an overlapping range
186 Wide_Wide_Character'Max (LS (L).Low, RS (R).Low);
188 Wide_Wide_Character'Min (LS (L).High, RS (R).High);
190 if RS (R).High = LS (L).High then
193 elsif RS (R).High < LS (L).High then
201 return (AF.Controlled with
202 Set => new Wide_Wide_Character_Ranges'(Result
(1 .. N
)));
210 (Right
: Wide_Wide_Character_Set
) return Wide_Wide_Character_Set
212 RS
: constant Wide_Wide_Character_Ranges_Access
:= Right
.Set
;
214 Result
: Wide_Wide_Character_Ranges
(1 .. RS
'Last + 1);
220 Result
(1) := (Low
=> Wide_Wide_Character
'First,
221 High
=> Wide_Wide_Character
'Last);
224 if RS
(1).Low
/= Wide_Wide_Character
'First then
226 Result
(N
).Low
:= Wide_Wide_Character
'First;
227 Result
(N
).High
:= Wide_Wide_Character
'Pred (RS
(1).Low
);
230 for K
in 1 .. RS
'Last - 1 loop
232 Result
(N
).Low
:= Wide_Wide_Character
'Succ (RS
(K
).High
);
233 Result
(N
).High
:= Wide_Wide_Character
'Pred (RS
(K
+ 1).Low
);
236 if RS
(RS
'Last).High
/= Wide_Wide_Character
'Last then
238 Result
(N
).Low
:= Wide_Wide_Character
'Succ (RS
(RS
'Last).High
);
239 Result
(N
).High
:= Wide_Wide_Character
'Last;
243 return (AF
.Controlled
with
244 Set
=> new Wide_Wide_Character_Ranges
'(Result (1 .. N)));
252 (Left, Right : Wide_Wide_Character_Set) return Wide_Wide_Character_Set
254 LS : constant Wide_Wide_Character_Ranges_Access := Left.Set;
255 RS : constant Wide_Wide_Character_Ranges_Access := Right.Set;
257 Result : Wide_Wide_Character_Ranges (1 .. LS'Last + RS'Last);
266 -- Loop through ranges in output file
269 -- If no left ranges left, copy next right range
272 exit when R > RS'Last;
274 Result (N) := RS (R);
277 -- If no right ranges left, copy next left range
279 elsif R > RS'Last then
281 Result (N) := LS (L);
285 -- We have two ranges, choose lower one
289 if LS (L).Low <= RS (R).Low then
290 Result (N) := LS (L);
293 Result (N) := RS (R);
297 -- Loop to collapse ranges into last range
300 -- Collapse next length range into current result range
304 and then LS (L).Low <=
305 Wide_Wide_Character'Succ (Result (N).High)
308 Wide_Wide_Character'Max (Result (N).High, LS (L).High);
311 -- Collapse next right range into current result range
315 and then RS (R).Low <=
316 Wide_Wide_Character'Succ (Result (N).High)
319 Wide_Wide_Character'Max (Result (N).High, RS (R).High);
322 -- If neither range collapses, then done with this range
331 return (AF.Controlled with
332 Set => new Wide_Wide_Character_Ranges'(Result
(1 .. N
)));
340 (Left
, Right
: Wide_Wide_Character_Set
) return Wide_Wide_Character_Set
343 return (Left
or Right
) - (Left
and Right
);
350 procedure Adjust
(Object
: in out Wide_Wide_Character_Mapping
) is
352 Object
.Map
:= new Wide_Wide_Character_Mapping_Values
'(Object.Map.all);
355 procedure Adjust (Object : in out Wide_Wide_Character_Set) is
357 Object.Set := new Wide_Wide_Character_Ranges'(Object
.Set
.all);
364 procedure Finalize
(Object
: in out Wide_Wide_Character_Mapping
) is
366 procedure Free
is new Ada
.Unchecked_Deallocation
367 (Wide_Wide_Character_Mapping_Values
,
368 Wide_Wide_Character_Mapping_Values_Access
);
371 if Object
.Map
/= Null_Map
'Unrestricted_Access then
376 procedure Finalize
(Object
: in out Wide_Wide_Character_Set
) is
378 procedure Free
is new Ada
.Unchecked_Deallocation
379 (Wide_Wide_Character_Ranges
,
380 Wide_Wide_Character_Ranges_Access
);
383 if Object
.Set
/= Null_Range
'Unrestricted_Access then
392 procedure Initialize
(Object
: in out Wide_Wide_Character_Mapping
) is
397 procedure Initialize
(Object
: in out Wide_Wide_Character_Set
) is
407 (Element
: Wide_Wide_Character
;
408 Set
: Wide_Wide_Character_Set
) return Boolean
411 SS
: constant Wide_Wide_Character_Ranges_Access
:= Set
.Set
;
417 -- Binary search loop. The invariant is that if Element is in any of
418 -- of the constituent ranges it is in one between Set (L) and Set (R).
427 if Element
> SS
(M
).High
then
429 elsif Element
< SS
(M
).Low
then
443 (Elements
: Wide_Wide_Character_Set
;
444 Set
: Wide_Wide_Character_Set
) return Boolean
446 ES
: constant Wide_Wide_Character_Ranges_Access
:= Elements
.Set
;
447 SS
: constant Wide_Wide_Character_Ranges_Access
:= Set
.Set
;
454 -- If no more element ranges, done, and result is true
459 -- If more element ranges, but no more set ranges, result is false
461 elsif S
> SS
'Last then
464 -- Remove irrelevant set range
466 elsif SS
(S
).High
< ES
(E
).Low
then
469 -- Get rid of element range that is properly covered by set
471 elsif SS
(S
).Low
<= ES
(E
).Low
472 and then ES
(E
).High
<= SS
(S
).High
476 -- Otherwise we have a non-covered element range, result is false
489 (Map
: Wide_Wide_Character_Mapping
) return Wide_Wide_Character_Sequence
492 return Map
.Map
.Domain
;
500 (From
, To
: Wide_Wide_Character_Sequence
)
501 return Wide_Wide_Character_Mapping
503 Domain
: Wide_Wide_Character_Sequence
(1 .. From
'Length);
504 Rangev
: Wide_Wide_Character_Sequence
(1 .. To
'Length);
508 if From
'Length /= To
'Length then
509 raise Translation_Error
;
512 pragma Warnings
(Off
); -- apparent uninit use of Domain
514 for J
in From
'Range loop
516 if From
(J
) = Domain
(M
) then
517 raise Translation_Error
;
518 elsif From
(J
) < Domain
(M
) then
519 Domain
(M
+ 1 .. N
+ 1) := Domain
(M
.. N
);
520 Rangev
(M
+ 1 .. N
+ 1) := Rangev
(M
.. N
);
521 Domain
(M
) := From
(J
);
522 Rangev
(M
) := To
(J
);
527 Domain
(N
+ 1) := From
(J
);
528 Rangev
(N
+ 1) := To
(J
);
534 pragma Warnings
(On
);
536 return (AF
.Controlled
with
537 Map
=> new Wide_Wide_Character_Mapping_Values
'(
539 Domain => Domain (1 .. N),
540 Rangev => Rangev (1 .. N)));
549 (Map : Wide_Wide_Character_Mapping) return Wide_Wide_Character_Sequence
552 return Map.Map.Rangev;
560 (Set : Wide_Wide_Character_Set) return Wide_Wide_Character_Ranges
571 (Set : Wide_Wide_Character_Set) return Wide_Wide_Character_Sequence
573 SS : constant Wide_Wide_Character_Ranges_Access := Set.Set;
575 Count : Natural := 0;
578 for J in SS'Range loop
580 Count + (Wide_Wide_Character'Pos (SS (J).High) -
581 Wide_Wide_Character'Pos (SS (J).Low) + 1);
584 return Result : Wide_Wide_String (1 .. Count) do
585 for J in SS'Range loop
586 for K in SS (J).Low .. SS (J).High loop
598 -- Case of multiple range input
601 (Ranges : Wide_Wide_Character_Ranges) return Wide_Wide_Character_Set
603 Result : Wide_Wide_Character_Ranges (Ranges'Range);
608 -- The output of To_Set is required to be sorted by increasing Low
609 -- values, and discontiguous, so first we sort them as we enter them,
610 -- using a simple insertion sort.
612 pragma Warnings (Off);
613 -- Kill bogus warning on Result being uninitialized
615 for J in Ranges'Range loop
617 if Ranges (J).Low < Result (K).Low then
618 Result (K + 1 .. N + 1) := Result (K .. N);
619 Result (K) := Ranges (J);
624 Result (N + 1) := Ranges (J);
630 pragma Warnings (On);
632 -- Now collapse any contiguous or overlapping ranges
636 if Result (J).High < Result (J).Low then
638 Result (J .. N) := Result (J + 1 .. N + 1);
640 elsif Wide_Wide_Character'Succ (Result (J).High) >=
644 Wide_Wide_Character'Max (Result (J).High, Result (J + 1).High);
647 Result (J + 1 .. N) := Result (J + 2 .. N + 1);
654 if Result (N).High < Result (N).Low then
658 return (AF.Controlled with
659 Set => new Wide_Wide_Character_Ranges'(Result
(1 .. N
)));
662 -- Case of single range input
665 (Span
: Wide_Wide_Character_Range
) return Wide_Wide_Character_Set
668 if Span
.Low
> Span
.High
then
670 -- This is safe, because there is no procedure with parameter
671 -- Wide_Wide_Character_Set of mode "out" or "in out".
674 return (AF
.Controlled
with
675 Set
=> new Wide_Wide_Character_Ranges
'(1 => Span));
679 -- Case of wide string input
682 (Sequence : Wide_Wide_Character_Sequence) return Wide_Wide_Character_Set
684 R : Wide_Wide_Character_Ranges (1 .. Sequence'Length);
687 for J in R'Range loop
688 R (J) := (Sequence (J), Sequence (J));
694 -- Case of single wide character input
697 (Singleton : Wide_Wide_Character) return Wide_Wide_Character_Set
702 Set => new Wide_Wide_Character_Ranges'(1 => (Singleton
, Singleton
)));
710 (Map
: Wide_Wide_Character_Mapping
;
711 Element
: Wide_Wide_Character
) return Wide_Wide_Character
715 MV
: constant Wide_Wide_Character_Mapping_Values_Access
:= Map
.Map
;
721 -- Binary search loop
724 -- If not found, identity
729 -- Otherwise do binary divide
734 if Element
< MV
.Domain
(M
) then
737 elsif Element
> MV
.Domain
(M
) then
740 else -- Element = MV.Domain (M) then
741 return MV
.Rangev
(M
);
747 end Ada
.Strings
.Wide_Wide_Maps
;