Debian Codegraph @

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