Debian Codegraph @

gcc/ada/libgnat/a-strunb.ads

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 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
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. --
14 -- --
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. --
21 -- --
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. --
25 -- --
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/>. --
30 -- --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 -- --
34 ------------------------------------------------------------------------------
35
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. Postconditions and
39 -- contract cases should not be executed at runtime as well, in order not to
40 -- slow down the execution of these functions.
41
42 pragma Assertion_Policy (Pre => Ignore,
43 Post => Ignore,
44 Contract_Cases => Ignore,
45 Ghost => Ignore);
46
47 with Ada.Strings.Maps; use type Ada.Strings.Maps.Character_Mapping_Function;
48 with Ada.Finalization;
49 with Ada.Strings.Search;
50 private with Ada.Strings.Text_Buffers;
51
52 -- The language-defined package Strings.Unbounded provides a private type
53 -- Unbounded_String and a set of operations. An object of type
54 -- Unbounded_String represents a String whose low bound is 1 and whose length
55 -- can vary conceptually between 0 and Natural'Last. The subprograms for
56 -- fixed-length string handling are either overloaded directly for
57 -- Unbounded_String, or are modified as needed to reflect the flexibility in
58 -- length. Since the Unbounded_String type is private, relevant constructor
59 -- and selector operations are provided.
60
61 package Ada.Strings.Unbounded with
62 SPARK_Mode,
63 Initial_Condition => Length (Null_Unbounded_String) = 0,
64 Always_Terminates
65 is
66 pragma Preelaborate;
67
68 -- Contracts may call function Length in the prefix of attribute reference
69 -- to Old as in Length (Source)'Old. Allow this use.
70
71 pragma Unevaluated_Use_Of_Old (Allow);
72
73 subtype String_1 is String (1 .. <>) with Ghost; -- Type used in contracts
74
75 type Unbounded_String is private with
76 Default_Initial_Condition => Length (Unbounded_String) = 0;
77 pragma Preelaborable_Initialization (Unbounded_String);
78
79 Null_Unbounded_String : constant Unbounded_String;
80 -- Represents the null String. If an object of type Unbounded_String is not
81 -- otherwise initialized, it will be initialized to the same value as
82 -- Null_Unbounded_String.
83
84 function Length (Source : Unbounded_String) return Natural with
85 Global => null;
86 -- Returns the length of the String represented by Source
87
88 type String_Access is access all String;
89 -- Provides a (nonprivate) access type for explicit processing of
90 -- unbounded-length strings.
91
92 procedure Free (X : in out String_Access) with SPARK_Mode => Off;
93 -- Performs an unchecked deallocation of an object of type String_Access
94
95 --------------------------------------------------------
96 -- Conversion, Concatenation, and Selection Functions --
97 --------------------------------------------------------
98
99 function To_Unbounded_String
100 (Source : String) return Unbounded_String
101 with
102 Post => To_String (To_Unbounded_String'Result) = Source
103 and then Length (To_Unbounded_String'Result) = Source'Length,
104 Global => null;
105 -- Returns an Unbounded_String that represents Source
106
107 function To_Unbounded_String
108 (Length : Natural) return Unbounded_String
109 with
110 SPARK_Mode => Off,
111 Global => null;
112 -- Returns an Unbounded_String that represents an uninitialized String
113 -- whose length is Length.
114
115 function To_String (Source : Unbounded_String) return String with
116 Post =>
117 To_String'Result'First = 1
118 and then To_String'Result'Length = Length (Source),
119 Global => null;
120 -- Returns the String with lower bound 1 represented by Source
121
122 -- To_String and To_Unbounded_String are related as follows:
123 --
124 -- * If S is a String, then To_String (To_Unbounded_String (S)) = S.
125 --
126 -- * If U is an Unbounded_String, then
127 -- To_Unbounded_String (To_String (U)) = U.
128
129 procedure Set_Unbounded_String
130 (Target : out Unbounded_String;
131 Source : String)
132 with
133 Post => To_String (Target) = Source,
134 Global => null;
135 pragma Ada_05 (Set_Unbounded_String);
136 -- Sets Target to an Unbounded_String that represents Source
137
138 procedure Append
139 (Source : in out Unbounded_String;
140 New_Item : Unbounded_String)
141 with
142 Pre => Length (New_Item) <= Natural'Last - Length (Source),
143 Post =>
144 To_String (Source) = To_String (Source)'Old & To_String (New_Item),
145 Global => null;
146
147 procedure Append
148 (Source : in out Unbounded_String;
149 New_Item : String)
150 with
151 Pre => New_Item'Length <= Natural'Last - Length (Source),
152 Post => To_String (Source) = To_String (Source)'Old & New_Item,
153 Global => null;
154
155 procedure Append
156 (Source : in out Unbounded_String;
157 New_Item : Character)
158 with
159 Pre => Length (Source) < Natural'Last,
160 Post => To_String (Source) = To_String (Source)'Old & New_Item,
161 Global => null;
162
163 -- For each of the Append procedures, the resulting string represented by
164 -- the Source parameter is given by the concatenation of the original value
165 -- of Source and the value of New_Item.
166
167 function "&"
168 (Left : Unbounded_String;
169 Right : Unbounded_String) return Unbounded_String
170 with
171 Pre => Length (Right) <= Natural'Last - Length (Left),
172 Post => To_String ("&"'Result) = To_String (Left) & To_String (Right),
173 Global => null;
174
175 function "&"
176 (Left : Unbounded_String;
177 Right : String) return Unbounded_String
178 with
179 Pre => Right'Length <= Natural'Last - Length (Left),
180 Post => To_String ("&"'Result) = To_String (Left) & Right,
181 Global => null;
182
183 function "&"
184 (Left : String;
185 Right : Unbounded_String) return Unbounded_String
186 with
187 Pre => Left'Length <= Natural'Last - Length (Right),
188 Post => To_String ("&"'Result) = String_1 (Left) & To_String (Right),
189 Global => null;
190
191 function "&"
192 (Left : Unbounded_String;
193 Right : Character) return Unbounded_String
194 with
195 Pre => Length (Left) < Natural'Last,
196 Post => To_String ("&"'Result) = To_String (Left) & Right,
197 Global => null;
198
199 function "&"
200 (Left : Character;
201 Right : Unbounded_String) return Unbounded_String
202 with
203 Pre => Length (Right) < Natural'Last,
204 Post => To_String ("&"'Result) = Left & To_String (Right),
205 Global => null;
206
207 -- Each of the "&" functions returns an Unbounded_String obtained by
208 -- concatenating the string or character given or represented by one of the
209 -- parameters, with the string or character given or represented by the
210 -- other parameter, and applying To_Unbounded_String to the concatenation
211 -- result string.
212
213 function Element
214 (Source : Unbounded_String;
215 Index : Positive) return Character
216 with
217 Pre => Index <= Length (Source),
218 Post => Element'Result = To_String (Source) (Index),
219 Global => null;
220 -- Returns the character at position Index in the string represented by
221 -- Source; propagates Index_Error if Index > Length (Source).
222
223 procedure Replace_Element
224 (Source : in out Unbounded_String;
225 Index : Positive;
226 By : Character)
227 with
228 Pre => Index <= Length (Source),
229 Post =>
230 To_String (Source) = (To_String (Source)'Old with delta Index => By),
231 Global => null;
232 -- Updates Source such that the character at position Index in the string
233 -- represented by Source is By; propagates Index_Error if
234 -- Index > Length (Source).
235
236 function Slice
237 (Source : Unbounded_String;
238 Low : Positive;
239 High : Natural) return String
240 with
241 Pre => Low - 1 <= Length (Source) and then High <= Length (Source),
242 Post => Slice'Result'First = Low
243 and then Slice'Result'Last = High
244 and then Slice'Result = To_String (Source) (Low .. High),
245 Global => null;
246 -- Returns the slice at positions Low through High in the string
247 -- represented by Source; propagates Index_Error if
248 -- Low > Length (Source) + 1 or High > Length (Source). The bounds of the
249 -- returned string are Low and High.
250
251 function Unbounded_Slice
252 (Source : Unbounded_String;
253 Low : Positive;
254 High : Natural) return Unbounded_String
255 with
256 Pre => Low - 1 <= Length (Source) and then High <= Length (Source),
257 Post =>
258 To_String (Unbounded_Slice'Result) = To_String (Source) (Low .. High),
259 Global => null;
260 pragma Ada_05 (Unbounded_Slice);
261 -- Returns the slice at positions Low through High in the string
262 -- represented by Source as an Unbounded_String. This propagates
263 -- Index_Error if Low > Length(Source) + 1 or High > Length (Source).
264
265 procedure Unbounded_Slice
266 (Source : Unbounded_String;
267 Target : out Unbounded_String;
268 Low : Positive;
269 High : Natural)
270 with
271 Pre => Low - 1 <= Length (Source) and then High <= Length (Source),
272 Post => To_String (Target) = To_String (Source) (Low .. High),
273 Global => null;
274 pragma Ada_05 (Unbounded_Slice);
275 -- Sets Target to the Unbounded_String representing the slice at positions
276 -- Low through High in the string represented by Source. This propagates
277 -- Index_Error if Low > Length(Source) + 1 or High > Length (Source).
278
279 function "="
280 (Left : Unbounded_String;
281 Right : Unbounded_String) return Boolean
282 with
283 Post => "="'Result = (To_String (Left) = To_String (Right)),
284 Global => null;
285
286 function "="
287 (Left : Unbounded_String;
288 Right : String) return Boolean
289 with
290 Post => "="'Result = (To_String (Left) = Right),
291 Global => null;
292
293 function "="
294 (Left : String;
295 Right : Unbounded_String) return Boolean
296 with
297 Post => "="'Result = (Left = To_String (Right)),
298 Global => null;
299
300 function "<"
301 (Left : Unbounded_String;
302 Right : Unbounded_String) return Boolean
303 with
304 Post => "<"'Result = (To_String (Left) < To_String (Right)),
305 Global => null;
306
307 function "<"
308 (Left : Unbounded_String;
309 Right : String) return Boolean
310 with
311 Post => "<"'Result = (To_String (Left) < Right),
312 Global => null;
313
314 function "<"
315 (Left : String;
316 Right : Unbounded_String) return Boolean
317 with
318 Post => "<"'Result = (Left < To_String (Right)),
319 Global => null;
320
321 function "<="
322 (Left : Unbounded_String;
323 Right : Unbounded_String) return Boolean
324 with
325 Post => "<="'Result = (To_String (Left) <= To_String (Right)),
326 Global => null;
327
328 function "<="
329 (Left : Unbounded_String;
330 Right : String) return Boolean
331 with
332 Post => "<="'Result = (To_String (Left) <= Right),
333 Global => null;
334
335 function "<="
336 (Left : String;
337 Right : Unbounded_String) return Boolean
338 with
339 Post => "<="'Result = (Left <= To_String (Right)),
340 Global => null;
341
342 function ">"
343 (Left : Unbounded_String;
344 Right : Unbounded_String) return Boolean
345 with
346 Post => ">"'Result = (To_String (Left) > To_String (Right)),
347 Global => null;
348
349 function ">"
350 (Left : Unbounded_String;
351 Right : String) return Boolean
352 with
353 Post => ">"'Result = (To_String (Left) > Right),
354 Global => null;
355
356 function ">"
357 (Left : String;
358 Right : Unbounded_String) return Boolean
359 with
360 Post => ">"'Result = (Left > To_String (Right)),
361 Global => null;
362
363 function ">="
364 (Left : Unbounded_String;
365 Right : Unbounded_String) return Boolean
366 with
367 Post => ">="'Result = (To_String (Left) >= To_String (Right)),
368 Global => null;
369
370 function ">="
371 (Left : Unbounded_String;
372 Right : String) return Boolean
373 with
374 Post => ">="'Result = (To_String (Left) >= Right),
375 Global => null;
376
377 function ">="
378 (Left : String;
379 Right : Unbounded_String) return Boolean
380 with
381 Post => ">="'Result = (Left >= To_String (Right)),
382 Global => null;
383
384 -- Each of the functions "=", "<", ">", "<=", and ">=" returns the same
385 -- result as the corresponding String operation applied to the String
386 -- values given or represented by Left and Right.
387
388 ------------------------
389 -- Search Subprograms --
390 ------------------------
391
392 function Index
393 (Source : Unbounded_String;
394 Pattern : String;
395 Going : Direction := Forward;
396 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
397 with
398 Pre => Pattern'Length > 0,
399 Post => Index'Result <= Length (Source),
400 Contract_Cases =>
401
402 -- If Source is the empty string, then 0 is returned
403
404 (Length (Source) = 0
405 =>
406 Index'Result = 0,
407
408 -- If some slice of Source matches Pattern, then a valid index is
409 -- returned.
410
411 Length (Source) > 0
412 and then
413 (for some J in 1 .. Length (Source) - (Pattern'Length - 1) =>
414 Search.Match (To_String (Source), Pattern, Mapping, J))
415 =>
416 -- The result is in the considered range of Source
417
418 Index'Result in 1 .. Length (Source) - (Pattern'Length - 1)
419
420 -- The slice beginning at the returned index matches Pattern
421
422 and then Search.Match
423 (To_String (Source), Pattern, Mapping, Index'Result)
424
425 -- The result is the smallest or largest index which satisfies
426 -- the matching, respectively when Going = Forward and Going =
427 -- Backward.
428
429 and then
430 (for all J in 1 .. Length (Source) =>
431 (if (if Going = Forward
432 then J <= Index'Result - 1
433 else J - 1 in Index'Result
434 .. Length (Source) - Pattern'Length)
435 then not Search.Match
436 (To_String (Source), Pattern, Mapping, J))),
437
438 -- Otherwise, 0 is returned
439
440 others
441 =>
442 Index'Result = 0),
443 Global => null;
444
445 function Index
446 (Source : Unbounded_String;
447 Pattern : String;
448 Going : Direction := Forward;
449 Mapping : Maps.Character_Mapping_Function) return Natural
450 with
451 Pre => Pattern'Length /= 0 and then Mapping /= null,
452 Post => Index'Result <= Length (Source),
453 Contract_Cases =>
454
455 -- If Source is the empty string, then 0 is returned
456
457 (Length (Source) = 0
458 =>
459 Index'Result = 0,
460
461 -- If some slice of Source matches Pattern, then a valid index is
462 -- returned.
463
464 Length (Source) > 0
465 and then
466 (for some J in 1 .. Length (Source) - (Pattern'Length - 1) =>
467 Search.Match (To_String (Source), Pattern, Mapping, J))
468 =>
469 -- The result is in the considered range of Source
470
471 Index'Result in 1 .. Length (Source) - (Pattern'Length - 1)
472
473 -- The slice beginning at the returned index matches Pattern
474
475 and then Search.Match
476 (To_String (Source), Pattern, Mapping, Index'Result)
477
478 -- The result is the smallest or largest index which satisfies
479 -- the matching, respectively when Going = Forward and Going =
480 -- Backward.
481
482 and then
483 (for all J in 1 .. Length (Source) =>
484 (if (if Going = Forward
485 then J <= Index'Result - 1
486 else J - 1 in Index'Result
487 .. Length (Source) - Pattern'Length)
488 then not Search.Match
489 (To_String (Source), Pattern, Mapping, J))),
490
491 -- Otherwise, 0 is returned
492
493 others
494 =>
495 Index'Result = 0),
496 Global => null;
497
498 function Index
499 (Source : Unbounded_String;
500 Set : Maps.Character_Set;
501 Test : Membership := Inside;
502 Going : Direction := Forward) return Natural
503 with
504 Post => Index'Result <= Length (Source),
505 Contract_Cases =>
506
507 -- If no character of Source satisfies the property Test on Set,
508 -- then 0 is returned.
509
510 ((for all C of To_String (Source) =>
511 (Test = Inside) /= Maps.Is_In (C, Set))
512 =>
513 Index'Result = 0,
514
515 -- Otherwise, an index in the range of Source is returned
516
517 others
518 =>
519 -- The result is in the range of Source
520
521 Index'Result in 1 .. Length (Source)
522
523 -- The character at the returned index satisfies the property
524 -- Test on Set.
525
526 and then
527 (Test = Inside) =
528 Maps.Is_In (Element (Source, Index'Result), Set)
529
530 -- The result is the smallest or largest index which satisfies
531 -- the property, respectively when Going = Forward and Going =
532 -- Backward.
533
534 and then
535 (for all J in 1 .. Length (Source) =>
536 (if J /= Index'Result
537 and then (J < Index'Result) = (Going = Forward)
538 then (Test = Inside)
539 /= Maps.Is_In (Element (Source, J), Set)))),
540 Global => null;
541
542 function Index
543 (Source : Unbounded_String;
544 Pattern : String;
545 From : Positive;
546 Going : Direction := Forward;
547 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
548 with
549 Pre =>
550 (if Length (Source) /= 0 then From <= Length (Source))
551 and then Pattern'Length /= 0,
552 Post => Index'Result <= Length (Source),
553 Contract_Cases =>
554
555 -- If Source is the empty string, then 0 is returned
556
557 (Length (Source) = 0
558 =>
559 Index'Result = 0,
560
561 -- If some slice of Source matches Pattern, then a valid index is
562 -- returned.
563
564 Length (Source) > 0
565 and then
566 (for some J in
567 (if Going = Forward then From else 1)
568 .. (if Going = Forward then Length (Source) else From)
569 - (Pattern'Length - 1) =>
570 Search.Match (To_String (Source), Pattern, Mapping, J))
571 =>
572 -- The result is in the considered range of Source
573
574 Index'Result in
575 (if Going = Forward then From else 1)
576 .. (if Going = Forward then Length (Source) else From)
577 - (Pattern'Length - 1)
578
579 -- The slice beginning at the returned index matches Pattern
580
581 and then Search.Match
582 (To_String (Source), Pattern, Mapping, Index'Result)
583
584 -- The result is the smallest or largest index which satisfies
585 -- the matching, respectively when Going = Forward and Going =
586 -- Backward.
587
588 and then
589 (for all J in 1 .. Length (Source) =>
590 (if (if Going = Forward
591 then J in From .. Index'Result - 1
592 else J - 1 in Index'Result
593 .. From - Pattern'Length)
594 then not Search.Match
595 (To_String (Source), Pattern, Mapping, J))),
596
597 -- Otherwise, 0 is returned
598
599 others
600 =>
601 Index'Result = 0),
602 Global => null;
603 pragma Ada_05 (Index);
604
605 function Index
606 (Source : Unbounded_String;
607 Pattern : String;
608 From : Positive;
609 Going : Direction := Forward;
610 Mapping : Maps.Character_Mapping_Function) return Natural
611 with
612 Pre =>
613 (if Length (Source) /= 0 then From <= Length (Source))
614 and then Pattern'Length /= 0
615 and then Mapping /= null,
616 Post => Index'Result <= Length (Source),
617 Contract_Cases =>
618
619 -- If Source is the empty string, then 0 is returned
620
621 (Length (Source) = 0
622 =>
623 Index'Result = 0,
624
625 -- If some slice of Source matches Pattern, then a valid index is
626 -- returned.
627
628 Length (Source) > 0
629 and then
630 (for some J in
631 (if Going = Forward then From else 1)
632 .. (if Going = Forward then Length (Source) else From)
633 - (Pattern'Length - 1) =>
634 Search.Match (To_String (Source), Pattern, Mapping, J))
635 =>
636 -- The result is in the considered range of Source
637
638 Index'Result in
639 (if Going = Forward then From else 1)
640 .. (if Going = Forward then Length (Source) else From)
641 - (Pattern'Length - 1)
642
643 -- The slice beginning at the returned index matches Pattern
644
645 and then Search.Match
646 (To_String (Source), Pattern, Mapping, Index'Result)
647
648 -- The result is the smallest or largest index which satisfies
649 -- the matching, respectively when Going = Forward and Going =
650 -- Backward.
651
652 and then
653 (for all J in 1 .. Length (Source) =>
654 (if (if Going = Forward
655 then J in From .. Index'Result - 1
656 else J - 1 in Index'Result
657 .. From - Pattern'Length)
658 then not Search.Match
659 (To_String (Source), Pattern, Mapping, J))),
660
661 -- Otherwise, 0 is returned
662
663 others
664 =>
665 Index'Result = 0),
666 Global => null;
667 pragma Ada_05 (Index);
668
669 function Index
670 (Source : Unbounded_String;
671 Set : Maps.Character_Set;
672 From : Positive;
673 Test : Membership := Inside;
674 Going : Direction := Forward) return Natural
675 with
676 Pre =>
677 (if Length (Source) /= 0 then From <= Length (Source)),
678 Post => Index'Result <= Length (Source),
679 Contract_Cases =>
680
681 -- If Source is the empty string, or no character of the considered
682 -- slice of Source satisfies the property Test on Set, then 0 is
683 -- returned.
684
685 (Length (Source) = 0
686 or else
687 (for all J in 1 .. Length (Source) =>
688 (if J = From or else (J > From) = (Going = Forward) then
689 (Test = Inside) /= Maps.Is_In (Element (Source, J), Set)))
690 =>
691 Index'Result = 0,
692
693 -- Otherwise, an index in the considered range of Source is
694 -- returned.
695
696 others
697 =>
698 -- The result is in the considered range of Source
699
700 Index'Result in 1 .. Length (Source)
701 and then
702 (Index'Result = From
703 or else (Index'Result > From) = (Going = Forward))
704
705 -- The character at the returned index satisfies the property
706 -- Test on Set.
707
708 and then
709 (Test = Inside) =
710 Maps.Is_In (Element (Source, Index'Result), Set)
711
712 -- The result is the smallest or largest index which satisfies
713 -- the property, respectively when Going = Forward and Going =
714 -- Backward.
715
716 and then
717 (for all J in 1 .. Length (Source) =>
718 (if J /= Index'Result
719 and then (J < Index'Result) = (Going = Forward)
720 and then (J = From
721 or else (J > From) = (Going = Forward))
722 then (Test = Inside)
723 /= Maps.Is_In (Element (Source, J), Set)))),
724 Global => null;
725 pragma Ada_05 (Index);
726
727 function Index_Non_Blank
728 (Source : Unbounded_String;
729 Going : Direction := Forward) return Natural
730 with
731 Post => Index_Non_Blank'Result <= Length (Source),
732 Contract_Cases =>
733
734 -- If all characters of Source are Space characters, then 0 is
735 -- returned.
736
737 ((for all C of To_String (Source) => C = ' ')
738 =>
739 Index_Non_Blank'Result = 0,
740
741 -- Otherwise, an index in the range of Source is returned
742
743 others
744 =>
745 -- The result is in the range of Source
746
747 Index_Non_Blank'Result in 1 .. Length (Source)
748
749 -- The character at the returned index is not a Space character
750
751 and then Element (Source, Index_Non_Blank'Result) /= ' '
752
753 -- The result is the smallest or largest index which is not a
754 -- Space character, respectively when Going = Forward and Going
755 -- = Backward.
756
757 and then
758 (for all J in 1 .. Length (Source) =>
759 (if J /= Index_Non_Blank'Result
760 and then
761 (J < Index_Non_Blank'Result) = (Going = Forward)
762 then Element (Source, J) = ' '))),
763 Global => null;
764
765 function Index_Non_Blank
766 (Source : Unbounded_String;
767 From : Positive;
768 Going : Direction := Forward) return Natural
769 with
770 Pre =>
771 (if Length (Source) /= 0 then From <= Length (Source)),
772 Post => Index_Non_Blank'Result <= Length (Source),
773 Contract_Cases =>
774
775 -- If Source is the empty string, or all characters of the
776 -- considered slice of Source are Space characters, then 0
777 -- is returned.
778
779 (Length (Source) = 0
780 or else
781 (for all J in 1 .. Length (Source) =>
782 (if J = From or else (J > From) = (Going = Forward) then
783 Element (Source, J) = ' '))
784 =>
785 Index_Non_Blank'Result = 0,
786
787 -- Otherwise, an index in the considered range of Source is
788 -- returned.
789
790 others
791 =>
792 -- The result is in the considered range of Source
793
794 Index_Non_Blank'Result in 1 .. Length (Source)
795 and then
796 (Index_Non_Blank'Result = From
797 or else
798 (Index_Non_Blank'Result > From) = (Going = Forward))
799
800 -- The character at the returned index is not a Space character
801
802 and then Element (Source, Index_Non_Blank'Result) /= ' '
803
804 -- The result is the smallest or largest index which isn't a
805 -- Space character, respectively when Going = Forward and Going
806 -- = Backward.
807
808 and then
809 (for all J in 1 .. Length (Source) =>
810 (if J /= Index_Non_Blank'Result
811 and then
812 (J < Index_Non_Blank'Result) = (Going = Forward)
813 and then (J = From
814 or else (J > From) = (Going = Forward))
815 then Element (Source, J) = ' '))),
816 Global => null;
817 pragma Ada_05 (Index_Non_Blank);
818
819 function Count
820 (Source : Unbounded_String;
821 Pattern : String;
822 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
823 with
824 Pre => Pattern'Length /= 0,
825 Global => null;
826
827 function Count
828 (Source : Unbounded_String;
829 Pattern : String;
830 Mapping : Maps.Character_Mapping_Function) return Natural
831 with
832 Pre => Pattern'Length /= 0 and then Mapping /= null,
833 Global => null;
834
835 function Count
836 (Source : Unbounded_String;
837 Set : Maps.Character_Set) return Natural
838 with
839 Global => null;
840
841 procedure Find_Token
842 (Source : Unbounded_String;
843 Set : Maps.Character_Set;
844 From : Positive;
845 Test : Membership;
846 First : out Positive;
847 Last : out Natural)
848 with
849 Pre =>
850 (if Length (Source) /= 0 then From <= Length (Source)),
851 Contract_Cases =>
852
853 -- If Source is the empty string, or if no character of the
854 -- considered slice of Source satisfies the property Test on
855 -- Set, then First is set to From and Last is set to 0.
856
857 (Length (Source) = 0
858 or else
859 (for all J in From .. Length (Source) =>
860 (Test = Inside) /= Maps.Is_In (Element (Source, J), Set))
861 =>
862 First = From and then Last = 0,
863
864 -- Otherwise, First and Last are set to valid indexes
865
866 others
867 =>
868 -- First and Last are in the considered range of Source
869
870 First in From .. Length (Source)
871 and then Last in First .. Length (Source)
872
873 -- No character between From and First satisfies the property
874 -- Test on Set.
875
876 and then
877 (for all J in From .. First - 1 =>
878 (Test = Inside) /= Maps.Is_In (Element (Source, J), Set))
879
880 -- All characters between First and Last satisfy the property
881 -- Test on Set.
882
883 and then
884 (for all J in First .. Last =>
885 (Test = Inside) = Maps.Is_In (Element (Source, J), Set))
886
887 -- If Last is not Source'Last, then the character at position
888 -- Last + 1 does not satify the property Test on Set.
889
890 and then
891 (if Last < Length (Source)
892 then
893 (Test = Inside)
894 /= Maps.Is_In (Element (Source, Last + 1), Set))),
895 Global => null;
896 pragma Ada_2012 (Find_Token);
897
898 procedure Find_Token
899 (Source : Unbounded_String;
900 Set : Maps.Character_Set;
901 Test : Membership;
902 First : out Positive;
903 Last : out Natural)
904 with
905 Contract_Cases =>
906
907 -- If Source is the empty string, or if no character of the
908 -- considered slice of Source satisfies the property Test on
909 -- Set, then First is set to 1 and Last is set to 0.
910
911 (Length (Source) = 0
912 or else
913 (for all J in 1 .. Length (Source) =>
914 (Test = Inside) /= Maps.Is_In (Element (Source, J), Set))
915 =>
916 First = 1 and then Last = 0,
917
918 -- Otherwise, First and Last are set to valid indexes
919
920 others
921 =>
922 -- First and Last are in the considered range of Source
923
924 First in 1 .. Length (Source)
925 and then Last in First .. Length (Source)
926
927 -- No character between 1 and First satisfies the property Test
928 -- on Set.
929
930 and then
931 (for all J in 1 .. First - 1 =>
932 (Test = Inside) /= Maps.Is_In (Element (Source, J), Set))
933
934 -- All characters between First and Last satisfy the property
935 -- Test on Set.
936
937 and then
938 (for all J in First .. Last =>
939 (Test = Inside) = Maps.Is_In (Element (Source, J), Set))
940
941 -- If Last is not Source'Last, then the character at position
942 -- Last + 1 does not satify the property Test on Set.
943
944 and then
945 (if Last < Length (Source)
946 then
947 (Test = Inside)
948 /= Maps.Is_In (Element (Source, Last + 1), Set))),
949 Global => null;
950
951 -- Each of the search subprograms (Index, Index_Non_Blank, Count,
952 -- Find_Token) has the same effect as the corresponding subprogram in
953 -- Strings.Fixed applied to the string represented by the Unbounded_String
954 -- parameter.
955
956 ------------------------------------
957 -- String Translation Subprograms --
958 ------------------------------------
959
960 function Translate
961 (Source : Unbounded_String;
962 Mapping : Maps.Character_Mapping) return Unbounded_String
963 with
964 Post => Length (Translate'Result) = Length (Source)
965 and then
966 (for all K in 1 .. Length (Source) =>
967 Element (Translate'Result, K) =
968 Ada.Strings.Maps.Value (Mapping, Element (Source, K))),
969 Global => null;
970
971 procedure Translate
972 (Source : in out Unbounded_String;
973 Mapping : Maps.Character_Mapping)
974 with
975 Post => Length (Source) = Length (Source)'Old
976 and then
977 (for all K in 1 .. Length (Source) =>
978 Element (Source, K) =
979 Ada.Strings.Maps.Value (Mapping, To_String (Source)'Old (K))),
980 Global => null;
981
982 function Translate
983 (Source : Unbounded_String;
984 Mapping : Maps.Character_Mapping_Function) return Unbounded_String
985 with
986 Pre => Mapping /= null,
987 Post => Length (Translate'Result) = Length (Source)
988 and then
989 (for all K in 1 .. Length (Source) =>
990 Element (Translate'Result, K) = Mapping (Element (Source, K))),
991 Global => null;
992
993 procedure Translate
994 (Source : in out Unbounded_String;
995 Mapping : Maps.Character_Mapping_Function)
996 with
997 Pre => Mapping /= null,
998 Post => Length (Source) = Length (Source)'Old
999 and then
1000 (for all K in 1 .. Length (Source) =>
1001 Element (Source, K) = Mapping (To_String (Source)'Old (K))),
1002 Global => null;
1003
1004 -- The Translate function has an analogous effect to the corresponding
1005 -- subprogram in Strings.Fixed. The translation is applied to the string
1006 -- represented by the Unbounded_String parameter, and the result is
1007 -- converted (via To_Unbounded_String) to an Unbounded_String.
1008
1009 ---------------------------------------
1010 -- String Transformation Subprograms --
1011 ---------------------------------------
1012
1013 function Replace_Slice
1014 (Source : Unbounded_String;
1015 Low : Positive;
1016 High : Natural;
1017 By : String) return Unbounded_String
1018 with
1019 Pre =>
1020 Low - 1 <= Length (Source)
1021 and then (if High >= Low
1022 then Low - 1
1023 <= Natural'Last - By'Length
1024 - Integer'Max (Length (Source) - High, 0)
1025 else Length (Source) <= Natural'Last - By'Length),
1026 Contract_Cases =>
1027
1028 -- If High >= Low, when considering the application of To_String to
1029 -- convert an Unbounded_String into String, then the returned string
1030 -- comprises Source (Source'First .. Low - 1) & By
1031 -- & Source(High + 1 .. Source'Last).
1032
1033 (High >= Low =>
1034
1035 -- Length of the returned string
1036
1037 Length (Replace_Slice'Result)
1038 = Low - 1 + By'Length + Integer'Max (Length (Source) - High, 0)
1039
1040 -- Elements starting at Low are replaced by elements of By
1041
1042 and then
1043 Slice (Replace_Slice'Result, 1, Low - 1) =
1044 Slice (Source, 1, Low - 1)
1045 and then
1046 Slice (Replace_Slice'Result, Low, Low - 1 + By'Length) = By
1047
1048 -- When there are remaining characters after the replaced slice,
1049 -- they are appended to the result.
1050
1051 and then
1052 (if High < Length (Source)
1053 then
1054 Slice (Replace_Slice'Result,
1055 Low + By'Length, Length (Replace_Slice'Result)) =
1056 Slice (Source, High + 1, Length (Source))),
1057
1058 -- If High < Low, then the returned string is
1059 -- Insert (Source, Before => Low, New_Item => By).
1060
1061 others =>
1062
1063 -- Length of the returned string
1064
1065 Length (Replace_Slice'Result) = Length (Source) + By'Length
1066
1067 -- Elements of By are inserted after the element at Low
1068
1069 and then
1070 Slice (Replace_Slice'Result, 1, Low - 1) =
1071 Slice (Source, 1, Low - 1)
1072 and then
1073 Slice (Replace_Slice'Result, Low, Low - 1 + By'Length) = By
1074
1075 -- When there are remaining characters after Low in Source, they
1076 -- are appended to the result.
1077
1078 and then
1079 (if Low < Length (Source)
1080 then
1081 Slice (Replace_Slice'Result,
1082 Low + By'Length, Length (Replace_Slice'Result)) =
1083 Slice (Source, Low, Length (Source)))),
1084 Global => null;
1085 -- If Low > Source'Last + 1, or High < Source'First - 1, then Index_Error
1086 -- is propagated. Otherwise:
1087 --
1088 -- * If High >= Low, then the returned string comprises
1089 -- Source (Source'First .. Low - 1)
1090 -- & By & Source(High + 1 .. Source'Last), but with lower bound 1.
1091 --
1092 -- * If High < Low, then the returned string is
1093 -- Insert (Source, Before => Low, New_Item => By).
1094
1095 procedure Replace_Slice
1096 (Source : in out Unbounded_String;
1097 Low : Positive;
1098 High : Natural;
1099 By : String)
1100 with
1101 Pre =>
1102 Low - 1 <= Length (Source)
1103 and then (if High >= Low
1104 then Low - 1
1105 <= Natural'Last - By'Length
1106 - Natural'Max (Length (Source) - High, 0)
1107 else Length (Source) <= Natural'Last - By'Length),
1108 Contract_Cases =>
1109
1110 -- If High >= Low, when considering the application of To_String to
1111 -- convert an Unbounded_String into String, then the returned string
1112 -- comprises Source (Source'First .. Low - 1) & By
1113 -- & Source(High + 1 .. Source'Last).
1114
1115 (High >= Low =>
1116
1117 -- Length of the returned string
1118
1119 Length (Source)
1120 = Low - 1 + By'Length + Integer'Max (Length (Source)'Old - High, 0)
1121
1122 -- Elements starting at Low are replaced by elements of By
1123
1124 and then Slice (Source, 1, Low - 1) =
1125 Slice (Source, 1, Low - 1)'Old
1126 and then Slice (Source, Low, Low - 1 + By'Length) = By
1127
1128 -- When there are remaining characters after the replaced slice,
1129 -- they are appended to the result.
1130
1131 and then
1132 (if High < Length (Source)'Old
1133 then Slice (Source, Low + By'Length, Length (Source)) =
1134 Slice (Source,
1135 -- Really "High + 1" but expressed with a conditional
1136 -- repeating the above test so that the call to Slice
1137 -- is valid on entry (under 'Old) even when the test
1138 -- evaluates to False.
1139 (if High < Length (Source) then High + 1 else 1),
1140 Length (Source))'Old),
1141
1142 -- If High < Low, then the returned string is
1143 -- Insert (Source, Before => Low, New_Item => By).
1144
1145 others =>
1146
1147 -- Length of the returned string
1148
1149 Length (Source) = Length (Source)'Old + By'Length
1150
1151 -- Elements of By are inserted after the element at Low
1152
1153 and then Slice (Source, 1, Low - 1) =
1154 Slice (Source, 1, Low - 1)'Old
1155 and then Slice (Source, Low, Low - 1 + By'Length) = By
1156
1157 -- When there are remaining characters after Low in Source, they
1158 -- are appended to the result.
1159
1160 and then
1161 (if Low < Length (Source)'Old
1162 then Slice (Source, Low + By'Length, Length (Source)) =
1163 Slice (Source, Low, Length (Source))'Old)),
1164 Global => null;
1165
1166 function Insert
1167 (Source : Unbounded_String;
1168 Before : Positive;
1169 New_Item : String) return Unbounded_String
1170 with
1171 Pre => Before - 1 <= Length (Source)
1172 and then New_Item'Length <= Natural'Last - Length (Source),
1173 Post =>
1174 -- Length of the returned string
1175
1176 Length (Insert'Result) = Length (Source) + New_Item'Length
1177
1178 -- Elements of New_Item are inserted after element at Before
1179
1180 and then
1181 Slice (Insert'Result, 1, Before - 1) = Slice (Source, 1, Before - 1)
1182 and then
1183 Slice (Insert'Result, Before, Before - 1 + New_Item'Length)
1184 = New_Item
1185
1186 -- When there are remaining characters after Before in Source, they
1187 -- are appended to the returned string.
1188
1189 and then
1190 (if Before <= Length (Source) then
1191 Slice (Insert'Result, Before + New_Item'Length,
1192 Length (Insert'Result)) =
1193 Slice (Source, Before, Length (Source))),
1194 Global => null;
1195 -- Propagates Index_Error if Before is not in
1196 -- Source'First .. Source'Last + 1; otherwise, returns
1197 -- Source (Source'First .. Before - 1)
1198 -- & New_Item & Source(Before..Source'Last), but with lower bound 1.
1199
1200 procedure Insert
1201 (Source : in out Unbounded_String;
1202 Before : Positive;
1203 New_Item : String)
1204 with
1205 Pre => Before - 1 <= Length (Source)
1206 and then New_Item'Length <= Natural'Last - Length (Source),
1207 Post =>
1208 -- Length of the returned string
1209
1210 Length (Source) = Length (Source)'Old + New_Item'Length
1211
1212 -- Elements of New_Item are inserted after element at Before
1213
1214 and then
1215 Slice (Source, 1, Before - 1) = Slice (Source, 1, Before - 1)'Old
1216 and then
1217 Slice (Source, Before, Before - 1 + New_Item'Length) = New_Item
1218
1219 -- When there are remaining characters after Before in Source, they
1220 -- are appended to the returned string.
1221
1222 and then
1223 (if Before <= Length (Source)'Old then
1224 Slice (Source, Before + New_Item'Length, Length (Source)) =
1225 Slice (Source, Before, Length (Source))'Old),
1226 Global => null;
1227
1228 function Overwrite
1229 (Source : Unbounded_String;
1230 Position : Positive;
1231 New_Item : String) return Unbounded_String
1232 with
1233 Pre => Position - 1 <= Length (Source)
1234 and then New_Item'Length <= Natural'Last - (Position - 1),
1235 Post =>
1236 -- Length of the returned string
1237
1238 Length (Overwrite'Result) =
1239 Integer'Max (Length (Source), Position - 1 + New_Item'Length)
1240
1241 -- Elements after Position are replaced by elements of New_Item
1242
1243 and then
1244 Slice (Overwrite'Result, 1, Position - 1) =
1245 Slice (Source, 1, Position - 1)
1246 and then
1247 Slice (Overwrite'Result, Position, Position - 1 + New_Item'Length) =
1248 New_Item
1249 and then
1250 (if Position - 1 + New_Item'Length < Length (Source) then
1251
1252 -- There are some unchanged characters of Source remaining
1253 -- after New_Item.
1254
1255 Slice (Overwrite'Result,
1256 Position + New_Item'Length, Length (Source)) =
1257 Slice (Source,
1258 Position + New_Item'Length, Length (Source))),
1259 Global => null;
1260
1261 procedure Overwrite
1262 (Source : in out Unbounded_String;
1263 Position : Positive;
1264 New_Item : String)
1265 with
1266 Pre => Position - 1 <= Length (Source)
1267 and then New_Item'Length <= Natural'Last - (Position - 1),
1268 Post =>
1269 -- Length of the returned string
1270
1271 Length (Source) =
1272 Integer'Max (Length (Source)'Old, Position - 1 + New_Item'Length)
1273
1274 -- Elements after Position are replaced by elements of New_Item
1275
1276 and then
1277 Slice (Source, 1, Position - 1) = Slice (Source, 1, Position - 1)
1278 and then
1279 Slice (Source, Position, Position - 1 + New_Item'Length) = New_Item
1280 and then
1281 (if Position - 1 + New_Item'Length < Length (Source)'Old then
1282
1283 -- There are some unchanged characters of Source remaining
1284 -- after New_Item.
1285
1286 Slice (Source,
1287 Position + New_Item'Length, Length (Source)'Old) =
1288 Slice (Source,
1289 -- Really "Position + New_Item'Length" but expressed with
1290 -- a conditional repeating the above test so that the
1291 -- call to Slice is valid on entry (under 'Old) even
1292 -- when the test evaluates to False.
1293 (if Position - 1 + New_Item'Length < Length (Source)
1294 then Position + New_Item'Length
1295 else 1),
1296 Length (Source))'Old),
1297 Global => null;
1298
1299 function Delete
1300 (Source : Unbounded_String;
1301 From : Positive;
1302 Through : Natural) return Unbounded_String
1303 with
1304 Pre => (if Through >= From then From - 1 <= Length (Source)),
1305 Contract_Cases =>
1306 (Through >= From =>
1307 Length (Delete'Result) =
1308 From - 1 + Natural'Max (Length (Source) - Through, 0)
1309 and then
1310 Slice (Delete'Result, 1, From - 1) = Slice (Source, 1, From - 1)
1311 and then
1312 (if Through < Length (Source) then
1313 Slice (Delete'Result, From, Length (Delete'Result)) =
1314 Slice (Source, Through + 1, Length (Source))),
1315 others =>
1316 Delete'Result = Source),
1317 Global => null;
1318
1319 procedure Delete
1320 (Source : in out Unbounded_String;
1321 From : Positive;
1322 Through : Natural)
1323 with
1324 Pre => (if Through >= From then From - 1 <= Length (Source)),
1325 Contract_Cases =>
1326 (Through >= From =>
1327 Length (Source) =
1328 From - 1 + Natural'Max (Length (Source)'Old - Through, 0)
1329 and then
1330 Slice (Source, 1, From - 1) =
1331 To_String (Source)'Old (1 .. From - 1)
1332 and then
1333 (if Through < Length (Source) then
1334 Slice (Source, From, Length (Source)) =
1335 To_String (Source)'Old (Through + 1 .. Length (Source)'Old)),
1336 others =>
1337 To_String (Source) = To_String (Source)'Old),
1338 Global => null;
1339
1340 function Trim
1341 (Source : Unbounded_String;
1342 Side : Trim_End) return Unbounded_String
1343 with
1344 Contract_Cases =>
1345 -- If all characters in Source are Space, the returned string is
1346 -- empty.
1347
1348 ((for all C of To_String (Source) => C = ' ')
1349 =>
1350 Length (Trim'Result) = 0,
1351
1352 -- Otherwise, the returned string is a slice of Source
1353
1354 others
1355 =>
1356 (declare
1357 Low : constant Positive :=
1358 (if Side = Right then 1
1359 else Index_Non_Blank (Source, Forward));
1360 High : constant Positive :=
1361 (if Side = Left then Length (Source)
1362 else Index_Non_Blank (Source, Backward));
1363 begin
1364 To_String (Trim'Result) = Slice (Source, Low, High))),
1365 Global => null;
1366
1367 procedure Trim
1368 (Source : in out Unbounded_String;
1369 Side : Trim_End)
1370 with
1371 Contract_Cases =>
1372 -- If all characters in Source are Space, the returned string is
1373 -- empty.
1374
1375 ((for all C of To_String (Source) => C = ' ')
1376 =>
1377 Length (Source) = 0,
1378
1379 -- Otherwise, the returned string is a slice of Source
1380
1381 others
1382 =>
1383 (declare
1384 Low : constant Positive :=
1385 (if Side = Right then 1
1386 else Index_Non_Blank (Source, Forward)'Old);
1387 High : constant Positive :=
1388 (if Side = Left then Length (Source)'Old
1389 else Index_Non_Blank (Source, Backward)'Old);
1390 begin
1391 To_String (Source) = To_String (Source)'Old (Low .. High))),
1392 Global => null;
1393
1394 function Trim
1395 (Source : Unbounded_String;
1396 Left : Maps.Character_Set;
1397 Right : Maps.Character_Set) return Unbounded_String
1398 with
1399 Contract_Cases =>
1400 -- If all characters in Source are contained in one of the sets Left
1401 -- or Right, then the returned string is empty.
1402
1403 ((for all C of To_String (Source) => Maps.Is_In (C, Left))
1404 or else
1405 (for all C of To_String (Source) => Maps.Is_In (C, Right))
1406 =>
1407 Length (Trim'Result) = 0,
1408
1409 -- Otherwise, the returned string is a slice of Source
1410
1411 others
1412 =>
1413 (declare
1414 Low : constant Positive :=
1415 Index (Source, Left, Outside, Forward);
1416 High : constant Positive :=
1417 Index (Source, Right, Outside, Backward);
1418 begin
1419 To_String (Trim'Result) = Slice (Source, Low, High))),
1420 Global => null;
1421
1422 procedure Trim
1423 (Source : in out Unbounded_String;
1424 Left : Maps.Character_Set;
1425 Right : Maps.Character_Set)
1426 with
1427 Contract_Cases =>
1428 -- If all characters in Source are contained in one of the sets Left
1429 -- or Right, then the returned string is empty.
1430
1431 ((for all C of To_String (Source) => Maps.Is_In (C, Left))
1432 or else
1433 (for all C of To_String (Source) => Maps.Is_In (C, Right))
1434 =>
1435 Length (Source) = 0,
1436
1437 -- Otherwise, the returned string is a slice of Source
1438
1439 others
1440 =>
1441 (declare
1442 Low : constant Positive :=
1443 Index (Source, Left, Outside, Forward)'Old;
1444 High : constant Positive :=
1445 Index (Source, Right, Outside, Backward)'Old;
1446 begin
1447 To_String (Source) = To_String (Source)'Old (Low .. High))),
1448 Global => null;
1449
1450 function Head
1451 (Source : Unbounded_String;
1452 Count : Natural;
1453 Pad : Character := Space) return Unbounded_String
1454 with
1455 Post => Length (Head'Result) = Count,
1456 Contract_Cases =>
1457 (Count <= Length (Source)
1458 =>
1459 -- Source is cut
1460
1461 To_String (Head'Result) = Slice (Source, 1, Count),
1462
1463 others
1464 =>
1465 -- Source is followed by Pad characters
1466
1467 Slice (Head'Result, 1, Length (Source)) = To_String (Source)
1468 and then
1469 Slice (Head'Result, Length (Source) + 1, Count) =
1470 [1 .. Count - Length (Source) => Pad]),
1471 Global => null;
1472
1473 procedure Head
1474 (Source : in out Unbounded_String;
1475 Count : Natural;
1476 Pad : Character := Space)
1477 with
1478 Post => Length (Source) = Count,
1479 Contract_Cases =>
1480 (Count <= Length (Source)
1481 =>
1482 -- Source is cut
1483
1484 To_String (Source) = Slice (Source, 1, Count),
1485
1486 others
1487 =>
1488 -- Source is followed by Pad characters
1489
1490 Slice (Source, 1, Length (Source)'Old) = To_String (Source)'Old
1491 and then
1492 Slice (Source, Length (Source)'Old + 1, Count) =
1493 [1 .. Count - Length (Source)'Old => Pad]),
1494 Global => null;
1495
1496 function Tail
1497 (Source : Unbounded_String;
1498 Count : Natural;
1499 Pad : Character := Space) return Unbounded_String
1500 with
1501 Post => Length (Tail'Result) = Count,
1502 Contract_Cases =>
1503 (Count = 0
1504 =>
1505 True,
1506
1507 (Count in 1 .. Length (Source))
1508 =>
1509 -- Source is cut
1510
1511 To_String (Tail'Result) =
1512 Slice (Source, Length (Source) - Count + 1, Length (Source)),
1513
1514 others
1515 =>
1516 -- Source is preceded by Pad characters
1517
1518 (if Length (Source) = 0 then
1519 To_String (Tail'Result) = [1 .. Count => Pad]
1520 else
1521 Slice (Tail'Result, 1, Count - Length (Source)) =
1522 [1 .. Count - Length (Source) => Pad]
1523 and then
1524 Slice (Tail'Result, Count - Length (Source) + 1, Count) =
1525 To_String (Source))),
1526 Global => null;
1527
1528 procedure Tail
1529 (Source : in out Unbounded_String;
1530 Count : Natural;
1531 Pad : Character := Space)
1532 with
1533 Post => Length (Source) = Count,
1534 Contract_Cases =>
1535 (Count = 0
1536 =>
1537 True,
1538
1539 (Count in 1 .. Length (Source))
1540 =>
1541 -- Source is cut
1542
1543 To_String (Source) =
1544 Slice (Source,
1545 -- Really "Length (Source) - Count + 1" but expressed with a
1546 -- conditional repeating the above guard so that the call to
1547 -- Slice is valid on entry (under 'Old) even when the test
1548 -- evaluates to False.
1549 (if Count <= Length (Source) then Length (Source) - Count + 1
1550 else 1),
1551 Length (Source))'Old,
1552
1553 others
1554 =>
1555 -- Source is preceded by Pad characters
1556
1557 (if Length (Source)'Old = 0 then
1558 To_String (Source) = [1 .. Count => Pad]
1559 else
1560 Slice (Source, 1, Count - Length (Source)'Old) =
1561 [1 .. Count - Length (Source)'Old => Pad]
1562 and then
1563 Slice (Source, Count - Length (Source)'Old + 1, Count) =
1564 To_String (Source)'Old)),
1565 Global => null;
1566
1567 function "*"
1568 (Left : Natural;
1569 Right : Character) return Unbounded_String
1570 with
1571 Pre => Left <= Natural'Last,
1572 Post => To_String ("*"'Result) = [1 .. Left => Right],
1573 Global => null;
1574
1575 function "*"
1576 (Left : Natural;
1577 Right : String) return Unbounded_String
1578 with
1579 Pre => (if Left /= 0 then Right'Length <= Natural'Last / Left),
1580 Post =>
1581 Length ("*"'Result) = Left * Right'Length
1582 and then
1583 (if Right'Length > 0 then
1584 (for all K in 1 .. Left * Right'Length =>
1585 Element ("*"'Result, K) =
1586 Right (Right'First + (K - 1) mod Right'Length))),
1587 Global => null;
1588
1589 function "*"
1590 (Left : Natural;
1591 Right : Unbounded_String) return Unbounded_String
1592 with
1593 Pre => (if Left /= 0 then Length (Right) <= Natural'Last / Left),
1594 Post =>
1595 Length ("*"'Result) = Left * Length (Right)
1596 and then
1597 (if Length (Right) > 0 then
1598 (for all K in 1 .. Left * Length (Right) =>
1599 Element ("*"'Result, K) =
1600 Element (Right, 1 + (K - 1) mod Length (Right)))),
1601 Global => null;
1602
1603 -- Each of the transformation functions (Replace_Slice, Insert, Overwrite,
1604 -- Delete), selector functions (Trim, Head, Tail), and constructor
1605 -- functions ("*") is likewise analogous to its corresponding subprogram in
1606 -- Strings.Fixed. For each of the subprograms, the corresponding
1607 -- fixed-length string subprogram is applied to the string represented by
1608 -- the Unbounded_String parameter, and To_Unbounded_String is applied the
1609 -- result string.
1610 --
1611 -- For each of the procedures Translate, Replace_Slice, Insert, Overwrite,
1612 -- Delete, Trim, Head, and Tail, the resulting string represented by the
1613 -- Source parameter is given by the corresponding function for fixed-length
1614 -- strings applied to the string represented by Source's original value.
1615
1616 private
1617 pragma SPARK_Mode (Off); -- Controlled types are not in SPARK
1618
1619 pragma Inline (Length);
1620
1621 package AF renames Ada.Finalization;
1622
1623 Null_String : aliased String := "";
1624
1625 function To_Unbounded (S : String) return Unbounded_String
1626 renames To_Unbounded_String;
1627
1628 type Unbounded_String is new AF.Controlled with record
1629 Reference : not null String_Access := Null_String'Access;
1630 Last : Natural := 0;
1631 end record with Put_Image => Put_Image;
1632
1633 procedure Put_Image
1634 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class;
1635 V : Unbounded_String);
1636
1637 -- The Unbounded_String is using a buffered implementation to increase
1638 -- speed of the Append/Delete/Insert procedures. The Reference string
1639 -- pointer above contains the current string value and extra room at the
1640 -- end to be used by the next Append routine. Last is the index of the
1641 -- string ending character. So the current string value is really
1642 -- Reference (1 .. Last).
1643
1644 pragma Stream_Convert (Unbounded_String, To_Unbounded, To_String);
1645 -- Provide stream routines without dragging in Ada.Streams
1646
1647 pragma Finalize_Storage_Only (Unbounded_String);
1648 -- Finalization is required only for freeing storage
1649
1650 procedure Initialize (Object : in out Unbounded_String);
1651 procedure Adjust (Object : in out Unbounded_String);
1652 procedure Finalize (Object : in out Unbounded_String);
1653
1654 procedure Realloc_For_Chunk
1655 (Source : in out Unbounded_String;
1656 Chunk_Size : Natural);
1657 pragma Inline (Realloc_For_Chunk);
1658 -- Adjust the size allocated for the string. Add at least Chunk_Size so it
1659 -- is safe to add a string of this size at the end of the current content.
1660 -- The real size allocated for the string is Chunk_Size + x of the current
1661 -- string size. This buffered handling makes the Append unbounded string
1662 -- routines very fast. This spec is in the private part so that it can be
1663 -- accessed from children (e.g. from Unbounded.Text_IO).
1664
1665 Null_Unbounded_String : constant Unbounded_String :=
1666 (AF.Controlled with
1667 Reference => Null_String'Access,
1668 Last => 0);
1669 end Ada.Strings.Unbounded;