OSDN Git Service

* env.c [__alpha__ && __osf__] (AES_SOURCE): Define.
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-wtedit.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                        GNAT RUN-TIME COMPONENTS                          --
4 --                                                                          --
5 --             A D A . W I D E _ T E X T _ I O . E D I T I N G              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2009, 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 with Ada.Strings.Fixed;
33 with Ada.Strings.Wide_Fixed;
34
35 package body Ada.Wide_Text_IO.Editing is
36
37    package Strings            renames Ada.Strings;
38    package Strings_Fixed      renames Ada.Strings.Fixed;
39    package Strings_Wide_Fixed renames Ada.Strings.Wide_Fixed;
40    package Wide_Text_IO       renames Ada.Wide_Text_IO;
41
42    -----------------------
43    -- Local_Subprograms --
44    -----------------------
45
46    function To_Wide (C : Character) return Wide_Character;
47    pragma Inline (To_Wide);
48    --  Convert Character to corresponding Wide_Character
49
50    ---------------------
51    -- Blank_When_Zero --
52    ---------------------
53
54    function Blank_When_Zero (Pic : Picture) return Boolean is
55    begin
56       return Pic.Contents.Original_BWZ;
57    end Blank_When_Zero;
58
59    --------------------
60    -- Decimal_Output --
61    --------------------
62
63    package body Decimal_Output is
64
65       -----------
66       -- Image --
67       -----------
68
69       function Image
70         (Item       : Num;
71          Pic        : Picture;
72          Currency   : Wide_String    := Default_Currency;
73          Fill       : Wide_Character := Default_Fill;
74          Separator  : Wide_Character := Default_Separator;
75          Radix_Mark : Wide_Character := Default_Radix_Mark) return Wide_String
76       is
77       begin
78          return Format_Number
79             (Pic.Contents, Num'Image (Item),
80              Currency, Fill, Separator, Radix_Mark);
81       end Image;
82
83       ------------
84       -- Length --
85       ------------
86
87       function Length
88         (Pic      : Picture;
89          Currency : Wide_String := Default_Currency) return Natural
90       is
91          Picstr     : constant String := Pic_String (Pic);
92          V_Adjust   : Integer := 0;
93          Cur_Adjust : Integer := 0;
94
95       begin
96          --  Check if Picstr has 'V' or '$'
97
98          --  If 'V', then length is 1 less than otherwise
99
100          --  If '$', then length is Currency'Length-1 more than otherwise
101
102          --  This should use the string handling package ???
103
104          for J in Picstr'Range loop
105             if Picstr (J) = 'V' then
106                V_Adjust := -1;
107
108             elsif Picstr (J) = '$' then
109                Cur_Adjust := Currency'Length - 1;
110             end if;
111          end loop;
112
113          return Picstr'Length - V_Adjust + Cur_Adjust;
114       end Length;
115
116       ---------
117       -- Put --
118       ---------
119
120       procedure Put
121         (File       : Wide_Text_IO.File_Type;
122          Item       : Num;
123          Pic        : Picture;
124          Currency   : Wide_String    := Default_Currency;
125          Fill       : Wide_Character := Default_Fill;
126          Separator  : Wide_Character := Default_Separator;
127          Radix_Mark : Wide_Character := Default_Radix_Mark)
128       is
129       begin
130          Wide_Text_IO.Put (File, Image (Item, Pic,
131                                    Currency, Fill, Separator, Radix_Mark));
132       end Put;
133
134       procedure Put
135         (Item       : Num;
136          Pic        : Picture;
137          Currency   : Wide_String    := Default_Currency;
138          Fill       : Wide_Character := Default_Fill;
139          Separator  : Wide_Character := Default_Separator;
140          Radix_Mark : Wide_Character := Default_Radix_Mark)
141       is
142       begin
143          Wide_Text_IO.Put (Image (Item, Pic,
144                              Currency, Fill, Separator, Radix_Mark));
145       end Put;
146
147       procedure Put
148         (To         : out Wide_String;
149          Item       : Num;
150          Pic        : Picture;
151          Currency   : Wide_String    := Default_Currency;
152          Fill       : Wide_Character := Default_Fill;
153          Separator  : Wide_Character := Default_Separator;
154          Radix_Mark : Wide_Character := Default_Radix_Mark)
155       is
156          Result : constant Wide_String :=
157            Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
158
159       begin
160          if Result'Length > To'Length then
161             raise Wide_Text_IO.Layout_Error;
162          else
163             Strings_Wide_Fixed.Move (Source => Result, Target => To,
164                                      Justify => Strings.Right);
165          end if;
166       end Put;
167
168       -----------
169       -- Valid --
170       -----------
171
172       function Valid
173         (Item     : Num;
174          Pic      : Picture;
175          Currency : Wide_String := Default_Currency) return Boolean
176       is
177       begin
178          declare
179             Temp : constant Wide_String := Image (Item, Pic, Currency);
180             pragma Warnings (Off, Temp);
181          begin
182             return True;
183          end;
184
185       exception
186          when Layout_Error => return False;
187
188       end Valid;
189    end Decimal_Output;
190
191    ------------
192    -- Expand --
193    ------------
194
195    function Expand (Picture : String) return String is
196       Result        : String (1 .. MAX_PICSIZE);
197       Picture_Index : Integer := Picture'First;
198       Result_Index  : Integer := Result'First;
199       Count         : Natural;
200       Last          : Integer;
201
202    begin
203       if Picture'Length < 1 then
204          raise Picture_Error;
205       end if;
206
207       if Picture (Picture'First) = '(' then
208          raise Picture_Error;
209       end if;
210
211       loop
212          case Picture (Picture_Index) is
213
214             when '(' =>
215
216                --  We now need to scan out the count after a left paren. In
217                --  the non-wide version we used Integer_IO.Get, but that is
218                --  not convenient here, since we don't want to drag in normal
219                --  Text_IO just for this purpose. So we do the scan ourselves,
220                --  with the normal validity checks.
221
222                Last := Picture_Index + 1;
223                Count := 0;
224
225                if Picture (Last) not in '0' .. '9' then
226                   raise Picture_Error;
227                end if;
228
229                Count := Character'Pos (Picture (Last)) - Character'Pos ('0');
230                Last := Last + 1;
231
232                loop
233                   if Last > Picture'Last then
234                      raise Picture_Error;
235                   end if;
236
237                   if Picture (Last) = '_' then
238                      if Picture (Last - 1) = '_' then
239                         raise Picture_Error;
240                      end if;
241
242                   elsif Picture (Last) = ')' then
243                      exit;
244
245                   elsif Picture (Last) not in '0' .. '9' then
246                      raise Picture_Error;
247
248                   else
249                      Count := Count * 10
250                                 +  Character'Pos (Picture (Last)) -
251                                    Character'Pos ('0');
252                   end if;
253
254                   Last := Last + 1;
255                end loop;
256
257                --  In what follows note that one copy of the repeated
258                --  character has already been made, so a count of one is
259                --  no-op, and a count of zero erases a character.
260
261                for J in 2 .. Count loop
262                   Result (Result_Index + J - 2) := Picture (Picture_Index - 1);
263                end loop;
264
265                Result_Index := Result_Index + Count - 1;
266
267                --  Last was a ')' throw it away too
268
269                Picture_Index := Last + 1;
270
271             when ')' =>
272                raise Picture_Error;
273
274             when others =>
275                Result (Result_Index) := Picture (Picture_Index);
276                Picture_Index := Picture_Index + 1;
277                Result_Index := Result_Index + 1;
278
279          end case;
280
281          exit when Picture_Index > Picture'Last;
282       end loop;
283
284       return Result (1 .. Result_Index - 1);
285
286    exception
287       when others =>
288          raise Picture_Error;
289    end Expand;
290
291    -------------------
292    -- Format_Number --
293    -------------------
294
295    function Format_Number
296      (Pic                 : Format_Record;
297       Number              : String;
298       Currency_Symbol     : Wide_String;
299       Fill_Character      : Wide_Character;
300       Separator_Character : Wide_Character;
301       Radix_Point         : Wide_Character) return Wide_String
302    is
303       Attrs    : Number_Attributes := Parse_Number_String (Number);
304       Position : Integer;
305       Rounded  : String := Number;
306
307       Sign_Position : Integer := Pic.Sign_Position; --  may float.
308
309       Answer       : Wide_String (1 .. Pic.Picture.Length);
310       Last         : Integer;
311       Currency_Pos : Integer := Pic.Start_Currency;
312
313       Dollar : Boolean := False;
314       --  Overridden immediately if necessary
315
316       Zero : Boolean := True;
317       --  Set to False when a non-zero digit is output
318
319    begin
320
321       --  If the picture has fewer decimal places than the number, the image
322       --  must be rounded according to the usual rules.
323
324       if Attrs.Has_Fraction then
325          declare
326             R : constant Integer :=
327               (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
328                 - Pic.Max_Trailing_Digits;
329             R_Pos : Integer;
330
331          begin
332             if R > 0 then
333                R_Pos := Rounded'Length - R;
334
335                if Rounded (R_Pos + 1) > '4' then
336
337                   if Rounded (R_Pos) = '.' then
338                      R_Pos := R_Pos - 1;
339                   end if;
340
341                   if Rounded (R_Pos) /= '9' then
342                      Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
343                   else
344                      Rounded (R_Pos) := '0';
345                      R_Pos := R_Pos - 1;
346
347                      while R_Pos > 1 loop
348                         if Rounded (R_Pos) = '.' then
349                            R_Pos := R_Pos - 1;
350                         end if;
351
352                         if Rounded (R_Pos) /= '9' then
353                            Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
354                            exit;
355                         else
356                            Rounded (R_Pos) := '0';
357                            R_Pos := R_Pos - 1;
358                         end if;
359                      end loop;
360
361                      --  The rounding may add a digit in front. Either the
362                      --  leading blank or the sign (already captured) can be
363                      --  overwritten.
364
365                      if R_Pos = 1 then
366                         Rounded (R_Pos) := '1';
367                         Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
368                      end if;
369                   end if;
370                end if;
371             end if;
372          end;
373       end if;
374
375       for J in Answer'Range loop
376          Answer (J) := To_Wide (Pic.Picture.Expanded (J));
377       end loop;
378
379       if Pic.Start_Currency /= Invalid_Position then
380          Dollar := Answer (Pic.Start_Currency) = '$';
381       end if;
382
383       --  Fix up "direct inserts" outside the playing field. Set up as one
384       --  loop to do the beginning, one (reverse) loop to do the end.
385
386       Last := 1;
387       loop
388          exit when Last = Pic.Start_Float;
389          exit when Last = Pic.Radix_Position;
390          exit when Answer (Last) = '9';
391
392          case Answer (Last) is
393
394             when '_' =>
395                Answer (Last) := Separator_Character;
396
397             when 'b' =>
398                Answer (Last) := ' ';
399
400             when others =>
401                null;
402
403          end case;
404
405          exit when Last = Answer'Last;
406
407          Last := Last + 1;
408       end loop;
409
410       --  Now for the end...
411
412       for J in reverse Last .. Answer'Last loop
413          exit when J = Pic.Radix_Position;
414
415          --  Do this test First, Separator_Character can equal Pic.Floater
416
417          if Answer (J) = Pic.Floater then
418             exit;
419          end if;
420
421          case Answer (J) is
422
423             when '_' =>
424                Answer (J) := Separator_Character;
425
426             when 'b' =>
427                Answer (J) := ' ';
428
429             when '9' =>
430                exit;
431
432             when others =>
433                null;
434
435          end case;
436       end loop;
437
438       --  Non-floating sign
439
440       if Pic.Start_Currency /= -1
441         and then Answer (Pic.Start_Currency) = '#'
442         and then Pic.Floater /= '#'
443       then
444          if Currency_Symbol'Length >
445             Pic.End_Currency - Pic.Start_Currency + 1
446          then
447             raise Picture_Error;
448
449          elsif Currency_Symbol'Length =
450             Pic.End_Currency - Pic.Start_Currency + 1
451          then
452             Answer (Pic.Start_Currency .. Pic.End_Currency) :=
453               Currency_Symbol;
454
455          elsif Pic.Radix_Position = Invalid_Position
456            or else Pic.Start_Currency < Pic.Radix_Position
457          then
458             Answer (Pic.Start_Currency .. Pic.End_Currency) :=
459                                                         (others => ' ');
460             Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
461                     Pic.End_Currency) := Currency_Symbol;
462
463          else
464             Answer (Pic.Start_Currency .. Pic.End_Currency) :=
465                                                         (others => ' ');
466             Answer (Pic.Start_Currency ..
467                     Pic.Start_Currency + Currency_Symbol'Length - 1) :=
468                                                         Currency_Symbol;
469          end if;
470       end if;
471
472       --  Fill in leading digits
473
474       if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
475                                                 Pic.Max_Leading_Digits
476       then
477          raise Layout_Error;
478       end if;
479
480       if Pic.Radix_Position = Invalid_Position then
481          Position := Answer'Last;
482       else
483          Position := Pic.Radix_Position - 1;
484       end if;
485
486       for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
487
488          while Answer (Position) /= '9'
489                  and then
490                Answer (Position) /= Pic.Floater
491          loop
492             if Answer (Position) = '_' then
493                Answer (Position) := Separator_Character;
494
495             elsif Answer (Position) = 'b' then
496                Answer (Position) := ' ';
497             end if;
498
499             Position := Position - 1;
500          end loop;
501
502          Answer (Position) := To_Wide (Rounded (J));
503
504          if Rounded (J) /= '0' then
505             Zero := False;
506          end if;
507
508          Position := Position - 1;
509       end loop;
510
511       --  Do lead float
512
513       if Pic.Start_Float = Invalid_Position then
514
515          --  No leading floats, but need to change '9' to '0', '_' to
516          --  Separator_Character and 'b' to ' '.
517
518          for J in Last .. Position loop
519
520             --  Last set when fixing the "uninteresting" leaders above.
521             --  Don't duplicate the work.
522
523             if Answer (J) = '9' then
524                Answer (J) := '0';
525
526             elsif Answer (J) = '_' then
527                Answer (J) := Separator_Character;
528
529             elsif Answer (J) = 'b' then
530                Answer (J) := ' ';
531
532             end if;
533
534          end loop;
535
536       elsif Pic.Floater = '<'
537               or else
538             Pic.Floater = '+'
539               or else
540             Pic.Floater = '-'
541       then
542          for J in Pic.End_Float .. Position loop --  May be null range
543             if Answer (J) = '9' then
544                Answer (J) := '0';
545
546             elsif Answer (J) = '_' then
547                Answer (J) := Separator_Character;
548
549             elsif Answer (J) = 'b' then
550                Answer (J) := ' ';
551
552             end if;
553          end loop;
554
555          if Position > Pic.End_Float then
556             Position := Pic.End_Float;
557          end if;
558
559          for J in Pic.Start_Float .. Position - 1 loop
560             Answer (J) := ' ';
561          end loop;
562
563          Answer (Position) := Pic.Floater;
564          Sign_Position     := Position;
565
566       elsif Pic.Floater = '$' then
567
568          for J in Pic.End_Float .. Position loop --  May be null range
569             if Answer (J) = '9' then
570                Answer (J) := '0';
571
572             elsif Answer (J) = '_' then
573                Answer (J) := ' ';   --  no separator before leftmost digit
574
575             elsif Answer (J) = 'b' then
576                Answer (J) := ' ';
577             end if;
578          end loop;
579
580          if Position > Pic.End_Float then
581             Position := Pic.End_Float;
582          end if;
583
584          for J in Pic.Start_Float .. Position - 1 loop
585             Answer (J) := ' ';
586          end loop;
587
588          Answer (Position) := Pic.Floater;
589          Currency_Pos      := Position;
590
591       elsif Pic.Floater = '*' then
592
593          for J in Pic.End_Float .. Position loop --  May be null range
594             if Answer (J) = '9' then
595                Answer (J) := '0';
596
597             elsif Answer (J) = '_' then
598                Answer (J) := Separator_Character;
599
600             elsif Answer (J) = 'b' then
601                Answer (J) := '*';
602             end if;
603          end loop;
604
605          if Position > Pic.End_Float then
606             Position := Pic.End_Float;
607          end if;
608
609          for J in Pic.Start_Float .. Position loop
610             Answer (J) := '*';
611          end loop;
612
613       else
614          if Pic.Floater = '#' then
615             Currency_Pos := Currency_Symbol'Length;
616          end if;
617
618          for J in reverse Pic.Start_Float .. Position loop
619             case Answer (J) is
620
621                when '*' =>
622                   Answer (J) := Fill_Character;
623
624                when 'Z' | 'b' | '/' | '0' =>
625                   Answer (J) := ' ';
626
627                when '9' =>
628                   Answer (J) := '0';
629
630                when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
631                   null;
632
633                when '#' =>
634                   if Currency_Pos = 0 then
635                      Answer (J) := ' ';
636                   else
637                      Answer (J)   := Currency_Symbol (Currency_Pos);
638                      Currency_Pos := Currency_Pos - 1;
639                   end if;
640
641                when '_' =>
642
643                   case Pic.Floater is
644
645                      when '*' =>
646                         Answer (J) := Fill_Character;
647
648                      when 'Z' | 'b' =>
649                         Answer (J) := ' ';
650
651                      when '#' =>
652                         if Currency_Pos = 0 then
653                            Answer (J) := ' ';
654
655                         else
656                            Answer (J)   := Currency_Symbol (Currency_Pos);
657                            Currency_Pos := Currency_Pos - 1;
658                         end if;
659
660                      when others =>
661                         null;
662
663                   end case;
664
665                when others =>
666                   null;
667
668             end case;
669          end loop;
670
671          if Pic.Floater = '#' and then Currency_Pos /= 0 then
672             raise Layout_Error;
673          end if;
674       end if;
675
676       --  Do sign
677
678       if Sign_Position = Invalid_Position then
679          if Attrs.Negative then
680             raise Layout_Error;
681          end if;
682
683       else
684          if Attrs.Negative then
685             case Answer (Sign_Position) is
686                when 'C' | 'D' | '-' =>
687                   null;
688
689                when '+' =>
690                   Answer (Sign_Position) := '-';
691
692                when '<' =>
693                   Answer (Sign_Position)   := '(';
694                   Answer (Pic.Second_Sign) := ')';
695
696                when others =>
697                   raise Picture_Error;
698
699             end case;
700
701          else --  positive
702
703             case Answer (Sign_Position) is
704
705                when '-' =>
706                   Answer (Sign_Position) := ' ';
707
708                when '<' | 'C' | 'D' =>
709                   Answer (Sign_Position)   := ' ';
710                   Answer (Pic.Second_Sign) := ' ';
711
712                when '+' =>
713                   null;
714
715                when others =>
716                   raise Picture_Error;
717
718             end case;
719          end if;
720       end if;
721
722       --  Fill in trailing digits
723
724       if Pic.Max_Trailing_Digits > 0 then
725
726          if Attrs.Has_Fraction then
727             Position := Attrs.Start_Of_Fraction;
728             Last     := Pic.Radix_Position + 1;
729
730             for J in Last .. Answer'Last loop
731
732                if Answer (J) = '9' or else Answer (J) = Pic.Floater then
733                   Answer (J) := To_Wide (Rounded (Position));
734
735                   if Rounded (Position) /= '0' then
736                      Zero := False;
737                   end if;
738
739                   Position := Position + 1;
740                   Last     := J + 1;
741
742                   --  Used up fraction but remember place in Answer
743
744                   exit when Position > Attrs.End_Of_Fraction;
745
746                elsif Answer (J) = 'b' then
747                   Answer (J) := ' ';
748
749                elsif Answer (J) = '_' then
750                   Answer (J) := Separator_Character;
751
752                end if;
753
754                Last := J + 1;
755             end loop;
756
757             Position := Last;
758
759          else
760             Position := Pic.Radix_Position + 1;
761          end if;
762
763          --  Now fill remaining 9's with zeros and _ with separators
764
765          Last := Answer'Last;
766
767          for J in Position .. Last loop
768             if Answer (J) = '9' then
769                Answer (J) := '0';
770
771             elsif Answer (J) = Pic.Floater then
772                Answer (J) := '0';
773
774             elsif Answer (J) = '_' then
775                Answer (J) := Separator_Character;
776
777             elsif Answer (J) = 'b' then
778                Answer (J) := ' ';
779
780             end if;
781          end loop;
782
783          Position := Last + 1;
784
785       else
786          if Pic.Floater = '#' and then Currency_Pos /= 0 then
787             raise Layout_Error;
788          end if;
789
790          --  No trailing digits, but now J may need to stick in a currency
791          --  symbol or sign.
792
793          if Pic.Start_Currency = Invalid_Position then
794             Position := Answer'Last + 1;
795          else
796             Position := Pic.Start_Currency;
797          end if;
798       end if;
799
800       for J in Position .. Answer'Last loop
801
802          if Pic.Start_Currency /= Invalid_Position and then
803             Answer (Pic.Start_Currency) = '#' then
804             Currency_Pos := 1;
805          end if;
806
807          --  Note: There are some weird cases J can imagine with 'b' or '#'
808          --  in currency strings where the following code will cause
809          --  glitches. The trick is to tell when the character in the
810          --  answer should be checked, and when to look at the original
811          --  string. Some other time. RIE 11/26/96 ???
812
813          case Answer (J) is
814             when '*' =>
815                Answer (J) := Fill_Character;
816
817             when 'b' =>
818                Answer (J) := ' ';
819
820             when '#' =>
821                if Currency_Pos > Currency_Symbol'Length then
822                   Answer (J) := ' ';
823
824                else
825                   Answer (J)   := Currency_Symbol (Currency_Pos);
826                   Currency_Pos := Currency_Pos + 1;
827                end if;
828
829             when '_' =>
830
831                case Pic.Floater is
832
833                   when '*' =>
834                      Answer (J) := Fill_Character;
835
836                   when 'Z' | 'z' =>
837                      Answer (J) := ' ';
838
839                   when '#' =>
840                      if Currency_Pos > Currency_Symbol'Length then
841                         Answer (J) := ' ';
842                      else
843                         Answer (J)   := Currency_Symbol (Currency_Pos);
844                         Currency_Pos := Currency_Pos + 1;
845                      end if;
846
847                   when others =>
848                      null;
849
850                end case;
851
852             when others =>
853                exit;
854
855          end case;
856       end loop;
857
858       --  Now get rid of Blank_when_Zero and complete Star fill
859
860       if Zero and then Pic.Blank_When_Zero then
861
862          --  Value is zero, and blank it
863
864          Last := Answer'Last;
865
866          if Dollar then
867             Last := Last - 1 + Currency_Symbol'Length;
868          end if;
869
870          if Pic.Radix_Position /= Invalid_Position and then
871             Answer (Pic.Radix_Position) = 'V' then
872             Last := Last - 1;
873          end if;
874
875          return Wide_String'(1 .. Last => ' ');
876
877       elsif Zero and then Pic.Star_Fill then
878          Last := Answer'Last;
879
880          if Dollar then
881             Last := Last - 1 + Currency_Symbol'Length;
882          end if;
883
884          if Pic.Radix_Position /= Invalid_Position then
885
886             if Answer (Pic.Radix_Position) = 'V' then
887                Last := Last - 1;
888
889             elsif Dollar then
890                if Pic.Radix_Position > Pic.Start_Currency then
891                   return Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
892                      Radix_Point &
893                      Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
894
895                else
896                   return
897                      Wide_String'
898                      (1 ..
899                       Pic.Radix_Position + Currency_Symbol'Length - 2
900                                              => '*') &
901                      Radix_Point &
902                      Wide_String'
903                        (Pic.Radix_Position + Currency_Symbol'Length .. Last
904                                              => '*');
905                end if;
906
907             else
908                return
909                  Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
910                  Radix_Point &
911                  Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
912             end if;
913          end if;
914
915          return Wide_String'(1 .. Last => '*');
916       end if;
917
918       --  This was once a simple return statement, now there are nine
919       --  different return cases.  Not to mention the five above to deal
920       --  with zeros.  Why not split things out?
921
922       --  Processing the radix and sign expansion separately
923       --  would require lots of copying--the string and some of its
924       --  indicies--without really simplifying the logic.  The cases are:
925
926       --  1) Expand $, replace '.' with Radix_Point
927       --  2) No currency expansion, replace '.' with Radix_Point
928       --  3) Expand $, radix blanked
929       --  4) No currency expansion, radix blanked
930       --  5) Elide V
931       --  6) Expand $, Elide V
932       --  7) Elide V, Expand $ (Two cases depending on order.)
933       --  8) No radix, expand $
934       --  9) No radix, no currency expansion
935
936       if Pic.Radix_Position /= Invalid_Position then
937
938          if Answer (Pic.Radix_Position) = '.' then
939             Answer (Pic.Radix_Position) := Radix_Point;
940
941             if Dollar then
942
943                --  1) Expand $, replace '.' with Radix_Point
944
945                return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
946                   Answer (Currency_Pos + 1 .. Answer'Last);
947
948             else
949                --  2) No currency expansion, replace '.' with Radix_Point
950
951                return Answer;
952             end if;
953
954          elsif Answer (Pic.Radix_Position) = ' ' then --  blanked radix.
955             if Dollar then
956
957                --  3) Expand $, radix blanked
958
959                return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
960                  Answer (Currency_Pos + 1 .. Answer'Last);
961
962             else
963                --  4) No expansion, radix blanked
964
965                return Answer;
966             end if;
967
968          --  V cases
969
970          else
971             if not Dollar then
972
973                --  5) Elide V
974
975                return Answer (1 .. Pic.Radix_Position - 1) &
976                   Answer (Pic.Radix_Position + 1 .. Answer'Last);
977
978             elsif Currency_Pos < Pic.Radix_Position then
979
980                --  6) Expand $, Elide V
981
982                return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
983                   Answer (Currency_Pos + 1 .. Pic.Radix_Position - 1) &
984                   Answer (Pic.Radix_Position + 1 .. Answer'Last);
985
986             else
987                --  7) Elide V, Expand $
988
989                return Answer (1 .. Pic.Radix_Position - 1) &
990                   Answer (Pic.Radix_Position + 1 .. Currency_Pos - 1) &
991                   Currency_Symbol &
992                   Answer (Currency_Pos + 1 .. Answer'Last);
993             end if;
994          end if;
995
996       elsif Dollar then
997
998          --  8) No radix, expand $
999
1000          return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
1001             Answer (Currency_Pos + 1 .. Answer'Last);
1002
1003       else
1004          --  9) No radix, no currency expansion
1005
1006          return Answer;
1007       end if;
1008    end Format_Number;
1009
1010    -------------------------
1011    -- Parse_Number_String --
1012    -------------------------
1013
1014    function Parse_Number_String (Str : String) return Number_Attributes is
1015       Answer : Number_Attributes;
1016
1017    begin
1018       for J in Str'Range loop
1019          case Str (J) is
1020
1021             when ' ' =>
1022                null; --  ignore
1023
1024             when '1' .. '9' =>
1025
1026                --  Decide if this is the start of a number.
1027                --  If so, figure out which one...
1028
1029                if Answer.Has_Fraction then
1030                   Answer.End_Of_Fraction := J;
1031                else
1032                   if Answer.Start_Of_Int = Invalid_Position then
1033                      --  start integer
1034                      Answer.Start_Of_Int := J;
1035                   end if;
1036                   Answer.End_Of_Int := J;
1037                end if;
1038
1039             when '0' =>
1040
1041                --  Only count a zero before the decimal point if it follows a
1042                --  non-zero digit.  After the decimal point, zeros will be
1043                --  counted if followed by a non-zero digit.
1044
1045                if not Answer.Has_Fraction then
1046                   if Answer.Start_Of_Int /= Invalid_Position then
1047                      Answer.End_Of_Int := J;
1048                   end if;
1049                end if;
1050
1051             when '-' =>
1052
1053                --  Set negative
1054
1055                Answer.Negative := True;
1056
1057             when '.' =>
1058
1059                --  Close integer, start fraction
1060
1061                if Answer.Has_Fraction then
1062                   raise Picture_Error;
1063                end if;
1064
1065                --  Two decimal points is a no-no
1066
1067                Answer.Has_Fraction    := True;
1068                Answer.End_Of_Fraction := J;
1069
1070                --  Could leave this at Invalid_Position, but this seems the
1071                --  right way to indicate a null range...
1072
1073                Answer.Start_Of_Fraction := J + 1;
1074                Answer.End_Of_Int        := J - 1;
1075
1076             when others =>
1077                raise Picture_Error; -- can this happen? probably not!
1078          end case;
1079       end loop;
1080
1081       if Answer.Start_Of_Int = Invalid_Position then
1082          Answer.Start_Of_Int := Answer.End_Of_Int + 1;
1083       end if;
1084
1085       --  No significant (intger) digits needs a null range
1086
1087       return Answer;
1088    end Parse_Number_String;
1089
1090    ----------------
1091    -- Pic_String --
1092    ----------------
1093
1094    --  The following ensures that we return B and not b being careful not
1095    --  to break things which expect lower case b for blank. See CXF3A02.
1096
1097    function Pic_String (Pic : Picture) return String is
1098       Temp : String (1 .. Pic.Contents.Picture.Length) :=
1099                               Pic.Contents.Picture.Expanded;
1100    begin
1101       for J in Temp'Range loop
1102          if Temp (J) = 'b' then
1103             Temp (J) := 'B';
1104          end if;
1105       end loop;
1106
1107       return Temp;
1108    end Pic_String;
1109
1110    ------------------
1111    -- Precalculate --
1112    ------------------
1113
1114    procedure Precalculate  (Pic : in out Format_Record) is
1115
1116       Computed_BWZ : Boolean := True;
1117
1118       type Legality is  (Okay, Reject);
1119       State : Legality := Reject;
1120       --  Start in reject, which will reject null strings
1121
1122       Index : Pic_Index := Pic.Picture.Expanded'First;
1123
1124       function At_End return Boolean;
1125       pragma Inline (At_End);
1126
1127       procedure Set_State (L : Legality);
1128       pragma Inline (Set_State);
1129
1130       function Look return Character;
1131       pragma Inline (Look);
1132
1133       function Is_Insert return Boolean;
1134       pragma Inline (Is_Insert);
1135
1136       procedure Skip;
1137       pragma Inline (Skip);
1138
1139       procedure Trailing_Currency;
1140       procedure Trailing_Bracket;
1141       procedure Number_Fraction;
1142       procedure Number_Completion;
1143       procedure Number_Fraction_Or_Bracket;
1144       procedure Number_Fraction_Or_Z_Fill;
1145       procedure Zero_Suppression;
1146       procedure Floating_Bracket;
1147       procedure Number_Fraction_Or_Star_Fill;
1148       procedure Star_Suppression;
1149       procedure Number_Fraction_Or_Dollar;
1150       procedure Leading_Dollar;
1151       procedure Number_Fraction_Or_Pound;
1152       procedure Leading_Pound;
1153       procedure Picture;
1154       procedure Floating_Plus;
1155       procedure Floating_Minus;
1156       procedure Picture_Plus;
1157       procedure Picture_Minus;
1158       procedure Picture_Bracket;
1159       procedure Number;
1160       procedure Optional_RHS_Sign;
1161       procedure Picture_String;
1162
1163       ------------
1164       -- At_End --
1165       ------------
1166
1167       function At_End return Boolean is
1168       begin
1169          return Index > Pic.Picture.Length;
1170       end At_End;
1171
1172       ----------------------
1173       -- Floating_Bracket --
1174       ----------------------
1175
1176       --  Note that Floating_Bracket is only called with an acceptable
1177       --  prefix. But we don't set Okay, because we must end with a '>'.
1178
1179       procedure Floating_Bracket is
1180       begin
1181          Pic.Floater := '<';
1182          Pic.End_Float := Index;
1183          Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1184
1185          --  First bracket wasn't counted...
1186
1187          Skip; --  known '<'
1188
1189          loop
1190             if At_End then
1191                return;
1192             end if;
1193
1194             case Look is
1195
1196                when '_' | '0' | '/' =>
1197                   Pic.End_Float := Index;
1198                   Skip;
1199
1200                when 'B' | 'b'  =>
1201                   Pic.End_Float := Index;
1202                   Pic.Picture.Expanded (Index) := 'b';
1203                   Skip;
1204
1205                when '<' =>
1206                   Pic.End_Float := Index;
1207                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1208                   Skip;
1209
1210                when '9' =>
1211                   Number_Completion;
1212
1213                when '$' =>
1214                   Leading_Dollar;
1215
1216                when '#' =>
1217                   Leading_Pound;
1218
1219                when 'V' | 'v' | '.' =>
1220                   Pic.Radix_Position := Index;
1221                   Skip;
1222                   Number_Fraction_Or_Bracket;
1223                   return;
1224
1225                when others =>
1226                return;
1227             end case;
1228          end loop;
1229       end Floating_Bracket;
1230
1231       --------------------
1232       -- Floating_Minus --
1233       --------------------
1234
1235       procedure Floating_Minus is
1236       begin
1237          loop
1238             if At_End then
1239                return;
1240             end if;
1241
1242             case Look is
1243                when '_' | '0' | '/' =>
1244                   Pic.End_Float := Index;
1245                   Skip;
1246
1247                when 'B' | 'b'  =>
1248                   Pic.End_Float := Index;
1249                   Pic.Picture.Expanded (Index) := 'b';
1250                   Skip;
1251
1252                when '-' =>
1253                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1254                   Pic.End_Float := Index;
1255                   Skip;
1256
1257                when '9' =>
1258                   Number_Completion;
1259                   return;
1260
1261                when '.' | 'V' | 'v' =>
1262                   Pic.Radix_Position := Index;
1263                   Skip; --  Radix
1264
1265                   while Is_Insert loop
1266                      Skip;
1267                   end loop;
1268
1269                   if At_End then
1270                      return;
1271                   end if;
1272
1273                   if Look = '-' then
1274                      loop
1275                         if At_End then
1276                            return;
1277                         end if;
1278
1279                         case Look is
1280
1281                            when '-' =>
1282                               Pic.Max_Trailing_Digits :=
1283                                 Pic.Max_Trailing_Digits + 1;
1284                               Pic.End_Float := Index;
1285                               Skip;
1286
1287                            when '_' | '0' | '/' =>
1288                               Skip;
1289
1290                            when 'B' | 'b'  =>
1291                               Pic.Picture.Expanded (Index) := 'b';
1292                               Skip;
1293
1294                            when others =>
1295                               return;
1296
1297                         end case;
1298                      end loop;
1299
1300                   else
1301                      Number_Completion;
1302                   end if;
1303
1304                   return;
1305
1306                when others =>
1307                   return;
1308             end case;
1309          end loop;
1310       end Floating_Minus;
1311
1312       -------------------
1313       -- Floating_Plus --
1314       -------------------
1315
1316       procedure Floating_Plus is
1317       begin
1318          loop
1319             if At_End then
1320                return;
1321             end if;
1322
1323             case Look is
1324                when '_' | '0' | '/' =>
1325                   Pic.End_Float := Index;
1326                   Skip;
1327
1328                when 'B' | 'b'  =>
1329                   Pic.End_Float := Index;
1330                   Pic.Picture.Expanded (Index) := 'b';
1331                   Skip;
1332
1333                when '+' =>
1334                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1335                   Pic.End_Float := Index;
1336                   Skip;
1337
1338                when '9' =>
1339                   Number_Completion;
1340                   return;
1341
1342                when '.' | 'V' | 'v' =>
1343                   Pic.Radix_Position := Index;
1344                   Skip; --  Radix
1345
1346                   while Is_Insert loop
1347                      Skip;
1348                   end loop;
1349
1350                   if At_End then
1351                      return;
1352                   end if;
1353
1354                   if Look = '+' then
1355                      loop
1356                         if At_End then
1357                            return;
1358                         end if;
1359
1360                         case Look is
1361
1362                            when '+' =>
1363                               Pic.Max_Trailing_Digits :=
1364                                 Pic.Max_Trailing_Digits + 1;
1365                               Pic.End_Float := Index;
1366                               Skip;
1367
1368                            when '_' | '0' | '/' =>
1369                               Skip;
1370
1371                            when 'B' | 'b'  =>
1372                               Pic.Picture.Expanded (Index) := 'b';
1373                               Skip;
1374
1375                            when others =>
1376                               return;
1377
1378                         end case;
1379                      end loop;
1380
1381                   else
1382                      Number_Completion;
1383                   end if;
1384
1385                   return;
1386
1387                when others =>
1388                   return;
1389
1390             end case;
1391          end loop;
1392       end Floating_Plus;
1393
1394       ---------------
1395       -- Is_Insert --
1396       ---------------
1397
1398       function Is_Insert return Boolean is
1399       begin
1400          if At_End then
1401             return False;
1402          end if;
1403
1404          case Pic.Picture.Expanded (Index) is
1405
1406             when '_' | '0' | '/' => return True;
1407
1408             when 'B' | 'b' =>
1409                Pic.Picture.Expanded (Index) := 'b'; --  canonical
1410                return True;
1411
1412             when others => return False;
1413          end case;
1414       end Is_Insert;
1415
1416       --------------------
1417       -- Leading_Dollar --
1418       --------------------
1419
1420       --  Note that Leading_Dollar can be called in either State.
1421       --  It will set state to Okay only if a 9 or (second) $
1422       --  is encountered.
1423
1424       --  Also notice the tricky bit with State and Zero_Suppression.
1425       --  Zero_Suppression is Picture_Error if a '$' or a '9' has been
1426       --  encountered, exactly the cases where State has been set.
1427
1428       procedure Leading_Dollar is
1429       begin
1430          --  Treat as a floating dollar, and unwind otherwise
1431
1432          Pic.Floater := '$';
1433          Pic.Start_Currency := Index;
1434          Pic.End_Currency := Index;
1435          Pic.Start_Float := Index;
1436          Pic.End_Float := Index;
1437
1438          --  Don't increment Pic.Max_Leading_Digits, we need one "real"
1439          --  currency place.
1440
1441          Skip; --  known '$'
1442
1443          loop
1444             if At_End then
1445                return;
1446             end if;
1447
1448             case Look is
1449
1450                when '_' | '0' | '/' =>
1451                   Pic.End_Float := Index;
1452                   Skip;
1453
1454                   --  A trailing insertion character is not part of the
1455                   --  floating currency, so need to look ahead.
1456
1457                   if Look /= '$' then
1458                      Pic.End_Float := Pic.End_Float - 1;
1459                   end if;
1460
1461                when 'B' | 'b'  =>
1462                   Pic.End_Float := Index;
1463                   Pic.Picture.Expanded (Index) := 'b';
1464                   Skip;
1465
1466                when 'Z' | 'z' =>
1467                   Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1468
1469                   if State = Okay then
1470                      raise Picture_Error;
1471                   else
1472                      --  Will overwrite Floater and Start_Float
1473
1474                      Zero_Suppression;
1475                   end if;
1476
1477                when '*' =>
1478                   if State = Okay then
1479                      raise Picture_Error;
1480                   else
1481                      --  Will overwrite Floater and Start_Float
1482
1483                      Star_Suppression;
1484                   end if;
1485
1486                when '$' =>
1487                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1488                   Pic.End_Float := Index;
1489                   Pic.End_Currency := Index;
1490                   Set_State (Okay); Skip;
1491
1492                when '9' =>
1493                   if State /= Okay then
1494                      Pic.Floater := '!';
1495                      Pic.Start_Float := Invalid_Position;
1496                      Pic.End_Float := Invalid_Position;
1497                   end if;
1498
1499                   --  A single dollar does not a floating make
1500
1501                   Number_Completion;
1502                   return;
1503
1504                when 'V' | 'v' | '.' =>
1505                   if State /= Okay then
1506                      Pic.Floater := '!';
1507                      Pic.Start_Float := Invalid_Position;
1508                      Pic.End_Float := Invalid_Position;
1509                   end if;
1510
1511                   --  Only one dollar before the sign is okay, but doesn't
1512                   --  float.
1513
1514                   Pic.Radix_Position := Index;
1515                   Skip;
1516                   Number_Fraction_Or_Dollar;
1517                   return;
1518
1519                when others =>
1520                   return;
1521
1522             end case;
1523          end loop;
1524       end Leading_Dollar;
1525
1526       -------------------
1527       -- Leading_Pound --
1528       -------------------
1529
1530       --  This one is complex!  A Leading_Pound can be fixed or floating,
1531       --  but in some cases the decision has to be deferred until we leave
1532       --  this procedure.  Also note that Leading_Pound can be called in
1533       --  either State.
1534
1535       --  It will set state to Okay only if a 9 or  (second) # is
1536       --  encountered.
1537
1538       --  One Last note:  In ambiguous cases, the currency is treated as
1539       --  floating unless there is only one '#'.
1540
1541       procedure Leading_Pound is
1542
1543          Inserts : Boolean := False;
1544          --  Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1545
1546          Must_Float : Boolean := False;
1547          --  Set to true if a '#' occurs after an insert
1548
1549       begin
1550          --  Treat as a floating currency. If it isn't, this will be
1551          --  overwritten later.
1552
1553          Pic.Floater := '#';
1554
1555          Pic.Start_Currency := Index;
1556          Pic.End_Currency := Index;
1557          Pic.Start_Float := Index;
1558          Pic.End_Float := Index;
1559
1560          --  Don't increment Pic.Max_Leading_Digits, we need one "real"
1561          --  currency place.
1562
1563          Pic.Max_Currency_Digits := 1; --  we've seen one.
1564
1565          Skip; --  known '#'
1566
1567          loop
1568             if At_End then
1569                return;
1570             end if;
1571
1572             case Look is
1573
1574                when '_' | '0' | '/' =>
1575                   Pic.End_Float := Index;
1576                   Inserts := True;
1577                   Skip;
1578
1579                when 'B' | 'b'  =>
1580                   Pic.Picture.Expanded (Index) := 'b';
1581                   Pic.End_Float := Index;
1582                   Inserts := True;
1583                   Skip;
1584
1585                when 'Z' | 'z' =>
1586                   Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1587
1588                   if Must_Float then
1589                      raise Picture_Error;
1590                   else
1591                      Pic.Max_Leading_Digits := 0;
1592
1593                      --  Will overwrite Floater and Start_Float
1594
1595                      Zero_Suppression;
1596                   end if;
1597
1598                when '*' =>
1599                   if Must_Float then
1600                      raise Picture_Error;
1601                   else
1602                      Pic.Max_Leading_Digits := 0;
1603
1604                      --  Will overwrite Floater and Start_Float
1605
1606                      Star_Suppression;
1607                   end if;
1608
1609                when '#' =>
1610                   if Inserts then
1611                      Must_Float := True;
1612                   end if;
1613
1614                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1615                   Pic.End_Float := Index;
1616                   Pic.End_Currency := Index;
1617                   Set_State (Okay);
1618                   Skip;
1619
1620                when '9' =>
1621                   if State /= Okay then
1622
1623                      --  A single '#' doesn't float
1624
1625                      Pic.Floater := '!';
1626                      Pic.Start_Float := Invalid_Position;
1627                      Pic.End_Float := Invalid_Position;
1628                   end if;
1629
1630                   Number_Completion;
1631                   return;
1632
1633                when 'V' | 'v' | '.' =>
1634                   if State /= Okay then
1635                      Pic.Floater := '!';
1636                      Pic.Start_Float := Invalid_Position;
1637                      Pic.End_Float := Invalid_Position;
1638                   end if;
1639
1640                   --  Only one pound before the sign is okay, but doesn't
1641                   --  float.
1642
1643                   Pic.Radix_Position := Index;
1644                   Skip;
1645                   Number_Fraction_Or_Pound;
1646                   return;
1647
1648                when others =>
1649                   return;
1650             end case;
1651          end loop;
1652       end Leading_Pound;
1653
1654       ----------
1655       -- Look --
1656       ----------
1657
1658       function Look return Character is
1659       begin
1660          if At_End then
1661             raise Picture_Error;
1662          end if;
1663
1664          return Pic.Picture.Expanded (Index);
1665       end Look;
1666
1667       ------------
1668       -- Number --
1669       ------------
1670
1671       procedure Number is
1672       begin
1673          loop
1674
1675             case Look is
1676                when '_' | '0' | '/' =>
1677                   Skip;
1678
1679                when 'B' | 'b'  =>
1680                   Pic.Picture.Expanded (Index) := 'b';
1681                   Skip;
1682
1683                when '9' =>
1684                   Computed_BWZ := False;
1685                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1686                   Set_State (Okay);
1687                   Skip;
1688
1689                when '.' | 'V' | 'v' =>
1690                   Pic.Radix_Position := Index;
1691                   Skip;
1692                   Number_Fraction;
1693                   return;
1694
1695                when others =>
1696                   return;
1697
1698             end case;
1699
1700             if At_End then
1701                return;
1702             end if;
1703
1704             --  Will return in Okay state if a '9' was seen
1705
1706          end loop;
1707       end Number;
1708
1709       -----------------------
1710       -- Number_Completion --
1711       -----------------------
1712
1713       procedure Number_Completion is
1714       begin
1715          while not At_End loop
1716             case Look is
1717
1718                when '_' | '0' | '/' =>
1719                   Skip;
1720
1721                when 'B' | 'b'  =>
1722                   Pic.Picture.Expanded (Index) := 'b';
1723                   Skip;
1724
1725                when '9' =>
1726                   Computed_BWZ := False;
1727                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1728                   Set_State (Okay);
1729                   Skip;
1730
1731                when 'V' | 'v' | '.' =>
1732                   Pic.Radix_Position := Index;
1733                   Skip;
1734                   Number_Fraction;
1735                   return;
1736
1737                when others =>
1738                   return;
1739             end case;
1740          end loop;
1741       end Number_Completion;
1742
1743       ---------------------
1744       -- Number_Fraction --
1745       ---------------------
1746
1747       procedure Number_Fraction is
1748       begin
1749          --  Note that number fraction can be called in either State.
1750          --  It will set state to Valid only if a 9 is encountered.
1751
1752          loop
1753             if At_End then
1754                return;
1755             end if;
1756
1757             case Look is
1758                when '_' | '0' | '/' =>
1759                   Skip;
1760
1761                when 'B' | 'b'  =>
1762                   Pic.Picture.Expanded (Index) := 'b';
1763                   Skip;
1764
1765                when '9' =>
1766                   Computed_BWZ := False;
1767                   Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1768                   Set_State (Okay); Skip;
1769
1770                when others =>
1771                   return;
1772             end case;
1773          end loop;
1774       end Number_Fraction;
1775
1776       --------------------------------
1777       -- Number_Fraction_Or_Bracket --
1778       --------------------------------
1779
1780       procedure Number_Fraction_Or_Bracket is
1781       begin
1782          loop
1783             if At_End then
1784                return;
1785             end if;
1786
1787             case Look is
1788
1789                when '_' | '0' | '/' => Skip;
1790
1791                when 'B' | 'b'  =>
1792                   Pic.Picture.Expanded (Index) := 'b';
1793                   Skip;
1794
1795                when '<' =>
1796                   Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1797                   Pic.End_Float := Index;
1798                   Skip;
1799
1800                   loop
1801                      if At_End then
1802                         return;
1803                      end if;
1804
1805                      case Look is
1806                         when '_' | '0' | '/' =>
1807                            Skip;
1808
1809                         when 'B' | 'b'  =>
1810                            Pic.Picture.Expanded (Index) := 'b';
1811                            Skip;
1812
1813                         when '<' =>
1814                            Pic.Max_Trailing_Digits :=
1815                              Pic.Max_Trailing_Digits + 1;
1816                            Pic.End_Float := Index;
1817                            Skip;
1818
1819                         when others =>
1820                            return;
1821                      end case;
1822                   end loop;
1823
1824                when others =>
1825                   Number_Fraction;
1826                   return;
1827             end case;
1828          end loop;
1829       end Number_Fraction_Or_Bracket;
1830
1831       -------------------------------
1832       -- Number_Fraction_Or_Dollar --
1833       -------------------------------
1834
1835       procedure Number_Fraction_Or_Dollar is
1836       begin
1837          loop
1838             if At_End then
1839                return;
1840             end if;
1841
1842             case Look is
1843                when '_' | '0' | '/' =>
1844                   Skip;
1845
1846                when 'B' | 'b'  =>
1847                   Pic.Picture.Expanded (Index) := 'b';
1848                   Skip;
1849
1850                when '$' =>
1851                   Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1852                   Pic.End_Float := Index;
1853                   Skip;
1854
1855                   loop
1856                      if At_End then
1857                         return;
1858                      end if;
1859
1860                      case Look is
1861                         when '_' | '0' | '/' =>
1862                            Skip;
1863
1864                         when 'B' | 'b'  =>
1865                            Pic.Picture.Expanded (Index) := 'b';
1866                            Skip;
1867
1868                         when '$' =>
1869                            Pic.Max_Trailing_Digits :=
1870                              Pic.Max_Trailing_Digits + 1;
1871                            Pic.End_Float := Index;
1872                            Skip;
1873
1874                         when others =>
1875                            return;
1876                      end case;
1877                   end loop;
1878
1879                when others =>
1880                   Number_Fraction;
1881                   return;
1882             end case;
1883          end loop;
1884       end Number_Fraction_Or_Dollar;
1885
1886       ------------------------------
1887       -- Number_Fraction_Or_Pound --
1888       ------------------------------
1889
1890       procedure Number_Fraction_Or_Pound is
1891       begin
1892          loop
1893             if At_End then
1894                return;
1895             end if;
1896
1897             case Look is
1898
1899                when '_' | '0' | '/' =>
1900                   Skip;
1901
1902                when 'B' | 'b'  =>
1903                   Pic.Picture.Expanded (Index) := 'b';
1904                   Skip;
1905
1906                when '#' =>
1907                   Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1908                   Pic.End_Float := Index;
1909                   Skip;
1910
1911                   loop
1912                      if At_End then
1913                         return;
1914                      end if;
1915
1916                      case Look is
1917
1918                         when '_' | '0' | '/' =>
1919                            Skip;
1920
1921                         when 'B' | 'b'  =>
1922                            Pic.Picture.Expanded (Index) := 'b';
1923                            Skip;
1924
1925                         when '#' =>
1926                            Pic.Max_Trailing_Digits :=
1927                              Pic.Max_Trailing_Digits + 1;
1928                            Pic.End_Float := Index;
1929                            Skip;
1930
1931                         when others =>
1932                            return;
1933
1934                      end case;
1935                   end loop;
1936
1937                when others =>
1938                   Number_Fraction;
1939                   return;
1940
1941             end case;
1942          end loop;
1943       end Number_Fraction_Or_Pound;
1944
1945       ----------------------------------
1946       -- Number_Fraction_Or_Star_Fill --
1947       ----------------------------------
1948
1949       procedure Number_Fraction_Or_Star_Fill is
1950       begin
1951          loop
1952             if At_End then
1953                return;
1954             end if;
1955
1956             case Look is
1957
1958                when '_' | '0' | '/' =>
1959                   Skip;
1960
1961                when 'B' | 'b'  =>
1962                   Pic.Picture.Expanded (Index) := 'b';
1963                   Skip;
1964
1965                when '*' =>
1966                   Pic.Star_Fill := True;
1967                   Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1968                   Pic.End_Float := Index;
1969                   Skip;
1970
1971                   loop
1972                      if At_End then
1973                         return;
1974                      end if;
1975
1976                      case Look is
1977
1978                         when '_' | '0' | '/' =>
1979                            Skip;
1980
1981                         when 'B' | 'b'  =>
1982                            Pic.Picture.Expanded (Index) := 'b';
1983                            Skip;
1984
1985                         when '*' =>
1986                            Pic.Star_Fill := True;
1987                            Pic.Max_Trailing_Digits :=
1988                              Pic.Max_Trailing_Digits + 1;
1989                            Pic.End_Float := Index;
1990                            Skip;
1991
1992                         when others =>
1993                            return;
1994                      end case;
1995                   end loop;
1996
1997                when others =>
1998                   Number_Fraction;
1999                   return;
2000
2001             end case;
2002          end loop;
2003       end Number_Fraction_Or_Star_Fill;
2004
2005       -------------------------------
2006       -- Number_Fraction_Or_Z_Fill --
2007       -------------------------------
2008
2009       procedure Number_Fraction_Or_Z_Fill is
2010       begin
2011          loop
2012             if At_End then
2013                return;
2014             end if;
2015
2016             case Look is
2017
2018                when '_' | '0' | '/' =>
2019                   Skip;
2020
2021                when 'B' | 'b'  =>
2022                   Pic.Picture.Expanded (Index) := 'b';
2023                   Skip;
2024
2025                when 'Z' | 'z' =>
2026                   Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
2027                   Pic.End_Float := Index;
2028                   Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2029
2030                   Skip;
2031
2032                   loop
2033                      if At_End then
2034                         return;
2035                      end if;
2036
2037                      case Look is
2038
2039                         when '_' | '0' | '/' =>
2040                            Skip;
2041
2042                         when 'B' | 'b'  =>
2043                            Pic.Picture.Expanded (Index) := 'b';
2044                            Skip;
2045
2046                         when 'Z' | 'z' =>
2047                            Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2048
2049                            Pic.Max_Trailing_Digits :=
2050                              Pic.Max_Trailing_Digits + 1;
2051                            Pic.End_Float := Index;
2052                            Skip;
2053
2054                         when others =>
2055                            return;
2056                      end case;
2057                   end loop;
2058
2059                when others =>
2060                   Number_Fraction;
2061                   return;
2062             end case;
2063          end loop;
2064       end Number_Fraction_Or_Z_Fill;
2065
2066       -----------------------
2067       -- Optional_RHS_Sign --
2068       -----------------------
2069
2070       procedure Optional_RHS_Sign is
2071       begin
2072          if At_End then
2073             return;
2074          end if;
2075
2076          case Look is
2077
2078             when '+' | '-' =>
2079                Pic.Sign_Position := Index;
2080                Skip;
2081                return;
2082
2083             when 'C' | 'c' =>
2084                Pic.Sign_Position := Index;
2085                Pic.Picture.Expanded (Index) := 'C';
2086                Skip;
2087
2088                if Look = 'R' or else Look = 'r' then
2089                   Pic.Second_Sign := Index;
2090                   Pic.Picture.Expanded (Index) := 'R';
2091                   Skip;
2092
2093                else
2094                   raise Picture_Error;
2095                end if;
2096
2097                return;
2098
2099             when 'D' | 'd' =>
2100                Pic.Sign_Position := Index;
2101                Pic.Picture.Expanded (Index) := 'D';
2102                Skip;
2103
2104                if Look = 'B' or else Look = 'b' then
2105                   Pic.Second_Sign := Index;
2106                   Pic.Picture.Expanded (Index) := 'B';
2107                   Skip;
2108
2109                else
2110                   raise Picture_Error;
2111                end if;
2112
2113                return;
2114
2115             when '>' =>
2116                if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2117                   Pic.Second_Sign := Index;
2118                   Skip;
2119
2120                else
2121                   raise Picture_Error;
2122                end if;
2123
2124             when others =>
2125                return;
2126
2127          end case;
2128       end Optional_RHS_Sign;
2129
2130       -------------
2131       -- Picture --
2132       -------------
2133
2134       --  Note that Picture can be called in either State
2135
2136       --  It will set state to Valid only if a 9 is encountered or floating
2137       --  currency is called.
2138
2139       procedure Picture is
2140       begin
2141          loop
2142             if At_End then
2143                return;
2144             end if;
2145
2146             case Look is
2147
2148                when '_' | '0' | '/' =>
2149                   Skip;
2150
2151                when 'B' | 'b'  =>
2152                   Pic.Picture.Expanded (Index) := 'b';
2153                   Skip;
2154
2155                when '$' =>
2156                   Leading_Dollar;
2157                   return;
2158
2159                when '#' =>
2160                   Leading_Pound;
2161                   return;
2162
2163                when '9' =>
2164                   Computed_BWZ := False;
2165                   Set_State (Okay);
2166                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2167                   Skip;
2168
2169                when 'V' | 'v' | '.' =>
2170                   Pic.Radix_Position := Index;
2171                   Skip;
2172                   Number_Fraction;
2173                   Trailing_Currency;
2174                   return;
2175
2176                when others =>
2177                   return;
2178
2179             end case;
2180          end loop;
2181       end Picture;
2182
2183       ---------------------
2184       -- Picture_Bracket --
2185       ---------------------
2186
2187       procedure Picture_Bracket is
2188       begin
2189          Pic.Sign_Position := Index;
2190          Pic.Sign_Position := Index;
2191
2192          --  Treat as a floating sign, and unwind otherwise
2193
2194          Pic.Floater := '<';
2195          Pic.Start_Float := Index;
2196          Pic.End_Float := Index;
2197
2198          --  Don't increment Pic.Max_Leading_Digits, we need one "real"
2199          --  sign place.
2200
2201          Skip; --  Known Bracket
2202
2203          loop
2204             case Look is
2205
2206                when '_' | '0' | '/' =>
2207                   Pic.End_Float := Index;
2208                   Skip;
2209
2210                when 'B' | 'b'  =>
2211                   Pic.End_Float := Index;
2212                   Pic.Picture.Expanded (Index) := 'b';
2213                   Skip;
2214
2215                when '<' =>
2216                   Set_State (Okay);  --  "<<>" is enough.
2217                   Floating_Bracket;
2218                   Trailing_Currency;
2219                   Trailing_Bracket;
2220                   return;
2221
2222                when '$' | '#' | '9' | '*' =>
2223                   if State /= Okay then
2224                      Pic.Floater := '!';
2225                      Pic.Start_Float := Invalid_Position;
2226                      Pic.End_Float := Invalid_Position;
2227                   end if;
2228
2229                   Picture;
2230                   Trailing_Bracket;
2231                   Set_State (Okay);
2232                   return;
2233
2234                when '.' | 'V' | 'v' =>
2235                   if State /= Okay then
2236                      Pic.Floater := '!';
2237                      Pic.Start_Float := Invalid_Position;
2238                      Pic.End_Float := Invalid_Position;
2239                   end if;
2240
2241                   --  Don't assume that state is okay, haven't seen a digit
2242
2243                   Picture;
2244                   Trailing_Bracket;
2245                   return;
2246
2247                when others =>
2248                   raise Picture_Error;
2249
2250             end case;
2251          end loop;
2252       end Picture_Bracket;
2253
2254       -------------------
2255       -- Picture_Minus --
2256       -------------------
2257
2258       procedure Picture_Minus is
2259       begin
2260          Pic.Sign_Position := Index;
2261
2262          --  Treat as a floating sign, and unwind otherwise
2263
2264          Pic.Floater := '-';
2265          Pic.Start_Float := Index;
2266          Pic.End_Float := Index;
2267
2268          --  Don't increment Pic.Max_Leading_Digits, we need one "real"
2269          --  sign place.
2270
2271          Skip; --  Known Minus
2272
2273          loop
2274             case Look is
2275
2276                when '_' | '0' | '/' =>
2277                   Pic.End_Float := Index;
2278                   Skip;
2279
2280                when 'B' | 'b'  =>
2281                   Pic.End_Float := Index;
2282                   Pic.Picture.Expanded (Index) := 'b';
2283                   Skip;
2284
2285                when '-' =>
2286                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2287                   Pic.End_Float := Index;
2288                   Skip;
2289                   Set_State (Okay);  --  "-- " is enough
2290                   Floating_Minus;
2291                   Trailing_Currency;
2292                   return;
2293
2294                when '$' | '#' | '9' | '*' =>
2295                   if State /= Okay then
2296                      Pic.Floater := '!';
2297                      Pic.Start_Float := Invalid_Position;
2298                      Pic.End_Float := Invalid_Position;
2299                   end if;
2300
2301                   Picture;
2302                   Set_State (Okay);
2303                   return;
2304
2305                when 'Z' | 'z' =>
2306
2307                   --  Can't have Z and a floating sign
2308
2309                   if State = Okay then
2310                      Set_State (Reject);
2311                   end if;
2312
2313                   Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2314                   Zero_Suppression;
2315                   Trailing_Currency;
2316                   Optional_RHS_Sign;
2317                   return;
2318
2319                when '.' | 'V' | 'v' =>
2320                   if State /= Okay then
2321                      Pic.Floater := '!';
2322                      Pic.Start_Float := Invalid_Position;
2323                      Pic.End_Float := Invalid_Position;
2324                   end if;
2325
2326                   --  Don't assume that state is okay, haven't seen a digit
2327
2328                   Picture;
2329                   return;
2330
2331                when others =>
2332                   return;
2333
2334             end case;
2335          end loop;
2336       end Picture_Minus;
2337
2338       ------------------
2339       -- Picture_Plus --
2340       ------------------
2341
2342       procedure Picture_Plus is
2343       begin
2344          Pic.Sign_Position := Index;
2345
2346          --  Treat as a floating sign, and unwind otherwise
2347
2348          Pic.Floater := '+';
2349          Pic.Start_Float := Index;
2350          Pic.End_Float := Index;
2351
2352          --  Don't increment Pic.Max_Leading_Digits, we need one "real"
2353          --  sign place.
2354
2355          Skip; --  Known Plus
2356
2357          loop
2358             case Look is
2359
2360                when '_' | '0' | '/' =>
2361                   Pic.End_Float := Index;
2362                   Skip;
2363
2364                when 'B' | 'b'  =>
2365                   Pic.End_Float := Index;
2366                   Pic.Picture.Expanded (Index) := 'b';
2367                   Skip;
2368
2369                when '+' =>
2370                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2371                   Pic.End_Float := Index;
2372                   Skip;
2373                   Set_State (Okay);  --  "++" is enough
2374                   Floating_Plus;
2375                   Trailing_Currency;
2376                   return;
2377
2378                when '$' | '#' | '9' | '*' =>
2379                   if State /= Okay then
2380                      Pic.Floater := '!';
2381                      Pic.Start_Float := Invalid_Position;
2382                      Pic.End_Float := Invalid_Position;
2383                   end if;
2384
2385                   Picture;
2386                   Set_State (Okay);
2387                   return;
2388
2389                when 'Z' | 'z' =>
2390                   if State = Okay then
2391                      Set_State (Reject);
2392                   end if;
2393
2394                   --  Can't have Z and a floating sign
2395
2396                   Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2397
2398                   --  '+Z' is acceptable
2399
2400                   Set_State (Okay);
2401
2402                   Zero_Suppression;
2403                   Trailing_Currency;
2404                   Optional_RHS_Sign;
2405                   return;
2406
2407                when '.' | 'V' | 'v' =>
2408                   if State /= Okay then
2409                      Pic.Floater := '!';
2410                      Pic.Start_Float := Invalid_Position;
2411                      Pic.End_Float := Invalid_Position;
2412                   end if;
2413
2414                   --  Don't assume that state is okay, haven't seen a digit
2415
2416                   Picture;
2417                   return;
2418
2419                when others =>
2420                   return;
2421
2422             end case;
2423          end loop;
2424       end Picture_Plus;
2425
2426       --------------------
2427       -- Picture_String --
2428       --------------------
2429
2430       procedure Picture_String is
2431       begin
2432          while Is_Insert loop
2433             Skip;
2434          end loop;
2435
2436          case Look is
2437
2438             when '$' | '#' =>
2439                Picture;
2440                Optional_RHS_Sign;
2441
2442             when '+' =>
2443                Picture_Plus;
2444
2445             when '-' =>
2446                Picture_Minus;
2447
2448             when '<' =>
2449                Picture_Bracket;
2450
2451             when 'Z' | 'z' =>
2452                Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2453                Zero_Suppression;
2454                Trailing_Currency;
2455                Optional_RHS_Sign;
2456
2457             when '*' =>
2458                Star_Suppression;
2459                Trailing_Currency;
2460                Optional_RHS_Sign;
2461
2462             when '9' | '.' | 'V' | 'v' =>
2463                Number;
2464                Trailing_Currency;
2465                Optional_RHS_Sign;
2466
2467             when others =>
2468                raise Picture_Error;
2469
2470          end case;
2471
2472          --  Blank when zero either if the PIC does not contain a '9' or if
2473          --  requested by the user and no '*'.
2474
2475          Pic.Blank_When_Zero :=
2476            (Computed_BWZ or else Pic.Blank_When_Zero)
2477              and then not Pic.Star_Fill;
2478
2479          --  Star fill if '*' and no '9'
2480
2481          Pic.Star_Fill := Pic.Star_Fill and then Computed_BWZ;
2482
2483          if not At_End then
2484             Set_State (Reject);
2485          end if;
2486
2487       end Picture_String;
2488
2489       ---------------
2490       -- Set_State --
2491       ---------------
2492
2493       procedure Set_State (L : Legality) is
2494       begin
2495          State := L;
2496       end Set_State;
2497
2498       ----------
2499       -- Skip --
2500       ----------
2501
2502       procedure Skip is
2503       begin
2504          Index := Index + 1;
2505       end Skip;
2506
2507       ----------------------
2508       -- Star_Suppression --
2509       ----------------------
2510
2511       procedure Star_Suppression is
2512       begin
2513          Pic.Floater := '*';
2514          Pic.Start_Float := Index;
2515          Pic.End_Float := Index;
2516          Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2517          Set_State (Okay);
2518
2519          --  Even a single * is a valid picture
2520
2521          Pic.Star_Fill := True;
2522          Skip; --  Known *
2523
2524          loop
2525             if At_End then
2526                return;
2527             end if;
2528
2529             case Look is
2530
2531                when '_' | '0' | '/' =>
2532                   Pic.End_Float := Index;
2533                   Skip;
2534
2535                when 'B' | 'b'  =>
2536                   Pic.End_Float := Index;
2537                   Pic.Picture.Expanded (Index) := 'b';
2538                   Skip;
2539
2540                when '*' =>
2541                   Pic.End_Float := Index;
2542                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2543                   Set_State (Okay); Skip;
2544
2545                when '9' =>
2546                   Set_State (Okay);
2547                   Number_Completion;
2548                   return;
2549
2550                when '.' | 'V' | 'v' =>
2551                   Pic.Radix_Position := Index;
2552                   Skip;
2553                   Number_Fraction_Or_Star_Fill;
2554                   return;
2555
2556                when '#' | '$' =>
2557                   Trailing_Currency;
2558                   Set_State (Okay);
2559                   return;
2560
2561                when others => raise Picture_Error;
2562             end case;
2563          end loop;
2564       end Star_Suppression;
2565
2566       ----------------------
2567       -- Trailing_Bracket --
2568       ----------------------
2569
2570       procedure Trailing_Bracket is
2571       begin
2572          if Look = '>' then
2573             Pic.Second_Sign := Index;
2574             Skip;
2575          else
2576             raise Picture_Error;
2577          end if;
2578       end Trailing_Bracket;
2579
2580       -----------------------
2581       -- Trailing_Currency --
2582       -----------------------
2583
2584       procedure Trailing_Currency is
2585       begin
2586          if At_End then
2587             return;
2588          end if;
2589
2590          if Look = '$' then
2591             Pic.Start_Currency := Index;
2592             Pic.End_Currency := Index;
2593             Skip;
2594
2595          else
2596             while not At_End and then Look = '#' loop
2597                if Pic.Start_Currency = Invalid_Position then
2598                   Pic.Start_Currency := Index;
2599                end if;
2600
2601                Pic.End_Currency := Index;
2602                Skip;
2603             end loop;
2604          end if;
2605
2606          loop
2607             if At_End then
2608                return;
2609             end if;
2610
2611             case Look is
2612                when '_' | '0' | '/' => Skip;
2613
2614                when 'B' | 'b'  =>
2615                   Pic.Picture.Expanded (Index) := 'b';
2616                   Skip;
2617
2618                when others => return;
2619             end case;
2620          end loop;
2621       end Trailing_Currency;
2622
2623       ----------------------
2624       -- Zero_Suppression --
2625       ----------------------
2626
2627       procedure Zero_Suppression is
2628       begin
2629          Pic.Floater := 'Z';
2630          Pic.Start_Float := Index;
2631          Pic.End_Float := Index;
2632          Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2633          Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2634
2635          Skip; --  Known Z
2636
2637          loop
2638             --  Even a single Z is a valid picture
2639
2640             if At_End then
2641                Set_State (Okay);
2642                return;
2643             end if;
2644
2645             case Look is
2646                when '_' | '0' | '/' =>
2647                   Pic.End_Float := Index;
2648                   Skip;
2649
2650                when 'B' | 'b'  =>
2651                   Pic.End_Float := Index;
2652                   Pic.Picture.Expanded (Index) := 'b';
2653                   Skip;
2654
2655                when 'Z' | 'z' =>
2656                   Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2657
2658                   Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2659                   Pic.End_Float := Index;
2660                   Set_State (Okay);
2661                   Skip;
2662
2663                when '9' =>
2664                   Set_State (Okay);
2665                   Number_Completion;
2666                   return;
2667
2668                when '.' | 'V' | 'v' =>
2669                   Pic.Radix_Position := Index;
2670                   Skip;
2671                   Number_Fraction_Or_Z_Fill;
2672                   return;
2673
2674                when '#' | '$' =>
2675                   Trailing_Currency;
2676                   Set_State (Okay);
2677                   return;
2678
2679                when others =>
2680                   return;
2681             end case;
2682          end loop;
2683       end Zero_Suppression;
2684
2685    --  Start of processing for Precalculate
2686
2687    begin
2688       Picture_String;
2689
2690       if State = Reject then
2691          raise Picture_Error;
2692       end if;
2693
2694    exception
2695
2696       when Constraint_Error =>
2697
2698       --  To deal with special cases like null strings
2699
2700       raise Picture_Error;
2701
2702    end Precalculate;
2703
2704    ----------------
2705    -- To_Picture --
2706    ----------------
2707
2708    function To_Picture
2709      (Pic_String      : String;
2710       Blank_When_Zero : Boolean := False) return Picture
2711    is
2712       Result : Picture;
2713
2714    begin
2715       declare
2716          Item : constant String := Expand (Pic_String);
2717
2718       begin
2719          Result.Contents.Picture         := (Item'Length, Item);
2720          Result.Contents.Original_BWZ := Blank_When_Zero;
2721          Result.Contents.Blank_When_Zero := Blank_When_Zero;
2722          Precalculate (Result.Contents);
2723          return Result;
2724       end;
2725
2726    exception
2727       when others =>
2728          raise Picture_Error;
2729
2730    end To_Picture;
2731
2732    -------------
2733    -- To_Wide --
2734    -------------
2735
2736    function To_Wide (C : Character) return Wide_Character is
2737    begin
2738       return Wide_Character'Val (Character'Pos (C));
2739    end To_Wide;
2740
2741    -----------
2742    -- Valid --
2743    -----------
2744
2745    function Valid
2746      (Pic_String      : String;
2747       Blank_When_Zero : Boolean := False) return Boolean
2748    is
2749    begin
2750       declare
2751          Expanded_Pic : constant String := Expand (Pic_String);
2752          --  Raises Picture_Error if Item not well-formed
2753
2754          Format_Rec : Format_Record;
2755
2756       begin
2757          Format_Rec.Picture := (Expanded_Pic'Length, Expanded_Pic);
2758          Format_Rec.Blank_When_Zero := Blank_When_Zero;
2759          Format_Rec.Original_BWZ := Blank_When_Zero;
2760          Precalculate (Format_Rec);
2761
2762          --  False only if Blank_When_0 is True but the pic string has a '*'
2763
2764          return not Blank_When_Zero
2765            or else Strings_Fixed.Index (Expanded_Pic, "*") = 0;
2766       end;
2767
2768    exception
2769       when others => return False;
2770    end Valid;
2771
2772 end Ada.Wide_Text_IO.Editing;