(PREFERRED_DEBUGGING_TYPE): Use DWARF2_DEBUG.
[official-gcc.git] / gcc / ada / a-stwima.adb
blobe3bacd4498aae8dd55767aefb9bb65cb48761d29
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- A D A . S T R I N G S . W I D E _ M A P S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
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. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Unchecked_Deallocation;
36 package body Ada.Strings.Wide_Maps is
38 ---------
39 -- "-" --
40 ---------
42 function "-"
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.
53 N : Natural := 0;
54 R : Natural := 1;
55 L : Natural := 1;
57 Left_Low : Wide_Character;
58 -- Left_Low is lowest character of the L'th range not yet dealt with
60 begin
61 if LS'Last = 0 or else RS'Last = 0 then
62 return Left;
63 end if;
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
71 R := R + 1;
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
77 N := N + 1;
78 Result (N).Low := Left_Low;
79 Result (N).High := LS (L).High;
80 L := L + 1;
81 exit when L > LS'Last;
82 Left_Low := LS (L).Low;
84 else
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
92 L := L + 1;
93 exit when L > LS'Last;
94 Left_Low := LS (L).Low;
96 -- Case of right range eats lower part of left range
98 else
99 Left_Low := Wide_Character'Succ (RS (R).High);
100 R := R + 1;
101 end if;
103 -- Next right range overlaps some of left range, but not bottom
105 else
106 N := N + 1;
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);
114 R := R + 1;
116 -- Case of right range overlaps top of left range
118 else
119 L := L + 1;
120 exit when L > LS'Last;
121 Left_Low := LS (L).Low;
122 end if;
123 end if;
124 end if;
125 end loop;
127 -- Copy remainder of left ranges to result
129 if L <= LS'Last then
130 N := N + 1;
131 Result (N).Low := Left_Low;
132 Result (N).High := LS (L).High;
134 loop
135 L := L + 1;
136 exit when L > LS'Last;
137 N := N + 1;
138 Result (N) := LS (L);
139 end loop;
140 end if;
142 return (AF.Controlled with
143 Set => new Wide_Character_Ranges'(Result (1 .. N)));
144 end "-";
146 ---------
147 -- "=" --
148 ---------
150 -- The sorted, discontiguous form is canonical, so equality can be used
152 function "=" (Left, Right : in Wide_Character_Set) return Boolean is
153 begin
154 return Left.Set.all = Right.Set.all;
155 end "=";
157 -----------
158 -- "and" --
159 -----------
161 function "and"
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);
169 N : Natural := 0;
170 L, R : Natural := 1;
172 begin
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
178 L := L + 1;
180 elsif RS (R).High < LS (L).Low then
181 R := R + 1;
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
187 else
188 N := N + 1;
189 Result (N).Low := Wide_Character'Max (LS (L).Low, RS (R).Low);
190 Result (N).High :=
191 Wide_Character'Min (LS (L).High, RS (R).High);
193 if RS (R).High = LS (L).High then
194 L := L + 1;
195 R := R + 1;
196 elsif RS (R).High < LS (L).High then
197 R := R + 1;
198 else
199 L := L + 1;
200 end if;
201 end if;
202 end loop;
204 return (AF.Controlled with
205 Set => new Wide_Character_Ranges'(Result (1 .. N)));
206 end "and";
208 -----------
209 -- "not" --
210 -----------
212 function "not"
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);
219 N : Natural := 0;
221 begin
222 if RS'Last = 0 then
223 N := 1;
224 Result (1) := (Low => Wide_Character'First,
225 High => Wide_Character'Last);
227 else
228 if RS (1).Low /= Wide_Character'First then
229 N := N + 1;
230 Result (N).Low := Wide_Character'First;
231 Result (N).High := Wide_Character'Pred (RS (1).Low);
232 end if;
234 for K in 1 .. RS'Last - 1 loop
235 N := N + 1;
236 Result (N).Low := Wide_Character'Succ (RS (K).High);
237 Result (N).High := Wide_Character'Pred (RS (K + 1).Low);
238 end loop;
240 if RS (RS'Last).High /= Wide_Character'Last then
241 N := N + 1;
242 Result (N).Low := Wide_Character'Succ (RS (RS'Last).High);
243 Result (N).High := Wide_Character'Last;
244 end if;
245 end if;
247 return (AF.Controlled with
248 Set => new Wide_Character_Ranges'(Result (1 .. N)));
249 end "not";
251 ----------
252 -- "or" --
253 ----------
255 function "or"
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);
263 N : Natural;
264 L, R : Natural;
266 begin
267 N := 0;
268 L := 1;
269 R := 1;
271 -- Loop through ranges in output file
273 loop
274 -- If no left ranges left, copy next right range
276 if L > LS'Last then
277 exit when R > RS'Last;
278 N := N + 1;
279 Result (N) := RS (R);
280 R := R + 1;
282 -- If no right ranges left, copy next left range
284 elsif R > RS'Last then
285 N := N + 1;
286 Result (N) := LS (L);
287 L := L + 1;
289 else
290 -- We have two ranges, choose lower one
292 N := N + 1;
294 if LS (L).Low <= RS (R).Low then
295 Result (N) := LS (L);
296 L := L + 1;
297 else
298 Result (N) := RS (R);
299 R := R + 1;
300 end if;
302 -- Loop to collapse ranges into last range
304 loop
305 -- Collapse next length range into current result range
306 -- if possible.
308 if L <= LS'Last
309 and then LS (L).Low <= Wide_Character'Succ (Result (N).High)
310 then
311 Result (N).High :=
312 Wide_Character'Max (Result (N).High, LS (L).High);
313 L := L + 1;
315 -- Collapse next right range into current result range
316 -- if possible
318 elsif R <= RS'Last
319 and then RS (R).Low <=
320 Wide_Character'Succ (Result (N).High)
321 then
322 Result (N).High :=
323 Wide_Character'Max (Result (N).High, RS (R).High);
324 R := R + 1;
326 -- If neither range collapses, then done with this range
328 else
329 exit;
330 end if;
331 end loop;
332 end if;
333 end loop;
335 return (AF.Controlled with
336 Set => new Wide_Character_Ranges'(Result (1 .. N)));
337 end "or";
339 -----------
340 -- "xor" --
341 -----------
343 function "xor"
344 (Left, Right : in Wide_Character_Set)
345 return Wide_Character_Set
347 begin
348 return (Left or Right) - (Left and Right);
349 end "xor";
351 ------------
352 -- Adjust --
353 ------------
355 procedure Adjust (Object : in out Wide_Character_Mapping) is
356 begin
357 Object.Map := new Wide_Character_Mapping_Values'(Object.Map.all);
358 end Adjust;
360 procedure Adjust (Object : in out Wide_Character_Set) is
361 begin
362 Object.Set := new Wide_Character_Ranges'(Object.Set.all);
363 end Adjust;
365 --------------
366 -- Finalize --
367 --------------
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);
375 begin
376 if Object.Map /= Null_Map'Unrestricted_Access then
377 Free (Object.Map);
378 end if;
379 end Finalize;
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);
387 begin
388 if Object.Set /= Null_Range'Unrestricted_Access then
389 Free (Object.Set);
390 end if;
391 end Finalize;
393 ----------------
394 -- Initialize --
395 ----------------
397 procedure Initialize (Object : in out Wide_Character_Mapping) is
398 begin
399 Object := Identity;
400 end Initialize;
402 procedure Initialize (Object : in out Wide_Character_Set) is
403 begin
404 Object := Null_Set;
405 end Initialize;
407 -----------
408 -- Is_In --
409 -----------
411 function Is_In
412 (Element : in Wide_Character;
413 Set : in Wide_Character_Set)
414 return Boolean
416 L, R, M : Natural;
417 SS : constant Wide_Character_Ranges_Access := Set.Set;
419 begin
420 L := 1;
421 R := SS'Last;
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).
426 loop
427 if L > R then
428 return False;
430 else
431 M := (L + R) / 2;
433 if Element > SS (M).High then
434 L := M + 1;
435 elsif Element < SS (M).Low then
436 R := M - 1;
437 else
438 return True;
439 end if;
440 end if;
441 end loop;
442 end Is_In;
444 ---------------
445 -- Is_Subset --
446 ---------------
448 function Is_Subset
449 (Elements : in Wide_Character_Set;
450 Set : in Wide_Character_Set)
451 return Boolean
453 ES : constant Wide_Character_Ranges_Access := Elements.Set;
454 SS : constant Wide_Character_Ranges_Access := Set.Set;
456 S : Positive := 1;
457 E : Positive := 1;
459 begin
460 loop
461 -- If no more element ranges, done, and result is true
463 if E > ES'Last then
464 return True;
466 -- If more element ranges, but no more set ranges, result is false
468 elsif S > SS'Last then
469 return False;
471 -- Remove irrelevant set range
473 elsif SS (S).High < ES (E).Low then
474 S := S + 1;
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
480 then
481 E := E + 1;
483 -- Otherwise we have a non-covered element range, result is false
485 else
486 return False;
487 end if;
488 end loop;
489 end Is_Subset;
491 ---------------
492 -- To_Domain --
493 ---------------
495 function To_Domain
496 (Map : in Wide_Character_Mapping)
497 return Wide_Character_Sequence
499 begin
500 return Map.Map.Domain;
501 end To_Domain;
503 ----------------
504 -- To_Mapping --
505 ----------------
507 function To_Mapping
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);
513 N : Natural := 0;
515 begin
516 if From'Length /= To'Length then
517 raise Translation_Error;
519 else
520 pragma Warnings (Off); -- apparent uninit use of Domain
522 for J in From'Range loop
523 for M in 1 .. N 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);
531 goto Continue;
532 end if;
533 end loop;
535 Domain (N + 1) := From (J);
536 Rangev (N + 1) := To (J);
538 <<Continue>>
539 N := N + 1;
540 end loop;
542 pragma Warnings (On);
544 return (AF.Controlled with
545 Map => new Wide_Character_Mapping_Values'(
546 Length => N,
547 Domain => Domain (1 .. N),
548 Rangev => Rangev (1 .. N)));
549 end if;
550 end To_Mapping;
552 --------------
553 -- To_Range --
554 --------------
556 function To_Range
557 (Map : in Wide_Character_Mapping)
558 return Wide_Character_Sequence
560 begin
561 return Map.Map.Rangev;
562 end To_Range;
564 ---------------
565 -- To_Ranges --
566 ---------------
568 function To_Ranges
569 (Set : in Wide_Character_Set)
570 return Wide_Character_Ranges
572 begin
573 return Set.Set.all;
574 end To_Ranges;
576 -----------------
577 -- To_Sequence --
578 -----------------
580 function To_Sequence
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);
587 N : Natural := 0;
589 begin
590 for J in SS'Range loop
591 for K in SS (J).Low .. SS (J).High loop
592 N := N + 1;
593 Result (N) := K;
594 end loop;
595 end loop;
597 return Result (1 .. N);
598 end To_Sequence;
600 ------------
601 -- To_Set --
602 ------------
604 -- Case of multiple range input
606 function To_Set
607 (Ranges : in Wide_Character_Ranges)
608 return Wide_Character_Set
610 Result : Wide_Character_Ranges (Ranges'Range);
611 N : Natural := 0;
612 J : Natural;
614 begin
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
623 for K in 1 .. N loop
624 if Ranges (J).Low < Result (K).Low then
625 Result (K + 1 .. N + 1) := Result (K .. N);
626 Result (K) := Ranges (J);
627 goto Continue;
628 end if;
629 end loop;
631 Result (N + 1) := Ranges (J);
633 <<Continue>>
634 N := N + 1;
635 end loop;
637 pragma Warnings (On);
639 -- Now collapse any contiguous or overlapping ranges
641 J := 1;
642 while J < N loop
643 if Result (J).High < Result (J).Low then
644 N := N - 1;
645 Result (J .. N) := Result (J + 1 .. N + 1);
647 elsif Wide_Character'Succ (Result (J).High) >= Result (J + 1).Low then
648 Result (J).High :=
649 Wide_Character'Max (Result (J).High, Result (J + 1).High);
651 N := N - 1;
652 Result (J + 1 .. N) := Result (J + 2 .. N + 1);
654 else
655 J := J + 1;
656 end if;
657 end loop;
659 if Result (N).High < Result (N).Low then
660 N := N - 1;
661 end if;
663 return (AF.Controlled with
664 Set => new Wide_Character_Ranges'(Result (1 .. N)));
665 end To_Set;
667 -- Case of single range input
669 function To_Set
670 (Span : in Wide_Character_Range)
671 return Wide_Character_Set
673 begin
674 if Span.Low > Span.High then
675 return Null_Set;
676 -- This is safe, because there is no procedure with parameter
677 -- Wide_Character_Set of mode "out" or "in out".
679 else
680 return (AF.Controlled with
681 Set => new Wide_Character_Ranges'(1 => Span));
682 end if;
683 end To_Set;
685 -- Case of wide string input
687 function To_Set
688 (Sequence : in Wide_Character_Sequence)
689 return Wide_Character_Set
691 R : Wide_Character_Ranges (1 .. Sequence'Length);
693 begin
694 for J in R'Range loop
695 R (J) := (Sequence (J), Sequence (J));
696 end loop;
698 return To_Set (R);
699 end To_Set;
701 -- Case of single wide character input
703 function To_Set
704 (Singleton : in Wide_Character)
705 return Wide_Character_Set
707 begin
708 return
709 (AF.Controlled with
710 Set => new Wide_Character_Ranges'(1 => (Singleton, Singleton)));
711 end To_Set;
713 -----------
714 -- Value --
715 -----------
717 function Value
718 (Map : in Wide_Character_Mapping;
719 Element : in Wide_Character)
720 return Wide_Character
722 L, R, M : Natural;
724 MV : constant Wide_Character_Mapping_Values_Access := Map.Map;
726 begin
727 L := 1;
728 R := MV.Domain'Last;
730 -- Binary search loop
732 loop
733 -- If not found, identity
735 if L > R then
736 return Element;
738 -- Otherwise do binary divide
740 else
741 M := (L + R) / 2;
743 if Element < MV.Domain (M) then
744 R := M - 1;
746 elsif Element > MV.Domain (M) then
747 L := M + 1;
749 else -- Element = MV.Domain (M) then
750 return MV.Rangev (M);
751 end if;
752 end if;
753 end loop;
754 end Value;
756 end Ada.Strings.Wide_Maps;