-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapcode_utils-as_u.adb
More file actions
613 lines (564 loc) · 19.1 KB
/
mapcode_utils-as_u.adb
File metadata and controls
613 lines (564 loc) · 19.1 KB
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
-- -----------------------------------------------------------------------------
-- Copyright (C) 2003-2019 Stichting Mapcode Foundation (http://www.mapcode.com)
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- -----------------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
package body Mapcode_Utils.As_U is
-- Each time a re-allocation is needed, increment Length by
-- Nb_To_Add + Growth_Offset + Curr_Length / Growth_Factor
-- so that some further growths will not lead to re-alloc
Growth_Factor : constant Natural := 64;
Growth_Offset : constant Natural := 32;
procedure Free (X : in out String_Access) is
procedure Deallocate is
new Ada.Unchecked_Deallocation (String, String_Access);
begin
-- Do not try to free statically allocated null array
if X /= Asu_Null.Ref then
Deallocate (X);
end if;
end Free;
procedure Check_Indexes (Low : in Positive;
High : in Natural;
Last : in Natural;
Check_High : in Boolean) is
begin
if Low > Last + 1 or else (Check_High and then High > Last) then
raise Index_Error;
end if;
end Check_Indexes;
procedure Check_Index (Index : in Positive;
Last : in Natural;
Allow_Append : in Boolean) is
begin
if Index <= Last then
-- Index within range
return;
end if;
if Allow_Append and then Index = Last + 1 then
-- Append = insert before last + 1
return;
end if;
raise Index_Error;
end Check_Index;
-- Size computed to Add to Length
function New_Size (Length : Natural; Add : Positive) return Positive is
(Length + Add + Growth_Offset + Length / Growth_Factor);
-- Store Item in Unbounded array, re-alloc if necessary
procedure Store (Within : in out Asu_Us;
Item : in String) is
Incr : Integer;
Acc : String_Access;
begin
Incr := Item'Length - Within.Ref'Length;
if Incr <= 0 then
if Item'Length = 0 then
-- Item to store is empty
Free (Within.Ref);
Within.Ref := Null_Access;
else
-- Item to store fits in Reference, no need to re-alloc
Within.Ref.all(1 .. Item'Length) := Item;
end if;
else
-- Re-alloc reference to new size
Acc := new String(1 .. New_Size (Within.Ref'Length, Incr));
Acc(1 .. Item'Length) := Item;
Free (Within.Ref);
Within.Ref := Acc;
end if;
Within.Last := Item'Length;
end Store;
procedure Init (Target : in out Asu_Us; Length : Natural) is
begin
Free (Target.Ref);
if Length = 0 then
Target := Asu_Null;
else
Target.Ref := new String(1 .. Length);
Target.Ref.all := (others => ' ');
Target.Last := Length;
end if;
end Init;
-- Move the slice Last .. Source.Last at Before
procedure Move (Source : in out Asu_Us; Before, Last : in Positive) is
Tmp : constant String := Source.Ref (Last .. Source.Last);
begin
-- Shift head from Before to Last - 1 at the tail
Source.Ref(Source.Last - Last + Before + 1 .. Source.Last) :=
Source.Ref(Before .. Last - 1);
-- Copy Tmp at Before
Source.Ref(Before .. Before + Tmp'Length - 1) := Tmp;
end Move;
-----------------------
-- PUBLIC operations --
-----------------------
procedure Set_Null (Target : in out Asu_Us) is
begin
-- Optim: avoid copying Asu_Null
Free (Target.Ref);
Target.Ref := Null_Access;
Target.Last := 0;
end Set_Null;
function Is_Null (Source : Asu_Us) return Boolean is (Source = Asu_Null);
function Length (Source : Asu_Us) return Natural is (Source.Last);
function Tus (Str : String) return Asu_Us is
Res : Asu_Us;
begin
if Str'Length = 0 then
Res := Asu_Null;
else
Res.Ref := new String(1 .. Str'Length);
Res.Ref.all := Str;
Res.Last := Str'Length;
end if;
return Res;
end Tus;
function Tus (Char : Character) return Asu_Us is
Res : Asu_Us;
begin
Res.Ref := new String(1 .. 1);
Res.Ref(1) := Char;
Res.Last := 1;
return Res;
end Tus;
function Image (Str : Asu_Us) return String is (Str.Ref (1 .. Str.Last));
procedure Set (Target : out Asu_Us; Val : in Asu_Us) is
begin
if Val.Last = 0 then
Target := Asu_Null;
else
Target.Ref := new String(1 .. Val.Last);
Target.Ref.all := Val.Ref(1 .. Val.Last);
Target.Last := Val.Last;
end if;
end Set;
procedure Set (Target : out Asu_Us; Val : in String) is
begin
if Val'Length = 0 then
Target := Asu_Null;
else
Target.Ref := new String(1 .. Val'Length);
Target.Ref.all := Val;
Target.Last := Val'Length;
end if;
end Set;
procedure Set (Target : out Asu_Us; Val : in Character) is
begin
Target.Ref := new String(1 .. 1);
Target.Ref(1) := Val;
Target.Last := 1;
end Set;
function Uslice (Source : Asu_Us;
Low : Positive; High : Natural) return Asu_Us is
begin
Check_Indexes (Low, High, Source.Last, True);
return Tus (Source.Ref.all(Low .. High));
end Uslice;
procedure Uslice (Source : in Asu_Us; Target : out Asu_Us;
Low : in Positive; High : in Natural) is
begin
Check_Indexes (Low, High, Source.Last, True);
Set (Target, Source.Ref.all(Low .. High));
end Uslice;
function Slice (Source : Asu_Us;
Low : Positive; High : Natural) return String is
begin
Check_Indexes (Low, High, Source.Last, True);
return Source.Ref.all(Low .. High);
end Slice;
procedure Prepend (Source : in out Asu_Us; New_Item : in Asu_Us) is
begin
Store (Source, New_Item.Ref.all & Source.Ref(1 .. Source.Last));
end Prepend;
procedure Prepend (Source : in out Asu_Us; New_Item : in String) is
begin
Store (Source, New_Item & Source.Ref(1 .. Source.Last));
end Prepend;
procedure Prepend (Source : in out Asu_Us; New_Item : in Character) is
begin
Store (Source, New_Item & Source.Ref(1 .. Source.Last));
end Prepend;
procedure Append (Source : in out Asu_Us; New_Item : in Asu_Us) is
begin
if New_Item.Last <= Source.Ref'Length - Source.Last then
-- Optim: no copy if New_Item fits
Source.Ref(Source.Last + 1 .. Source.Last + New_Item.Last) :=
New_Item.Ref(1 .. New_Item.Last);
Source.Last := Source.Last + New_Item.Last;
else
Store (Source, Source.Ref(1 .. Source.Last)
& New_Item.Ref(1 .. New_Item.Last));
end if;
end Append;
procedure Append (Source : in out Asu_Us; New_Item : in String) is
begin
if New_Item'Length <= Source.Ref'Length - Source.Last then
-- Optim: no copy if New_Item fits
Source.Ref(Source.Last + 1 .. Source.Last + New_Item'Length) := New_Item;
Source.Last := Source.Last + New_Item'Length;
else
Store (Source, Source.Ref(1 .. Source.Last) & New_Item);
end if;
end Append;
procedure Append (Source : in out Asu_Us; New_Item : in Character) is
begin
if 1 <= Source.Ref'Length - Source.Last then
-- Optim: no copy if New_Item fits
Source.Ref(Source.Last + 1) := New_Item;
Source.Last := Source.Last + 1;
else
Store (Source, Source.Ref.all & New_Item);
end if;
end Append;
function "&" (Left, Right : Asu_Us) return Asu_Us is
Res : Asu_Us := Left;
begin
Append (Res, Right);
return Res;
end "&";
function "&" (Left : Asu_Us; Right : String) return Asu_Us is
Res : Asu_Us := Left;
begin
Append (Res, Right);
return Res;
end "&";
function "&" (Left : String; Right : Asu_Us) return Asu_Us is
Res : Asu_Us := Tus (Left);
begin
Append (Res, Right);
return Res;
end "&";
function "&" (Left : Asu_Us; Right : Character) return Asu_Us is
Res : Asu_Us := Left;
begin
Append (Res, Right);
return Res;
end "&";
function "&" (Left : Character; Right : Asu_Us) return Asu_Us is
Res : Asu_Us := Tus (Left);
begin
Append (Res, Right);
return Res;
end "&";
function Element (Source : Asu_Us; Index : Positive) return Character is
begin
Check_Index (Index, Source.Last, False);
return Source.Ref.all(Index);
end Element;
procedure Replace_Element (Source : in out Asu_Us;
Index : in Positive;
By : in Character) is
begin
Check_Index (Index, Source.Last, False);
Source.Ref.all(Index) := By;
end Replace_Element;
overriding function "=" (Left, Right : Asu_Us) return Boolean is
(Left.Ref.all(1 .. Left.Last) = Right.Ref.all(1 .. Right.Last));
function "=" (Left : Asu_Us; Right : String) return Boolean is
(Left.Ref.all(1 .. Left.Last) = Right);
function "=" (Left : String; Right : Asu_Us) return Boolean is
(Left = Right.Ref.all(1 .. Right.Last));
function "<" (Left, Right : Asu_Us) return Boolean is
(Left.Ref.all(1 .. Left.Last) < Right.Ref.all(1 .. Right.Last));
function "<" (Left : Asu_Us; Right : String) return Boolean is
(Left.Ref.all(1 .. Left.Last) < Right);
function "<" (Left : String; Right : Asu_Us) return Boolean is
(Left < Right.Ref.all(1 .. Right.Last));
function "<=" (Left, Right : Asu_Us) return Boolean is
(Left.Ref.all(1 .. Left.Last) <= Right.Ref.all(1 .. Right.Last));
function "<=" (Left : Asu_Us; Right : String) return Boolean is
(Left.Ref.all(1 .. Left.Last) <= Right);
function "<=" (Left : String; Right : Asu_Us) return Boolean is
(Left <= Right.Ref.all(1 .. Right.Last));
function ">" (Left, Right : Asu_Us) return Boolean is
(Left.Ref.all(1 .. Left.Last) > Right.Ref.all(1 .. Right.Last));
function ">" (Left : Asu_Us; Right : String) return Boolean is
(Left.Ref.all(1 .. Left.Last) > Right);
function ">" (Left : String; Right : Asu_Us) return Boolean is
(Left > Right.Ref.all(1 .. Right.Last));
function ">=" (Left, Right : Asu_Us) return Boolean is
(Left.Ref.all(1 .. Left.Last) >= Right.Ref.all(1 .. Right.Last));
function ">=" (Left : Asu_Us; Right : String) return Boolean is
(Left.Ref.all(1 .. Left.Last) >= Right);
function ">=" (Left : String; Right : Asu_Us) return Boolean is
(Left >= Right.Ref.all(1 .. Right.Last));
function Locate (Within : Asu_Us;
Fragment : String;
From_Index : Natural := 0;
Forward : Boolean := True;
Occurence : Positive := 1) return Natural is
Index : Natural;
Found_Occurence : Natural := 0;
begin
-- Fix Index
Index := (if From_Index = 0 then
(if Forward then 1 else Within.Last)
else From_Index);
-- Handle limit or incorrect values
if Within.Last = 0
or else Fragment'Length = 0
or else Index > Within.Last then
return 0;
end if;
if Forward then
for I in Index .. Within.Last - Fragment'Length + 1 loop
if Within.Ref(I .. I + Fragment'Length - 1) = Fragment then
Found_Occurence := Found_Occurence + 1;
if Found_Occurence = Occurence then
return I;
end if;
end if;
end loop;
else
for I in reverse 1 .. Index - Fragment'Length + 1 loop
if Within.Ref(I .. I + Fragment'Length - 1) = Fragment then
Found_Occurence := Found_Occurence + 1;
if Found_Occurence = Occurence then
return I;
end if;
end if;
end loop;
end if;
return 0;
exception
when Constraint_Error =>
return 0;
end Locate;
function Count (Source : Asu_Us;
Pattern : String) return Natural is
Result : Natural := 0;
begin
for I in 1 .. Source.Last - Pattern'Length + 1 loop
if Source.Ref(I .. I + Pattern'Length - 1) = Pattern then
Result := Result + 1;
end if;
end loop;
return Result;
end Count;
procedure Overwrite (Source : in out Asu_Us;
Position : in Positive;
New_Item : in Asu_Us) is
-- Index in New_Item of last overwritting char (others are appended)
Lo : Natural;
begin
Check_Index (Position, Source.Last, True);
Lo := (if Position + New_Item.Last - 1 > Source.Last then
Source.Last - Position + 1
else New_Item.Last);
-- Overwrite by Lo chars from Position
Source.Ref(Position .. Position + Lo - 1) := New_Item.Ref(1 .. Lo);
-- Append others
Append (Source, New_Item.Ref(Lo + 1 .. New_Item.Last));
end Overwrite;
procedure Overwrite (Source : in out Asu_Us;
Position : in Positive;
New_Item : in String) is
-- Index in New_Item of last overwritting char (others are appended)
Lo : Natural;
begin
Check_Index (Position, Source.Last, True);
Lo := (if Position + New_Item'Length - 1 > Source.Last then
New_Item'First + Source.Last - Position
else New_Item'Last);
-- Overwrite by Lo chars from Position
Source.Ref(Position .. Position + Lo - New_Item'First) :=
New_Item(New_Item'First .. Lo);
-- Append others
Append (Source, New_Item(Lo + 1 .. New_Item'Last));
end Overwrite;
procedure Replace (Source : in out Asu_Us;
Low : in Positive;
High : in Natural;
By : in Asu_Us) is
Start_Tail : Positive;
begin
Check_Indexes (Low, High, Source.Last, False);
-- Replace or insert
Start_Tail := (if Low <= High then High + 1 else Low);
Store (Source, Source.Ref(1 .. Low - 1)
& By.Ref(1 .. By.Last)
& Source.Ref(Start_Tail .. Source.Last));
end Replace;
procedure Replace (Source : in out Asu_Us;
Low : in Positive;
High : in Natural;
By : in String) is
Start_Tail : Positive;
begin
Check_Indexes (Low, High, Source.Last, False);
-- Replace or insert
Start_Tail := (if Low <= High then High + 1 else Low);
Store (Source, Source.Ref(1 .. Low - 1)
& By
& Source.Ref(Start_Tail .. Source.Last));
end Replace;
procedure Insert (Source : in out Asu_Us;
Before : in Positive;
New_Item : in Asu_Us) is
Last : constant Natural := Source.Last;
begin
if Before > Source.Last + 1 then
raise Index_Error;
end if;
Append (Source, New_Item);
Move (Source, Before, Last + 1);
end Insert;
procedure Insert (Source : in out Asu_Us;
Before : in Positive;
New_Item : in String) is
Last : constant Natural := Source.Last;
begin
if Before > Source.Last + 1 then
raise Index_Error;
end if;
Append (Source, New_Item);
Move (Source, Before, Last + 1);
end Insert;
procedure Insert (Source : in out Asu_Us;
Before : in Positive;
New_Item : in Character) is
Last : constant Natural := Source.Last;
begin
if Before > Source.Last + 1 then
raise Index_Error;
end if;
Append (Source, New_Item);
Move (Source, Before, Last + 1);
end Insert;
procedure Delete (Source : in out Asu_Us;
From : in Positive;
Through : in Natural) is
New_Len : Natural;
begin
if Through < From then
return;
end if;
if Through > Source.Last then
raise Index_Error;
end if;
New_Len := Source.Last - (Through - From + 1);
if New_Len = 0 then
Free(Source.Ref);
Source := Asu_Null;
else
Source.Ref(1 .. New_Len) :=
Source.Ref(1 .. From - 1)
& Source.Ref(Through + 1 .. Source.Last);
Source.Last := New_Len;
end if;
end Delete;
procedure Delete_Nb (Source : in out Asu_Us;
From : in Positive;
Number : in Natural) is
begin
if From + Number - 1 <= Source.Last then
-- We can delete Number characters
Delete (Source, From, From + Number - 1);
else
-- We delete from From to Last
Delete (Source, From, Source.Last);
end if;
end Delete_Nb;
procedure Trail (Source : in out Asu_Us;
Number : in Natural) is
begin
if Number >= Source.Last then
Free(Source.Ref);
Source := Asu_Null;
else
Source.Last := Source.Last - Number;
end if;
end Trail;
function Head (Source : Asu_Us; Count : Natural; Pad : Character := Space)
return Asu_Us is
Result : Asu_Us;
begin
Init (Result, Count);
if Count <= Source.Last then
Result.Ref.all := Source.Ref(1 .. Count);
else
Result.Ref(1 .. Source.Last) := Source.Ref(1 .. Source.Last);
for I in Source.Last + 1 .. Count loop
Result.Ref(I) := Pad;
end loop;
end if;
return Result;
end Head;
function Tail (Source : Asu_Us; Count : Natural; Pad : Character := Space)
return Asu_Us is
Result : Asu_Us;
begin
Init (Result, Count);
if Count <= Source.Last then
Result.Ref.all := Source.Ref(Source.Last - Count + 1 .. Source.Last);
else
for I in 1 .. Count - Source.Last loop
Result.Ref(I) := Pad;
end loop;
Result.Ref(Count - Source.Last + 1 .. Count) := Source.Ref.all;
end if;
return Result;
end Tail;
function "*" (Left : Natural; Right : Character) return Asu_Us is
Result : Asu_Us;
begin
Init (Result, Left);
for I in 1 .. Left loop
Result.Ref(I) := Right;
end loop;
return Result;
end "*";
function "*" (Left : Natural; Right : String) return Asu_Us is
Result : Asu_Us;
Ptr : Integer := 1;
begin
Init (Result, Left * Right'Length);
for I in 1 .. Left loop
Result.Ref(Ptr .. Ptr + Right'Length - 1) := Right;
Ptr := Ptr + Right'Length;
end loop;
return Result;
end "*";
function "*" (Left : Natural; Right : Asu_Us) return Asu_Us is
Result : Asu_Us;
Ptr : Integer := 1;
begin
Init (Result, Left * Right.Last);
for I in 1 .. Left loop
Result.Ref(Ptr .. Ptr + Right.Last - 1) := Right.Ref(1 .. Right.Last);
Ptr := Ptr + Right.Last;
end loop;
return Result;
end "*";
-- Life cycle
overriding procedure Initialize (Object : in out Asu_Us) is
begin
Object.Ref := Null_Access;
Object.Last := 0;
end Initialize;
overriding procedure Adjust (Object : in out Asu_Us) is
begin
if Object.Ref /= Null_Access then
-- Real copy
Object.Ref := new String'(Object.Ref(1 .. Object.Last));
end if;
end Adjust;
overriding procedure Finalize (Object : in out Asu_Us) is
begin
Free (Object.Ref);
Object.Ref := Null_Access;
Object.Last := 0;
end Finalize;
end Mapcode_Utils.As_U;