Debian Codegraph @

gcc/ada/libgnat/a-strfix.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . S T R I N G S . F I X E D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- Note: This code is derived from the ADAR.CSH public domain Ada 83 versions
33 -- of the Appendix C string handling packages. One change is to avoid the use
34 -- of Is_In, so that we are not dependent on inlining. Note that the search
35 -- function implementations are to be found in the auxiliary package
36 -- Ada.Strings.Search. Also the Move procedure is directly incorporated (ADAR
37 -- used a subunit for this procedure). The number of errors having to do with
38 -- bounds of function return results were also fixed, and use of & removed for
39 -- efficiency reasons.
40
41 -- Ghost code, loop invariants and assertions in this unit are meant for
42 -- analysis only, not for run-time checking, as it would be too costly
43 -- otherwise. This is enforced by setting the assertion policy to Ignore.
44
45 pragma Assertion_Policy (Ghost => Ignore,
46 Loop_Invariant => Ignore,
47 Assert => Ignore);
48
49 with Ada.Strings.Maps; use Ada.Strings.Maps;
50
51 package body Ada.Strings.Fixed with SPARK_Mode is
52
53 ------------------------
54 -- Search Subprograms --
55 ------------------------
56
57 function Index
58 (Source : String;
59 Pattern : String;
60 Going : Direction := Forward;
61 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
62 renames Ada.Strings.Search.Index;
63
64 function Index
65 (Source : String;
66 Pattern : String;
67 Going : Direction := Forward;
68 Mapping : Maps.Character_Mapping_Function) return Natural
69 renames Ada.Strings.Search.Index;
70
71 function Index
72 (Source : String;
73 Set : Maps.Character_Set;
74 Test : Membership := Inside;
75 Going : Direction := Forward) return Natural
76 renames Ada.Strings.Search.Index;
77
78 function Index
79 (Source : String;
80 Pattern : String;
81 From : Positive;
82 Going : Direction := Forward;
83 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
84 renames Ada.Strings.Search.Index;
85
86 function Index
87 (Source : String;
88 Pattern : String;
89 From : Positive;
90 Going : Direction := Forward;
91 Mapping : Maps.Character_Mapping_Function) return Natural
92 renames Ada.Strings.Search.Index;
93
94 function Index
95 (Source : String;
96 Set : Maps.Character_Set;
97 From : Positive;
98 Test : Membership := Inside;
99 Going : Direction := Forward) return Natural
100 renames Ada.Strings.Search.Index;
101
102 function Index_Non_Blank
103 (Source : String;
104 Going : Direction := Forward) return Natural
105 renames Ada.Strings.Search.Index_Non_Blank;
106
107 function Index_Non_Blank
108 (Source : String;
109 From : Positive;
110 Going : Direction := Forward) return Natural
111 renames Ada.Strings.Search.Index_Non_Blank;
112
113 function Count
114 (Source : String;
115 Pattern : String;
116 Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
117 renames Ada.Strings.Search.Count;
118
119 function Count
120 (Source : String;
121 Pattern : String;
122 Mapping : Maps.Character_Mapping_Function) return Natural
123 renames Ada.Strings.Search.Count;
124
125 function Count
126 (Source : String;
127 Set : Maps.Character_Set) return Natural
128 renames Ada.Strings.Search.Count;
129
130 procedure Find_Token
131 (Source : String;
132 Set : Maps.Character_Set;
133 From : Positive;
134 Test : Membership;
135 First : out Positive;
136 Last : out Natural)
137 renames Ada.Strings.Search.Find_Token;
138
139 procedure Find_Token
140 (Source : String;
141 Set : Maps.Character_Set;
142 Test : Membership;
143 First : out Positive;
144 Last : out Natural)
145 renames Ada.Strings.Search.Find_Token;
146
147 ---------
148 -- "*" --
149 ---------
150
151 function "*"
152 (Left : Natural;
153 Right : Character) return String
154 is
155 begin
156 return Result : String (1 .. Left) with Relaxed_Initialization do
157 for J in Result'Range loop
158 Result (J) := Right;
159 pragma Loop_Invariant
160 (for all K in 1 .. J =>
161 Result (K)'Initialized and then Result (K) = Right);
162 end loop;
163 end return;
164 end "*";
165
166 function "*"
167 (Left : Natural;
168 Right : String) return String
169 is
170 Ptr : Integer := 0;
171
172 -- Parts of the proof involving manipulations with the modulo operator
173 -- are complicated for the prover and can't be done automatically in
174 -- the global subprogram. That's why we isolate them in these two ghost
175 -- lemmas.
176
177 procedure Lemma_Mod (K : Integer) with
178 Ghost,
179 Pre =>
180 Right'Length /= 0
181 and then Ptr mod Right'Length = 0
182 and then Ptr in 0 .. Natural'Last - Right'Length
183 and then K in Ptr .. Ptr + Right'Length - 1,
184 Post => K mod Right'Length = K - Ptr;
185 -- Lemma_Mod is applied to an index considered in Lemma_Split to prove
186 -- that it has the right value modulo Right'Length.
187
188 procedure Lemma_Split (Result : String) with
189 Ghost,
190 Relaxed_Initialization => Result,
191 Pre =>
192 Right'Length /= 0
193 and then Result'First = 1
194 and then Result'Last >= 0
195 and then Ptr mod Right'Length = 0
196 and then Ptr in 0 .. Result'Last - Right'Length
197 and then Result (Result'First .. Ptr + Right'Length)'Initialized
198 and then Result (Ptr + 1 .. Ptr + Right'Length) = Right,
199 Post =>
200 (for all K in Ptr + 1 .. Ptr + Right'Length =>
201 Result (K) = Right (Right'First + (K - 1) mod Right'Length));
202 -- Lemma_Split is used after Result (Ptr + 1 .. Ptr + Right'Length) is
203 -- updated to Right and concludes that the characters match for each
204 -- index when taken modulo Right'Length, as the considered slice starts
205 -- at index 1 modulo Right'Length.
206
207 ---------------
208 -- Lemma_Mod --
209 ---------------
210
211 procedure Lemma_Mod (K : Integer) is null;
212
213 -----------------
214 -- Lemma_Split --
215 -----------------
216
217 procedure Lemma_Split (Result : String)
218 is
219 begin
220 for K in Ptr + 1 .. Ptr + Right'Length loop
221 Lemma_Mod (K - 1);
222 pragma Loop_Invariant
223 (for all J in Ptr + 1 .. K =>
224 Result (J) = Right (Right'First + (J - 1) mod Right'Length));
225 end loop;
226 end Lemma_Split;
227
228 -- Start of processing for "*"
229
230 begin
231 if Right'Length = 0 then
232 return "";
233 end if;
234
235 return Result : String (1 .. Left * Right'Length)
236 with Relaxed_Initialization
237 do
238 for J in 1 .. Left loop
239 Result (Ptr + 1 .. Ptr + Right'Length) := Right;
240 Lemma_Split (Result);
241 Ptr := Ptr + Right'Length;
242 pragma Loop_Invariant (Ptr = J * Right'Length);
243 pragma Loop_Invariant (Result (1 .. Ptr)'Initialized);
244 pragma Loop_Invariant
245 (for all K in 1 .. Ptr =>
246 Result (K) = Right (Right'First + (K - 1) mod Right'Length));
247 end loop;
248 end return;
249 end "*";
250
251 ------------
252 -- Delete --
253 ------------
254
255 function Delete
256 (Source : String;
257 From : Positive;
258 Through : Natural) return String
259 is
260 begin
261 if From > Through then
262 declare
263 subtype Result_Type is String (1 .. Source'Length);
264
265 begin
266 return Result_Type (Source);
267 end;
268
269 else
270 declare
271 Front_Len : constant Integer :=
272 Integer'Max (0, From - Source'First);
273 -- Length of prefix of Source copied to result
274
275 Back_Len : constant Integer :=
276 Integer'Max (0, Source'Last - Through);
277 -- Length of suffix of Source copied to result
278
279 Result_Length : constant Integer := Front_Len + Back_Len;
280 -- Length of result
281 begin
282 return Result : String (1 .. Result_Length)
283 with Relaxed_Initialization
284 do
285 Result (1 .. Front_Len) :=
286 Source (Source'First .. From - 1);
287
288 if Through < Source'Last then
289 Result (Front_Len + 1 .. Result'Last) :=
290 Source (Through + 1 .. Source'Last);
291 end if;
292 end return;
293 end;
294 end if;
295 end Delete;
296
297 procedure Delete
298 (Source : in out String;
299 From : Positive;
300 Through : Natural;
301 Justify : Alignment := Left;
302 Pad : Character := Space)
303 is
304 begin
305 Move (Source => Delete (Source, From, Through),
306 Target => Source,
307 Justify => Justify,
308 Pad => Pad);
309 end Delete;
310
311 ----------
312 -- Head --
313 ----------
314
315 function Head
316 (Source : String;
317 Count : Natural;
318 Pad : Character := Space) return String
319 is
320 subtype Result_Type is String (1 .. Count);
321
322 begin
323 if Count <= Source'Length then
324 return
325 Result_Type (Source (Source'First .. Source'First + (Count - 1)));
326
327 else
328 return Result : Result_Type with Relaxed_Initialization do
329 Result (1 .. Source'Length) := Source;
330
331 for J in Source'Length + 1 .. Count loop
332 Result (J) := Pad;
333 pragma Loop_Invariant
334 (for all K in Source'Length + 1 .. J =>
335 Result (K)'Initialized and then Result (K) = Pad);
336 end loop;
337 end return;
338 end if;
339 end Head;
340
341 procedure Head
342 (Source : in out String;
343 Count : Natural;
344 Justify : Alignment := Left;
345 Pad : Character := Space)
346 is
347 begin
348 Move (Source => Head (Source, Count, Pad),
349 Target => Source,
350 Drop => Error,
351 Justify => Justify,
352 Pad => Pad);
353 end Head;
354
355 ------------
356 -- Insert --
357 ------------
358
359 function Insert
360 (Source : String;
361 Before : Positive;
362 New_Item : String) return String
363 is
364 Front : constant Integer := Before - Source'First;
365
366 begin
367 if Before - 1 not in Source'First - 1 .. Source'Last then
368 raise Index_Error;
369 end if;
370
371 return Result : String (1 .. Source'Length + New_Item'Length)
372 with Relaxed_Initialization
373 do
374 Result (1 .. Front) :=
375 Source (Source'First .. Before - 1);
376 Result (Front + 1 .. Front + New_Item'Length) :=
377 New_Item;
378
379 pragma Assert
380 (Result (1 .. Before - Source'First)
381 = Source (Source'First .. Before - 1));
382 pragma Assert
383 (Result
384 (Before - Source'First + 1
385 .. Before - Source'First + New_Item'Length)
386 = New_Item);
387
388 if Before <= Source'Last then
389 Result (Front + New_Item'Length + 1 .. Result'Last) :=
390 Source (Before .. Source'Last);
391 end if;
392
393 pragma Assert
394 (Result (1 .. Before - Source'First)
395 = Source (Source'First .. Before - 1));
396 end return;
397 end Insert;
398
399 procedure Insert
400 (Source : in out String;
401 Before : Positive;
402 New_Item : String;
403 Drop : Truncation := Error)
404 is
405 begin
406 Move (Source => Insert (Source, Before, New_Item),
407 Target => Source,
408 Drop => Drop);
409 end Insert;
410
411 ----------
412 -- Move --
413 ----------
414
415 procedure Move
416 (Source : String;
417 Target : out String;
418 Drop : Truncation := Error;
419 Justify : Alignment := Left;
420 Pad : Character := Space)
421 with SPARK_Mode => Off
422 is
423 Sfirst : constant Integer := Source'First;
424 Slast : constant Integer := Source'Last;
425 Slength : constant Integer := Source'Length;
426
427 Tfirst : constant Integer := Target'First;
428 Tlast : constant Integer := Target'Last;
429 Tlength : constant Integer := Target'Length;
430
431 function Is_Padding (Item : String) return Boolean;
432 -- Check if Item is all Pad characters, return True if so, False if not
433
434 function Is_Padding (Item : String) return Boolean is
435 begin
436 for J in Item'Range loop
437 if Item (J) /= Pad then
438 return False;
439 end if;
440 end loop;
441
442 return True;
443 end Is_Padding;
444
445 -- Start of processing for Move
446
447 begin
448 if Slength = Tlength then
449 Target := Source;
450
451 elsif Slength > Tlength then
452 case Drop is
453 when Left =>
454 Target := Source (Slast - Tlength + 1 .. Slast);
455
456 when Right =>
457 Target := Source (Sfirst .. Sfirst + Tlength - 1);
458
459 when Error =>
460 case Justify is
461 when Left =>
462 if Is_Padding (Source (Sfirst + Tlength .. Slast)) then
463 Target :=
464 Source (Sfirst .. Sfirst + Target'Length - 1);
465 else
466 raise Length_Error;
467 end if;
468
469 when Right =>
470 if Is_Padding (Source (Sfirst .. Slast - Tlength)) then
471 Target := Source (Slast - Tlength + 1 .. Slast);
472 else
473 raise Length_Error;
474 end if;
475
476 when Center =>
477 raise Length_Error;
478 end case;
479 end case;
480
481 -- Source'Length < Target'Length
482
483 else
484 case Justify is
485 when Left =>
486 Target (Tfirst .. Tfirst + Slength - 1) := Source;
487
488 for I in Tfirst + Slength .. Tlast loop
489 Target (I) := Pad;
490 end loop;
491
492 when Right =>
493 for I in Tfirst .. Tlast - Slength loop
494 Target (I) := Pad;
495 end loop;
496
497 Target (Tlast - Slength + 1 .. Tlast) := Source;
498
499 when Center =>
500 declare
501 Front_Pad : constant Integer := (Tlength - Slength) / 2;
502 Tfirst_Fpad : constant Integer := Tfirst + Front_Pad;
503
504 begin
505 for I in Tfirst .. Tfirst_Fpad - 1 loop
506 Target (I) := Pad;
507 end loop;
508
509 Target (Tfirst_Fpad .. Tfirst_Fpad + Slength - 1) := Source;
510
511 for I in Tfirst_Fpad + Slength .. Tlast loop
512 Target (I) := Pad;
513 end loop;
514 end;
515 end case;
516 end if;
517 end Move;
518
519 ---------------
520 -- Overwrite --
521 ---------------
522
523 function Overwrite
524 (Source : String;
525 Position : Positive;
526 New_Item : String) return String is
527 begin
528 if Position - 1 not in Source'First - 1 .. Source'Last then
529 raise Index_Error;
530 end if;
531
532 declare
533 Result_Length : constant Natural :=
534 Integer'Max (Source'Length,
535 Position - Source'First + New_Item'Length);
536 Front : constant Integer := Position - Source'First;
537
538 begin
539 return Result : String (1 .. Result_Length)
540 with Relaxed_Initialization
541 do
542 Result (1 .. Front) := Source (Source'First .. Position - 1);
543 pragma Assert
544 (Result (1 .. Position - Source'First)
545 = Source (Source'First .. Position - 1));
546 Result (Front + 1 .. Front + New_Item'Length) := New_Item;
547 pragma Assert
548 (Result
549 (Position - Source'First + 1
550 .. Position - Source'First + New_Item'Length)
551 = New_Item);
552
553 if Position <= Source'Last - New_Item'Length then
554 Result (Front + New_Item'Length + 1 .. Result'Last) :=
555 Source (Position + New_Item'Length .. Source'Last);
556
557 pragma Assert
558 (Result
559 (Position - Source'First + New_Item'Length + 1
560 .. Result'Last)
561 = Source (Position + New_Item'Length .. Source'Last));
562 end if;
563
564 pragma Assert
565 (if Position <= Source'Last - New_Item'Length
566 then
567 Result
568 (Position - Source'First + New_Item'Length + 1
569 .. Result'Last)
570 = Source (Position + New_Item'Length .. Source'Last));
571 end return;
572 end;
573 end Overwrite;
574
575 procedure Overwrite
576 (Source : in out String;
577 Position : Positive;
578 New_Item : String;
579 Drop : Truncation := Right)
580 is
581 begin
582 Move (Source => Overwrite (Source, Position, New_Item),
583 Target => Source,
584 Drop => Drop);
585 end Overwrite;
586
587 -------------------
588 -- Replace_Slice --
589 -------------------
590
591 function Replace_Slice
592 (Source : String;
593 Low : Positive;
594 High : Natural;
595 By : String) return String is
596 begin
597 if Low - 1 > Source'Last or else High < Source'First - 1 then
598 raise Index_Error;
599 end if;
600
601 if High >= Low then
602 declare
603 Front_Len : constant Integer :=
604 Integer'Max (0, Low - Source'First);
605 -- Length of prefix of Source copied to result
606
607 Back_Len : constant Integer := Integer'Max (0, Source'Last - High);
608 -- Length of suffix of Source copied to result
609
610 Result_Length : constant Integer :=
611 Front_Len + By'Length + Back_Len;
612 -- Length of result
613
614 begin
615 return Result : String (1 .. Result_Length)
616 with Relaxed_Initialization do
617 Result (1 .. Front_Len) := Source (Source'First .. Low - 1);
618 pragma Assert
619 (Result (1 .. Integer'Max (0, Low - Source'First))
620 = Source (Source'First .. Low - 1));
621 Result (Front_Len + 1 .. Front_Len + By'Length) := By;
622 pragma Assert
623 (Result
624 (Integer'Max (0, Low - Source'First) + 1
625 .. Integer'Max (0, Low - Source'First) + By'Length)
626 = By);
627
628 if High < Source'Last then
629 Result (Front_Len + By'Length + 1 .. Result'Last) :=
630 Source (High + 1 .. Source'Last);
631 end if;
632
633 pragma Assert
634 (Result (1 .. Integer'Max (0, Low - Source'First))
635 = Source (Source'First .. Low - 1));
636 pragma Assert
637 (Result
638 (Integer'Max (0, Low - Source'First) + 1
639 .. Integer'Max (0, Low - Source'First) + By'Length)
640 = By);
641 pragma Assert
642 (if High < Source'Last
643 then
644 Result
645 (Integer'Max (0, Low - Source'First) + By'Length + 1
646 .. Result'Last)
647 = Source (High + 1 .. Source'Last));
648 end return;
649 end;
650 else
651 return Insert (Source, Before => Low, New_Item => By);
652 end if;
653 end Replace_Slice;
654
655 procedure Replace_Slice
656 (Source : in out String;
657 Low : Positive;
658 High : Natural;
659 By : String;
660 Drop : Truncation := Error;
661 Justify : Alignment := Left;
662 Pad : Character := Space)
663 is
664 begin
665 Move (Replace_Slice (Source, Low, High, By), Source, Drop, Justify, Pad);
666 end Replace_Slice;
667
668 ----------
669 -- Tail --
670 ----------
671
672 function Tail
673 (Source : String;
674 Count : Natural;
675 Pad : Character := Space) return String
676 is
677 subtype Result_Type is String (1 .. Count);
678
679 begin
680 if Count = 0 then
681 return "";
682
683 elsif Count < Source'Length then
684 return Result_Type (Source (Source'Last - Count + 1 .. Source'Last));
685
686 -- Pad on left
687
688 else
689 return Result : Result_Type with Relaxed_Initialization do
690 for J in 1 .. Count - Source'Length loop
691 Result (J) := Pad;
692 pragma Loop_Invariant
693 (for all K in 1 .. J =>
694 Result (K)'Initialized and then Result (K) = Pad);
695 end loop;
696
697 if Source'Length /= 0 then
698 Result (Count - Source'Length + 1 .. Count) := Source;
699 end if;
700 end return;
701 end if;
702 end Tail;
703
704 procedure Tail
705 (Source : in out String;
706 Count : Natural;
707 Justify : Alignment := Left;
708 Pad : Character := Space)
709 is
710 begin
711 Move (Source => Tail (Source, Count, Pad),
712 Target => Source,
713 Drop => Error,
714 Justify => Justify,
715 Pad => Pad);
716 end Tail;
717
718 ---------------
719 -- Translate --
720 ---------------
721
722 function Translate
723 (Source : String;
724 Mapping : Maps.Character_Mapping) return String
725 is
726 begin
727 return Result : String (1 .. Source'Length)
728 with Relaxed_Initialization
729 do
730 for J in Source'Range loop
731 Result (J - (Source'First - 1)) := Value (Mapping, Source (J));
732 pragma Loop_Invariant
733 (for all K in Source'First .. J =>
734 Result (K - (Source'First - 1))'Initialized);
735 pragma Loop_Invariant
736 (for all K in Source'First .. J =>
737 Result (K - (Source'First - 1)) =
738 Value (Mapping, Source (K)));
739 end loop;
740 end return;
741 end Translate;
742
743 procedure Translate
744 (Source : in out String;
745 Mapping : Maps.Character_Mapping)
746 is
747 begin
748 for J in Source'Range loop
749 Source (J) := Value (Mapping, Source (J));
750 pragma Loop_Invariant
751 (for all K in Source'First .. J =>
752 Source (K) = Value (Mapping, Source'Loop_Entry (K)));
753 end loop;
754 end Translate;
755
756 function Translate
757 (Source : String;
758 Mapping : Maps.Character_Mapping_Function) return String
759 is
760 pragma Unsuppress (Access_Check);
761 begin
762 return Result : String (1 .. Source'Length)
763 with Relaxed_Initialization
764 do
765 for J in Source'Range loop
766 Result (J - (Source'First - 1)) := Mapping.all (Source (J));
767 pragma Annotate (GNATprove, False_Positive,
768 "call via access-to-subprogram",
769 "function Mapping must always terminate");
770 pragma Loop_Invariant
771 (for all K in Source'First .. J =>
772 Result (K - (Source'First - 1))'Initialized);
773 pragma Loop_Invariant
774 (for all K in Source'First .. J =>
775 Result (K - (Source'First - 1)) = Mapping (Source (K)));
776 pragma Annotate (GNATprove, False_Positive,
777 "call via access-to-subprogram",
778 "function Mapping must always terminate");
779 end loop;
780 end return;
781 end Translate;
782
783 procedure Translate
784 (Source : in out String;
785 Mapping : Maps.Character_Mapping_Function)
786 is
787 pragma Unsuppress (Access_Check);
788 begin
789 for J in Source'Range loop
790 Source (J) := Mapping.all (Source (J));
791 pragma Annotate (GNATprove, False_Positive,
792 "call via access-to-subprogram",
793 "function Mapping must always terminate");
794 pragma Loop_Invariant
795 (for all K in Source'First .. J =>
796 Source (K) = Mapping (Source'Loop_Entry (K)));
797 pragma Annotate (GNATprove, False_Positive,
798 "call via access-to-subprogram",
799 "function Mapping must always terminate");
800 end loop;
801 end Translate;
802
803 ----------
804 -- Trim --
805 ----------
806
807 function Trim
808 (Source : String;
809 Side : Trim_End) return String
810 is
811 Empty_String : constant String (1 .. 0) := "";
812 -- Without declaring the empty string as a separate string starting
813 -- at 1, SPARK provers have trouble proving the postcondition.
814 begin
815 case Side is
816 when Strings.Left =>
817 declare
818 Low : constant Natural := Index_Non_Blank (Source, Forward);
819 begin
820 -- All blanks case
821
822 if Low = 0 then
823 return Empty_String;
824 end if;
825
826 declare
827 subtype Result_Type is String (1 .. Source'Last - Low + 1);
828 begin
829 return Result_Type (Source (Low .. Source'Last));
830 end;
831 end;
832
833 when Strings.Right =>
834 declare
835 High : constant Natural := Index_Non_Blank (Source, Backward);
836 begin
837 -- All blanks case
838
839 if High = 0 then
840 return Empty_String;
841 end if;
842
843 declare
844 subtype Result_Type is String (1 .. High - Source'First + 1);
845 begin
846 return Result_Type (Source (Source'First .. High));
847 end;
848 end;
849
850 when Strings.Both =>
851 declare
852 Low : constant Natural := Index_Non_Blank (Source, Forward);
853 begin
854 -- All blanks case
855
856 if Low = 0 then
857 return Empty_String;
858 end if;
859
860 declare
861 High : constant Natural :=
862 Index_Non_Blank (Source, Backward);
863 subtype Result_Type is String (1 .. High - Low + 1);
864 begin
865 return Result_Type (Source (Low .. High));
866 end;
867 end;
868 end case;
869 end Trim;
870
871 procedure Trim
872 (Source : in out String;
873 Side : Trim_End;
874 Justify : Alignment := Left;
875 Pad : Character := Space)
876 is
877 begin
878 Move (Trim (Source, Side),
879 Source,
880 Justify => Justify,
881 Pad => Pad);
882 end Trim;
883
884 function Trim
885 (Source : String;
886 Left : Maps.Character_Set;
887 Right : Maps.Character_Set) return String
888 is
889 High, Low : Integer;
890
891 begin
892 Low := Index (Source, Set => Left, Test => Outside, Going => Forward);
893
894 -- Case where source comprises only characters in Left
895
896 if Low = 0 then
897 return "";
898 end if;
899
900 High := Index (Source, Set => Right, Test => Outside, Going => Backward);
901
902 -- Case where source comprises only characters in Right
903
904 if High = 0 then
905 return "";
906 end if;
907
908 declare
909 Result_Length : constant Integer := High - Low + 1;
910 subtype Result_Type is String (1 .. Result_Length);
911
912 begin
913 return Result_Type (Source (Low .. High));
914 end;
915 end Trim;
916
917 procedure Trim
918 (Source : in out String;
919 Left : Maps.Character_Set;
920 Right : Maps.Character_Set;
921 Justify : Alignment := Strings.Left;
922 Pad : Character := Space)
923 is
924 begin
925 Move (Source => Trim (Source, Left, Right),
926 Target => Source,
927 Justify => Justify,
928 Pad => Pad);
929 end Trim;
930
931 end Ada.Strings.Fixed;