aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/libgnat/a-strfix.adb
blob: 2da5367985b8c44c12574d2e3e7ef8155a2b464a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT RUN-TIME COMPONENTS                         --
--                                                                          --
--                    A D A . S T R I N G S . F I X E D                     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 1992-2024, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

--  Note: This code is derived from the ADAR.CSH public domain Ada 83 versions
--  of the Appendix C string handling packages. One change is to avoid the use
--  of Is_In, so that we are not dependent on inlining. Note that the search
--  function implementations are to be found in the auxiliary package
--  Ada.Strings.Search. Also the Move procedure is directly incorporated (ADAR
--  used a subunit for this procedure). The number of errors having to do with
--  bounds of function return results were also fixed, and use of & removed for
--  efficiency reasons.

--  Ghost code, loop invariants and assertions in this unit are meant for
--  analysis only, not for run-time checking, as it would be too costly
--  otherwise. This is enforced by setting the assertion policy to Ignore.

pragma Assertion_Policy (Ghost          => Ignore,
                         Loop_Invariant => Ignore,
                         Assert         => Ignore);

with Ada.Strings.Maps; use Ada.Strings.Maps;

package body Ada.Strings.Fixed with SPARK_Mode is

   ------------------------
   -- Search Subprograms --
   ------------------------

   function Index
     (Source  : String;
      Pattern : String;
      Going   : Direction := Forward;
      Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
   renames Ada.Strings.Search.Index;

   function Index
     (Source  : String;
      Pattern : String;
      Going   : Direction := Forward;
      Mapping : Maps.Character_Mapping_Function) return Natural
   renames Ada.Strings.Search.Index;

   function Index
     (Source : String;
      Set    : Maps.Character_Set;
      Test   : Membership := Inside;
      Going  : Direction  := Forward) return Natural
   renames Ada.Strings.Search.Index;

   function Index
     (Source  : String;
      Pattern : String;
      From    : Positive;
      Going   : Direction := Forward;
      Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
   renames Ada.Strings.Search.Index;

   function Index
     (Source  : String;
      Pattern : String;
      From    : Positive;
      Going   : Direction := Forward;
      Mapping : Maps.Character_Mapping_Function) return Natural
   renames Ada.Strings.Search.Index;

   function Index
     (Source  : String;
      Set     : Maps.Character_Set;
      From    : Positive;
      Test    : Membership := Inside;
      Going   : Direction := Forward) return Natural
   renames Ada.Strings.Search.Index;

   function Index_Non_Blank
     (Source : String;
      Going  : Direction := Forward) return Natural
   renames Ada.Strings.Search.Index_Non_Blank;

   function Index_Non_Blank
     (Source : String;
      From   : Positive;
      Going  : Direction := Forward) return Natural
   renames Ada.Strings.Search.Index_Non_Blank;

   function Count
     (Source  : String;
      Pattern : String;
      Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
   renames Ada.Strings.Search.Count;

   function Count
     (Source  : String;
      Pattern : String;
      Mapping : Maps.Character_Mapping_Function) return Natural
   renames Ada.Strings.Search.Count;

   function Count
     (Source : String;
      Set    : Maps.Character_Set) return Natural
   renames Ada.Strings.Search.Count;

   procedure Find_Token
     (Source : String;
      Set    : Maps.Character_Set;
      From   : Positive;
      Test   : Membership;
      First  : out Positive;
      Last   : out Natural)
   renames Ada.Strings.Search.Find_Token;

   procedure Find_Token
     (Source : String;
      Set    : Maps.Character_Set;
      Test   : Membership;
      First  : out Positive;
      Last   : out Natural)
   renames Ada.Strings.Search.Find_Token;

   ---------
   -- "*" --
   ---------

   function "*"
     (Left  : Natural;
      Right : Character) return String
   is
   begin
      return Result : String (1 .. Left) with Relaxed_Initialization do
         for J in Result'Range loop
            Result (J) := Right;
            pragma Loop_Invariant
              (for all K in 1 .. J =>
                 Result (K)'Initialized and then Result (K) = Right);
         end loop;
      end return;
   end "*";

   function "*"
     (Left  : Natural;
      Right : String) return String
   is
      Ptr : Integer := 0;

      --  Parts of the proof involving manipulations with the modulo operator
      --  are complicated for the prover and can't be done automatically in
      --  the global subprogram. That's why we isolate them in these two ghost
      --  lemmas.

      procedure Lemma_Mod (K : Integer) with
        Ghost,
        Pre =>
          Right'Length /= 0
          and then Ptr mod Right'Length = 0
          and then Ptr in 0 .. Natural'Last - Right'Length
          and then K in Ptr .. Ptr + Right'Length - 1,
        Post => K mod Right'Length = K - Ptr;
      --  Lemma_Mod is applied to an index considered in Lemma_Split to prove
      --  that it has the right value modulo Right'Length.

      procedure Lemma_Split (Result : String) with
        Ghost,
        Relaxed_Initialization => Result,
        Pre                    =>
          Right'Length /= 0
            and then Result'First = 1
            and then Result'Last >= 0
            and then Ptr mod Right'Length = 0
            and then Ptr in 0 .. Result'Last - Right'Length
            and then Result (Result'First .. Ptr + Right'Length)'Initialized
            and then Result (Ptr + 1 .. Ptr + Right'Length) = Right,
        Post                   =>
          (for all K in Ptr + 1 .. Ptr + Right'Length =>
            Result (K) = Right (Right'First + (K - 1) mod Right'Length));
      --  Lemma_Split is used after Result (Ptr + 1 .. Ptr + Right'Length) is
      --  updated to Right and concludes that the characters match for each
      --  index when taken modulo Right'Length, as the considered slice starts
      --  at index 1 modulo Right'Length.

      ---------------
      -- Lemma_Mod --
      ---------------

      procedure Lemma_Mod (K : Integer) is null;

      -----------------
      -- Lemma_Split --
      -----------------

      procedure Lemma_Split (Result : String)
      is
      begin
         for K in Ptr + 1 .. Ptr + Right'Length loop
            Lemma_Mod (K - 1);
            pragma Loop_Invariant
              (for all J in Ptr + 1 .. K =>
                 Result (J) = Right (Right'First + (J - 1) mod Right'Length));
         end loop;
      end Lemma_Split;

   --  Start of processing for "*"

   begin
      if Right'Length = 0 then
         return "";
      end if;

      return Result : String (1 .. Left * Right'Length)
        with Relaxed_Initialization
      do
         for J in 1 .. Left loop
            Result (Ptr + 1 .. Ptr + Right'Length) := Right;
            Lemma_Split (Result);
            Ptr := Ptr + Right'Length;
            pragma Loop_Invariant (Ptr = J * Right'Length);
            pragma Loop_Invariant (Result (1 .. Ptr)'Initialized);
            pragma Loop_Invariant
              (for all K in 1 .. Ptr =>
                 Result (K) = Right (Right'First + (K - 1) mod Right'Length));
         end loop;
      end return;
   end "*";

   ------------
   -- Delete --
   ------------

   function Delete
     (Source  : String;
      From    : Positive;
      Through : Natural) return String
   is
   begin
      if From > Through then
         declare
            subtype Result_Type is String (1 .. Source'Length);

         begin
            return Result_Type (Source);
         end;

      else
         declare
            Front_Len     : constant Integer :=
              Integer'Max (0, From - Source'First);
            --  Length of prefix of Source copied to result

            Back_Len      : constant Integer :=
              Integer'Max (0, Source'Last - Through);
            --  Length of suffix of Source copied to result

            Result_Length : constant Integer := Front_Len + Back_Len;
            --  Length of result
         begin
            return Result : String (1 .. Result_Length)
              with Relaxed_Initialization
            do
               Result (1 .. Front_Len) :=
                 Source (Source'First .. From - 1);

               if Through < Source'Last then
                  Result (Front_Len + 1 .. Result'Last) :=
                    Source (Through + 1 .. Source'Last);
               end if;
            end return;
         end;
      end if;
   end Delete;

   procedure Delete
     (Source  : in out String;
      From    : Positive;
      Through : Natural;
      Justify : Alignment := Left;
      Pad     : Character := Space)
   is
   begin
      Move (Source  => Delete (Source, From, Through),
            Target  => Source,
            Justify => Justify,
            Pad     => Pad);
   end Delete;

   ----------
   -- Head --
   ----------

   function Head
     (Source : String;
      Count  : Natural;
      Pad    : Character := Space) return String
   is
      subtype Result_Type is String (1 .. Count);

   begin
      if Count <= Source'Length then
         return
           Result_Type (Source (Source'First .. Source'First + (Count - 1)));

      else
         return Result : Result_Type with Relaxed_Initialization do
            Result (1 .. Source'Length) := Source;

            for J in Source'Length + 1 .. Count loop
               Result (J) := Pad;
               pragma Loop_Invariant
                 (for all K in Source'Length + 1 .. J =>
                    Result (K)'Initialized and then Result (K) = Pad);
            end loop;
         end return;
      end if;
   end Head;

   procedure Head
     (Source  : in out String;
      Count   : Natural;
      Justify : Alignment := Left;
      Pad     : Character := Space)
   is
   begin
      Move (Source  => Head (Source, Count, Pad),
            Target  => Source,
            Drop    => Error,
            Justify => Justify,
            Pad     => Pad);
   end Head;

   ------------
   -- Insert --
   ------------

   function Insert
     (Source   : String;
      Before   : Positive;
      New_Item : String) return String
   is
      Front : constant Integer := Before - Source'First;

   begin
      if Before - 1 not in Source'First - 1 .. Source'Last then
         raise Index_Error;
      end if;

      return Result : String (1 .. Source'Length + New_Item'Length)
        with Relaxed_Initialization
      do
         Result (1 .. Front) :=
           Source (Source'First .. Before - 1);
         Result (Front + 1 .. Front + New_Item'Length) :=
           New_Item;

         pragma Assert
           (Result (1 .. Before - Source'First)
            = Source (Source'First .. Before - 1));
         pragma Assert
           (Result
              (Before - Source'First + 1
               .. Before - Source'First + New_Item'Length)
            = New_Item);

         if Before <= Source'Last then
            Result (Front + New_Item'Length + 1 .. Result'Last) :=
              Source (Before .. Source'Last);
         end if;

         pragma Assert
           (Result (1 .. Before - Source'First)
            = Source (Source'First .. Before - 1));
      end return;
   end Insert;

   procedure Insert
     (Source   : in out String;
      Before   : Positive;
      New_Item : String;
      Drop     : Truncation := Error)
   is
   begin
      Move (Source => Insert (Source, Before, New_Item),
            Target => Source,
            Drop   => Drop);
   end Insert;

   ----------
   -- Move --
   ----------

   procedure Move
     (Source  : String;
      Target  : out String;
      Drop    : Truncation := Error;
      Justify : Alignment  := Left;
      Pad     : Character  := Space)
   with SPARK_Mode => Off
   is
      Sfirst  : constant Integer := Source'First;
      Slast   : constant Integer := Source'Last;
      Slength : constant Integer := Source'Length;

      Tfirst  : constant Integer := Target'First;
      Tlast   : constant Integer := Target'Last;
      Tlength : constant Integer := Target'Length;

      function Is_Padding (Item : String) return Boolean;
      --  Check if Item is all Pad characters, return True if so, False if not

      function Is_Padding (Item : String) return Boolean is
      begin
         for J in Item'Range loop
            if Item (J) /= Pad then
               return False;
            end if;
         end loop;

         return True;
      end Is_Padding;

   --  Start of processing for Move

   begin
      if Slength = Tlength then
         Target := Source;

      elsif Slength > Tlength then
         case Drop is
            when Left =>
               Target := Source (Slast - Tlength + 1 .. Slast);

            when Right =>
               Target := Source (Sfirst .. Sfirst + Tlength - 1);

            when Error =>
               case Justify is
                  when Left =>
                     if Is_Padding (Source (Sfirst + Tlength .. Slast)) then
                        Target :=
                          Source (Sfirst .. Sfirst + Target'Length - 1);
                     else
                        raise Length_Error;
                     end if;

                  when Right =>
                     if Is_Padding (Source (Sfirst .. Slast - Tlength)) then
                        Target := Source (Slast - Tlength + 1 .. Slast);
                     else
                        raise Length_Error;
                     end if;

                  when Center =>
                     raise Length_Error;
               end case;
         end case;

      --  Source'Length < Target'Length

      else
         case Justify is
            when Left =>
               Target (Tfirst .. Tfirst + Slength - 1) := Source;

               for I in Tfirst + Slength .. Tlast loop
                  Target (I) := Pad;
               end loop;

            when Right =>
               for I in Tfirst .. Tlast - Slength loop
                  Target (I) := Pad;
               end loop;

               Target (Tlast - Slength + 1 .. Tlast) := Source;

            when Center =>
               declare
                  Front_Pad   : constant Integer := (Tlength - Slength) / 2;
                  Tfirst_Fpad : constant Integer := Tfirst + Front_Pad;

               begin
                  for I in Tfirst .. Tfirst_Fpad - 1 loop
                     Target (I) := Pad;
                  end loop;

                  Target (Tfirst_Fpad .. Tfirst_Fpad + Slength - 1) := Source;

                  for I in Tfirst_Fpad + Slength .. Tlast loop
                     Target (I) := Pad;
                  end loop;
               end;
         end case;
      end if;
   end Move;

   ---------------
   -- Overwrite --
   ---------------

   function Overwrite
     (Source   : String;
      Position : Positive;
      New_Item : String) return String is
   begin
      if Position - 1 not in Source'First - 1 .. Source'Last then
         raise Index_Error;
      end if;

      declare
         Result_Length : constant Natural :=
           Integer'Max (Source'Length,
                        Position - Source'First + New_Item'Length);
         Front         : constant Integer := Position - Source'First;

      begin
         return Result : String (1 .. Result_Length)
           with Relaxed_Initialization
         do
            Result (1 .. Front) := Source (Source'First .. Position - 1);
            pragma Assert
              (Result (1 .. Position - Source'First)
               = Source (Source'First .. Position - 1));
            Result (Front + 1 .. Front + New_Item'Length) := New_Item;
            pragma Assert
              (Result
                 (Position - Source'First + 1
                  .. Position - Source'First + New_Item'Length)
               = New_Item);

            if Position <= Source'Last - New_Item'Length then
               Result (Front + New_Item'Length + 1 .. Result'Last) :=
                 Source (Position + New_Item'Length .. Source'Last);

               pragma Assert
                 (Result
                    (Position - Source'First + New_Item'Length + 1
                     .. Result'Last)
                  = Source (Position + New_Item'Length .. Source'Last));
            end if;

            pragma Assert
              (if Position <= Source'Last - New_Item'Length
               then
                  Result
                    (Position - Source'First + New_Item'Length + 1
                     .. Result'Last)
                  = Source (Position + New_Item'Length .. Source'Last));
         end return;
      end;
   end Overwrite;

   procedure Overwrite
     (Source   : in out String;
      Position : Positive;
      New_Item : String;
      Drop     : Truncation := Right)
   is
   begin
      Move (Source => Overwrite (Source, Position, New_Item),
            Target => Source,
            Drop   => Drop);
   end Overwrite;

   -------------------
   -- Replace_Slice --
   -------------------

   function Replace_Slice
     (Source : String;
      Low    : Positive;
      High   : Natural;
      By     : String) return String is
   begin
      if Low - 1 > Source'Last or else High < Source'First - 1 then
         raise Index_Error;
      end if;

      if High >= Low then
         declare
            Front_Len : constant Integer :=
              Integer'Max (0, Low - Source'First);
            --  Length of prefix of Source copied to result

            Back_Len : constant Integer := Integer'Max (0, Source'Last - High);
            --  Length of suffix of Source copied to result

            Result_Length : constant Integer :=
              Front_Len + By'Length + Back_Len;
            --  Length of result

         begin
            return Result : String (1 .. Result_Length)
              with Relaxed_Initialization do
               Result (1 .. Front_Len) := Source (Source'First .. Low - 1);
               pragma Assert
                 (Result (1 .. Integer'Max (0, Low - Source'First))
                  = Source (Source'First .. Low - 1));
               Result (Front_Len + 1 .. Front_Len + By'Length) := By;
               pragma Assert
                 (Result
                    (Integer'Max (0, Low - Source'First) + 1
                     .. Integer'Max (0, Low - Source'First) + By'Length)
                  = By);

               if High < Source'Last then
                  Result (Front_Len + By'Length + 1 .. Result'Last) :=
                    Source (High + 1 .. Source'Last);
               end if;

               pragma Assert
                 (Result (1 .. Integer'Max (0, Low - Source'First))
                  = Source (Source'First .. Low - 1));
               pragma Assert
                 (Result
                    (Integer'Max (0, Low - Source'First) + 1
                     .. Integer'Max (0, Low - Source'First) + By'Length)
                  = By);
               pragma Assert
                 (if High < Source'Last
                  then
                     Result
                    (Integer'Max (0, Low - Source'First) + By'Length + 1
                     .. Result'Last)
                  = Source (High + 1 .. Source'Last));
            end return;
         end;
      else
         return Insert (Source, Before => Low, New_Item => By);
      end if;
   end Replace_Slice;

   procedure Replace_Slice
     (Source   : in out String;
      Low      : Positive;
      High     : Natural;
      By       : String;
      Drop     : Truncation := Error;
      Justify  : Alignment  := Left;
      Pad      : Character  := Space)
   is
   begin
      Move (Replace_Slice (Source, Low, High, By), Source, Drop, Justify, Pad);
   end Replace_Slice;

   ----------
   -- Tail --
   ----------

   function Tail
     (Source : String;
      Count  : Natural;
      Pad    : Character := Space) return String
   is
      subtype Result_Type is String (1 .. Count);

   begin
      if Count = 0 then
         return "";

      elsif Count < Source'Length then
         return Result_Type (Source (Source'Last - Count + 1 .. Source'Last));

      --  Pad on left

      else
         return Result : Result_Type with Relaxed_Initialization do
            for J in 1 .. Count - Source'Length loop
               Result (J) := Pad;
               pragma Loop_Invariant
                 (for all K in 1 .. J =>
                    Result (K)'Initialized and then Result (K) = Pad);
            end loop;

            if Source'Length /= 0 then
               Result (Count - Source'Length + 1 .. Count) := Source;
            end if;
         end return;
      end if;
   end Tail;

   procedure Tail
     (Source  : in out String;
      Count   : Natural;
      Justify : Alignment := Left;
      Pad     : Character := Space)
   is
   begin
      Move (Source  => Tail (Source, Count, Pad),
            Target  => Source,
            Drop    => Error,
            Justify => Justify,
            Pad     => Pad);
   end Tail;

   ---------------
   -- Translate --
   ---------------

   function Translate
     (Source  : String;
      Mapping : Maps.Character_Mapping) return String
   is
   begin
      return Result : String (1 .. Source'Length)
        with Relaxed_Initialization
      do
         for J in Source'Range loop
            Result (J - (Source'First - 1)) := Value (Mapping, Source (J));
            pragma Loop_Invariant
              (for all K in Source'First .. J =>
                 Result (K - (Source'First - 1))'Initialized);
            pragma Loop_Invariant
              (for all K in Source'First .. J =>
                 Result (K - (Source'First - 1)) =
                   Value (Mapping, Source (K)));
         end loop;
      end return;
   end Translate;

   procedure Translate
     (Source  : in out String;
      Mapping : Maps.Character_Mapping)
   is
   begin
      for J in Source'Range loop
         Source (J) := Value (Mapping, Source (J));
         pragma Loop_Invariant
           (for all K in Source'First .. J =>
              Source (K) = Value (Mapping, Source'Loop_Entry (K)));
      end loop;
   end Translate;

   function Translate
     (Source  : String;
      Mapping : Maps.Character_Mapping_Function) return String
   is
      pragma Unsuppress (Access_Check);
   begin
      return Result : String (1 .. Source'Length)
        with Relaxed_Initialization
      do
         for J in Source'Range loop
            Result (J - (Source'First - 1)) := Mapping.all (Source (J));
            pragma Annotate (GNATprove, False_Positive,
                             "call via access-to-subprogram",
                             "function Mapping must always terminate");
            pragma Loop_Invariant
              (for all K in Source'First .. J =>
                 Result (K - (Source'First - 1))'Initialized);
            pragma Loop_Invariant
              (for all K in Source'First .. J =>
                 Result (K - (Source'First - 1)) = Mapping (Source (K)));
            pragma Annotate (GNATprove, False_Positive,
                             "call via access-to-subprogram",
                             "function Mapping must always terminate");
         end loop;
      end return;
   end Translate;

   procedure Translate
     (Source  : in out String;
      Mapping : Maps.Character_Mapping_Function)
   is
      pragma Unsuppress (Access_Check);
   begin
      for J in Source'Range loop
         Source (J) := Mapping.all (Source (J));
         pragma Annotate (GNATprove, False_Positive,
                          "call via access-to-subprogram",
                          "function Mapping must always terminate");
         pragma Loop_Invariant
           (for all K in Source'First .. J =>
              Source (K) = Mapping (Source'Loop_Entry (K)));
         pragma Annotate (GNATprove, False_Positive,
                          "call via access-to-subprogram",
                          "function Mapping must always terminate");
      end loop;
   end Translate;

   ----------
   -- Trim --
   ----------

   function Trim
     (Source : String;
      Side   : Trim_End) return String
   is
      Empty_String : constant String (1 .. 0) := "";
      --  Without declaring the empty string as a separate string starting
      --  at 1, SPARK provers have trouble proving the postcondition.
   begin
      case Side is
         when Strings.Left =>
            declare
               Low : constant Natural := Index_Non_Blank (Source, Forward);
            begin
               --  All blanks case

               if Low = 0 then
                  return Empty_String;
               end if;

               declare
                  subtype Result_Type is String (1 .. Source'Last - Low + 1);
               begin
                  return Result_Type (Source (Low .. Source'Last));
               end;
            end;

         when Strings.Right =>
            declare
               High : constant Natural := Index_Non_Blank (Source, Backward);
            begin
               --  All blanks case

               if High = 0 then
                  return Empty_String;
               end if;

               declare
                  subtype Result_Type is String (1 .. High - Source'First + 1);
               begin
                  return Result_Type (Source (Source'First .. High));
               end;
            end;

         when Strings.Both =>
            declare
               Low : constant Natural := Index_Non_Blank (Source, Forward);
            begin
               --  All blanks case

               if Low = 0 then
                  return Empty_String;
               end if;

               declare
                  High : constant Natural :=
                    Index_Non_Blank (Source, Backward);
                  subtype Result_Type is String (1 .. High - Low + 1);
               begin
                  return Result_Type (Source (Low .. High));
               end;
            end;
      end case;
   end Trim;

   procedure Trim
     (Source  : in out String;
      Side    : Trim_End;
      Justify : Alignment := Left;
      Pad     : Character := Space)
   is
   begin
      Move (Trim (Source, Side),
            Source,
            Justify => Justify,
            Pad => Pad);
   end Trim;

   function Trim
     (Source : String;
      Left   : Maps.Character_Set;
      Right  : Maps.Character_Set) return String
   is
      High, Low : Integer;

   begin
      Low := Index (Source, Set => Left, Test => Outside, Going => Forward);

      --  Case where source comprises only characters in Left

      if Low = 0 then
         return "";
      end if;

      High := Index (Source, Set => Right, Test => Outside, Going => Backward);

      --  Case where source comprises only characters in Right

      if High = 0 then
         return "";
      end if;

      declare
         Result_Length : constant Integer := High - Low + 1;
         subtype Result_Type is String (1 .. Result_Length);

      begin
         return Result_Type (Source (Low .. High));
      end;
   end Trim;

   procedure Trim
     (Source  : in out String;
      Left    : Maps.Character_Set;
      Right   : Maps.Character_Set;
      Justify : Alignment := Strings.Left;
      Pad     : Character := Space)
   is
   begin
      Move (Source  => Trim (Source, Left, Right),
            Target  => Source,
            Justify => Justify,
            Pad     => Pad);
   end Trim;

end Ada.Strings.Fixed;