Debian Codegraph @

gcc/ada/libgnat/a-stwiun__shared.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . S T R I N G S . W I D E _ U N B O U N D E D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Strings.Wide_Search;
33 with Ada.Unchecked_Deallocation;
34
35 package body Ada.Strings.Wide_Unbounded is
36
37 use Ada.Strings.Wide_Maps;
38
39 procedure Non_Inlined_Append
40 (Source : in out Unbounded_Wide_String;
41 New_Item : Unbounded_Wide_String);
42
43 procedure Non_Inlined_Append
44 (Source : in out Unbounded_Wide_String;
45 New_Item : Wide_String);
46
47 procedure Non_Inlined_Append
48 (Source : in out Unbounded_Wide_String;
49 New_Item : Wide_Character);
50 -- Non_Inlined_Append are part of the respective Append method that
51 -- should not be inlined. The idea is that the code of Append is inlined.
52 -- In order to make inlining efficient it is better to have the inlined
53 -- code as small as possible. Thus most common cases are inlined and less
54 -- common cases are deferred in these functions.
55
56 Growth_Factor : constant := 2;
57 -- The growth factor controls how much extra space is allocated when
58 -- we have to increase the size of an allocated unbounded string. By
59 -- allocating extra space, we avoid the need to reallocate on every
60 -- append, particularly important when a string is built up by repeated
61 -- append operations of small pieces. This is expressed as a factor so
62 -- 32 means add 1/32 of the length of the string as growth space.
63
64 Min_Mul_Alloc : constant := Standard'Maximum_Alignment;
65 -- Allocation will be done by a multiple of Min_Mul_Alloc. This causes
66 -- no memory loss as most (all?) malloc implementations are obliged to
67 -- align the returned memory on the maximum alignment as malloc does not
68 -- know the target alignment.
69
70 function Aligned_Max_Length (Max_Length : Natural) return Natural;
71 -- Returns recommended length of the shared string which is greater or
72 -- equal to specified length. Calculation take in sense alignment of
73 -- the allocated memory segments to use memory effectively by
74 -- Append/Insert/etc operations.
75
76 ---------
77 -- "&" --
78 ---------
79
80 function "&"
81 (Left : Unbounded_Wide_String;
82 Right : Unbounded_Wide_String) return Unbounded_Wide_String
83 is
84 LR : constant Shared_Wide_String_Access := Left.Reference;
85 RR : constant Shared_Wide_String_Access := Right.Reference;
86 DL : constant Natural := LR.Last + RR.Last;
87 DR : Shared_Wide_String_Access;
88
89 begin
90 -- Result is an empty string, reuse shared empty string
91
92 if DL = 0 then
93 Reference (Empty_Shared_Wide_String'Access);
94 DR := Empty_Shared_Wide_String'Access;
95
96 -- Left string is empty, return Right string
97
98 elsif LR.Last = 0 then
99 Reference (RR);
100 DR := RR;
101
102 -- Right string is empty, return Left string
103
104 elsif RR.Last = 0 then
105 Reference (LR);
106 DR := LR;
107
108 -- Overwise, allocate new shared string and fill data
109
110 else
111 DR := Allocate (DL);
112 DR.Data (1 .. LR.Last) := LR.Data (1 .. LR.Last);
113 DR.Data (LR.Last + 1 .. DL) := RR.Data (1 .. RR.Last);
114 DR.Last := DL;
115 end if;
116
117 return (AF.Controlled with Reference => DR);
118 end "&";
119
120 function "&"
121 (Left : Unbounded_Wide_String;
122 Right : Wide_String) return Unbounded_Wide_String
123 is
124 LR : constant Shared_Wide_String_Access := Left.Reference;
125 DL : constant Natural := LR.Last + Right'Length;
126 DR : Shared_Wide_String_Access;
127
128 begin
129 -- Result is an empty string, reuse shared empty string
130
131 if DL = 0 then
132 Reference (Empty_Shared_Wide_String'Access);
133 DR := Empty_Shared_Wide_String'Access;
134
135 -- Right is an empty string, return Left string
136
137 elsif Right'Length = 0 then
138 Reference (LR);
139 DR := LR;
140
141 -- Otherwise, allocate new shared string and fill it
142
143 else
144 DR := Allocate (DL);
145 DR.Data (1 .. LR.Last) := LR.Data (1 .. LR.Last);
146 DR.Data (LR.Last + 1 .. DL) := Right;
147 DR.Last := DL;
148 end if;
149
150 return (AF.Controlled with Reference => DR);
151 end "&";
152
153 function "&"
154 (Left : Wide_String;
155 Right : Unbounded_Wide_String) return Unbounded_Wide_String
156 is
157 RR : constant Shared_Wide_String_Access := Right.Reference;
158 DL : constant Natural := Left'Length + RR.Last;
159 DR : Shared_Wide_String_Access;
160
161 begin
162 -- Result is an empty string, reuse shared one
163
164 if DL = 0 then
165 Reference (Empty_Shared_Wide_String'Access);
166 DR := Empty_Shared_Wide_String'Access;
167
168 -- Left is empty string, return Right string
169
170 elsif Left'Length = 0 then
171 Reference (RR);
172 DR := RR;
173
174 -- Otherwise, allocate new shared string and fill it
175
176 else
177 DR := Allocate (DL);
178 DR.Data (1 .. Left'Length) := Left;
179 DR.Data (Left'Length + 1 .. DL) := RR.Data (1 .. RR.Last);
180 DR.Last := DL;
181 end if;
182
183 return (AF.Controlled with Reference => DR);
184 end "&";
185
186 function "&"
187 (Left : Unbounded_Wide_String;
188 Right : Wide_Character) return Unbounded_Wide_String
189 is
190 LR : constant Shared_Wide_String_Access := Left.Reference;
191 DL : constant Natural := LR.Last + 1;
192 DR : Shared_Wide_String_Access;
193
194 begin
195 DR := Allocate (DL);
196 DR.Data (1 .. LR.Last) := LR.Data (1 .. LR.Last);
197 DR.Data (DL) := Right;
198 DR.Last := DL;
199
200 return (AF.Controlled with Reference => DR);
201 end "&";
202
203 function "&"
204 (Left : Wide_Character;
205 Right : Unbounded_Wide_String) return Unbounded_Wide_String
206 is
207 RR : constant Shared_Wide_String_Access := Right.Reference;
208 DL : constant Natural := 1 + RR.Last;
209 DR : Shared_Wide_String_Access;
210
211 begin
212 DR := Allocate (DL);
213 DR.Data (1) := Left;
214 DR.Data (2 .. DL) := RR.Data (1 .. RR.Last);
215 DR.Last := DL;
216
217 return (AF.Controlled with Reference => DR);
218 end "&";
219
220 ---------
221 -- "*" --
222 ---------
223
224 function "*"
225 (Left : Natural;
226 Right : Wide_Character) return Unbounded_Wide_String
227 is
228 DR : Shared_Wide_String_Access;
229
230 begin
231 -- Result is an empty string, reuse shared empty string
232
233 if Left = 0 then
234 Reference (Empty_Shared_Wide_String'Access);
235 DR := Empty_Shared_Wide_String'Access;
236
237 -- Otherwise, allocate new shared string and fill it
238
239 else
240 DR := Allocate (Left);
241
242 for J in 1 .. Left loop
243 DR.Data (J) := Right;
244 end loop;
245
246 DR.Last := Left;
247 end if;
248
249 return (AF.Controlled with Reference => DR);
250 end "*";
251
252 function "*"
253 (Left : Natural;
254 Right : Wide_String) return Unbounded_Wide_String
255 is
256 DL : constant Natural := Left * Right'Length;
257 DR : Shared_Wide_String_Access;
258 K : Positive;
259
260 begin
261 -- Result is an empty string, reuse shared empty string
262
263 if DL = 0 then
264 Reference (Empty_Shared_Wide_String'Access);
265 DR := Empty_Shared_Wide_String'Access;
266
267 -- Otherwise, allocate new shared string and fill it
268
269 else
270 DR := Allocate (DL);
271 K := 1;
272
273 for J in 1 .. Left loop
274 DR.Data (K .. K + Right'Length - 1) := Right;
275 K := K + Right'Length;
276 end loop;
277
278 DR.Last := DL;
279 end if;
280
281 return (AF.Controlled with Reference => DR);
282 end "*";
283
284 function "*"
285 (Left : Natural;
286 Right : Unbounded_Wide_String) return Unbounded_Wide_String
287 is
288 RR : constant Shared_Wide_String_Access := Right.Reference;
289 DL : constant Natural := Left * RR.Last;
290 DR : Shared_Wide_String_Access;
291 K : Positive;
292
293 begin
294 -- Result is an empty string, reuse shared empty string
295
296 if DL = 0 then
297 Reference (Empty_Shared_Wide_String'Access);
298 DR := Empty_Shared_Wide_String'Access;
299
300 -- Coefficient is one, just return string itself
301
302 elsif Left = 1 then
303 Reference (RR);
304 DR := RR;
305
306 -- Otherwise, allocate new shared string and fill it
307
308 else
309 DR := Allocate (DL);
310 K := 1;
311
312 for J in 1 .. Left loop
313 DR.Data (K .. K + RR.Last - 1) := RR.Data (1 .. RR.Last);
314 K := K + RR.Last;
315 end loop;
316
317 DR.Last := DL;
318 end if;
319
320 return (AF.Controlled with Reference => DR);
321 end "*";
322
323 ---------
324 -- "<" --
325 ---------
326
327 function "<"
328 (Left : Unbounded_Wide_String;
329 Right : Unbounded_Wide_String) return Boolean
330 is
331 LR : constant Shared_Wide_String_Access := Left.Reference;
332 RR : constant Shared_Wide_String_Access := Right.Reference;
333 begin
334 return LR.Data (1 .. LR.Last) < RR.Data (1 .. RR.Last);
335 end "<";
336
337 function "<"
338 (Left : Unbounded_Wide_String;
339 Right : Wide_String) return Boolean
340 is
341 LR : constant Shared_Wide_String_Access := Left.Reference;
342 begin
343 return LR.Data (1 .. LR.Last) < Right;
344 end "<";
345
346 function "<"
347 (Left : Wide_String;
348 Right : Unbounded_Wide_String) return Boolean
349 is
350 RR : constant Shared_Wide_String_Access := Right.Reference;
351 begin
352 return Left < RR.Data (1 .. RR.Last);
353 end "<";
354
355 ----------
356 -- "<=" --
357 ----------
358
359 function "<="
360 (Left : Unbounded_Wide_String;
361 Right : Unbounded_Wide_String) return Boolean
362 is
363 LR : constant Shared_Wide_String_Access := Left.Reference;
364 RR : constant Shared_Wide_String_Access := Right.Reference;
365
366 begin
367 -- LR = RR means two strings shares shared string, thus they are equal
368
369 return LR = RR or else LR.Data (1 .. LR.Last) <= RR.Data (1 .. RR.Last);
370 end "<=";
371
372 function "<="
373 (Left : Unbounded_Wide_String;
374 Right : Wide_String) return Boolean
375 is
376 LR : constant Shared_Wide_String_Access := Left.Reference;
377 begin
378 return LR.Data (1 .. LR.Last) <= Right;
379 end "<=";
380
381 function "<="
382 (Left : Wide_String;
383 Right : Unbounded_Wide_String) return Boolean
384 is
385 RR : constant Shared_Wide_String_Access := Right.Reference;
386 begin
387 return Left <= RR.Data (1 .. RR.Last);
388 end "<=";
389
390 ---------
391 -- "=" --
392 ---------
393
394 function "="
395 (Left : Unbounded_Wide_String;
396 Right : Unbounded_Wide_String) return Boolean
397 is
398 LR : constant Shared_Wide_String_Access := Left.Reference;
399 RR : constant Shared_Wide_String_Access := Right.Reference;
400
401 begin
402 return LR = RR or else LR.Data (1 .. LR.Last) = RR.Data (1 .. RR.Last);
403 -- LR = RR means two strings shares shared string, thus they are equal
404 end "=";
405
406 function "="
407 (Left : Unbounded_Wide_String;
408 Right : Wide_String) return Boolean
409 is
410 LR : constant Shared_Wide_String_Access := Left.Reference;
411 begin
412 return LR.Data (1 .. LR.Last) = Right;
413 end "=";
414
415 function "="
416 (Left : Wide_String;
417 Right : Unbounded_Wide_String) return Boolean
418 is
419 RR : constant Shared_Wide_String_Access := Right.Reference;
420 begin
421 return Left = RR.Data (1 .. RR.Last);
422 end "=";
423
424 ---------
425 -- ">" --
426 ---------
427
428 function ">"
429 (Left : Unbounded_Wide_String;
430 Right : Unbounded_Wide_String) return Boolean
431 is
432 LR : constant Shared_Wide_String_Access := Left.Reference;
433 RR : constant Shared_Wide_String_Access := Right.Reference;
434 begin
435 return LR.Data (1 .. LR.Last) > RR.Data (1 .. RR.Last);
436 end ">";
437
438 function ">"
439 (Left : Unbounded_Wide_String;
440 Right : Wide_String) return Boolean
441 is
442 LR : constant Shared_Wide_String_Access := Left.Reference;
443 begin
444 return LR.Data (1 .. LR.Last) > Right;
445 end ">";
446
447 function ">"
448 (Left : Wide_String;
449 Right : Unbounded_Wide_String) return Boolean
450 is
451 RR : constant Shared_Wide_String_Access := Right.Reference;
452 begin
453 return Left > RR.Data (1 .. RR.Last);
454 end ">";
455
456 ----------
457 -- ">=" --
458 ----------
459
460 function ">="
461 (Left : Unbounded_Wide_String;
462 Right : Unbounded_Wide_String) return Boolean
463 is
464 LR : constant Shared_Wide_String_Access := Left.Reference;
465 RR : constant Shared_Wide_String_Access := Right.Reference;
466
467 begin
468 -- LR = RR means two strings shares shared string, thus they are equal
469
470 return LR = RR or else LR.Data (1 .. LR.Last) >= RR.Data (1 .. RR.Last);
471 end ">=";
472
473 function ">="
474 (Left : Unbounded_Wide_String;
475 Right : Wide_String) return Boolean
476 is
477 LR : constant Shared_Wide_String_Access := Left.Reference;
478 begin
479 return LR.Data (1 .. LR.Last) >= Right;
480 end ">=";
481
482 function ">="
483 (Left : Wide_String;
484 Right : Unbounded_Wide_String) return Boolean
485 is
486 RR : constant Shared_Wide_String_Access := Right.Reference;
487 begin
488 return Left >= RR.Data (1 .. RR.Last);
489 end ">=";
490
491 ------------
492 -- Adjust --
493 ------------
494
495 procedure Adjust (Object : in out Unbounded_Wide_String) is
496 begin
497 Reference (Object.Reference);
498 end Adjust;
499
500 ------------------------
501 -- Aligned_Max_Length --
502 ------------------------
503
504 function Aligned_Max_Length (Max_Length : Natural) return Natural is
505 Static_Size : constant Natural :=
506 Empty_Shared_Wide_String'Size / Standard'Storage_Unit;
507 -- Total size of all static components
508
509 Element_Size : constant Natural :=
510 Wide_Character'Size / Standard'Storage_Unit;
511
512 begin
513 return
514 (((Static_Size + Max_Length * Element_Size - 1) / Min_Mul_Alloc + 2)
515 * Min_Mul_Alloc - Static_Size) / Element_Size;
516 end Aligned_Max_Length;
517
518 --------------
519 -- Allocate --
520 --------------
521
522 function Allocate (Max_Length : Natural) return Shared_Wide_String_Access is
523 begin
524 -- Empty string requested, return shared empty string
525
526 if Max_Length = 0 then
527 Reference (Empty_Shared_Wide_String'Access);
528 return Empty_Shared_Wide_String'Access;
529
530 -- Otherwise, allocate requested space (and probably some more room)
531
532 else
533 return new Shared_Wide_String (Aligned_Max_Length (Max_Length));
534 end if;
535 end Allocate;
536
537 ------------
538 -- Append --
539 ------------
540
541 procedure Append
542 (Source : in out Unbounded_Wide_String;
543 New_Item : Unbounded_Wide_String)
544 is
545 pragma Suppress (All_Checks);
546 -- Suppress checks as they are redundant with the checks done in that
547 -- function.
548
549 SR : constant Shared_Wide_String_Access := Source.Reference;
550 NR : constant Shared_Wide_String_Access := New_Item.Reference;
551
552 begin
553 -- Source is an empty string, reuse New_Item data
554
555 if SR.Last = 0 then
556 Reference (NR);
557 Source.Reference := NR;
558 Unreference (SR);
559
560 -- New_Item is empty string, nothing to do
561
562 elsif NR.Last = 0 then
563 null;
564
565 -- Try to reuse existent shared string
566
567 elsif System.Atomic_Counters.Is_One (SR.Counter)
568 and then NR.Last <= SR.Max_Length
569 and then SR.Max_Length - NR.Last >= SR.Last
570 then
571 SR.Data (SR.Last + 1 .. SR.Last + NR.Last) := NR.Data (1 .. NR.Last);
572 SR.Last := SR.Last + NR.Last;
573
574 -- Otherwise, allocate new one and fill it
575
576 else
577 Non_Inlined_Append (Source, New_Item);
578 end if;
579 end Append;
580
581 procedure Append
582 (Source : in out Unbounded_Wide_String;
583 New_Item : Wide_String)
584 is
585 pragma Suppress (All_Checks);
586 -- Suppress checks as they are redundant with the checks done in that
587 -- function.
588
589 New_Item_Length : constant Natural := New_Item'Length;
590 SR : constant Shared_Wide_String_Access := Source.Reference;
591 begin
592
593 if New_Item'Length = 0 then
594 -- New_Item is an empty string, nothing to do
595 null;
596
597 elsif System.Atomic_Counters.Is_One (SR.Counter)
598 -- The following test checks in fact that
599 -- SR.Max_Length >= SR.Last + New_Item_Length without causing
600 -- overflow.
601 and then New_Item_Length <= SR.Max_Length
602 and then SR.Max_Length - New_Item_Length >= SR.Last
603 then
604 -- Try to reuse existing shared string
605 SR.Data (SR.Last + 1 .. SR.Last + New_Item_Length) := New_Item;
606 SR.Last := SR.Last + New_Item_Length;
607
608 else
609 -- Otherwise, allocate new one and fill it. Deferring the worst case
610 -- into a separate non-inlined function ensure that inlined Append
611 -- code size remains short and thus efficient.
612 Non_Inlined_Append (Source, New_Item);
613 end if;
614 end Append;
615
616 procedure Append
617 (Source : in out Unbounded_Wide_String;
618 New_Item : Wide_Character)
619 is
620 pragma Suppress (All_Checks);
621 -- Suppress checks as they are redundant with the checks done in that
622 -- function.
623
624 SR : constant Shared_Wide_String_Access := Source.Reference;
625 begin
626 if System.Atomic_Counters.Is_One (SR.Counter)
627 and then SR.Max_Length > SR.Last
628 then
629 -- Try to reuse existing shared string
630 SR.Data (SR.Last + 1) := New_Item;
631 SR.Last := SR.Last + 1;
632
633 else
634 -- Otherwise, allocate new one and fill it. Deferring the worst case
635 -- into a separate non-inlined function ensure that inlined Append
636 -- code size remains short and thus efficient.
637 Non_Inlined_Append (Source, New_Item);
638 end if;
639 end Append;
640
641 -------------------
642 -- Can_Be_Reused --
643 -------------------
644
645 function Can_Be_Reused
646 (Item : Shared_Wide_String_Access;
647 Length : Natural) return Boolean is
648 begin
649 return
650 System.Atomic_Counters.Is_One (Item.Counter)
651 and then Item.Max_Length >= Length
652 and then Item.Max_Length <=
653 Aligned_Max_Length (Length + Length / Growth_Factor);
654 end Can_Be_Reused;
655
656 -----------
657 -- Count --
658 -----------
659
660 function Count
661 (Source : Unbounded_Wide_String;
662 Pattern : Wide_String;
663 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
664 return Natural
665 is
666 SR : constant Shared_Wide_String_Access := Source.Reference;
667 begin
668 return Wide_Search.Count (SR.Data (1 .. SR.Last), Pattern, Mapping);
669 end Count;
670
671 function Count
672 (Source : Unbounded_Wide_String;
673 Pattern : Wide_String;
674 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
675 is
676 SR : constant Shared_Wide_String_Access := Source.Reference;
677 begin
678 return Wide_Search.Count (SR.Data (1 .. SR.Last), Pattern, Mapping);
679 end Count;
680
681 function Count
682 (Source : Unbounded_Wide_String;
683 Set : Wide_Maps.Wide_Character_Set) return Natural
684 is
685 SR : constant Shared_Wide_String_Access := Source.Reference;
686 begin
687 return Wide_Search.Count (SR.Data (1 .. SR.Last), Set);
688 end Count;
689
690 ------------
691 -- Delete --
692 ------------
693
694 function Delete
695 (Source : Unbounded_Wide_String;
696 From : Positive;
697 Through : Natural) return Unbounded_Wide_String
698 is
699 SR : constant Shared_Wide_String_Access := Source.Reference;
700 DL : Natural;
701 DR : Shared_Wide_String_Access;
702
703 begin
704 -- Empty slice is deleted, use the same shared string
705
706 if From > Through then
707 Reference (SR);
708 DR := SR;
709
710 -- Index is out of range
711
712 elsif Through > SR.Last then
713 raise Index_Error;
714
715 -- Compute size of the result
716
717 else
718 DL := SR.Last - (Through - From + 1);
719
720 -- Result is an empty string, reuse shared empty string
721
722 if DL = 0 then
723 Reference (Empty_Shared_Wide_String'Access);
724 DR := Empty_Shared_Wide_String'Access;
725
726 -- Otherwise, allocate new shared string and fill it
727
728 else
729 DR := Allocate (DL);
730 DR.Data (1 .. From - 1) := SR.Data (1 .. From - 1);
731 DR.Data (From .. DL) := SR.Data (Through + 1 .. SR.Last);
732 DR.Last := DL;
733 end if;
734 end if;
735
736 return (AF.Controlled with Reference => DR);
737 end Delete;
738
739 procedure Delete
740 (Source : in out Unbounded_Wide_String;
741 From : Positive;
742 Through : Natural)
743 is
744 SR : constant Shared_Wide_String_Access := Source.Reference;
745 DL : Natural;
746 DR : Shared_Wide_String_Access;
747
748 begin
749 -- Nothing changed, return
750
751 if From > Through then
752 null;
753
754 -- Through is outside of the range
755
756 elsif Through > SR.Last then
757 raise Index_Error;
758
759 else
760 DL := SR.Last - (Through - From + 1);
761
762 -- Result is empty, reuse shared empty string
763
764 if DL = 0 then
765 Reference (Empty_Shared_Wide_String'Access);
766 Source.Reference := Empty_Shared_Wide_String'Access;
767 Unreference (SR);
768
769 -- Try to reuse existent shared string
770
771 elsif Can_Be_Reused (SR, DL) then
772 SR.Data (From .. DL) := SR.Data (Through + 1 .. SR.Last);
773 SR.Last := DL;
774
775 -- Otherwise, allocate new shared string
776
777 else
778 DR := Allocate (DL);
779 DR.Data (1 .. From - 1) := SR.Data (1 .. From - 1);
780 DR.Data (From .. DL) := SR.Data (Through + 1 .. SR.Last);
781 DR.Last := DL;
782 Source.Reference := DR;
783 Unreference (SR);
784 end if;
785 end if;
786 end Delete;
787
788 -------------
789 -- Element --
790 -------------
791
792 function Element
793 (Source : Unbounded_Wide_String;
794 Index : Positive) return Wide_Character
795 is
796 SR : constant Shared_Wide_String_Access := Source.Reference;
797 begin
798 if Index <= SR.Last then
799 return SR.Data (Index);
800 else
801 raise Index_Error;
802 end if;
803 end Element;
804
805 --------------
806 -- Finalize --
807 --------------
808
809 procedure Finalize (Object : in out Unbounded_Wide_String) is
810 SR : constant Shared_Wide_String_Access := Object.Reference;
811
812 begin
813 if SR /= null then
814
815 -- The same controlled object can be finalized several times for
816 -- some reason. As per 7.6.1(24) this should have no ill effect,
817 -- so we need to add a guard for the case of finalizing the same
818 -- object twice.
819
820 Object.Reference := null;
821 Unreference (SR);
822 end if;
823 end Finalize;
824
825 ----------------
826 -- Find_Token --
827 ----------------
828
829 procedure Find_Token
830 (Source : Unbounded_Wide_String;
831 Set : Wide_Maps.Wide_Character_Set;
832 From : Positive;
833 Test : Strings.Membership;
834 First : out Positive;
835 Last : out Natural)
836 is
837 SR : constant Shared_Wide_String_Access := Source.Reference;
838 begin
839 Wide_Search.Find_Token
840 (SR.Data (From .. SR.Last), Set, Test, First, Last);
841 end Find_Token;
842
843 procedure Find_Token
844 (Source : Unbounded_Wide_String;
845 Set : Wide_Maps.Wide_Character_Set;
846 Test : Strings.Membership;
847 First : out Positive;
848 Last : out Natural)
849 is
850 SR : constant Shared_Wide_String_Access := Source.Reference;
851 begin
852 Wide_Search.Find_Token
853 (SR.Data (1 .. SR.Last), Set, Test, First, Last);
854 end Find_Token;
855
856 ----------
857 -- Free --
858 ----------
859
860 procedure Free (X : in out Wide_String_Access) is
861 procedure Deallocate is
862 new Ada.Unchecked_Deallocation (Wide_String, Wide_String_Access);
863 begin
864 Deallocate (X);
865 end Free;
866
867 ----------
868 -- Head --
869 ----------
870
871 function Head
872 (Source : Unbounded_Wide_String;
873 Count : Natural;
874 Pad : Wide_Character := Wide_Space) return Unbounded_Wide_String
875 is
876 SR : constant Shared_Wide_String_Access := Source.Reference;
877 DR : Shared_Wide_String_Access;
878
879 begin
880 -- Result is empty, reuse shared empty string
881
882 if Count = 0 then
883 Reference (Empty_Shared_Wide_String'Access);
884 DR := Empty_Shared_Wide_String'Access;
885
886 -- Length of the string is the same as requested, reuse source shared
887 -- string.
888
889 elsif Count = SR.Last then
890 Reference (SR);
891 DR := SR;
892
893 -- Otherwise, allocate new shared string and fill it
894
895 else
896 DR := Allocate (Count);
897
898 -- Length of the source string is more than requested, copy
899 -- corresponding slice.
900
901 if Count < SR.Last then
902 DR.Data (1 .. Count) := SR.Data (1 .. Count);
903
904 -- Length of the source string is less than requested, copy all
905 -- contents and fill others by Pad character.
906
907 else
908 DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
909
910 for J in SR.Last + 1 .. Count loop
911 DR.Data (J) := Pad;
912 end loop;
913 end if;
914
915 DR.Last := Count;
916 end if;
917
918 return (AF.Controlled with Reference => DR);
919 end Head;
920
921 procedure Head
922 (Source : in out Unbounded_Wide_String;
923 Count : Natural;
924 Pad : Wide_Character := Wide_Space)
925 is
926 SR : constant Shared_Wide_String_Access := Source.Reference;
927 DR : Shared_Wide_String_Access;
928
929 begin
930 -- Result is empty, reuse empty shared string
931
932 if Count = 0 then
933 Reference (Empty_Shared_Wide_String'Access);
934 Source.Reference := Empty_Shared_Wide_String'Access;
935 Unreference (SR);
936
937 -- Result is same with source string, reuse source shared string
938
939 elsif Count = SR.Last then
940 null;
941
942 -- Try to reuse existent shared string
943
944 elsif Can_Be_Reused (SR, Count) then
945 if Count > SR.Last then
946 for J in SR.Last + 1 .. Count loop
947 SR.Data (J) := Pad;
948 end loop;
949 end if;
950
951 SR.Last := Count;
952
953 -- Otherwise, allocate new shared string and fill it
954
955 else
956 DR := Allocate (Count);
957
958 -- Length of the source string is greater than requested, copy
959 -- corresponding slice.
960
961 if Count < SR.Last then
962 DR.Data (1 .. Count) := SR.Data (1 .. Count);
963
964 -- Length of the source string is less than requested, copy all
965 -- exists data and fill others by Pad character.
966
967 else
968 DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
969
970 for J in SR.Last + 1 .. Count loop
971 DR.Data (J) := Pad;
972 end loop;
973 end if;
974
975 DR.Last := Count;
976 Source.Reference := DR;
977 Unreference (SR);
978 end if;
979 end Head;
980
981 -----------
982 -- Index --
983 -----------
984
985 function Index
986 (Source : Unbounded_Wide_String;
987 Pattern : Wide_String;
988 Going : Strings.Direction := Strings.Forward;
989 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
990 return Natural
991 is
992 SR : constant Shared_Wide_String_Access := Source.Reference;
993 begin
994 return Wide_Search.Index
995 (SR.Data (1 .. SR.Last), Pattern, Going, Mapping);
996 end Index;
997
998 function Index
999 (Source : Unbounded_Wide_String;
1000 Pattern : Wide_String;
1001 Going : Direction := Forward;
1002 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
1003 is
1004 SR : constant Shared_Wide_String_Access := Source.Reference;
1005 begin
1006 return Wide_Search.Index
1007 (SR.Data (1 .. SR.Last), Pattern, Going, Mapping);
1008 end Index;
1009
1010 function Index
1011 (Source : Unbounded_Wide_String;
1012 Set : Wide_Maps.Wide_Character_Set;
1013 Test : Strings.Membership := Strings.Inside;
1014 Going : Strings.Direction := Strings.Forward) return Natural
1015 is
1016 SR : constant Shared_Wide_String_Access := Source.Reference;
1017 begin
1018 return Wide_Search.Index (SR.Data (1 .. SR.Last), Set, Test, Going);
1019 end Index;
1020
1021 function Index
1022 (Source : Unbounded_Wide_String;
1023 Pattern : Wide_String;
1024 From : Positive;
1025 Going : Direction := Forward;
1026 Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity)
1027 return Natural
1028 is
1029 SR : constant Shared_Wide_String_Access := Source.Reference;
1030 begin
1031 return Wide_Search.Index
1032 (SR.Data (1 .. SR.Last), Pattern, From, Going, Mapping);
1033 end Index;
1034
1035 function Index
1036 (Source : Unbounded_Wide_String;
1037 Pattern : Wide_String;
1038 From : Positive;
1039 Going : Direction := Forward;
1040 Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
1041 is
1042 SR : constant Shared_Wide_String_Access := Source.Reference;
1043 begin
1044 return Wide_Search.Index
1045 (SR.Data (1 .. SR.Last), Pattern, From, Going, Mapping);
1046 end Index;
1047
1048 function Index
1049 (Source : Unbounded_Wide_String;
1050 Set : Wide_Maps.Wide_Character_Set;
1051 From : Positive;
1052 Test : Membership := Inside;
1053 Going : Direction := Forward) return Natural
1054 is
1055 SR : constant Shared_Wide_String_Access := Source.Reference;
1056 begin
1057 return Wide_Search.Index
1058 (SR.Data (1 .. SR.Last), Set, From, Test, Going);
1059 end Index;
1060
1061 ---------------------
1062 -- Index_Non_Blank --
1063 ---------------------
1064
1065 function Index_Non_Blank
1066 (Source : Unbounded_Wide_String;
1067 Going : Strings.Direction := Strings.Forward) return Natural
1068 is
1069 SR : constant Shared_Wide_String_Access := Source.Reference;
1070 begin
1071 return Wide_Search.Index_Non_Blank (SR.Data (1 .. SR.Last), Going);
1072 end Index_Non_Blank;
1073
1074 function Index_Non_Blank
1075 (Source : Unbounded_Wide_String;
1076 From : Positive;
1077 Going : Direction := Forward) return Natural
1078 is
1079 SR : constant Shared_Wide_String_Access := Source.Reference;
1080 begin
1081 return Wide_Search.Index_Non_Blank
1082 (SR.Data (1 .. SR.Last), From, Going);
1083 end Index_Non_Blank;
1084
1085 ----------------
1086 -- Initialize --
1087 ----------------
1088
1089 procedure Initialize (Object : in out Unbounded_Wide_String) is
1090 begin
1091 Reference (Object.Reference);
1092 end Initialize;
1093
1094 ------------
1095 -- Insert --
1096 ------------
1097
1098 function Insert
1099 (Source : Unbounded_Wide_String;
1100 Before : Positive;
1101 New_Item : Wide_String) return Unbounded_Wide_String
1102 is
1103 SR : constant Shared_Wide_String_Access := Source.Reference;
1104 DL : constant Natural := SR.Last + New_Item'Length;
1105 DR : Shared_Wide_String_Access;
1106
1107 begin
1108 -- Check index first
1109
1110 if Before > SR.Last + 1 then
1111 raise Index_Error;
1112 end if;
1113
1114 -- Result is empty, reuse empty shared string
1115
1116 if DL = 0 then
1117 Reference (Empty_Shared_Wide_String'Access);
1118 DR := Empty_Shared_Wide_String'Access;
1119
1120 -- Inserted string is empty, reuse source shared string
1121
1122 elsif New_Item'Length = 0 then
1123 Reference (SR);
1124 DR := SR;
1125
1126 -- Otherwise, allocate new shared string and fill it
1127
1128 else
1129 DR := Allocate (DL + DL / Growth_Factor);
1130 DR.Data (1 .. Before - 1) := SR.Data (1 .. Before - 1);
1131 DR.Data (Before .. Before + New_Item'Length - 1) := New_Item;
1132 DR.Data (Before + New_Item'Length .. DL) :=
1133 SR.Data (Before .. SR.Last);
1134 DR.Last := DL;
1135 end if;
1136
1137 return (AF.Controlled with Reference => DR);
1138 end Insert;
1139
1140 procedure Insert
1141 (Source : in out Unbounded_Wide_String;
1142 Before : Positive;
1143 New_Item : Wide_String)
1144 is
1145 SR : constant Shared_Wide_String_Access := Source.Reference;
1146 DL : constant Natural := SR.Last + New_Item'Length;
1147 DR : Shared_Wide_String_Access;
1148
1149 begin
1150 -- Check bounds
1151
1152 if Before > SR.Last + 1 then
1153 raise Index_Error;
1154 end if;
1155
1156 -- Result is empty string, reuse empty shared string
1157
1158 if DL = 0 then
1159 Reference (Empty_Shared_Wide_String'Access);
1160 Source.Reference := Empty_Shared_Wide_String'Access;
1161 Unreference (SR);
1162
1163 -- Inserted string is empty, nothing to do
1164
1165 elsif New_Item'Length = 0 then
1166 null;
1167
1168 -- Try to reuse existent shared string first
1169
1170 elsif Can_Be_Reused (SR, DL) then
1171 SR.Data (Before + New_Item'Length .. DL) :=
1172 SR.Data (Before .. SR.Last);
1173 SR.Data (Before .. Before + New_Item'Length - 1) := New_Item;
1174 SR.Last := DL;
1175
1176 -- Otherwise, allocate new shared string and fill it
1177
1178 else
1179 DR := Allocate (DL + DL / Growth_Factor);
1180 DR.Data (1 .. Before - 1) := SR.Data (1 .. Before - 1);
1181 DR.Data (Before .. Before + New_Item'Length - 1) := New_Item;
1182 DR.Data (Before + New_Item'Length .. DL) :=
1183 SR.Data (Before .. SR.Last);
1184 DR.Last := DL;
1185 Source.Reference := DR;
1186 Unreference (SR);
1187 end if;
1188 end Insert;
1189
1190 ------------
1191 -- Length --
1192 ------------
1193
1194 function Length (Source : Unbounded_Wide_String) return Natural is
1195 begin
1196 return Source.Reference.Last;
1197 end Length;
1198
1199 ------------------------
1200 -- Non_Inlined_Append --
1201 ------------------------
1202
1203 procedure Non_Inlined_Append
1204 (Source : in out Unbounded_Wide_String;
1205 New_Item : Unbounded_Wide_String)
1206 is
1207 SR : constant Shared_Wide_String_Access := Source.Reference;
1208 NR : constant Shared_Wide_String_Access := New_Item.Reference;
1209 DL : constant Natural := SR.Last + NR.Last;
1210 DR : Shared_Wide_String_Access;
1211 begin
1212 DR := Allocate (DL + DL / Growth_Factor);
1213 DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
1214 DR.Data (SR.Last + 1 .. DL) := NR.Data (1 .. NR.Last);
1215 DR.Last := DL;
1216 Source.Reference := DR;
1217 Unreference (SR);
1218 end Non_Inlined_Append;
1219
1220 procedure Non_Inlined_Append
1221 (Source : in out Unbounded_Wide_String;
1222 New_Item : Wide_String)
1223 is
1224 SR : constant Shared_Wide_String_Access := Source.Reference;
1225 DL : constant Natural := SR.Last + New_Item'Length;
1226 DR : Shared_Wide_String_Access;
1227 begin
1228 DR := Allocate (DL + DL / Growth_Factor);
1229 DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
1230 DR.Data (SR.Last + 1 .. DL) := New_Item;
1231 DR.Last := DL;
1232 Source.Reference := DR;
1233 Unreference (SR);
1234 end Non_Inlined_Append;
1235
1236 procedure Non_Inlined_Append
1237 (Source : in out Unbounded_Wide_String;
1238 New_Item : Wide_Character)
1239 is
1240 SR : constant Shared_Wide_String_Access := Source.Reference;
1241 begin
1242 if SR.Last = Natural'Last then
1243 raise Constraint_Error;
1244 else
1245 declare
1246 DL : constant Natural := SR.Last + 1;
1247 DR : Shared_Wide_String_Access;
1248 begin
1249 DR := Allocate (DL + DL / Growth_Factor);
1250 DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
1251 DR.Data (DL) := New_Item;
1252 DR.Last := DL;
1253 Source.Reference := DR;
1254 Unreference (SR);
1255 end;
1256 end if;
1257 end Non_Inlined_Append;
1258
1259 ---------------
1260 -- Overwrite --
1261 ---------------
1262
1263 function Overwrite
1264 (Source : Unbounded_Wide_String;
1265 Position : Positive;
1266 New_Item : Wide_String) return Unbounded_Wide_String
1267 is
1268 SR : constant Shared_Wide_String_Access := Source.Reference;
1269 DL : Natural;
1270 DR : Shared_Wide_String_Access;
1271
1272 begin
1273 -- Check bounds
1274
1275 if Position > SR.Last + 1 then
1276 raise Index_Error;
1277 end if;
1278
1279 DL := Integer'Max (SR.Last, Position + New_Item'Length - 1);
1280
1281 -- Result is empty string, reuse empty shared string
1282
1283 if DL = 0 then
1284 Reference (Empty_Shared_Wide_String'Access);
1285 DR := Empty_Shared_Wide_String'Access;
1286
1287 -- Result is same with source string, reuse source shared string
1288
1289 elsif New_Item'Length = 0 then
1290 Reference (SR);
1291 DR := SR;
1292
1293 -- Otherwise, allocate new shared string and fill it
1294
1295 else
1296 DR := Allocate (DL);
1297 DR.Data (1 .. Position - 1) := SR.Data (1 .. Position - 1);
1298 DR.Data (Position .. Position + New_Item'Length - 1) := New_Item;
1299 DR.Data (Position + New_Item'Length .. DL) :=
1300 SR.Data (Position + New_Item'Length .. SR.Last);
1301 DR.Last := DL;
1302 end if;
1303
1304 return (AF.Controlled with Reference => DR);
1305 end Overwrite;
1306
1307 procedure Overwrite
1308 (Source : in out Unbounded_Wide_String;
1309 Position : Positive;
1310 New_Item : Wide_String)
1311 is
1312 SR : constant Shared_Wide_String_Access := Source.Reference;
1313 DL : Natural;
1314 DR : Shared_Wide_String_Access;
1315
1316 begin
1317 -- Bounds check
1318
1319 if Position > SR.Last + 1 then
1320 raise Index_Error;
1321 end if;
1322
1323 DL := Integer'Max (SR.Last, Position + New_Item'Length - 1);
1324
1325 -- Result is empty string, reuse empty shared string
1326
1327 if DL = 0 then
1328 Reference (Empty_Shared_Wide_String'Access);
1329 Source.Reference := Empty_Shared_Wide_String'Access;
1330 Unreference (SR);
1331
1332 -- String unchanged, nothing to do
1333
1334 elsif New_Item'Length = 0 then
1335 null;
1336
1337 -- Try to reuse existent shared string
1338
1339 elsif Can_Be_Reused (SR, DL) then
1340 SR.Data (Position .. Position + New_Item'Length - 1) := New_Item;
1341 SR.Last := DL;
1342
1343 -- Otherwise allocate new shared string and fill it
1344
1345 else
1346 DR := Allocate (DL);
1347 DR.Data (1 .. Position - 1) := SR.Data (1 .. Position - 1);
1348 DR.Data (Position .. Position + New_Item'Length - 1) := New_Item;
1349 DR.Data (Position + New_Item'Length .. DL) :=
1350 SR.Data (Position + New_Item'Length .. SR.Last);
1351 DR.Last := DL;
1352 Source.Reference := DR;
1353 Unreference (SR);
1354 end if;
1355 end Overwrite;
1356
1357 ---------------
1358 -- Reference --
1359 ---------------
1360
1361 procedure Reference (Item : not null Shared_Wide_String_Access) is
1362 begin
1363 System.Atomic_Counters.Increment (Item.Counter);
1364 end Reference;
1365
1366 ---------------------
1367 -- Replace_Element --
1368 ---------------------
1369
1370 procedure Replace_Element
1371 (Source : in out Unbounded_Wide_String;
1372 Index : Positive;
1373 By : Wide_Character)
1374 is
1375 SR : constant Shared_Wide_String_Access := Source.Reference;
1376 DR : Shared_Wide_String_Access;
1377
1378 begin
1379 -- Bounds check
1380
1381 if Index <= SR.Last then
1382
1383 -- Try to reuse existent shared string
1384
1385 if Can_Be_Reused (SR, SR.Last) then
1386 SR.Data (Index) := By;
1387
1388 -- Otherwise allocate new shared string and fill it
1389
1390 else
1391 DR := Allocate (SR.Last);
1392 DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
1393 DR.Data (Index) := By;
1394 DR.Last := SR.Last;
1395 Source.Reference := DR;
1396 Unreference (SR);
1397 end if;
1398
1399 else
1400 raise Index_Error;
1401 end if;
1402 end Replace_Element;
1403
1404 -------------------
1405 -- Replace_Slice --
1406 -------------------
1407
1408 function Replace_Slice
1409 (Source : Unbounded_Wide_String;
1410 Low : Positive;
1411 High : Natural;
1412 By : Wide_String) return Unbounded_Wide_String
1413 is
1414 SR : constant Shared_Wide_String_Access := Source.Reference;
1415 DL : Natural;
1416 DR : Shared_Wide_String_Access;
1417
1418 begin
1419 -- Check bounds
1420
1421 if Low > SR.Last + 1 then
1422 raise Index_Error;
1423 end if;
1424
1425 -- Do replace operation when removed slice is not empty
1426
1427 if High >= Low then
1428 DL := By'Length + SR.Last + Low - Integer'Min (High, SR.Last) - 1;
1429 -- This is the number of characters remaining in the string after
1430 -- replacing the slice.
1431
1432 -- Result is empty string, reuse empty shared string
1433
1434 if DL = 0 then
1435 Reference (Empty_Shared_Wide_String'Access);
1436 DR := Empty_Shared_Wide_String'Access;
1437
1438 -- Otherwise allocate new shared string and fill it
1439
1440 else
1441 DR := Allocate (DL);
1442 DR.Data (1 .. Low - 1) := SR.Data (1 .. Low - 1);
1443 DR.Data (Low .. Low + By'Length - 1) := By;
1444 DR.Data (Low + By'Length .. DL) := SR.Data (High + 1 .. SR.Last);
1445 DR.Last := DL;
1446 end if;
1447
1448 return (AF.Controlled with Reference => DR);
1449
1450 -- Otherwise just insert string
1451
1452 else
1453 return Insert (Source, Low, By);
1454 end if;
1455 end Replace_Slice;
1456
1457 procedure Replace_Slice
1458 (Source : in out Unbounded_Wide_String;
1459 Low : Positive;
1460 High : Natural;
1461 By : Wide_String)
1462 is
1463 SR : constant Shared_Wide_String_Access := Source.Reference;
1464 DL : Natural;
1465 DR : Shared_Wide_String_Access;
1466
1467 begin
1468 -- Bounds check
1469
1470 if Low > SR.Last + 1 then
1471 raise Index_Error;
1472 end if;
1473
1474 -- Do replace operation only when replaced slice is not empty
1475
1476 if High >= Low then
1477 DL := By'Length + SR.Last + Low - Integer'Min (High, SR.Last) - 1;
1478 -- This is the number of characters remaining in the string after
1479 -- replacing the slice.
1480
1481 -- Result is empty string, reuse empty shared string
1482
1483 if DL = 0 then
1484 Reference (Empty_Shared_Wide_String'Access);
1485 Source.Reference := Empty_Shared_Wide_String'Access;
1486 Unreference (SR);
1487
1488 -- Try to reuse existent shared string
1489
1490 elsif Can_Be_Reused (SR, DL) then
1491 SR.Data (Low + By'Length .. DL) := SR.Data (High + 1 .. SR.Last);
1492 SR.Data (Low .. Low + By'Length - 1) := By;
1493 SR.Last := DL;
1494
1495 -- Otherwise allocate new shared string and fill it
1496
1497 else
1498 DR := Allocate (DL);
1499 DR.Data (1 .. Low - 1) := SR.Data (1 .. Low - 1);
1500 DR.Data (Low .. Low + By'Length - 1) := By;
1501 DR.Data (Low + By'Length .. DL) := SR.Data (High + 1 .. SR.Last);
1502 DR.Last := DL;
1503 Source.Reference := DR;
1504 Unreference (SR);
1505 end if;
1506
1507 -- Otherwise just insert item
1508
1509 else
1510 Insert (Source, Low, By);
1511 end if;
1512 end Replace_Slice;
1513
1514 -------------------------------
1515 -- Set_Unbounded_Wide_String --
1516 -------------------------------
1517
1518 procedure Set_Unbounded_Wide_String
1519 (Target : out Unbounded_Wide_String;
1520 Source : Wide_String)
1521 is
1522 TR : constant Shared_Wide_String_Access := Target.Reference;
1523 DR : Shared_Wide_String_Access;
1524
1525 begin
1526 -- In case of empty string, reuse empty shared string
1527
1528 if Source'Length = 0 then
1529 Reference (Empty_Shared_Wide_String'Access);
1530 Target.Reference := Empty_Shared_Wide_String'Access;
1531
1532 else
1533 -- Try to reuse existent shared string
1534
1535 if Can_Be_Reused (TR, Source'Length) then
1536 Reference (TR);
1537 DR := TR;
1538
1539 -- Otherwise allocate new shared string
1540
1541 else
1542 DR := Allocate (Source'Length);
1543 Target.Reference := DR;
1544 end if;
1545
1546 DR.Data (1 .. Source'Length) := Source;
1547 DR.Last := Source'Length;
1548 end if;
1549
1550 Unreference (TR);
1551 end Set_Unbounded_Wide_String;
1552
1553 -----------
1554 -- Slice --
1555 -----------
1556
1557 function Slice
1558 (Source : Unbounded_Wide_String;
1559 Low : Positive;
1560 High : Natural) return Wide_String
1561 is
1562 SR : constant Shared_Wide_String_Access := Source.Reference;
1563
1564 begin
1565 -- Note: test of High > Length is in accordance with AI95-00128
1566
1567 if Low > SR.Last + 1 or else High > SR.Last then
1568 raise Index_Error;
1569
1570 else
1571 return SR.Data (Low .. High);
1572 end if;
1573 end Slice;
1574
1575 ----------
1576 -- Tail --
1577 ----------
1578
1579 function Tail
1580 (Source : Unbounded_Wide_String;
1581 Count : Natural;
1582 Pad : Wide_Character := Wide_Space) return Unbounded_Wide_String
1583 is
1584 SR : constant Shared_Wide_String_Access := Source.Reference;
1585 DR : Shared_Wide_String_Access;
1586
1587 begin
1588 -- For empty result reuse empty shared string
1589
1590 if Count = 0 then
1591 Reference (Empty_Shared_Wide_String'Access);
1592 DR := Empty_Shared_Wide_String'Access;
1593
1594 -- Result is hole source string, reuse source shared string
1595
1596 elsif Count = SR.Last then
1597 Reference (SR);
1598 DR := SR;
1599
1600 -- Otherwise allocate new shared string and fill it
1601
1602 else
1603 DR := Allocate (Count);
1604
1605 if Count < SR.Last then
1606 DR.Data (1 .. Count) := SR.Data (SR.Last - Count + 1 .. SR.Last);
1607
1608 else
1609 for J in 1 .. Count - SR.Last loop
1610 DR.Data (J) := Pad;
1611 end loop;
1612
1613 DR.Data (Count - SR.Last + 1 .. Count) := SR.Data (1 .. SR.Last);
1614 end if;
1615
1616 DR.Last := Count;
1617 end if;
1618
1619 return (AF.Controlled with Reference => DR);
1620 end Tail;
1621
1622 procedure Tail
1623 (Source : in out Unbounded_Wide_String;
1624 Count : Natural;
1625 Pad : Wide_Character := Wide_Space)
1626 is
1627 SR : constant Shared_Wide_String_Access := Source.Reference;
1628 DR : Shared_Wide_String_Access;
1629
1630 procedure Common
1631 (SR : Shared_Wide_String_Access;
1632 DR : Shared_Wide_String_Access;
1633 Count : Natural);
1634 -- Common code of tail computation. SR/DR can point to the same object
1635
1636 ------------
1637 -- Common --
1638 ------------
1639
1640 procedure Common
1641 (SR : Shared_Wide_String_Access;
1642 DR : Shared_Wide_String_Access;
1643 Count : Natural) is
1644 begin
1645 if Count < SR.Last then
1646 DR.Data (1 .. Count) := SR.Data (SR.Last - Count + 1 .. SR.Last);
1647
1648 else
1649 DR.Data (Count - SR.Last + 1 .. Count) := SR.Data (1 .. SR.Last);
1650
1651 for J in 1 .. Count - SR.Last loop
1652 DR.Data (J) := Pad;
1653 end loop;
1654 end if;
1655
1656 DR.Last := Count;
1657 end Common;
1658
1659 begin
1660 -- Result is empty string, reuse empty shared string
1661
1662 if Count = 0 then
1663 Reference (Empty_Shared_Wide_String'Access);
1664 Source.Reference := Empty_Shared_Wide_String'Access;
1665 Unreference (SR);
1666
1667 -- Length of the result is the same with length of the source string,
1668 -- reuse source shared string.
1669
1670 elsif Count = SR.Last then
1671 null;
1672
1673 -- Try to reuse existent shared string
1674
1675 elsif Can_Be_Reused (SR, Count) then
1676 Common (SR, SR, Count);
1677
1678 -- Otherwise allocate new shared string and fill it
1679
1680 else
1681 DR := Allocate (Count);
1682 Common (SR, DR, Count);
1683 Source.Reference := DR;
1684 Unreference (SR);
1685 end if;
1686 end Tail;
1687
1688 --------------------
1689 -- To_Wide_String --
1690 --------------------
1691
1692 function To_Wide_String
1693 (Source : Unbounded_Wide_String) return Wide_String is
1694 begin
1695 return Source.Reference.Data (1 .. Source.Reference.Last);
1696 end To_Wide_String;
1697
1698 ------------------------------
1699 -- To_Unbounded_Wide_String --
1700 ------------------------------
1701
1702 function To_Unbounded_Wide_String
1703 (Source : Wide_String) return Unbounded_Wide_String
1704 is
1705 DR : Shared_Wide_String_Access;
1706
1707 begin
1708 if Source'Length = 0 then
1709 Reference (Empty_Shared_Wide_String'Access);
1710 DR := Empty_Shared_Wide_String'Access;
1711
1712 else
1713 DR := Allocate (Source'Length);
1714 DR.Data (1 .. Source'Length) := Source;
1715 DR.Last := Source'Length;
1716 end if;
1717
1718 return (AF.Controlled with Reference => DR);
1719 end To_Unbounded_Wide_String;
1720
1721 function To_Unbounded_Wide_String
1722 (Length : Natural) return Unbounded_Wide_String
1723 is
1724 DR : Shared_Wide_String_Access;
1725
1726 begin
1727 if Length = 0 then
1728 Reference (Empty_Shared_Wide_String'Access);
1729 DR := Empty_Shared_Wide_String'Access;
1730
1731 else
1732 DR := Allocate (Length);
1733 DR.Last := Length;
1734 end if;
1735
1736 return (AF.Controlled with Reference => DR);
1737 end To_Unbounded_Wide_String;
1738
1739 ---------------
1740 -- Translate --
1741 ---------------
1742
1743 function Translate
1744 (Source : Unbounded_Wide_String;
1745 Mapping : Wide_Maps.Wide_Character_Mapping) return Unbounded_Wide_String
1746 is
1747 SR : constant Shared_Wide_String_Access := Source.Reference;
1748 DR : Shared_Wide_String_Access;
1749
1750 begin
1751 -- Nothing to translate, reuse empty shared string
1752
1753 if SR.Last = 0 then
1754 Reference (Empty_Shared_Wide_String'Access);
1755 DR := Empty_Shared_Wide_String'Access;
1756
1757 -- Otherwise, allocate new shared string and fill it
1758
1759 else
1760 DR := Allocate (SR.Last);
1761
1762 for J in 1 .. SR.Last loop
1763 DR.Data (J) := Value (Mapping, SR.Data (J));
1764 end loop;
1765
1766 DR.Last := SR.Last;
1767 end if;
1768
1769 return (AF.Controlled with Reference => DR);
1770 end Translate;
1771
1772 procedure Translate
1773 (Source : in out Unbounded_Wide_String;
1774 Mapping : Wide_Maps.Wide_Character_Mapping)
1775 is
1776 SR : constant Shared_Wide_String_Access := Source.Reference;
1777 DR : Shared_Wide_String_Access;
1778
1779 begin
1780 -- Nothing to translate
1781
1782 if SR.Last = 0 then
1783 null;
1784
1785 -- Try to reuse shared string
1786
1787 elsif Can_Be_Reused (SR, SR.Last) then
1788 for J in 1 .. SR.Last loop
1789 SR.Data (J) := Value (Mapping, SR.Data (J));
1790 end loop;
1791
1792 -- Otherwise, allocate new shared string
1793
1794 else
1795 DR := Allocate (SR.Last);
1796
1797 for J in 1 .. SR.Last loop
1798 DR.Data (J) := Value (Mapping, SR.Data (J));
1799 end loop;
1800
1801 DR.Last := SR.Last;
1802 Source.Reference := DR;
1803 Unreference (SR);
1804 end if;
1805 end Translate;
1806
1807 function Translate
1808 (Source : Unbounded_Wide_String;
1809 Mapping : Wide_Maps.Wide_Character_Mapping_Function)
1810 return Unbounded_Wide_String
1811 is
1812 SR : constant Shared_Wide_String_Access := Source.Reference;
1813 DR : Shared_Wide_String_Access;
1814
1815 begin
1816 -- Nothing to translate, reuse empty shared string
1817
1818 if SR.Last = 0 then
1819 Reference (Empty_Shared_Wide_String'Access);
1820 DR := Empty_Shared_Wide_String'Access;
1821
1822 -- Otherwise, allocate new shared string and fill it
1823
1824 else
1825 DR := Allocate (SR.Last);
1826
1827 for J in 1 .. SR.Last loop
1828 DR.Data (J) := Mapping.all (SR.Data (J));
1829 end loop;
1830
1831 DR.Last := SR.Last;
1832 end if;
1833
1834 return (AF.Controlled with Reference => DR);
1835
1836 exception
1837 when others =>
1838 Unreference (DR);
1839
1840 raise;
1841 end Translate;
1842
1843 procedure Translate
1844 (Source : in out Unbounded_Wide_String;
1845 Mapping : Wide_Maps.Wide_Character_Mapping_Function)
1846 is
1847 SR : constant Shared_Wide_String_Access := Source.Reference;
1848 DR : Shared_Wide_String_Access;
1849
1850 begin
1851 -- Nothing to translate
1852
1853 if SR.Last = 0 then
1854 null;
1855
1856 -- Try to reuse shared string
1857
1858 elsif Can_Be_Reused (SR, SR.Last) then
1859 for J in 1 .. SR.Last loop
1860 SR.Data (J) := Mapping.all (SR.Data (J));
1861 end loop;
1862
1863 -- Otherwise allocate new shared string and fill it
1864
1865 else
1866 DR := Allocate (SR.Last);
1867
1868 for J in 1 .. SR.Last loop
1869 DR.Data (J) := Mapping.all (SR.Data (J));
1870 end loop;
1871
1872 DR.Last := SR.Last;
1873 Source.Reference := DR;
1874 Unreference (SR);
1875 end if;
1876
1877 exception
1878 when others =>
1879 if DR /= null then
1880 Unreference (DR);
1881 end if;
1882
1883 raise;
1884 end Translate;
1885
1886 ----------
1887 -- Trim --
1888 ----------
1889
1890 function Trim
1891 (Source : Unbounded_Wide_String;
1892 Side : Trim_End) return Unbounded_Wide_String
1893 is
1894 SR : constant Shared_Wide_String_Access := Source.Reference;
1895 DL : Natural;
1896 DR : Shared_Wide_String_Access;
1897 Low : Natural;
1898 High : Natural;
1899
1900 begin
1901 Low := Index_Non_Blank (Source, Forward);
1902
1903 -- All blanks, reuse empty shared string
1904
1905 if Low = 0 then
1906 Reference (Empty_Shared_Wide_String'Access);
1907 DR := Empty_Shared_Wide_String'Access;
1908
1909 else
1910 case Side is
1911 when Left =>
1912 High := SR.Last;
1913 DL := SR.Last - Low + 1;
1914
1915 when Right =>
1916 Low := 1;
1917 High := Index_Non_Blank (Source, Backward);
1918 DL := High;
1919
1920 when Both =>
1921 High := Index_Non_Blank (Source, Backward);
1922 DL := High - Low + 1;
1923 end case;
1924
1925 -- Length of the result is the same as length of the source string,
1926 -- reuse source shared string.
1927
1928 if DL = SR.Last then
1929 Reference (SR);
1930 DR := SR;
1931
1932 -- Otherwise, allocate new shared string
1933
1934 else
1935 DR := Allocate (DL);
1936 DR.Data (1 .. DL) := SR.Data (Low .. High);
1937 DR.Last := DL;
1938 end if;
1939 end if;
1940
1941 return (AF.Controlled with Reference => DR);
1942 end Trim;
1943
1944 procedure Trim
1945 (Source : in out Unbounded_Wide_String;
1946 Side : Trim_End)
1947 is
1948 SR : constant Shared_Wide_String_Access := Source.Reference;
1949 DL : Natural;
1950 DR : Shared_Wide_String_Access;
1951 Low : Natural;
1952 High : Natural;
1953
1954 begin
1955 Low := Index_Non_Blank (Source, Forward);
1956
1957 -- All blanks, reuse empty shared string
1958
1959 if Low = 0 then
1960 Reference (Empty_Shared_Wide_String'Access);
1961 Source.Reference := Empty_Shared_Wide_String'Access;
1962 Unreference (SR);
1963
1964 else
1965 case Side is
1966 when Left =>
1967 High := SR.Last;
1968 DL := SR.Last - Low + 1;
1969
1970 when Right =>
1971 Low := 1;
1972 High := Index_Non_Blank (Source, Backward);
1973 DL := High;
1974
1975 when Both =>
1976 High := Index_Non_Blank (Source, Backward);
1977 DL := High - Low + 1;
1978 end case;
1979
1980 -- Length of the result is the same as length of the source string,
1981 -- nothing to do.
1982
1983 if DL = SR.Last then
1984 null;
1985
1986 -- Try to reuse existent shared string
1987
1988 elsif Can_Be_Reused (SR, DL) then
1989 SR.Data (1 .. DL) := SR.Data (Low .. High);
1990 SR.Last := DL;
1991
1992 -- Otherwise, allocate new shared string
1993
1994 else
1995 DR := Allocate (DL);
1996 DR.Data (1 .. DL) := SR.Data (Low .. High);
1997 DR.Last := DL;
1998 Source.Reference := DR;
1999 Unreference (SR);
2000 end if;
2001 end if;
2002 end Trim;
2003
2004 function Trim
2005 (Source : Unbounded_Wide_String;
2006 Left : Wide_Maps.Wide_Character_Set;
2007 Right : Wide_Maps.Wide_Character_Set) return Unbounded_Wide_String
2008 is
2009 SR : constant Shared_Wide_String_Access := Source.Reference;
2010 DL : Natural;
2011 DR : Shared_Wide_String_Access;
2012 Low : Natural;
2013 High : Natural;
2014
2015 begin
2016 Low := Index (Source, Left, Outside, Forward);
2017
2018 -- Source includes only characters from Left set, reuse empty shared
2019 -- string.
2020
2021 if Low = 0 then
2022 Reference (Empty_Shared_Wide_String'Access);
2023 DR := Empty_Shared_Wide_String'Access;
2024
2025 else
2026 High := Index (Source, Right, Outside, Backward);
2027 DL := Integer'Max (0, High - Low + 1);
2028
2029 -- Source includes only characters from Right set or result string
2030 -- is empty, reuse empty shared string.
2031
2032 if High = 0 or else DL = 0 then
2033 Reference (Empty_Shared_Wide_String'Access);
2034 DR := Empty_Shared_Wide_String'Access;
2035
2036 -- Otherwise, allocate new shared string and fill it
2037
2038 else
2039 DR := Allocate (DL);
2040 DR.Data (1 .. DL) := SR.Data (Low .. High);
2041 DR.Last := DL;
2042 end if;
2043 end if;
2044
2045 return (AF.Controlled with Reference => DR);
2046 end Trim;
2047
2048 procedure Trim
2049 (Source : in out Unbounded_Wide_String;
2050 Left : Wide_Maps.Wide_Character_Set;
2051 Right : Wide_Maps.Wide_Character_Set)
2052 is
2053 SR : constant Shared_Wide_String_Access := Source.Reference;
2054 DL : Natural;
2055 DR : Shared_Wide_String_Access;
2056 Low : Natural;
2057 High : Natural;
2058
2059 begin
2060 Low := Index (Source, Left, Outside, Forward);
2061
2062 -- Source includes only characters from Left set, reuse empty shared
2063 -- string.
2064
2065 if Low = 0 then
2066 Reference (Empty_Shared_Wide_String'Access);
2067 Source.Reference := Empty_Shared_Wide_String'Access;
2068 Unreference (SR);
2069
2070 else
2071 High := Index (Source, Right, Outside, Backward);
2072 DL := Integer'Max (0, High - Low + 1);
2073
2074 -- Source includes only characters from Right set or result string
2075 -- is empty, reuse empty shared string.
2076
2077 if High = 0 or else DL = 0 then
2078 Reference (Empty_Shared_Wide_String'Access);
2079 Source.Reference := Empty_Shared_Wide_String'Access;
2080 Unreference (SR);
2081
2082 -- Try to reuse existent shared string
2083
2084 elsif Can_Be_Reused (SR, DL) then
2085 SR.Data (1 .. DL) := SR.Data (Low .. High);
2086 SR.Last := DL;
2087
2088 -- Otherwise, allocate new shared string and fill it
2089
2090 else
2091 DR := Allocate (DL);
2092 DR.Data (1 .. DL) := SR.Data (Low .. High);
2093 DR.Last := DL;
2094 Source.Reference := DR;
2095 Unreference (SR);
2096 end if;
2097 end if;
2098 end Trim;
2099
2100 ---------------------
2101 -- Unbounded_Slice --
2102 ---------------------
2103
2104 function Unbounded_Slice
2105 (Source : Unbounded_Wide_String;
2106 Low : Positive;
2107 High : Natural) return Unbounded_Wide_String
2108 is
2109 SR : constant Shared_Wide_String_Access := Source.Reference;
2110 DL : Natural;
2111 DR : Shared_Wide_String_Access;
2112
2113 begin
2114 -- Check bounds
2115
2116 if Low > SR.Last + 1 or else High > SR.Last then
2117 raise Index_Error;
2118
2119 -- Result is empty slice, reuse empty shared string
2120
2121 elsif Low > High then
2122 Reference (Empty_Shared_Wide_String'Access);
2123 DR := Empty_Shared_Wide_String'Access;
2124
2125 -- Otherwise, allocate new shared string and fill it
2126
2127 else
2128 DL := High - Low + 1;
2129 DR := Allocate (DL);
2130 DR.Data (1 .. DL) := SR.Data (Low .. High);
2131 DR.Last := DL;
2132 end if;
2133
2134 return (AF.Controlled with Reference => DR);
2135 end Unbounded_Slice;
2136
2137 procedure Unbounded_Slice
2138 (Source : Unbounded_Wide_String;
2139 Target : out Unbounded_Wide_String;
2140 Low : Positive;
2141 High : Natural)
2142 is
2143 SR : constant Shared_Wide_String_Access := Source.Reference;
2144 TR : constant Shared_Wide_String_Access := Target.Reference;
2145 DL : Natural;
2146 DR : Shared_Wide_String_Access;
2147
2148 begin
2149 -- Check bounds
2150
2151 if Low > SR.Last + 1 or else High > SR.Last then
2152 raise Index_Error;
2153
2154 -- Result is empty slice, reuse empty shared string
2155
2156 elsif Low > High then
2157 Reference (Empty_Shared_Wide_String'Access);
2158 Target.Reference := Empty_Shared_Wide_String'Access;
2159 Unreference (TR);
2160
2161 else
2162 DL := High - Low + 1;
2163
2164 -- Try to reuse existent shared string
2165
2166 if Can_Be_Reused (TR, DL) then
2167 TR.Data (1 .. DL) := SR.Data (Low .. High);
2168 TR.Last := DL;
2169
2170 -- Otherwise, allocate new shared string and fill it
2171
2172 else
2173 DR := Allocate (DL);
2174 DR.Data (1 .. DL) := SR.Data (Low .. High);
2175 DR.Last := DL;
2176 Target.Reference := DR;
2177 Unreference (TR);
2178 end if;
2179 end if;
2180 end Unbounded_Slice;
2181
2182 -----------------
2183 -- Unreference --
2184 -----------------
2185
2186 procedure Unreference (Item : not null Shared_Wide_String_Access) is
2187
2188 procedure Free is
2189 new Ada.Unchecked_Deallocation
2190 (Shared_Wide_String, Shared_Wide_String_Access);
2191
2192 Aux : Shared_Wide_String_Access := Item;
2193
2194 begin
2195 if System.Atomic_Counters.Decrement (Aux.Counter) then
2196
2197 -- Reference counter of Empty_Shared_Wide_String must never reach
2198 -- zero.
2199
2200 pragma Assert (Aux /= Empty_Shared_Wide_String'Access);
2201
2202 Free (Aux);
2203 end if;
2204 end Unreference;
2205
2206 end Ada.Strings.Wide_Unbounded;