1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . W I D E _ W I D E _ U N B O U N D E D --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Strings
.Wide_Wide_Search
;
33 with Ada
.Unchecked_Deallocation
;
35 package body Ada
.Strings
.Wide_Wide_Unbounded
is
37 use Ada
.Strings
.Wide_Wide_Maps
;
39 Growth_Factor
: constant := 32;
40 -- The growth factor controls how much extra space is allocated when
41 -- we have to increase the size of an allocated unbounded string. By
42 -- allocating extra space, we avoid the need to reallocate on every
43 -- append, particularly important when a string is built up by repeated
44 -- append operations of small pieces. This is expressed as a factor so
45 -- 32 means add 1/32 of the length of the string as growth space.
47 Min_Mul_Alloc
: constant := Standard
'Maximum_Alignment;
48 -- Allocation will be done by a multiple of Min_Mul_Alloc. This causes
49 -- no memory loss as most (all?) malloc implementations are obliged to
50 -- align the returned memory on the maximum alignment as malloc does not
51 -- know the target alignment.
53 procedure Sync_Add_And_Fetch
54 (Ptr
: access Interfaces
.Unsigned_32
;
55 Value
: Interfaces
.Unsigned_32
);
56 pragma Import
(Intrinsic
, Sync_Add_And_Fetch
, "__sync_add_and_fetch_4");
58 function Sync_Sub_And_Fetch
59 (Ptr
: access Interfaces
.Unsigned_32
;
60 Value
: Interfaces
.Unsigned_32
) return Interfaces
.Unsigned_32
;
61 pragma Import
(Intrinsic
, Sync_Sub_And_Fetch
, "__sync_sub_and_fetch_4");
63 function Aligned_Max_Length
(Max_Length
: Natural) return Natural;
64 -- Returns recommended length of the shared string which is greater or
65 -- equal to specified length. Calculation take in sense alignment of
66 -- the allocated memory segments to use memory effectively by
67 -- Append/Insert/etc operations.
74 (Left
: Unbounded_Wide_Wide_String
;
75 Right
: Unbounded_Wide_Wide_String
) return Unbounded_Wide_Wide_String
77 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
78 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
79 DL
: constant Natural := LR
.Last
+ RR
.Last
;
80 DR
: Shared_Wide_Wide_String_Access
;
83 -- Result is an empty string, reuse shared empty string.
86 Reference
(Empty_Shared_Wide_Wide_String
'Access);
87 DR
:= Empty_Shared_Wide_Wide_String
'Access;
89 -- Left string is empty, return Rigth string.
91 elsif LR
.Last
= 0 then
95 -- Right string is empty, return Left string.
97 elsif RR
.Last
= 0 then
101 -- Overwise, allocate new shared string and fill data.
104 DR
:= Allocate
(LR
.Last
+ RR
.Last
);
105 DR
.Data
(1 .. LR
.Last
) := LR
.Data
(1 .. LR
.Last
);
106 DR
.Data
(LR
.Last
+ 1 .. DL
) := RR
.Data
(1 .. RR
.Last
);
110 return (AF
.Controlled
with Reference
=> DR
);
114 (Left
: Unbounded_Wide_Wide_String
;
115 Right
: Wide_Wide_String
) return Unbounded_Wide_Wide_String
117 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
118 DL
: constant Natural := LR
.Last
+ Right
'Length;
119 DR
: Shared_Wide_Wide_String_Access
;
122 -- Result is an empty string, reuse shared empty string.
125 Reference
(Empty_Shared_Wide_Wide_String
'Access);
126 DR
:= Empty_Shared_Wide_Wide_String
'Access;
128 -- Right is an empty string, return Left string.
130 elsif Right
'Length = 0 then
134 -- Otherwise, allocate new shared string and fill it.
138 DR
.Data
(1 .. LR
.Last
) := LR
.Data
(1 .. LR
.Last
);
139 DR
.Data
(LR
.Last
+ 1 .. DL
) := Right
;
143 return (AF
.Controlled
with Reference
=> DR
);
147 (Left
: Wide_Wide_String
;
148 Right
: Unbounded_Wide_Wide_String
) return Unbounded_Wide_Wide_String
150 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
151 DL
: constant Natural := Left
'Length + RR
.Last
;
152 DR
: Shared_Wide_Wide_String_Access
;
155 -- Result is an empty string, reuse shared one.
158 Reference
(Empty_Shared_Wide_Wide_String
'Access);
159 DR
:= Empty_Shared_Wide_Wide_String
'Access;
161 -- Left is empty string, return Right string.
163 elsif Left
'Length = 0 then
167 -- Otherwise, allocate new shared string and fill it.
171 DR
.Data
(1 .. Left
'Length) := Left
;
172 DR
.Data
(Left
'Length + 1 .. DL
) := RR
.Data
(1 .. RR
.Last
);
176 return (AF
.Controlled
with Reference
=> DR
);
180 (Left
: Unbounded_Wide_Wide_String
;
181 Right
: Wide_Wide_Character
) return Unbounded_Wide_Wide_String
183 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
184 DL
: constant Natural := LR
.Last
+ 1;
185 DR
: Shared_Wide_Wide_String_Access
;
189 DR
.Data
(1 .. LR
.Last
) := LR
.Data
(1 .. LR
.Last
);
190 DR
.Data
(DL
) := Right
;
193 return (AF
.Controlled
with Reference
=> DR
);
197 (Left
: Wide_Wide_Character
;
198 Right
: Unbounded_Wide_Wide_String
) return Unbounded_Wide_Wide_String
200 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
201 DL
: constant Natural := 1 + RR
.Last
;
202 DR
: Shared_Wide_Wide_String_Access
;
207 DR
.Data
(2 .. DL
) := RR
.Data
(1 .. RR
.Last
);
210 return (AF
.Controlled
with Reference
=> DR
);
219 Right
: Wide_Wide_Character
) return Unbounded_Wide_Wide_String
221 DR
: Shared_Wide_Wide_String_Access
;
224 -- Result is an empty string, reuse shared empty string.
227 Reference
(Empty_Shared_Wide_Wide_String
'Access);
228 DR
:= Empty_Shared_Wide_Wide_String
'Access;
230 -- Otherwise, allocate new shared string and fill it.
233 DR
:= Allocate
(Left
);
235 for J
in 1 .. Left
loop
236 DR
.Data
(J
) := Right
;
242 return (AF
.Controlled
with Reference
=> DR
);
247 Right
: Wide_Wide_String
) return Unbounded_Wide_Wide_String
249 DL
: constant Natural := Left
* Right
'Length;
250 DR
: Shared_Wide_Wide_String_Access
;
254 -- Result is an empty string, reuse shared empty string.
257 Reference
(Empty_Shared_Wide_Wide_String
'Access);
258 DR
:= Empty_Shared_Wide_Wide_String
'Access;
260 -- Otherwise, allocate new shared string and fill it.
266 for J
in 1 .. Left
loop
267 DR
.Data
(K
.. K
+ Right
'Length - 1) := Right
;
268 K
:= K
+ Right
'Length;
274 return (AF
.Controlled
with Reference
=> DR
);
279 Right
: Unbounded_Wide_Wide_String
) return Unbounded_Wide_Wide_String
281 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
282 DL
: constant Natural := Left
* RR
.Last
;
283 DR
: Shared_Wide_Wide_String_Access
;
287 -- Result is an empty string, reuse shared empty string.
290 Reference
(Empty_Shared_Wide_Wide_String
'Access);
291 DR
:= Empty_Shared_Wide_Wide_String
'Access;
293 -- Coefficient is one, just return string itself.
299 -- Otherwise, allocate new shared string and fill it.
305 for J
in 1 .. Left
loop
306 DR
.Data
(K
.. K
+ RR
.Last
- 1) := RR
.Data
(1 .. RR
.Last
);
313 return (AF
.Controlled
with Reference
=> DR
);
321 (Left
: Unbounded_Wide_Wide_String
;
322 Right
: Unbounded_Wide_Wide_String
) return Boolean
324 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
325 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
327 return LR
.Data
(1 .. LR
.Last
) < RR
.Data
(1 .. RR
.Last
);
331 (Left
: Unbounded_Wide_Wide_String
;
332 Right
: Wide_Wide_String
) return Boolean
334 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
336 return LR
.Data
(1 .. LR
.Last
) < Right
;
340 (Left
: Wide_Wide_String
;
341 Right
: Unbounded_Wide_Wide_String
) return Boolean
343 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
345 return Left
< RR
.Data
(1 .. RR
.Last
);
353 (Left
: Unbounded_Wide_Wide_String
;
354 Right
: Unbounded_Wide_Wide_String
) return Boolean
356 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
357 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
360 -- LR = RR means two strings shares shared string, thus they are equal
362 return LR
= RR
or else LR
.Data
(1 .. LR
.Last
) <= RR
.Data
(1 .. RR
.Last
);
366 (Left
: Unbounded_Wide_Wide_String
;
367 Right
: Wide_Wide_String
) return Boolean
369 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
371 return LR
.Data
(1 .. LR
.Last
) <= Right
;
375 (Left
: Wide_Wide_String
;
376 Right
: Unbounded_Wide_Wide_String
) return Boolean
378 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
380 return Left
<= RR
.Data
(1 .. RR
.Last
);
388 (Left
: Unbounded_Wide_Wide_String
;
389 Right
: Unbounded_Wide_Wide_String
) return Boolean
391 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
392 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
395 return LR
= RR
or else LR
.Data
(1 .. LR
.Last
) = RR
.Data
(1 .. RR
.Last
);
396 -- LR = RR means two strings shares shared string, thus they are equal.
400 (Left
: Unbounded_Wide_Wide_String
;
401 Right
: Wide_Wide_String
) return Boolean
403 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
405 return LR
.Data
(1 .. LR
.Last
) = Right
;
409 (Left
: Wide_Wide_String
;
410 Right
: Unbounded_Wide_Wide_String
) return Boolean
412 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
414 return Left
= RR
.Data
(1 .. RR
.Last
);
422 (Left
: Unbounded_Wide_Wide_String
;
423 Right
: Unbounded_Wide_Wide_String
) return Boolean
425 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
426 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
428 return LR
.Data
(1 .. LR
.Last
) > RR
.Data
(1 .. RR
.Last
);
432 (Left
: Unbounded_Wide_Wide_String
;
433 Right
: Wide_Wide_String
) return Boolean
435 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
437 return LR
.Data
(1 .. LR
.Last
) > Right
;
441 (Left
: Wide_Wide_String
;
442 Right
: Unbounded_Wide_Wide_String
) return Boolean
444 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
446 return Left
> RR
.Data
(1 .. RR
.Last
);
454 (Left
: Unbounded_Wide_Wide_String
;
455 Right
: Unbounded_Wide_Wide_String
) return Boolean
457 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
458 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
461 -- LR = RR means two strings shares shared string, thus they are equal
463 return LR
= RR
or else LR
.Data
(1 .. LR
.Last
) >= RR
.Data
(1 .. RR
.Last
);
467 (Left
: Unbounded_Wide_Wide_String
;
468 Right
: Wide_Wide_String
) return Boolean
470 LR
: constant Shared_Wide_Wide_String_Access
:= Left
.Reference
;
472 return LR
.Data
(1 .. LR
.Last
) >= Right
;
476 (Left
: Wide_Wide_String
;
477 Right
: Unbounded_Wide_Wide_String
) return Boolean
479 RR
: constant Shared_Wide_Wide_String_Access
:= Right
.Reference
;
481 return Left
>= RR
.Data
(1 .. RR
.Last
);
488 procedure Adjust
(Object
: in out Unbounded_Wide_Wide_String
) is
490 Reference
(Object
.Reference
);
493 ------------------------
494 -- Aligned_Max_Length --
495 ------------------------
497 function Aligned_Max_Length
(Max_Length
: Natural) return Natural is
498 Static_Size
: constant Natural :=
499 Empty_Shared_Wide_Wide_String
'Size
500 / Standard
'Storage_Unit;
501 -- Total size of all static components
503 Element_Size
: constant Natural :=
504 Wide_Wide_Character
'Size / Standard
'Storage_Unit;
508 (((Static_Size
+ Max_Length
* Element_Size
- 1) / Min_Mul_Alloc
+ 2)
509 * Min_Mul_Alloc
- Static_Size
) / Element_Size
;
510 end Aligned_Max_Length
;
517 (Max_Length
: Natural) return Shared_Wide_Wide_String_Access
is
519 -- Empty string requested, return shared empty string
521 if Max_Length
= 0 then
522 Reference
(Empty_Shared_Wide_Wide_String
'Access);
523 return Empty_Shared_Wide_Wide_String
'Access;
525 -- Otherwise, allocate requested space (and probably some more room)
528 return new Shared_Wide_Wide_String
(Aligned_Max_Length
(Max_Length
));
537 (Source
: in out Unbounded_Wide_Wide_String
;
538 New_Item
: Unbounded_Wide_Wide_String
)
540 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
541 NR
: constant Shared_Wide_Wide_String_Access
:= New_Item
.Reference
;
542 DL
: constant Natural := SR
.Last
+ NR
.Last
;
543 DR
: Shared_Wide_Wide_String_Access
;
546 -- Source is an empty string, reuse New_Item data
550 Source
.Reference
:= NR
;
553 -- New_Item is empty string, nothing to do
555 elsif NR
.Last
= 0 then
558 -- Try to reuse existent shared string
560 elsif Can_Be_Reused
(SR
, DL
) then
561 SR
.Data
(SR
.Last
+ 1 .. DL
) := NR
.Data
(1 .. NR
.Last
);
564 -- Otherwise, allocate new one and fill it
567 DR
:= Allocate
(DL
+ DL
/ Growth_Factor
);
568 DR
.Data
(1 .. SR
.Last
) := SR
.Data
(1 .. SR
.Last
);
569 DR
.Data
(SR
.Last
+ 1 .. DL
) := NR
.Data
(1 .. NR
.Last
);
571 Source
.Reference
:= DR
;
577 (Source
: in out Unbounded_Wide_Wide_String
;
578 New_Item
: Wide_Wide_String
)
580 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
581 DL
: constant Natural := SR
.Last
+ New_Item
'Length;
582 DR
: Shared_Wide_Wide_String_Access
;
585 -- New_Item is an empty string, nothing to do
587 if New_Item
'Length = 0 then
590 -- Try to reuse existing shared string
592 elsif Can_Be_Reused
(SR
, DL
) then
593 SR
.Data
(SR
.Last
+ 1 .. DL
) := New_Item
;
596 -- Otherwise, allocate new one and fill it
599 DR
:= Allocate
(DL
+ DL
/ Growth_Factor
);
600 DR
.Data
(1 .. SR
.Last
) := SR
.Data
(1 .. SR
.Last
);
601 DR
.Data
(SR
.Last
+ 1 .. DL
) := New_Item
;
603 Source
.Reference
:= DR
;
609 (Source
: in out Unbounded_Wide_Wide_String
;
610 New_Item
: Wide_Wide_Character
)
612 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
613 DL
: constant Natural := SR
.Last
+ 1;
614 DR
: Shared_Wide_Wide_String_Access
;
617 -- Try to reuse existing shared string
619 if Can_Be_Reused
(SR
, SR
.Last
+ 1) then
620 SR
.Data
(SR
.Last
+ 1) := New_Item
;
621 SR
.Last
:= SR
.Last
+ 1;
623 -- Otherwise, allocate new one and fill it
626 DR
:= Allocate
(DL
+ DL
/ Growth_Factor
);
627 DR
.Data
(1 .. SR
.Last
) := SR
.Data
(1 .. SR
.Last
);
628 DR
.Data
(DL
) := New_Item
;
630 Source
.Reference
:= DR
;
639 function Can_Be_Reused
640 (Item
: Shared_Wide_Wide_String_Access
;
641 Length
: Natural) return Boolean
647 and then Item
.Max_Length
>= Length
648 and then Item
.Max_Length
<=
649 Aligned_Max_Length
(Length
+ Length
/ Growth_Factor
);
657 (Source
: Unbounded_Wide_Wide_String
;
658 Pattern
: Wide_Wide_String
;
659 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
:=
660 Wide_Wide_Maps
.Identity
)
663 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
665 return Wide_Wide_Search
.Count
(SR
.Data
(1 .. SR
.Last
), Pattern
, Mapping
);
669 (Source
: Unbounded_Wide_Wide_String
;
670 Pattern
: Wide_Wide_String
;
671 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
674 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
676 return Wide_Wide_Search
.Count
(SR
.Data
(1 .. SR
.Last
), Pattern
, Mapping
);
680 (Source
: Unbounded_Wide_Wide_String
;
681 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
) return Natural
683 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
685 return Wide_Wide_Search
.Count
(SR
.Data
(1 .. SR
.Last
), Set
);
693 (Source
: Unbounded_Wide_Wide_String
;
695 Through
: Natural) return Unbounded_Wide_Wide_String
697 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
699 DR
: Shared_Wide_Wide_String_Access
;
702 -- Empty slice is deleted, use the same shared string
704 if From
> Through
then
708 -- Index is out of range
710 elsif Through
> SR
.Last
then
713 -- Compute size of the result
716 DL
:= SR
.Last
- (Through
- From
+ 1);
718 -- Result is an empty string, reuse shared empty string
721 Reference
(Empty_Shared_Wide_Wide_String
'Access);
722 DR
:= Empty_Shared_Wide_Wide_String
'Access;
724 -- Otherwise, allocate new shared string and fill it
728 DR
.Data
(1 .. From
- 1) := SR
.Data
(1 .. From
- 1);
729 DR
.Data
(From
.. DL
) := SR
.Data
(Through
+ 1 .. SR
.Last
);
734 return (AF
.Controlled
with Reference
=> DR
);
738 (Source
: in out Unbounded_Wide_Wide_String
;
742 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
744 DR
: Shared_Wide_Wide_String_Access
;
747 -- Nothing changed, return
749 if From
> Through
then
752 -- Through is outside of the range
754 elsif Through
> SR
.Last
then
758 DL
:= SR
.Last
- (Through
- From
+ 1);
760 -- Result is empty, reuse shared empty string
763 Reference
(Empty_Shared_Wide_Wide_String
'Access);
764 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
767 -- Try to reuse existent shared string
769 elsif Can_Be_Reused
(SR
, DL
) then
770 SR
.Data
(From
.. DL
) := SR
.Data
(Through
+ 1 .. SR
.Last
);
773 -- Otherwise, allocate new shared string
777 DR
.Data
(1 .. From
- 1) := SR
.Data
(1 .. From
- 1);
778 DR
.Data
(From
.. DL
) := SR
.Data
(Through
+ 1 .. SR
.Last
);
780 Source
.Reference
:= DR
;
791 (Source
: Unbounded_Wide_Wide_String
;
792 Index
: Positive) return Wide_Wide_Character
794 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
796 if Index
<= SR
.Last
then
797 return SR
.Data
(Index
);
807 procedure Finalize
(Object
: in out Unbounded_Wide_Wide_String
) is
808 SR
: constant Shared_Wide_Wide_String_Access
:= Object
.Reference
;
813 -- The same controlled object can be finalized several times for
814 -- some reason. As per 7.6.1(24) this should have no ill effect,
815 -- so we need to add a guard for the case of finalizing the same
818 Object
.Reference
:= null;
828 (Source
: Unbounded_Wide_Wide_String
;
829 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
830 Test
: Strings
.Membership
;
831 First
: out Positive;
834 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
836 Wide_Wide_Search
.Find_Token
837 (SR
.Data
(1 .. SR
.Last
), Set
, Test
, First
, Last
);
844 procedure Free
(X
: in out Wide_Wide_String_Access
) is
845 procedure Deallocate
is
846 new Ada
.Unchecked_Deallocation
847 (Wide_Wide_String
, Wide_Wide_String_Access
);
857 (Source
: Unbounded_Wide_Wide_String
;
859 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
860 return Unbounded_Wide_Wide_String
862 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
863 DR
: Shared_Wide_Wide_String_Access
;
866 -- Result is empty, reuse shared empty string
869 Reference
(Empty_Shared_Wide_Wide_String
'Access);
870 DR
:= Empty_Shared_Wide_Wide_String
'Access;
872 -- Length of the string is the same as requested, reuse source shared
875 elsif Count
= SR
.Last
then
879 -- Otherwise, allocate new shared string and fill it
882 DR
:= Allocate
(Count
);
884 -- Length of the source string is more than requested, copy
885 -- corresponding slice.
887 if Count
< SR
.Last
then
888 DR
.Data
(1 .. Count
) := SR
.Data
(1 .. Count
);
890 -- Length of the source string is less then requested, copy all
891 -- contents and fill others by Pad character.
894 DR
.Data
(1 .. SR
.Last
) := SR
.Data
(1 .. SR
.Last
);
896 for J
in SR
.Last
+ 1 .. Count
loop
904 return (AF
.Controlled
with Reference
=> DR
);
908 (Source
: in out Unbounded_Wide_Wide_String
;
910 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
912 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
913 DR
: Shared_Wide_Wide_String_Access
;
916 -- Result is empty, reuse empty shared string
919 Reference
(Empty_Shared_Wide_Wide_String
'Access);
920 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
923 -- Result is same with source string, reuse source shared string
925 elsif Count
= SR
.Last
then
928 -- Try to reuse existent shared string
930 elsif Can_Be_Reused
(SR
, Count
) then
931 if Count
> SR
.Last
then
932 for J
in SR
.Last
+ 1 .. Count
loop
939 -- Otherwise, allocate new shared string and fill it
942 DR
:= Allocate
(Count
);
944 -- Length of the source string is greater then requested, copy
945 -- corresponding slice.
947 if Count
< SR
.Last
then
948 DR
.Data
(1 .. Count
) := SR
.Data
(1 .. Count
);
950 -- Length of the source string is less the requested, copy all
951 -- exists data and fill others by Pad character.
954 DR
.Data
(1 .. SR
.Last
) := SR
.Data
(1 .. SR
.Last
);
956 for J
in SR
.Last
+ 1 .. Count
loop
962 Source
.Reference
:= DR
;
972 (Source
: Unbounded_Wide_Wide_String
;
973 Pattern
: Wide_Wide_String
;
974 Going
: Strings
.Direction
:= Strings
.Forward
;
975 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
:=
976 Wide_Wide_Maps
.Identity
)
979 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
981 return Wide_Wide_Search
.Index
982 (SR
.Data
(1 .. SR
.Last
), Pattern
, Going
, Mapping
);
986 (Source
: Unbounded_Wide_Wide_String
;
987 Pattern
: Wide_Wide_String
;
988 Going
: Direction
:= Forward
;
989 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
992 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
994 return Wide_Wide_Search
.Index
995 (SR
.Data
(1 .. SR
.Last
), Pattern
, Going
, Mapping
);
999 (Source
: Unbounded_Wide_Wide_String
;
1000 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
1001 Test
: Strings
.Membership
:= Strings
.Inside
;
1002 Going
: Strings
.Direction
:= Strings
.Forward
) return Natural
1004 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1006 return Wide_Wide_Search
.Index
(SR
.Data
(1 .. SR
.Last
), Set
, Test
, Going
);
1010 (Source
: Unbounded_Wide_Wide_String
;
1011 Pattern
: Wide_Wide_String
;
1013 Going
: Direction
:= Forward
;
1014 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
:=
1015 Wide_Wide_Maps
.Identity
)
1018 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1020 return Wide_Wide_Search
.Index
1021 (SR
.Data
(1 .. SR
.Last
), Pattern
, From
, Going
, Mapping
);
1025 (Source
: Unbounded_Wide_Wide_String
;
1026 Pattern
: Wide_Wide_String
;
1028 Going
: Direction
:= Forward
;
1029 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
1032 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1034 return Wide_Wide_Search
.Index
1035 (SR
.Data
(1 .. SR
.Last
), Pattern
, From
, Going
, Mapping
);
1039 (Source
: Unbounded_Wide_Wide_String
;
1040 Set
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
1042 Test
: Membership
:= Inside
;
1043 Going
: Direction
:= Forward
) return Natural
1045 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1047 return Wide_Wide_Search
.Index
1048 (SR
.Data
(1 .. SR
.Last
), Set
, From
, Test
, Going
);
1051 ---------------------
1052 -- Index_Non_Blank --
1053 ---------------------
1055 function Index_Non_Blank
1056 (Source
: Unbounded_Wide_Wide_String
;
1057 Going
: Strings
.Direction
:= Strings
.Forward
) return Natural
1059 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1061 return Wide_Wide_Search
.Index_Non_Blank
(SR
.Data
(1 .. SR
.Last
), Going
);
1062 end Index_Non_Blank
;
1064 function Index_Non_Blank
1065 (Source
: Unbounded_Wide_Wide_String
;
1067 Going
: Direction
:= Forward
) return Natural
1069 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1071 return Wide_Wide_Search
.Index_Non_Blank
1072 (SR
.Data
(1 .. SR
.Last
), From
, Going
);
1073 end Index_Non_Blank
;
1079 procedure Initialize
(Object
: in out Unbounded_Wide_Wide_String
) is
1081 Reference
(Object
.Reference
);
1089 (Source
: Unbounded_Wide_Wide_String
;
1091 New_Item
: Wide_Wide_String
) return Unbounded_Wide_Wide_String
1093 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1094 DL
: constant Natural := SR
.Last
+ New_Item
'Length;
1095 DR
: Shared_Wide_Wide_String_Access
;
1098 -- Check index first
1100 if Before
> SR
.Last
+ 1 then
1104 -- Result is empty, reuse empty shared string
1107 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1108 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1110 -- Inserted string is empty, reuse source shared string
1112 elsif New_Item
'Length = 0 then
1116 -- Otherwise, allocate new shared string and fill it
1119 DR
:= Allocate
(DL
+ DL
/ Growth_Factor
);
1120 DR
.Data
(1 .. Before
- 1) := SR
.Data
(1 .. Before
- 1);
1121 DR
.Data
(Before
.. Before
+ New_Item
'Length - 1) := New_Item
;
1122 DR
.Data
(Before
+ New_Item
'Length .. DL
) :=
1123 SR
.Data
(Before
.. SR
.Last
);
1127 return (AF
.Controlled
with Reference
=> DR
);
1131 (Source
: in out Unbounded_Wide_Wide_String
;
1133 New_Item
: Wide_Wide_String
)
1135 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1136 DL
: constant Natural := SR
.Last
+ New_Item
'Length;
1137 DR
: Shared_Wide_Wide_String_Access
;
1142 if Before
> SR
.Last
+ 1 then
1146 -- Result is empty string, reuse empty shared string
1149 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1150 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1153 -- Inserted string is empty, nothing to do
1155 elsif New_Item
'Length = 0 then
1158 -- Try to reuse existent shared string first
1160 elsif Can_Be_Reused
(SR
, DL
) then
1161 SR
.Data
(Before
+ New_Item
'Length .. DL
) :=
1162 SR
.Data
(Before
.. SR
.Last
);
1163 SR
.Data
(Before
.. Before
+ New_Item
'Length - 1) := New_Item
;
1166 -- Otherwise, allocate new shared string and fill it
1169 DR
:= Allocate
(DL
+ DL
/ Growth_Factor
);
1170 DR
.Data
(1 .. Before
- 1) := SR
.Data
(1 .. Before
- 1);
1171 DR
.Data
(Before
.. Before
+ New_Item
'Length - 1) := New_Item
;
1172 DR
.Data
(Before
+ New_Item
'Length .. DL
) :=
1173 SR
.Data
(Before
.. SR
.Last
);
1175 Source
.Reference
:= DR
;
1184 function Length
(Source
: Unbounded_Wide_Wide_String
) return Natural is
1186 return Source
.Reference
.Last
;
1194 (Source
: Unbounded_Wide_Wide_String
;
1195 Position
: Positive;
1196 New_Item
: Wide_Wide_String
) return Unbounded_Wide_Wide_String
1198 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1200 DR
: Shared_Wide_Wide_String_Access
;
1205 if Position
> SR
.Last
+ 1 then
1209 DL
:= Integer'Max (SR
.Last
, Position
+ New_Item
'Length - 1);
1211 -- Result is empty string, reuse empty shared string
1214 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1215 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1217 -- Result is same with source string, reuse source shared string
1219 elsif New_Item
'Length = 0 then
1223 -- Otherwise, allocate new shared string and fill it
1226 DR
:= Allocate
(DL
);
1227 DR
.Data
(1 .. Position
- 1) := SR
.Data
(1 .. Position
- 1);
1228 DR
.Data
(Position
.. Position
+ New_Item
'Length - 1) := New_Item
;
1229 DR
.Data
(Position
+ New_Item
'Length .. DL
) :=
1230 SR
.Data
(Position
+ New_Item
'Length .. SR
.Last
);
1234 return (AF
.Controlled
with Reference
=> DR
);
1238 (Source
: in out Unbounded_Wide_Wide_String
;
1239 Position
: Positive;
1240 New_Item
: Wide_Wide_String
)
1242 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1244 DR
: Shared_Wide_Wide_String_Access
;
1249 if Position
> SR
.Last
+ 1 then
1253 DL
:= Integer'Max (SR
.Last
, Position
+ New_Item
'Length - 1);
1255 -- Result is empty string, reuse empty shared string
1258 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1259 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1262 -- String unchanged, nothing to do
1264 elsif New_Item
'Length = 0 then
1267 -- Try to reuse existent shared string
1269 elsif Can_Be_Reused
(SR
, DL
) then
1270 SR
.Data
(Position
.. Position
+ New_Item
'Length - 1) := New_Item
;
1273 -- Otherwise allocate new shared string and fill it
1276 DR
:= Allocate
(DL
);
1277 DR
.Data
(1 .. Position
- 1) := SR
.Data
(1 .. Position
- 1);
1278 DR
.Data
(Position
.. Position
+ New_Item
'Length - 1) := New_Item
;
1279 DR
.Data
(Position
+ New_Item
'Length .. DL
) :=
1280 SR
.Data
(Position
+ New_Item
'Length .. SR
.Last
);
1282 Source
.Reference
:= DR
;
1291 procedure Reference
(Item
: not null Shared_Wide_Wide_String_Access
) is
1293 Sync_Add_And_Fetch
(Item
.Counter
'Access, 1);
1296 ---------------------
1297 -- Replace_Element --
1298 ---------------------
1300 procedure Replace_Element
1301 (Source
: in out Unbounded_Wide_Wide_String
;
1303 By
: Wide_Wide_Character
)
1305 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1306 DR
: Shared_Wide_Wide_String_Access
;
1311 if Index
<= SR
.Last
then
1313 -- Try to reuse existent shared string
1315 if Can_Be_Reused
(SR
, SR
.Last
) then
1316 SR
.Data
(Index
) := By
;
1318 -- Otherwise allocate new shared string and fill it
1321 DR
:= Allocate
(SR
.Last
);
1322 DR
.Data
(1 .. SR
.Last
) := SR
.Data
(1 .. SR
.Last
);
1323 DR
.Data
(Index
) := By
;
1325 Source
.Reference
:= DR
;
1332 end Replace_Element
;
1338 function Replace_Slice
1339 (Source
: Unbounded_Wide_Wide_String
;
1342 By
: Wide_Wide_String
) return Unbounded_Wide_Wide_String
1344 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1346 DR
: Shared_Wide_Wide_String_Access
;
1351 if Low
> SR
.Last
+ 1 then
1355 -- Do replace operation when removed slice is not empty
1358 DL
:= By
'Length + SR
.Last
+ Low
- High
- 1;
1360 -- Result is empty string, reuse empty shared string
1363 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1364 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1366 -- Otherwise allocate new shared string and fill it
1369 DR
:= Allocate
(DL
);
1370 DR
.Data
(1 .. Low
- 1) := SR
.Data
(1 .. Low
- 1);
1371 DR
.Data
(Low
.. Low
+ By
'Length - 1) := By
;
1372 DR
.Data
(Low
+ By
'Length .. DL
) := SR
.Data
(High
+ 1 .. SR
.Last
);
1376 return (AF
.Controlled
with Reference
=> DR
);
1378 -- Otherwise just insert string
1381 return Insert
(Source
, Low
, By
);
1385 procedure Replace_Slice
1386 (Source
: in out Unbounded_Wide_Wide_String
;
1389 By
: Wide_Wide_String
)
1391 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1393 DR
: Shared_Wide_Wide_String_Access
;
1398 if Low
> SR
.Last
+ 1 then
1402 -- Do replace operation only when replaced slice is not empty
1405 DL
:= By
'Length + SR
.Last
+ Low
- High
- 1;
1407 -- Result is empty string, reuse empty shared string
1410 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1411 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1414 -- Try to reuse existent shared string
1416 elsif Can_Be_Reused
(SR
, DL
) then
1417 SR
.Data
(Low
+ By
'Length .. DL
) := SR
.Data
(High
+ 1 .. SR
.Last
);
1418 SR
.Data
(Low
.. Low
+ By
'Length - 1) := By
;
1421 -- Otherwise allocate new shared string and fill it
1424 DR
:= Allocate
(DL
);
1425 DR
.Data
(1 .. Low
- 1) := SR
.Data
(1 .. Low
- 1);
1426 DR
.Data
(Low
.. Low
+ By
'Length - 1) := By
;
1427 DR
.Data
(Low
+ By
'Length .. DL
) := SR
.Data
(High
+ 1 .. SR
.Last
);
1429 Source
.Reference
:= DR
;
1433 -- Otherwise just insert item
1436 Insert
(Source
, Low
, By
);
1440 -------------------------------
1441 -- Set_Unbounded_Wide_Wide_String --
1442 -------------------------------
1444 procedure Set_Unbounded_Wide_Wide_String
1445 (Target
: out Unbounded_Wide_Wide_String
;
1446 Source
: Wide_Wide_String
)
1448 TR
: constant Shared_Wide_Wide_String_Access
:= Target
.Reference
;
1449 DR
: Shared_Wide_Wide_String_Access
;
1452 -- In case of empty string, reuse empty shared string
1454 if Source
'Length = 0 then
1455 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1456 Target
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1459 -- Try to reuse existent shared string
1461 if Can_Be_Reused
(TR
, Source
'Length) then
1465 -- Otherwise allocate new shared string
1468 DR
:= Allocate
(Source
'Length);
1469 Target
.Reference
:= DR
;
1472 DR
.Data
(1 .. Source
'Length) := Source
;
1473 DR
.Last
:= Source
'Length;
1477 end Set_Unbounded_Wide_Wide_String
;
1484 (Source
: Unbounded_Wide_Wide_String
;
1486 High
: Natural) return Wide_Wide_String
1488 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1491 -- Note: test of High > Length is in accordance with AI95-00128
1493 if Low
> SR
.Last
+ 1 or else High
> SR
.Last
then
1497 return SR
.Data
(Low
.. High
);
1506 (Source
: Unbounded_Wide_Wide_String
;
1508 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
1509 return Unbounded_Wide_Wide_String
1511 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1512 DR
: Shared_Wide_Wide_String_Access
;
1515 -- For empty result reuse empty shared string
1518 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1519 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1521 -- Result is hole source string, reuse source shared string
1523 elsif Count
= SR
.Last
then
1527 -- Otherwise allocate new shared string and fill it
1530 DR
:= Allocate
(Count
);
1532 if Count
< SR
.Last
then
1533 DR
.Data
(1 .. Count
) := SR
.Data
(SR
.Last
- Count
+ 1 .. SR
.Last
);
1536 for J
in 1 .. Count
- SR
.Last
loop
1540 DR
.Data
(Count
- SR
.Last
+ 1 .. Count
) := SR
.Data
(1 .. SR
.Last
);
1546 return (AF
.Controlled
with Reference
=> DR
);
1550 (Source
: in out Unbounded_Wide_Wide_String
;
1552 Pad
: Wide_Wide_Character
:= Wide_Wide_Space
)
1554 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1555 DR
: Shared_Wide_Wide_String_Access
;
1558 (SR
: Shared_Wide_Wide_String_Access
;
1559 DR
: Shared_Wide_Wide_String_Access
;
1561 -- Common code of tail computation. SR/DR can point to the same object
1568 (SR
: Shared_Wide_Wide_String_Access
;
1569 DR
: Shared_Wide_Wide_String_Access
;
1572 if Count
< SR
.Last
then
1573 DR
.Data
(1 .. Count
) := SR
.Data
(SR
.Last
- Count
+ 1 .. SR
.Last
);
1576 DR
.Data
(Count
- SR
.Last
+ 1 .. Count
) := SR
.Data
(1 .. SR
.Last
);
1578 for J
in 1 .. Count
- SR
.Last
loop
1587 -- Result is empty string, reuse empty shared string
1590 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1591 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1594 -- Length of the result is the same with length of the source string,
1595 -- reuse source shared string.
1597 elsif Count
= SR
.Last
then
1600 -- Try to reuse existent shared string
1602 elsif Can_Be_Reused
(SR
, Count
) then
1603 Common
(SR
, SR
, Count
);
1605 -- Otherwise allocate new shared string and fill it
1608 DR
:= Allocate
(Count
);
1609 Common
(SR
, DR
, Count
);
1610 Source
.Reference
:= DR
;
1615 --------------------
1616 -- To_Wide_Wide_String --
1617 --------------------
1619 function To_Wide_Wide_String
1620 (Source
: Unbounded_Wide_Wide_String
) return Wide_Wide_String
is
1622 return Source
.Reference
.Data
(1 .. Source
.Reference
.Last
);
1623 end To_Wide_Wide_String
;
1625 ------------------------------
1626 -- To_Unbounded_Wide_Wide_String --
1627 ------------------------------
1629 function To_Unbounded_Wide_Wide_String
1630 (Source
: Wide_Wide_String
) return Unbounded_Wide_Wide_String
1632 DR
: constant Shared_Wide_Wide_String_Access
:= Allocate
(Source
'Length);
1634 DR
.Data
(1 .. Source
'Length) := Source
;
1635 DR
.Last
:= Source
'Length;
1636 return (AF
.Controlled
with Reference
=> DR
);
1637 end To_Unbounded_Wide_Wide_String
;
1639 function To_Unbounded_Wide_Wide_String
1640 (Length
: Natural) return Unbounded_Wide_Wide_String
1642 DR
: constant Shared_Wide_Wide_String_Access
:= Allocate
(Length
);
1645 return (AF
.Controlled
with Reference
=> DR
);
1646 end To_Unbounded_Wide_Wide_String
;
1653 (Source
: Unbounded_Wide_Wide_String
;
1654 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
)
1655 return Unbounded_Wide_Wide_String
1657 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1658 DR
: Shared_Wide_Wide_String_Access
;
1661 -- Nothing to translate, reuse empty shared string
1664 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1665 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1667 -- Otherwise, allocate new shared string and fill it
1670 DR
:= Allocate
(SR
.Last
);
1672 for J
in 1 .. SR
.Last
loop
1673 DR
.Data
(J
) := Value
(Mapping
, SR
.Data
(J
));
1679 return (AF
.Controlled
with Reference
=> DR
);
1683 (Source
: in out Unbounded_Wide_Wide_String
;
1684 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping
)
1686 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1687 DR
: Shared_Wide_Wide_String_Access
;
1690 -- Nothing to translate
1695 -- Try to reuse shared string
1697 elsif Can_Be_Reused
(SR
, SR
.Last
) then
1698 for J
in 1 .. SR
.Last
loop
1699 SR
.Data
(J
) := Value
(Mapping
, SR
.Data
(J
));
1702 -- Otherwise, allocate new shared string
1705 DR
:= Allocate
(SR
.Last
);
1707 for J
in 1 .. SR
.Last
loop
1708 DR
.Data
(J
) := Value
(Mapping
, SR
.Data
(J
));
1712 Source
.Reference
:= DR
;
1718 (Source
: Unbounded_Wide_Wide_String
;
1719 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
1720 return Unbounded_Wide_Wide_String
1722 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1723 DR
: Shared_Wide_Wide_String_Access
;
1726 -- Nothing to translate, reuse empty shared string
1729 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1730 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1732 -- Otherwise, allocate new shared string and fill it
1735 DR
:= Allocate
(SR
.Last
);
1737 for J
in 1 .. SR
.Last
loop
1738 DR
.Data
(J
) := Mapping
.all (SR
.Data
(J
));
1744 return (AF
.Controlled
with Reference
=> DR
);
1754 (Source
: in out Unbounded_Wide_Wide_String
;
1755 Mapping
: Wide_Wide_Maps
.Wide_Wide_Character_Mapping_Function
)
1757 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1758 DR
: Shared_Wide_Wide_String_Access
;
1761 -- Nothing to translate
1766 -- Try to reuse shared string
1768 elsif Can_Be_Reused
(SR
, SR
.Last
) then
1769 for J
in 1 .. SR
.Last
loop
1770 SR
.Data
(J
) := Mapping
.all (SR
.Data
(J
));
1773 -- Otherwise allocate new shared string and fill it
1776 DR
:= Allocate
(SR
.Last
);
1778 for J
in 1 .. SR
.Last
loop
1779 DR
.Data
(J
) := Mapping
.all (SR
.Data
(J
));
1783 Source
.Reference
:= DR
;
1801 (Source
: Unbounded_Wide_Wide_String
;
1802 Side
: Trim_End
) return Unbounded_Wide_Wide_String
1804 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1806 DR
: Shared_Wide_Wide_String_Access
;
1811 Low
:= Index_Non_Blank
(Source
, Forward
);
1813 -- All blanks, reuse empty shared string
1816 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1817 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1823 DL
:= SR
.Last
- Low
+ 1;
1827 High
:= Index_Non_Blank
(Source
, Backward
);
1831 High
:= Index_Non_Blank
(Source
, Backward
);
1832 DL
:= High
- Low
+ 1;
1835 -- Length of the result is the same as length of the source string,
1836 -- reuse source shared string.
1838 if DL
= SR
.Last
then
1842 -- Otherwise, allocate new shared string
1845 DR
:= Allocate
(DL
);
1846 DR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
1851 return (AF
.Controlled
with Reference
=> DR
);
1855 (Source
: in out Unbounded_Wide_Wide_String
;
1858 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1860 DR
: Shared_Wide_Wide_String_Access
;
1865 Low
:= Index_Non_Blank
(Source
, Forward
);
1867 -- All blanks, reuse empty shared string
1870 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1871 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1878 DL
:= SR
.Last
- Low
+ 1;
1882 High
:= Index_Non_Blank
(Source
, Backward
);
1886 High
:= Index_Non_Blank
(Source
, Backward
);
1887 DL
:= High
- Low
+ 1;
1890 -- Length of the result is the same as length of the source string,
1893 if DL
= SR
.Last
then
1896 -- Try to reuse existent shared string
1898 elsif Can_Be_Reused
(SR
, DL
) then
1899 SR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
1902 -- Otherwise, allocate new shared string
1905 DR
:= Allocate
(DL
);
1906 DR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
1908 Source
.Reference
:= DR
;
1915 (Source
: Unbounded_Wide_Wide_String
;
1916 Left
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
1917 Right
: Wide_Wide_Maps
.Wide_Wide_Character_Set
)
1918 return Unbounded_Wide_Wide_String
1920 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1922 DR
: Shared_Wide_Wide_String_Access
;
1927 Low
:= Index
(Source
, Left
, Outside
, Forward
);
1929 -- Source includes only characters from Left set, reuse empty shared
1933 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1934 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1937 High
:= Index
(Source
, Right
, Outside
, Backward
);
1938 DL
:= Integer'Max (0, High
- Low
+ 1);
1940 -- Source includes only characters from Right set or result string
1941 -- is empty, reuse empty shared string.
1943 if High
= 0 or else DL
= 0 then
1944 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1945 DR
:= Empty_Shared_Wide_Wide_String
'Access;
1947 -- Otherwise, allocate new shared string and fill it
1950 DR
:= Allocate
(DL
);
1951 DR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
1956 return (AF
.Controlled
with Reference
=> DR
);
1960 (Source
: in out Unbounded_Wide_Wide_String
;
1961 Left
: Wide_Wide_Maps
.Wide_Wide_Character_Set
;
1962 Right
: Wide_Wide_Maps
.Wide_Wide_Character_Set
)
1964 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
1966 DR
: Shared_Wide_Wide_String_Access
;
1971 Low
:= Index
(Source
, Left
, Outside
, Forward
);
1973 -- Source includes only characters from Left set, reuse empty shared
1977 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1978 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1982 High
:= Index
(Source
, Right
, Outside
, Backward
);
1983 DL
:= Integer'Max (0, High
- Low
+ 1);
1985 -- Source includes only characters from Right set or result string
1986 -- is empty, reuse empty shared string.
1988 if High
= 0 or else DL
= 0 then
1989 Reference
(Empty_Shared_Wide_Wide_String
'Access);
1990 Source
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
1993 -- Try to reuse existent shared string
1995 elsif Can_Be_Reused
(SR
, DL
) then
1996 SR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
1999 -- Otherwise, allocate new shared string and fill it
2002 DR
:= Allocate
(DL
);
2003 DR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
2005 Source
.Reference
:= DR
;
2011 ---------------------
2012 -- Unbounded_Slice --
2013 ---------------------
2015 function Unbounded_Slice
2016 (Source
: Unbounded_Wide_Wide_String
;
2018 High
: Natural) return Unbounded_Wide_Wide_String
2020 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
2022 DR
: Shared_Wide_Wide_String_Access
;
2027 if Low
> SR
.Last
+ 1 or else High
> SR
.Last
then
2030 -- Result is empty slice, reuse empty shared string
2032 elsif Low
> High
then
2033 Reference
(Empty_Shared_Wide_Wide_String
'Access);
2034 DR
:= Empty_Shared_Wide_Wide_String
'Access;
2036 -- Otherwise, allocate new shared string and fill it
2039 DL
:= High
- Low
+ 1;
2040 DR
:= Allocate
(DL
);
2041 DR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
2045 return (AF
.Controlled
with Reference
=> DR
);
2046 end Unbounded_Slice
;
2048 procedure Unbounded_Slice
2049 (Source
: Unbounded_Wide_Wide_String
;
2050 Target
: out Unbounded_Wide_Wide_String
;
2054 SR
: constant Shared_Wide_Wide_String_Access
:= Source
.Reference
;
2055 TR
: constant Shared_Wide_Wide_String_Access
:= Target
.Reference
;
2057 DR
: Shared_Wide_Wide_String_Access
;
2062 if Low
> SR
.Last
+ 1 or else High
> SR
.Last
then
2065 -- Result is empty slice, reuse empty shared string
2067 elsif Low
> High
then
2068 Reference
(Empty_Shared_Wide_Wide_String
'Access);
2069 Target
.Reference
:= Empty_Shared_Wide_Wide_String
'Access;
2073 DL
:= High
- Low
+ 1;
2075 -- Try to reuse existent shared string
2077 if Can_Be_Reused
(TR
, DL
) then
2078 TR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
2081 -- Otherwise, allocate new shared string and fill it
2084 DR
:= Allocate
(DL
);
2085 DR
.Data
(1 .. DL
) := SR
.Data
(Low
.. High
);
2087 Target
.Reference
:= DR
;
2091 end Unbounded_Slice
;
2097 procedure Unreference
(Item
: not null Shared_Wide_Wide_String_Access
) is
2101 new Ada
.Unchecked_Deallocation
2102 (Shared_Wide_Wide_String
, Shared_Wide_Wide_String_Access
);
2104 Aux
: Shared_Wide_Wide_String_Access
:= Item
;
2107 if Sync_Sub_And_Fetch
(Aux
.Counter
'Access, 1) = 0 then
2109 -- Reference counter of Empty_Shared_Wide_Wide_String must never
2112 pragma Assert
(Aux
/= Empty_Shared_Wide_Wide_String
'Access);
2118 end Ada
.Strings
.Wide_Wide_Unbounded
;