Debian Codegraph @

gcc/ada/libgnat/a-strunb__shared.adb

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