1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
|
------------------------------------------------------------------------------
-- --
-- GNAT RUN-TIME COMPONENTS --
-- --
-- A D A . S T R I N G S . W I D E _ M A P S --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
package body Ada.Strings.Wide_Maps is
---------
-- "-" --
---------
function "-"
(Left, Right : Wide_Character_Set) return Wide_Character_Set
is
LS : constant Wide_Character_Ranges_Access := Left.Set;
RS : constant Wide_Character_Ranges_Access := Right.Set;
Result : Wide_Character_Ranges (1 .. LS'Last + RS'Last);
-- Each range on the right can generate at least one more range in
-- the result, by splitting one of the left operand ranges.
N : Natural := 0;
R : Natural := 1;
L : Natural := 1;
Left_Low : Wide_Character;
-- Left_Low is lowest character of the L'th range not yet dealt with
begin
if LS'Last = 0 or else RS'Last = 0 then
return Left;
end if;
Left_Low := LS (L).Low;
while R <= RS'Last loop
-- If next right range is below current left range, skip it
if RS (R).High < Left_Low then
R := R + 1;
-- If next right range above current left range, copy remainder
-- of the left range to the result
elsif RS (R).Low > LS (L).High then
N := N + 1;
Result (N).Low := Left_Low;
Result (N).High := LS (L).High;
L := L + 1;
exit when L > LS'Last;
Left_Low := LS (L).Low;
else
-- Next right range overlaps bottom of left range
if RS (R).Low <= Left_Low then
-- Case of right range complete overlaps left range
if RS (R).High >= LS (L).High then
L := L + 1;
exit when L > LS'Last;
Left_Low := LS (L).Low;
-- Case of right range eats lower part of left range
else
Left_Low := Wide_Character'Succ (RS (R).High);
R := R + 1;
end if;
-- Next right range overlaps some of left range, but not bottom
else
N := N + 1;
Result (N).Low := Left_Low;
Result (N).High := Wide_Character'Pred (RS (R).Low);
-- Case of right range splits left range
if RS (R).High < LS (L).High then
Left_Low := Wide_Character'Succ (RS (R).High);
R := R + 1;
-- Case of right range overlaps top of left range
else
L := L + 1;
exit when L > LS'Last;
Left_Low := LS (L).Low;
end if;
end if;
end if;
end loop;
-- Copy remainder of left ranges to result
if L <= LS'Last then
N := N + 1;
Result (N).Low := Left_Low;
Result (N).High := LS (L).High;
loop
L := L + 1;
exit when L > LS'Last;
N := N + 1;
Result (N) := LS (L);
end loop;
end if;
return (AF.Controlled with
Set => new Wide_Character_Ranges'(Result (1 .. N)));
end "-";
---------
-- "=" --
---------
-- The sorted, discontiguous form is canonical, so equality can be used
function "=" (Left, Right : Wide_Character_Set) return Boolean is
begin
return Left.Set.all = Right.Set.all;
end "=";
-----------
-- "and" --
-----------
function "and"
(Left, Right : Wide_Character_Set) return Wide_Character_Set
is
LS : constant Wide_Character_Ranges_Access := Left.Set;
RS : constant Wide_Character_Ranges_Access := Right.Set;
Result : Wide_Character_Ranges (1 .. LS'Last + RS'Last);
N : Natural := 0;
L, R : Natural := 1;
begin
-- Loop to search for overlapping character ranges
while L <= LS'Last and then R <= RS'Last loop
if LS (L).High < RS (R).Low then
L := L + 1;
elsif RS (R).High < LS (L).Low then
R := R + 1;
-- Here we have LS (L).High >= RS (R).Low
-- and RS (R).High >= LS (L).Low
-- so we have an overlapping range
else
N := N + 1;
Result (N).Low := Wide_Character'Max (LS (L).Low, RS (R).Low);
Result (N).High :=
Wide_Character'Min (LS (L).High, RS (R).High);
if RS (R).High = LS (L).High then
L := L + 1;
R := R + 1;
elsif RS (R).High < LS (L).High then
R := R + 1;
else
L := L + 1;
end if;
end if;
end loop;
return (AF.Controlled with
Set => new Wide_Character_Ranges'(Result (1 .. N)));
end "and";
-----------
-- "not" --
-----------
function "not"
(Right : Wide_Character_Set) return Wide_Character_Set
is
RS : constant Wide_Character_Ranges_Access := Right.Set;
Result : Wide_Character_Ranges (1 .. RS'Last + 1);
N : Natural := 0;
begin
if RS'Last = 0 then
N := 1;
Result (1) := (Low => Wide_Character'First,
High => Wide_Character'Last);
else
if RS (1).Low /= Wide_Character'First then
N := N + 1;
Result (N).Low := Wide_Character'First;
Result (N).High := Wide_Character'Pred (RS (1).Low);
end if;
for K in 1 .. RS'Last - 1 loop
N := N + 1;
Result (N).Low := Wide_Character'Succ (RS (K).High);
Result (N).High := Wide_Character'Pred (RS (K + 1).Low);
end loop;
if RS (RS'Last).High /= Wide_Character'Last then
N := N + 1;
Result (N).Low := Wide_Character'Succ (RS (RS'Last).High);
Result (N).High := Wide_Character'Last;
end if;
end if;
return (AF.Controlled with
Set => new Wide_Character_Ranges'(Result (1 .. N)));
end "not";
----------
-- "or" --
----------
function "or"
(Left, Right : Wide_Character_Set) return Wide_Character_Set
is
LS : constant Wide_Character_Ranges_Access := Left.Set;
RS : constant Wide_Character_Ranges_Access := Right.Set;
Result : Wide_Character_Ranges (1 .. LS'Last + RS'Last);
N : Natural;
L, R : Natural;
begin
N := 0;
L := 1;
R := 1;
-- Loop through ranges in output file
loop
-- If no left ranges left, copy next right range
if L > LS'Last then
exit when R > RS'Last;
N := N + 1;
Result (N) := RS (R);
R := R + 1;
-- If no right ranges left, copy next left range
elsif R > RS'Last then
N := N + 1;
Result (N) := LS (L);
L := L + 1;
else
-- We have two ranges, choose lower one
N := N + 1;
if LS (L).Low <= RS (R).Low then
Result (N) := LS (L);
L := L + 1;
else
Result (N) := RS (R);
R := R + 1;
end if;
-- Loop to collapse ranges into last range
loop
-- Collapse next length range into current result range
-- if possible.
if L <= LS'Last
and then LS (L).Low <= Wide_Character'Succ (Result (N).High)
then
Result (N).High :=
Wide_Character'Max (Result (N).High, LS (L).High);
L := L + 1;
-- Collapse next right range into current result range
-- if possible
elsif R <= RS'Last
and then RS (R).Low <=
Wide_Character'Succ (Result (N).High)
then
Result (N).High :=
Wide_Character'Max (Result (N).High, RS (R).High);
R := R + 1;
-- If neither range collapses, then done with this range
else
exit;
end if;
end loop;
end if;
end loop;
return (AF.Controlled with
Set => new Wide_Character_Ranges'(Result (1 .. N)));
end "or";
-----------
-- "xor" --
-----------
function "xor"
(Left, Right : Wide_Character_Set) return Wide_Character_Set
is
begin
return (Left or Right) - (Left and Right);
end "xor";
------------
-- Adjust --
------------
procedure Adjust (Object : in out Wide_Character_Mapping) is
begin
Object.Map := new Wide_Character_Mapping_Values'(Object.Map.all);
end Adjust;
procedure Adjust (Object : in out Wide_Character_Set) is
begin
Object.Set := new Wide_Character_Ranges'(Object.Set.all);
end Adjust;
--------------
-- Finalize --
--------------
procedure Finalize (Object : in out Wide_Character_Mapping) is
procedure Free is new Ada.Unchecked_Deallocation
(Wide_Character_Mapping_Values,
Wide_Character_Mapping_Values_Access);
begin
if Object.Map /= Null_Map'Unrestricted_Access then
Free (Object.Map);
end if;
end Finalize;
procedure Finalize (Object : in out Wide_Character_Set) is
procedure Free is new Ada.Unchecked_Deallocation
(Wide_Character_Ranges,
Wide_Character_Ranges_Access);
begin
if Object.Set /= Null_Range'Unrestricted_Access then
Free (Object.Set);
end if;
end Finalize;
----------------
-- Initialize --
----------------
procedure Initialize (Object : in out Wide_Character_Mapping) is
begin
Object := Identity;
end Initialize;
procedure Initialize (Object : in out Wide_Character_Set) is
begin
Object := Null_Set;
end Initialize;
-----------
-- Is_In --
-----------
function Is_In
(Element : Wide_Character;
Set : Wide_Character_Set) return Boolean
is
L, R, M : Natural;
SS : constant Wide_Character_Ranges_Access := Set.Set;
begin
L := 1;
R := SS'Last;
-- Binary search loop. The invariant is that if Element is in any of
-- of the constituent ranges it is in one between Set (L) and Set (R).
loop
if L > R then
return False;
else
M := (L + R) / 2;
if Element > SS (M).High then
L := M + 1;
elsif Element < SS (M).Low then
R := M - 1;
else
return True;
end if;
end if;
end loop;
end Is_In;
---------------
-- Is_Subset --
---------------
function Is_Subset
(Elements : Wide_Character_Set;
Set : Wide_Character_Set) return Boolean
is
ES : constant Wide_Character_Ranges_Access := Elements.Set;
SS : constant Wide_Character_Ranges_Access := Set.Set;
S : Positive := 1;
E : Positive := 1;
begin
loop
-- If no more element ranges, done, and result is true
if E > ES'Last then
return True;
-- If more element ranges, but no more set ranges, result is false
elsif S > SS'Last then
return False;
-- Remove irrelevant set range
elsif SS (S).High < ES (E).Low then
S := S + 1;
-- Get rid of element range that is properly covered by set
elsif SS (S).Low <= ES (E).Low
and then ES (E).High <= SS (S).High
then
E := E + 1;
-- Otherwise we have a non-covered element range, result is false
else
return False;
end if;
end loop;
end Is_Subset;
---------------
-- To_Domain --
---------------
function To_Domain
(Map : Wide_Character_Mapping) return Wide_Character_Sequence
is
begin
return Map.Map.Domain;
end To_Domain;
----------------
-- To_Mapping --
----------------
function To_Mapping
(From, To : Wide_Character_Sequence) return Wide_Character_Mapping
is
Domain : Wide_Character_Sequence (1 .. From'Length);
Rangev : Wide_Character_Sequence (1 .. To'Length);
N : Natural := 0;
begin
if From'Length /= To'Length then
raise Translation_Error;
else
pragma Warnings (Off); -- apparent uninit use of Domain
for J in From'Range loop
for M in 1 .. N loop
if From (J) = Domain (M) then
raise Translation_Error;
elsif From (J) < Domain (M) then
Domain (M + 1 .. N + 1) := Domain (M .. N);
Rangev (M + 1 .. N + 1) := Rangev (M .. N);
Domain (M) := From (J);
Rangev (M) := To (J);
goto Continue;
end if;
end loop;
Domain (N + 1) := From (J);
Rangev (N + 1) := To (J);
<<Continue>>
N := N + 1;
end loop;
pragma Warnings (On);
return (AF.Controlled with
Map => new Wide_Character_Mapping_Values'(
Length => N,
Domain => Domain (1 .. N),
Rangev => Rangev (1 .. N)));
end if;
end To_Mapping;
--------------
-- To_Range --
--------------
function To_Range
(Map : Wide_Character_Mapping) return Wide_Character_Sequence
is
begin
return Map.Map.Rangev;
end To_Range;
---------------
-- To_Ranges --
---------------
function To_Ranges
(Set : Wide_Character_Set) return Wide_Character_Ranges
is
begin
return Set.Set.all;
end To_Ranges;
-----------------
-- To_Sequence --
-----------------
function To_Sequence
(Set : Wide_Character_Set) return Wide_Character_Sequence
is
SS : constant Wide_Character_Ranges_Access := Set.Set;
Result : Wide_String (Positive range 1 .. 2 ** 16);
N : Natural := 0;
begin
for J in SS'Range loop
for K in SS (J).Low .. SS (J).High loop
N := N + 1;
Result (N) := K;
end loop;
end loop;
return Result (1 .. N);
end To_Sequence;
------------
-- To_Set --
------------
-- Case of multiple range input
function To_Set
(Ranges : Wide_Character_Ranges) return Wide_Character_Set
is
Result : Wide_Character_Ranges (Ranges'Range);
N : Natural := 0;
J : Natural;
begin
-- The output of To_Set is required to be sorted by increasing Low
-- values, and discontiguous, so first we sort them as we enter them,
-- using a simple insertion sort.
pragma Warnings (Off);
-- Kill bogus warning on Result being uninitialized
for J in Ranges'Range loop
for K in 1 .. N loop
if Ranges (J).Low < Result (K).Low then
Result (K + 1 .. N + 1) := Result (K .. N);
Result (K) := Ranges (J);
goto Continue;
end if;
end loop;
Result (N + 1) := Ranges (J);
<<Continue>>
N := N + 1;
end loop;
pragma Warnings (On);
-- Now collapse any contiguous or overlapping ranges
J := 1;
while J < N loop
if Result (J).High < Result (J).Low then
N := N - 1;
Result (J .. N) := Result (J + 1 .. N + 1);
elsif Wide_Character'Succ (Result (J).High) >= Result (J + 1).Low then
Result (J).High :=
Wide_Character'Max (Result (J).High, Result (J + 1).High);
N := N - 1;
Result (J + 1 .. N) := Result (J + 2 .. N + 1);
else
J := J + 1;
end if;
end loop;
if N > 0 and then Result (N).High < Result (N).Low then
N := N - 1;
end if;
return (AF.Controlled with
Set => new Wide_Character_Ranges'(Result (1 .. N)));
end To_Set;
-- Case of single range input
function To_Set
(Span : Wide_Character_Range) return Wide_Character_Set
is
begin
if Span.Low > Span.High then
return Null_Set;
-- This is safe, because there is no procedure with parameter
-- Wide_Character_Set of mode "out" or "in out".
else
return (AF.Controlled with
Set => new Wide_Character_Ranges'(1 => Span));
end if;
end To_Set;
-- Case of wide string input
function To_Set
(Sequence : Wide_Character_Sequence) return Wide_Character_Set
is
R : Wide_Character_Ranges (1 .. Sequence'Length);
begin
for J in R'Range loop
R (J) := (Sequence (J), Sequence (J));
end loop;
return To_Set (R);
end To_Set;
-- Case of single wide character input
function To_Set
(Singleton : Wide_Character) return Wide_Character_Set
is
begin
return
(AF.Controlled with
Set => new Wide_Character_Ranges'(1 => (Singleton, Singleton)));
end To_Set;
-----------
-- Value --
-----------
function Value
(Map : Wide_Character_Mapping;
Element : Wide_Character) return Wide_Character
is
L, R, M : Natural;
MV : constant Wide_Character_Mapping_Values_Access := Map.Map;
begin
L := 1;
R := MV.Domain'Last;
-- Binary search loop
loop
-- If not found, identity
if L > R then
return Element;
-- Otherwise do binary divide
else
M := (L + R) / 2;
if Element < MV.Domain (M) then
R := M - 1;
elsif Element > MV.Domain (M) then
L := M + 1;
else -- Element = MV.Domain (M) then
return MV.Rangev (M);
end if;
end if;
end loop;
end Value;
end Ada.Strings.Wide_Maps;
|