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-2023, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
36 -- Preconditions in this unit are meant for analysis only, not for run-time
37 -- checking, so that the expected exceptions are raised. This is enforced by
38 -- setting the corresponding assertion policy to Ignore.
40 pragma Assertion_Policy
(Pre
=> Ignore
);
42 with Ada
.Strings
.Maps
;
43 with Ada
.Finalization
;
44 private with Ada
.Strings
.Text_Buffers
;
46 -- The language-defined package Strings.Unbounded provides a private type
47 -- Unbounded_String and a set of operations. An object of type
48 -- Unbounded_String represents a String whose low bound is 1 and whose length
49 -- can vary conceptually between 0 and Natural'Last. The subprograms for
50 -- fixed-length string handling are either overloaded directly for
51 -- Unbounded_String, or are modified as needed to reflect the flexibility in
52 -- length. Since the Unbounded_String type is private, relevant constructor
53 -- and selector operations are provided.
55 package Ada
.Strings
.Unbounded
with
57 Initial_Condition
=> Length
(Null_Unbounded_String
) = 0,
62 type Unbounded_String
is private with
63 Default_Initial_Condition
=> Length
(Unbounded_String
) = 0;
64 pragma Preelaborable_Initialization
(Unbounded_String
);
66 Null_Unbounded_String
: constant Unbounded_String
;
67 -- Represents the null String. If an object of type Unbounded_String is not
68 -- otherwise initialized, it will be initialized to the same value as
69 -- Null_Unbounded_String.
71 function Length
(Source
: Unbounded_String
) return Natural with
73 -- Returns the length of the String represented by Source
75 type String_Access
is access all String;
76 -- Provides a (nonprivate) access type for explicit processing of
77 -- unbounded-length strings.
79 procedure Free
(X
: in out String_Access
) with SPARK_Mode
=> Off
;
80 -- Performs an unchecked deallocation of an object of type String_Access
82 --------------------------------------------------------
83 -- Conversion, Concatenation, and Selection Functions --
84 --------------------------------------------------------
86 function To_Unbounded_String
87 (Source
: String) return Unbounded_String
89 Post
=> To_String
(To_Unbounded_String
'Result) = Source
,
91 -- Returns an Unbounded_String that represents Source
93 function To_Unbounded_String
94 (Length
: Natural) return Unbounded_String
98 -- Returns an Unbounded_String that represents an uninitialized String
99 -- whose length is Length.
101 function To_String
(Source
: Unbounded_String
) return String with
103 To_String
'Result'First = 1
104 and then To_String'Result'Length
= Length
(Source
),
106 -- Returns the String with lower bound 1 represented by Source
108 -- To_String and To_Unbounded_String are related as follows:
110 -- * If S is a String, then To_String (To_Unbounded_String (S)) = S.
112 -- * If U is an Unbounded_String, then
113 -- To_Unbounded_String (To_String (U)) = U.
115 procedure Set_Unbounded_String
116 (Target
: out Unbounded_String
;
119 Post
=> To_String
(Target
) = Source
,
121 pragma Ada_05
(Set_Unbounded_String
);
122 -- Sets Target to an Unbounded_String that represents Source
125 (Source
: in out Unbounded_String
;
126 New_Item
: Unbounded_String
)
128 Pre
=> Length
(New_Item
) <= Natural'Last - Length
(Source
),
129 Post
=> Length
(Source
) = Length
(Source
)'Old + Length
(New_Item
),
133 (Source
: in out Unbounded_String
;
136 Pre
=> New_Item
'Length <= Natural'Last - Length
(Source
),
137 Post
=> Length
(Source
) = Length
(Source
)'Old + New_Item
'Length,
141 (Source
: in out Unbounded_String
;
142 New_Item
: Character)
144 Pre
=> Length
(Source
) < Natural'Last,
145 Post
=> Length
(Source
) = Length
(Source
)'Old + 1,
148 -- For each of the Append procedures, the resulting string represented by
149 -- the Source parameter is given by the concatenation of the original value
150 -- of Source and the value of New_Item.
153 (Left
: Unbounded_String
;
154 Right
: Unbounded_String
) return Unbounded_String
156 Pre
=> Length
(Right
) <= Natural'Last - Length
(Left
),
157 Post
=> Length
("&"'Result) = Length (Left) + Length (Right),
161 (Left : Unbounded_String;
162 Right : String) return Unbounded_String
164 Pre => Right'Length <= Natural'Last - Length (Left),
165 Post => Length ("&"'Result
) = Length
(Left
) + Right
'Length,
170 Right
: Unbounded_String
) return Unbounded_String
172 Pre
=> Left
'Length <= Natural'Last - Length
(Right
),
173 Post
=> Length
("&"'Result) = Left'Length + Length (Right),
177 (Left : Unbounded_String;
178 Right : Character) return Unbounded_String
180 Pre => Length (Left) < Natural'Last,
181 Post => Length ("&"'Result
) = Length
(Left
) + 1,
186 Right
: Unbounded_String
) return Unbounded_String
188 Pre
=> Length
(Right
) < Natural'Last,
189 Post
=> Length
("&"'Result) = Length (Right) + 1,
192 -- Each of the "&" functions returns an Unbounded_String obtained by
193 -- concatenating the string or character given or represented by one of the
194 -- parameters, with the string or character given or represented by the
195 -- other parameter, and applying To_Unbounded_String to the concatenation
199 (Source : Unbounded_String;
200 Index : Positive) return Character
202 Pre => Index <= Length (Source),
203 Post => Element'Result = To_String (Source) (Index),
205 -- Returns the character at position Index in the string represented by
206 -- Source; propagates Index_Error if Index > Length (Source).
208 procedure Replace_Element
209 (Source : in out Unbounded_String;
213 Pre => Index <= Length (Source),
214 Post => Length (Source) = Length (Source)'Old,
216 -- Updates Source such that the character at position Index in the string
217 -- represented by Source is By; propagates Index_Error if
218 -- Index > Length (Source).
221 (Source : Unbounded_String;
223 High : Natural) return String
225 Pre => Low - 1 <= Length (Source) and then High <= Length (Source),
226 Post => Slice'Result'Length
= Natural'Max (0, High
- Low
+ 1),
228 -- Returns the slice at positions Low through High in the string
229 -- represented by Source; propagates Index_Error if
230 -- Low > Length (Source) + 1 or High > Length (Source). The bounds of the
231 -- returned string are Low and High.
233 function Unbounded_Slice
234 (Source
: Unbounded_String
;
236 High
: Natural) return Unbounded_String
238 Pre
=> Low
- 1 <= Length
(Source
) and then High
<= Length
(Source
),
240 Length
(Unbounded_Slice
'Result) = Natural'Max (0, High
- Low
+ 1),
242 pragma Ada_05
(Unbounded_Slice
);
243 -- Returns the slice at positions Low through High in the string
244 -- represented by Source as an Unbounded_String. This propagates
245 -- Index_Error if Low > Length(Source) + 1 or High > Length (Source).
247 procedure Unbounded_Slice
248 (Source
: Unbounded_String
;
249 Target
: out Unbounded_String
;
253 Pre
=> Low
- 1 <= Length
(Source
) and then High
<= Length
(Source
),
254 Post
=> Length
(Target
) = Natural'Max (0, High
- Low
+ 1),
256 pragma Ada_05
(Unbounded_Slice
);
257 -- Sets Target to the Unbounded_String representing the slice at positions
258 -- Low through High in the string represented by Source. This propagates
259 -- Index_Error if Low > Length(Source) + 1 or High > Length (Source).
262 (Left
: Unbounded_String
;
263 Right
: Unbounded_String
) return Boolean
265 Post
=> "="'Result = (To_String (Left) = To_String (Right)),
269 (Left : Unbounded_String;
270 Right : String) return Boolean
272 Post => "="'Result
= (To_String
(Left
) = Right
),
277 Right
: Unbounded_String
) return Boolean
279 Post
=> "="'Result = (Left = To_String (Right)),
283 (Left : Unbounded_String;
284 Right : Unbounded_String) return Boolean
289 (Left : Unbounded_String;
290 Right : String) return Boolean
296 Right : Unbounded_String) return Boolean
301 (Left : Unbounded_String;
302 Right : Unbounded_String) return Boolean
307 (Left : Unbounded_String;
308 Right : String) return Boolean
314 Right : Unbounded_String) return Boolean
319 (Left : Unbounded_String;
320 Right : Unbounded_String) return Boolean
325 (Left : Unbounded_String;
326 Right : String) return Boolean
332 Right : Unbounded_String) return Boolean
337 (Left : Unbounded_String;
338 Right : Unbounded_String) return Boolean
343 (Left : Unbounded_String;
344 Right : String) return Boolean
350 Right : Unbounded_String) return Boolean
354 -- Each of the functions "=", "<", ">", "<=", and ">=" returns the same
355 -- result as the corresponding String operation applied to the String
356 -- values given or represented by Left and Right.
358 ------------------------
359 -- Search Subprograms --
360 ------------------------
363 (Source : Unbounded_String;
365 Going : Direction := Forward;
366 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
368 Pre => Pattern'Length /= 0,
372 (Source : Unbounded_String;
374 Going : Direction := Forward;
375 Mapping : Maps.Character_Mapping_Function) return Natural
377 Pre => Pattern'Length /= 0,
381 (Source : Unbounded_String;
382 Set : Maps.Character_Set;
383 Test : Membership := Inside;
384 Going : Direction := Forward) return Natural
389 (Source : Unbounded_String;
392 Going : Direction := Forward;
393 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
395 Pre => (if Length (Source) /= 0 then From <= Length (Source))
396 and then Pattern'Length /= 0,
398 pragma Ada_05 (Index);
401 (Source : Unbounded_String;
404 Going : Direction := Forward;
405 Mapping : Maps.Character_Mapping_Function) return Natural
407 Pre => (if Length (Source) /= 0 then From <= Length (Source))
408 and then Pattern'Length /= 0,
410 pragma Ada_05 (Index);
413 (Source : Unbounded_String;
414 Set : Maps.Character_Set;
416 Test : Membership := Inside;
417 Going : Direction := Forward) return Natural
419 Pre => (if Length (Source) /= 0 then From <= Length (Source)),
421 pragma Ada_05 (Index);
423 function Index_Non_Blank
424 (Source : Unbounded_String;
425 Going : Direction := Forward) return Natural
429 function Index_Non_Blank
430 (Source : Unbounded_String;
432 Going : Direction := Forward) return Natural
434 Pre => (if Length (Source) /= 0 then From <= Length (Source)),
436 pragma Ada_05 (Index_Non_Blank);
439 (Source : Unbounded_String;
441 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
443 Pre => Pattern'Length /= 0,
447 (Source : Unbounded_String;
449 Mapping : Maps.Character_Mapping_Function) return Natural
451 Pre => Pattern'Length /= 0,
455 (Source : Unbounded_String;
456 Set : Maps.Character_Set) return Natural
461 (Source : Unbounded_String;
462 Set : Maps.Character_Set;
465 First : out Positive;
468 Pre => (if Length (Source) /= 0 then From <= Length (Source)),
470 pragma Ada_2012 (Find_Token);
473 (Source : Unbounded_String;
474 Set : Maps.Character_Set;
476 First : out Positive;
481 -- Each of the search subprograms (Index, Index_Non_Blank, Count,
482 -- Find_Token) has the same effect as the corresponding subprogram in
483 -- Strings.Fixed applied to the string represented by the Unbounded_String
486 ------------------------------------
487 -- String Translation Subprograms --
488 ------------------------------------
491 (Source : Unbounded_String;
492 Mapping : Maps.Character_Mapping) return Unbounded_String
494 Post => Length (Translate'Result) = Length (Source),
498 (Source : in out Unbounded_String;
499 Mapping : Maps.Character_Mapping)
501 Post => Length (Source) = Length (Source)'Old,
505 (Source : Unbounded_String;
506 Mapping : Maps.Character_Mapping_Function) return Unbounded_String
508 Post => Length (Translate'Result) = Length (Source),
512 (Source : in out Unbounded_String;
513 Mapping : Maps.Character_Mapping_Function)
515 Post => Length (Source) = Length (Source)'Old,
518 -- The Translate function has an analogous effect to the corresponding
519 -- subprogram in Strings.Fixed. The translation is applied to the string
520 -- represented by the Unbounded_String parameter, and the result is
521 -- converted (via To_Unbounded_String) to an Unbounded_String.
523 ---------------------------------------
524 -- String Transformation Subprograms --
525 ---------------------------------------
527 function Replace_Slice
528 (Source : Unbounded_String;
531 By : String) return Unbounded_String
534 Low - 1 <= Length (Source)
535 and then (if High >= Low
537 <= Natural'Last - By'Length
538 - Natural'Max (Length (Source) - High, 0)
539 else Length (Source) <= Natural'Last - By'Length),
542 Length (Replace_Slice'Result)
543 = Low - 1 + By'Length + Natural'Max (Length (Source)'Old - High, 0),
545 Length (Replace_Slice'Result) = Length (Source)'Old + By'Length),
548 procedure Replace_Slice
549 (Source : in out Unbounded_String;
555 Low - 1 <= Length (Source)
556 and then (if High >= Low
558 <= Natural'Last - By'Length
559 - Natural'Max (Length (Source) - High, 0)
560 else Length (Source) <= Natural'Last - By'Length),
564 = Low - 1 + By'Length + Natural'Max (Length (Source)'Old - High, 0),
566 Length (Source) = Length (Source)'Old + By'Length),
570 (Source : Unbounded_String;
572 New_Item : String) return Unbounded_String
574 Pre => Before - 1 <= Length (Source)
575 and then New_Item'Length <= Natural'Last - Length (Source),
576 Post => Length (Insert'Result) = Length (Source) + New_Item'Length,
580 (Source : in out Unbounded_String;
584 Pre => Before - 1 <= Length (Source)
585 and then New_Item'Length <= Natural'Last - Length (Source),
586 Post => Length (Source) = Length (Source)'Old + New_Item'Length,
590 (Source : Unbounded_String;
592 New_Item : String) return Unbounded_String
594 Pre => Position - 1 <= Length (Source)
595 and then (if New_Item'Length /= 0
597 New_Item'Length <= Natural'Last - (Position - 1)),
599 Length (Overwrite'Result)
600 = Natural'Max (Length (Source), Position - 1 + New_Item'Length),
604 (Source : in out Unbounded_String;
608 Pre => Position - 1 <= Length (Source)
609 and then (if New_Item'Length /= 0
611 New_Item'Length <= Natural'Last - (Position - 1)),
614 = Natural'Max (Length (Source)'Old, Position - 1 + New_Item'Length),
619 (Source : Unbounded_String;
621 Through : Natural) return Unbounded_String
623 Pre => (if Through <= From then From - 1 <= Length (Source)),
626 Length (Delete'Result) = Length (Source) - (Through - From + 1),
628 Length (Delete'Result) = Length (Source)),
632 (Source : in out Unbounded_String;
636 Pre => (if Through <= From then From - 1 <= Length (Source)),
639 Length (Source) = Length (Source)'Old - (Through - From + 1),
641 Length (Source) = Length (Source)'Old),
645 (Source : Unbounded_String;
646 Side : Trim_End) return Unbounded_String
648 Post => Length (Trim'Result) <= Length (Source),
652 (Source : in out Unbounded_String;
655 Post => Length (Source) <= Length (Source)'Old,
659 (Source : Unbounded_String;
660 Left : Maps.Character_Set;
661 Right : Maps.Character_Set) return Unbounded_String
663 Post => Length (Trim'Result) <= Length (Source),
667 (Source : in out Unbounded_String;
668 Left : Maps.Character_Set;
669 Right : Maps.Character_Set)
671 Post => Length (Source) <= Length (Source)'Old,
675 (Source : Unbounded_String;
677 Pad : Character := Space) return Unbounded_String
679 Post => Length (Head'Result) = Count,
683 (Source : in out Unbounded_String;
685 Pad : Character := Space)
687 Post => Length (Source) = Count,
691 (Source : Unbounded_String;
693 Pad : Character := Space) return Unbounded_String
695 Post => Length (Tail'Result) = Count,
699 (Source : in out Unbounded_String;
701 Pad : Character := Space)
703 Post => Length (Source) = Count,
708 Right : Character) return Unbounded_String
710 Pre => Left <= Natural'Last,
711 Post => Length ("*"'Result
) = Left
,
716 Right
: String) return Unbounded_String
718 Pre
=> (if Left
/= 0 then Right
'Length <= Natural'Last / Left
),
719 Post
=> Length
("*"'Result) = Left * Right'Length,
724 Right : Unbounded_String) return Unbounded_String
726 Pre => (if Left /= 0 then Length (Right) <= Natural'Last / Left),
727 Post => Length ("*"'Result
) = Left
* Length
(Right
),
730 -- Each of the transformation functions (Replace_Slice, Insert, Overwrite,
731 -- Delete), selector functions (Trim, Head, Tail), and constructor
732 -- functions ("*") is likewise analogous to its corresponding subprogram in
733 -- Strings.Fixed. For each of the subprograms, the corresponding
734 -- fixed-length string subprogram is applied to the string represented by
735 -- the Unbounded_String parameter, and To_Unbounded_String is applied the
738 -- For each of the procedures Translate, Replace_Slice, Insert, Overwrite,
739 -- Delete, Trim, Head, and Tail, the resulting string represented by the
740 -- Source parameter is given by the corresponding function for fixed-length
741 -- strings applied to the string represented by Source's original value.
744 pragma SPARK_Mode
(Off
); -- Controlled types are not in SPARK
746 pragma Inline
(Length
);
748 package AF
renames Ada
.Finalization
;
750 Null_String
: aliased String := "";
752 function To_Unbounded
(S
: String) return Unbounded_String
753 renames To_Unbounded_String
;
755 type Unbounded_String
is new AF
.Controlled
with record
756 Reference
: not null String_Access
:= Null_String
'Access;
758 end record with Put_Image
=> Put_Image
;
761 (S
: in out Ada
.Strings
.Text_Buffers
.Root_Buffer_Type
'Class;
762 V
: Unbounded_String
);
764 -- The Unbounded_String is using a buffered implementation to increase
765 -- speed of the Append/Delete/Insert procedures. The Reference string
766 -- pointer above contains the current string value and extra room at the
767 -- end to be used by the next Append routine. Last is the index of the
768 -- string ending character. So the current string value is really
769 -- Reference (1 .. Last).
771 pragma Stream_Convert
(Unbounded_String
, To_Unbounded
, To_String
);
772 -- Provide stream routines without dragging in Ada.Streams
774 pragma Finalize_Storage_Only
(Unbounded_String
);
775 -- Finalization is required only for freeing storage
777 procedure Initialize
(Object
: in out Unbounded_String
);
778 procedure Adjust
(Object
: in out Unbounded_String
);
779 procedure Finalize
(Object
: in out Unbounded_String
);
781 procedure Realloc_For_Chunk
782 (Source
: in out Unbounded_String
;
783 Chunk_Size
: Natural);
784 pragma Inline
(Realloc_For_Chunk
);
785 -- Adjust the size allocated for the string. Add at least Chunk_Size so it
786 -- is safe to add a string of this size at the end of the current content.
787 -- The real size allocated for the string is Chunk_Size + x of the current
788 -- string size. This buffered handling makes the Append unbounded string
789 -- routines very fast. This spec is in the private part so that it can be
790 -- accessed from children (e.g. from Unbounded.Text_IO).
792 Null_Unbounded_String
: constant Unbounded_String
:=
794 Reference
=> Null_String
'Access,
796 end Ada
.Strings
.Unbounded
;