1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . S T R I N G S . U N B O U N D E D --
9 -- Copyright (C) 1992-2009, 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.Fixed;
33 with Ada.Strings.Search;
34 with Ada.Unchecked_Deallocation;
36 package body Ada.Strings.Unbounded is
45 (Left : Unbounded_String;
46 Right : Unbounded_String) return Unbounded_String
48 L_Length : constant Natural := Left.Last;
49 R_Length : constant Natural := Right.Last;
50 Result : Unbounded_String;
53 Result.Last := L_Length + R_Length;
55 Result.Reference := new String (1 .. Result.Last);
57 Result.Reference (1 .. L_Length) :=
58 Left.Reference (1 .. Left.Last);
59 Result.Reference (L_Length + 1 .. Result.Last) :=
60 Right.Reference (1 .. Right.Last);
66 (Left : Unbounded_String;
67 Right : String) return Unbounded_String
69 L_Length : constant Natural := Left.Last;
70 Result : Unbounded_String;
73 Result.Last := L_Length + Right'Length;
75 Result.Reference := new String (1 .. Result.Last);
77 Result.Reference (1 .. L_Length) := Left.Reference (1 .. Left.Last);
78 Result.Reference (L_Length + 1 .. Result.Last) := Right;
85 Right : Unbounded_String) return Unbounded_String
87 R_Length : constant Natural := Right.Last;
88 Result : Unbounded_String;
91 Result.Last := Left'Length + R_Length;
93 Result.Reference := new String (1 .. Result.Last);
95 Result.Reference (1 .. Left'Length) := Left;
96 Result.Reference (Left'Length + 1 .. Result.Last) :=
97 Right.Reference (1 .. Right.Last);
103 (Left : Unbounded_String;
104 Right : Character) return Unbounded_String
106 Result : Unbounded_String;
109 Result.Last := Left.Last + 1;
111 Result.Reference := new String (1 .. Result.Last);
113 Result.Reference (1 .. Result.Last - 1) :=
114 Left.Reference (1 .. Left.Last);
115 Result.Reference (Result.Last) := Right;
122 Right : Unbounded_String) return Unbounded_String
124 Result : Unbounded_String;
127 Result.Last := Right.Last + 1;
129 Result.Reference := new String (1 .. Result.Last);
130 Result.Reference (1) := Left;
131 Result.Reference (2 .. Result.Last) :=
132 Right.Reference (1 .. Right.Last);
142 Right : Character) return Unbounded_String
144 Result : Unbounded_String;
149 Result.Reference := new String (1 .. Left);
150 for J in Result.Reference'Range loop
151 Result.Reference (J) := Right;
159 Right : String) return Unbounded_String
161 Len : constant Natural := Right'Length;
163 Result : Unbounded_String;
166 Result.Last := Left * Len;
168 Result.Reference := new String (1 .. Result.Last);
171 for J in 1 .. Left loop
172 Result.Reference (K .. K + Len - 1) := Right;
181 Right : Unbounded_String) return Unbounded_String
183 Len : constant Natural := Right.Last;
185 Result : Unbounded_String;
188 Result.Last := Left * Len;
190 Result.Reference := new String (1 .. Result.Last);
193 for J in 1 .. Left loop
194 Result.Reference (K .. K + Len - 1) :=
195 Right.Reference (1 .. Right.Last);
207 (Left : Unbounded_String;
208 Right : Unbounded_String) return Boolean
212 Left.Reference (1 .. Left.Last) < Right.Reference (1 .. Right.Last);
216 (Left : Unbounded_String;
217 Right : String) return Boolean
220 return Left.Reference (1 .. Left.Last) < Right;
225 Right : Unbounded_String) return Boolean
228 return Left < Right.Reference (1 .. Right.Last);
236 (Left : Unbounded_String;
237 Right : Unbounded_String) return Boolean
241 Left.Reference (1 .. Left.Last) <= Right.Reference (1 .. Right.Last);
245 (Left : Unbounded_String;
246 Right : String) return Boolean
249 return Left.Reference (1 .. Left.Last) <= Right;
254 Right : Unbounded_String) return Boolean
257 return Left <= Right.Reference (1 .. Right.Last);
265 (Left : Unbounded_String;
266 Right : Unbounded_String) return Boolean
270 Left.Reference (1 .. Left.Last) = Right.Reference (1 .. Right.Last);
274 (Left : Unbounded_String;
275 Right : String) return Boolean
278 return Left.Reference (1 .. Left.Last) = Right;
283 Right : Unbounded_String) return Boolean
286 return Left = Right.Reference (1 .. Right.Last);
294 (Left : Unbounded_String;
295 Right : Unbounded_String) return Boolean
299 Left.Reference (1 .. Left.Last) > Right.Reference (1 .. Right.Last);
303 (Left : Unbounded_String;
304 Right : String) return Boolean
307 return Left.Reference (1 .. Left.Last) > Right;
312 Right : Unbounded_String) return Boolean
315 return Left > Right.Reference (1 .. Right.Last);
323 (Left : Unbounded_String;
324 Right : Unbounded_String) return Boolean
328 Left.Reference (1 .. Left.Last) >= Right.Reference (1 .. Right.Last);
332 (Left : Unbounded_String;
333 Right : String) return Boolean
336 return Left.Reference (1 .. Left.Last) >= Right;
341 Right : Unbounded_String) return Boolean
344 return Left >= Right.Reference (1 .. Right.Last);
351 procedure Adjust (Object : in out Unbounded_String) is
353 -- Copy string, except we do not copy the statically allocated null
354 -- string since it can never be deallocated. Note that we do not copy
355 -- extra string room here to avoid dragging unused allocated memory.
357 if Object.Reference /= Null_String'Access then
358 Object.Reference := new String'(Object.Reference (1 .. Object.Last));
367 (Source : in out Unbounded_String;
368 New_Item : Unbounded_String)
371 Realloc_For_Chunk (Source, New_Item.Last);
372 Source.Reference (Source.Last + 1 .. Source.Last + New_Item.Last) :=
373 New_Item.Reference (1 .. New_Item.Last);
374 Source.Last := Source.Last + New_Item.Last;
378 (Source : in out Unbounded_String;
382 Realloc_For_Chunk (Source, New_Item'Length);
383 Source.Reference (Source.Last + 1 .. Source.Last + New_Item'Length) :=
385 Source.Last := Source.Last + New_Item'Length;
389 (Source : in out Unbounded_String;
390 New_Item : Character)
393 Realloc_For_Chunk (Source, 1);
394 Source.Reference (Source.Last + 1) := New_Item;
395 Source.Last := Source.Last + 1;
403 (Source : Unbounded_String;
405 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
409 Search.Count (Source.Reference (1 .. Source.Last), Pattern, Mapping);
413 (Source : Unbounded_String;
415 Mapping : Maps.Character_Mapping_Function) return Natural
419 Search.Count (Source.Reference (1 .. Source.Last), Pattern, Mapping);
423 (Source : Unbounded_String;
424 Set : Maps.Character_Set) return Natural
427 return Search.Count (Source.Reference (1 .. Source.Last), Set);
435 (Source : Unbounded_String;
437 Through : Natural) return Unbounded_String
442 (Fixed.Delete (Source.Reference (1 .. Source.Last), From, Through));
446 (Source : in out Unbounded_String;
451 if From > Through then
454 elsif From < Source.Reference'First or else Through > Source.Last then
459 Len : constant Natural := Through - From + 1;
462 Source.Reference (From .. Source.Last - Len) :=
463 Source.Reference (Through + 1 .. Source.Last);
464 Source.Last := Source.Last - Len;
474 (Source : Unbounded_String;
475 Index : Positive) return Character
478 if Index <= Source.Last then
479 return Source.Reference (Index);
481 raise Strings.Index_Error;
489 procedure Finalize (Object : in out Unbounded_String) is
490 procedure Deallocate is
491 new Ada.Unchecked_Deallocation (String, String_Access);
494 -- Note: Don't try to free statically allocated null string
496 if Object.Reference /= Null_String'Access then
497 Deallocate (Object.Reference);
498 Object.Reference := Null_Unbounded_String.Reference;
508 (Source : Unbounded_String;
509 Set : Maps.Character_Set;
510 Test : Strings.Membership;
511 First : out Positive;
516 (Source.Reference (1 .. Source.Last), Set, Test, First, Last);
523 procedure Free (X : in out String_Access) is
524 procedure Deallocate is
525 new Ada.Unchecked_Deallocation (String, String_Access);
528 -- Note: Do not try to free statically allocated null string
530 if X /= Null_Unbounded_String.Reference then
540 (Source : Unbounded_String;
542 Pad : Character := Space) return Unbounded_String
545 return To_Unbounded_String
546 (Fixed.Head (Source.Reference (1 .. Source.Last), Count, Pad));
550 (Source : in out Unbounded_String;
552 Pad : Character := Space)
554 Old : String_Access := Source.Reference;
557 new String'(Fixed.Head (Source.Reference (1 .. Source.Last),
559 Source.Last := Source.Reference'Length;
568 (Source : Unbounded_String;
570 Going : Strings.Direction := Strings.Forward;
571 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
575 (Source.Reference (1 .. Source.Last), Pattern, Going, Mapping);
579 (Source : Unbounded_String;
581 Going : Direction := Forward;
582 Mapping : Maps.Character_Mapping_Function) return Natural
586 (Source.Reference (1 .. Source.Last), Pattern, Going, Mapping);
590 (Source : Unbounded_String;
591 Set : Maps.Character_Set;
592 Test : Strings.Membership := Strings.Inside;
593 Going : Strings.Direction := Strings.Forward) return Natural
597 (Source.Reference (1 .. Source.Last), Set, Test, Going);
601 (Source : Unbounded_String;
604 Going : Direction := Forward;
605 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
609 (Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping);
613 (Source : Unbounded_String;
616 Going : Direction := Forward;
617 Mapping : Maps.Character_Mapping_Function) return Natural
621 (Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping);
625 (Source : Unbounded_String;
626 Set : Maps.Character_Set;
628 Test : Membership := Inside;
629 Going : Direction := Forward) return Natural
633 (Source.Reference (1 .. Source.Last), Set, From, Test, Going);
636 function Index_Non_Blank
637 (Source : Unbounded_String;
638 Going : Strings.Direction := Strings.Forward) return Natural
642 Search.Index_Non_Blank
643 (Source.Reference (1 .. Source.Last), Going);
646 function Index_Non_Blank
647 (Source : Unbounded_String;
649 Going : Direction := Forward) return Natural
653 Search.Index_Non_Blank
654 (Source.Reference (1 .. Source.Last), From, Going);
661 procedure Initialize (Object : in out Unbounded_String) is
663 Object.Reference := Null_Unbounded_String.Reference;
672 (Source : Unbounded_String;
674 New_Item : String) return Unbounded_String
677 return To_Unbounded_String
678 (Fixed.Insert (Source.Reference (1 .. Source.Last), Before, New_Item));
682 (Source : in out Unbounded_String;
687 if Before not in Source.Reference'First .. Source.Last + 1 then
691 Realloc_For_Chunk (Source, New_Item'Length);
694 (Before + New_Item'Length .. Source.Last + New_Item'Length) :=
695 Source.Reference (Before .. Source.Last);
697 Source.Reference (Before .. Before + New_Item'Length - 1) := New_Item;
698 Source.Last := Source.Last + New_Item'Length;
705 function Length (Source : Unbounded_String) return Natural is
715 (Source : Unbounded_String;
717 New_Item : String) return Unbounded_String
720 return To_Unbounded_String
722 (Source.Reference (1 .. Source.Last), Position, New_Item));
726 (Source : in out Unbounded_String;
730 NL : constant Natural := New_Item'Length;
732 if Position <= Source.Last - NL + 1 then
733 Source.Reference (Position .. Position + NL - 1) := New_Item;
736 Old : String_Access := Source.Reference;
738 Source.Reference := new String'
740 (Source.Reference (1 .. Source.Last), Position, New_Item));
741 Source.Last := Source.Reference'Length;
747 -----------------------
748 -- Realloc_For_Chunk --
749 -----------------------
751 procedure Realloc_For_Chunk
752 (Source : in out Unbounded_String;
753 Chunk_Size : Natural)
755 Growth_Factor : constant := 32;
756 -- The growth factor controls how much extra space is allocated when
757 -- we have to increase the size of an allocated unbounded string. By
758 -- allocating extra space, we avoid the need to reallocate on every
759 -- append, particularly important when a string is built up by repeated
760 -- append operations of small pieces. This is expressed as a factor so
761 -- 32 means add 1/32 of the length of the string as growth space.
763 Min_Mul_Alloc : constant := Standard'Maximum_Alignment;
764 -- Allocation will be done by a multiple of Min_Mul_Alloc This causes
765 -- no memory loss as most (all?) malloc implementations are obliged to
766 -- align the returned memory on the maximum alignment as malloc does not
767 -- know the target alignment.
769 S_Length : constant Natural := Source.Reference'Length;
772 if Chunk_Size > S_Length - Source.Last then
774 New_Size : constant Positive :=
775 S_Length + Chunk_Size + (S_Length / Growth_Factor);
777 New_Rounded_Up_Size : constant Positive :=
778 ((New_Size - 1) / Min_Mul_Alloc + 1) *
781 Tmp : constant String_Access :=
782 new String (1 .. New_Rounded_Up_Size);
785 Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last);
786 Free (Source.Reference);
787 Source.Reference := Tmp;
790 end Realloc_For_Chunk;
792 ---------------------
793 -- Replace_Element --
794 ---------------------
796 procedure Replace_Element
797 (Source : in out Unbounded_String;
802 if Index <= Source.Last then
803 Source.Reference (Index) := By;
805 raise Strings.Index_Error;
813 function Replace_Slice
814 (Source : Unbounded_String;
817 By : String) return Unbounded_String
820 return To_Unbounded_String
822 (Source.Reference (1 .. Source.Last), Low, High, By));
825 procedure Replace_Slice
826 (Source : in out Unbounded_String;
831 Old : String_Access := Source.Reference;
833 Source.Reference := new String'
835 (Source.Reference (1 .. Source.Last), Low, High, By));
836 Source.Last := Source.Reference'Length;
840 --------------------------
841 -- Set_Unbounded_String --
842 --------------------------
844 procedure Set_Unbounded_String
845 (Target : out Unbounded_String;
848 Old : String_Access := Target.Reference;
850 Target.Last := Source'Length;
851 Target.Reference := new String (1 .. Source'Length);
852 Target.Reference.all := Source;
854 end Set_Unbounded_String;
861 (Source : Unbounded_String;
863 High : Natural) return String
866 -- Note: test of High > Length is in accordance with AI95-00128
868 if Low > Source.Last + 1 or else High > Source.Last then
871 return Source.Reference (Low .. High);
880 (Source : Unbounded_String;
882 Pad : Character := Space) return Unbounded_String is
884 return To_Unbounded_String
885 (Fixed.Tail (Source.Reference (1 .. Source.Last), Count, Pad));
889 (Source : in out Unbounded_String;
891 Pad : Character := Space)
893 Old : String_Access := Source.Reference;
895 Source.Reference := new String'
896 (Fixed.Tail (Source.Reference (1 .. Source.Last), Count, Pad));
897 Source.Last := Source.Reference'Length;
905 function To_String (Source : Unbounded_String) return String is
907 return Source.Reference (1 .. Source.Last);
910 -------------------------
911 -- To_Unbounded_String --
912 -------------------------
914 function To_Unbounded_String (Source : String) return Unbounded_String is
915 Result : Unbounded_String;
917 Result.Last := Source'Length;
918 Result.Reference := new String (1 .. Source'Length);
919 Result.Reference.all := Source;
921 end To_Unbounded_String;
923 function To_Unbounded_String
924 (Length : Natural) return Unbounded_String
926 Result : Unbounded_String;
928 Result.Last := Length;
929 Result.Reference := new String (1 .. Length);
931 end To_Unbounded_String;
938 (Source : Unbounded_String;
939 Mapping : Maps.Character_Mapping) return Unbounded_String
942 return To_Unbounded_String
943 (Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping));
947 (Source : in out Unbounded_String;
948 Mapping : Maps.Character_Mapping)
951 Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping);
955 (Source : Unbounded_String;
956 Mapping : Maps.Character_Mapping_Function) return Unbounded_String
959 return To_Unbounded_String
960 (Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping));
964 (Source : in out Unbounded_String;
965 Mapping : Maps.Character_Mapping_Function)
968 Fixed.Translate (Source.Reference (1 .. Source.Last), Mapping);
976 (Source : Unbounded_String;
977 Side : Trim_End) return Unbounded_String
980 return To_Unbounded_String
981 (Fixed.Trim (Source.Reference (1 .. Source.Last), Side));
985 (Source : in out Unbounded_String;
988 Old : String_Access := Source.Reference;
990 Source.Reference := new String'
991 (Fixed.Trim (Source.Reference (1 .. Source.Last), Side));
992 Source.Last := Source.Reference'Length;
997 (Source : Unbounded_String;
998 Left : Maps.Character_Set;
999 Right : Maps.Character_Set) return Unbounded_String
1002 return To_Unbounded_String
1003 (Fixed.Trim (Source.Reference (1 .. Source.Last), Left, Right));
1007 (Source : in out Unbounded_String;
1008 Left : Maps.Character_Set;
1009 Right : Maps.Character_Set)
1011 Old : String_Access := Source.Reference;
1013 Source.Reference := new String'
1014 (Fixed.Trim (Source.Reference (1 .. Source.Last), Left, Right));
1015 Source.Last := Source.Reference'Length;
1019 ---------------------
1020 -- Unbounded_Slice --
1021 ---------------------
1023 function Unbounded_Slice
1024 (Source : Unbounded_String;
1026 High : Natural) return Unbounded_String
1029 if Low > Source.Last + 1 or else High > Source.Last then
1032 return To_Unbounded_String (Source.Reference.all (Low .. High));
1034 end Unbounded_Slice;
1036 procedure Unbounded_Slice
1037 (Source : Unbounded_String;
1038 Target : out Unbounded_String;
1043 if Low > Source.Last + 1 or else High > Source.Last then
1046 Target := To_Unbounded_String (Source.Reference.all (Low .. High));
1048 end Unbounded_Slice;
1050 end Ada.Strings.Unbounded;