1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . T E X T _ I O . E D I T I N G --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Ada.Strings.Fixed;
37 package body Ada.Text_IO.Editing is
39 package Strings renames Ada.Strings;
40 package Strings_Fixed renames Ada.Strings.Fixed;
41 package Text_IO renames Ada.Text_IO;
47 function Blank_When_Zero (Pic : in Picture) return Boolean is
49 return Pic.Contents.Original_BWZ;
56 function Expand (Picture : in String) return String is
57 Result : String (1 .. MAX_PICSIZE);
58 Picture_Index : Integer := Picture'First;
59 Result_Index : Integer := Result'First;
63 package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
66 if Picture'Length < 1 then
70 if Picture (Picture'First) = '(' then
75 case Picture (Picture_Index) is
78 Int_IO.Get (Picture (Picture_Index + 1 .. Picture'Last),
81 if Picture (Last + 1) /= ')' then
85 -- In what follows note that one copy of the repeated
86 -- character has already been made, so a count of one is a
87 -- no-op, and a count of zero erases a character.
89 for J in 2 .. Count loop
90 Result (Result_Index + J - 2) := Picture (Picture_Index - 1);
93 Result_Index := Result_Index + Count - 1;
95 -- Last + 1 was a ')' throw it away too.
97 Picture_Index := Last + 2;
103 Result (Result_Index) := Picture (Picture_Index);
104 Picture_Index := Picture_Index + 1;
105 Result_Index := Result_Index + 1;
109 exit when Picture_Index > Picture'Last;
112 return Result (1 .. Result_Index - 1);
124 function Format_Number
125 (Pic : Format_Record;
127 Currency_Symbol : String;
128 Fill_Character : Character;
129 Separator_Character : Character;
130 Radix_Point : Character)
133 Attrs : Number_Attributes := Parse_Number_String (Number);
135 Rounded : String := Number;
137 Sign_Position : Integer := Pic.Sign_Position; -- may float.
139 Answer : String (1 .. Pic.Picture.Length) := Pic.Picture.Expanded;
141 Currency_Pos : Integer := Pic.Start_Currency;
143 Dollar : Boolean := False;
144 -- Overridden immediately if necessary.
146 Zero : Boolean := True;
147 -- Set to False when a non-zero digit is output.
151 -- If the picture has fewer decimal places than the number, the image
152 -- must be rounded according to the usual rules.
154 if Attrs.Has_Fraction then
156 R : constant Integer :=
157 (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
158 - Pic.Max_Trailing_Digits;
163 R_Pos := Attrs.End_Of_Fraction - R;
165 if Rounded (R_Pos + 1) > '4' then
167 if Rounded (R_Pos) = '.' then
171 if Rounded (R_Pos) /= '9' then
172 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
174 Rounded (R_Pos) := '0';
178 if Rounded (R_Pos) = '.' then
182 if Rounded (R_Pos) /= '9' then
183 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
186 Rounded (R_Pos) := '0';
191 -- The rounding may add a digit in front. Either the
192 -- leading blank or the sign (already captured) can
196 Rounded (R_Pos) := '1';
197 Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
205 if Pic.Start_Currency /= Invalid_Position then
206 Dollar := Answer (Pic.Start_Currency) = '$';
209 -- Fix up "direct inserts" outside the playing field. Set up as one
210 -- loop to do the beginning, one (reverse) loop to do the end.
214 exit when Last = Pic.Start_Float;
215 exit when Last = Pic.Radix_Position;
216 exit when Answer (Last) = '9';
218 case Answer (Last) is
221 Answer (Last) := Separator_Character;
224 Answer (Last) := ' ';
231 exit when Last = Answer'Last;
236 -- Now for the end...
238 for J in reverse Last .. Answer'Last loop
239 exit when J = Pic.Radix_Position;
241 -- Do this test First, Separator_Character can equal Pic.Floater.
243 if Answer (J) = Pic.Floater then
250 Answer (J) := Separator_Character;
266 if Pic.Start_Currency /= -1
267 and then Answer (Pic.Start_Currency) = '#'
268 and then Pic.Floater /= '#'
270 if Currency_Symbol'Length >
271 Pic.End_Currency - Pic.Start_Currency + 1
275 elsif Currency_Symbol'Length =
276 Pic.End_Currency - Pic.Start_Currency + 1
278 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
281 elsif Pic.Radix_Position = Invalid_Position
282 or else Pic.Start_Currency < Pic.Radix_Position
284 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
286 Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
287 Pic.End_Currency) := Currency_Symbol;
290 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
292 Answer (Pic.Start_Currency ..
293 Pic.Start_Currency + Currency_Symbol'Length - 1) :=
298 -- Fill in leading digits
300 if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
301 Pic.Max_Leading_Digits
306 if Pic.Radix_Position = Invalid_Position then
307 Position := Answer'Last;
309 Position := Pic.Radix_Position - 1;
312 for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
314 while Answer (Position) /= '9'
315 and Answer (Position) /= Pic.Floater
317 if Answer (Position) = '_' then
318 Answer (Position) := Separator_Character;
320 elsif Answer (Position) = 'b' then
321 Answer (Position) := ' ';
324 Position := Position - 1;
327 Answer (Position) := Rounded (J);
329 if Rounded (J) /= '0' then
333 Position := Position - 1;
338 if Pic.Start_Float = Invalid_Position then
340 -- No leading floats, but need to change '9' to '0', '_' to
341 -- Separator_Character and 'b' to ' '.
343 for J in Last .. Position loop
345 -- Last set when fixing the "uninteresting" leaders above.
346 -- Don't duplicate the work.
348 if Answer (J) = '9' then
351 elsif Answer (J) = '_' then
352 Answer (J) := Separator_Character;
354 elsif Answer (J) = 'b' then
359 elsif Pic.Floater = '<'
365 for J in Pic.End_Float .. Position loop -- May be null range.
366 if Answer (J) = '9' then
369 elsif Answer (J) = '_' then
370 Answer (J) := Separator_Character;
372 elsif Answer (J) = 'b' then
377 if Position > Pic.End_Float then
378 Position := Pic.End_Float;
381 for J in Pic.Start_Float .. Position - 1 loop
385 Answer (Position) := Pic.Floater;
386 Sign_Position := Position;
388 elsif Pic.Floater = '$' then
390 for J in Pic.End_Float .. Position loop -- May be null range.
391 if Answer (J) = '9' then
394 elsif Answer (J) = '_' then
395 Answer (J) := ' '; -- no separators before leftmost digit.
397 elsif Answer (J) = 'b' then
402 if Position > Pic.End_Float then
403 Position := Pic.End_Float;
406 for J in Pic.Start_Float .. Position - 1 loop
410 Answer (Position) := Pic.Floater;
411 Currency_Pos := Position;
413 elsif Pic.Floater = '*' then
415 for J in Pic.End_Float .. Position loop -- May be null range.
416 if Answer (J) = '9' then
419 elsif Answer (J) = '_' then
420 Answer (J) := Separator_Character;
422 elsif Answer (J) = 'b' then
427 if Position > Pic.End_Float then
428 Position := Pic.End_Float;
431 for J in Pic.Start_Float .. Position loop
436 if Pic.Floater = '#' then
437 Currency_Pos := Currency_Symbol'Length;
440 for J in reverse Pic.Start_Float .. Position loop
444 Answer (J) := Fill_Character;
446 when 'Z' | 'b' | '/' | '0' =>
452 when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
456 if Currency_Pos = 0 then
459 Answer (J) := Currency_Symbol (Currency_Pos);
460 Currency_Pos := Currency_Pos - 1;
468 Answer (J) := Fill_Character;
474 if Currency_Pos = 0 then
478 Answer (J) := Currency_Symbol (Currency_Pos);
479 Currency_Pos := Currency_Pos - 1;
493 if Pic.Floater = '#' and then Currency_Pos /= 0 then
500 if Sign_Position = Invalid_Position then
501 if Attrs.Negative then
506 if Attrs.Negative then
507 case Answer (Sign_Position) is
508 when 'C' | 'D' | '-' =>
512 Answer (Sign_Position) := '-';
515 Answer (Sign_Position) := '(';
516 Answer (Pic.Second_Sign) := ')';
525 case Answer (Sign_Position) is
528 Answer (Sign_Position) := ' ';
530 when '<' | 'C' | 'D' =>
531 Answer (Sign_Position) := ' ';
532 Answer (Pic.Second_Sign) := ' ';
544 -- Fill in trailing digits
546 if Pic.Max_Trailing_Digits > 0 then
548 if Attrs.Has_Fraction then
549 Position := Attrs.Start_Of_Fraction;
550 Last := Pic.Radix_Position + 1;
552 for J in Last .. Answer'Last loop
554 if Answer (J) = '9' or Answer (J) = Pic.Floater then
555 Answer (J) := Rounded (Position);
557 if Rounded (Position) /= '0' then
561 Position := Position + 1;
564 -- Used up fraction but remember place in Answer
566 exit when Position > Attrs.End_Of_Fraction;
568 elsif Answer (J) = 'b' then
571 elsif Answer (J) = '_' then
572 Answer (J) := Separator_Character;
582 Position := Pic.Radix_Position + 1;
585 -- Now fill remaining 9's with zeros and _ with separators
589 for J in Position .. Last loop
590 if Answer (J) = '9' then
593 elsif Answer (J) = Pic.Floater then
596 elsif Answer (J) = '_' then
597 Answer (J) := Separator_Character;
599 elsif Answer (J) = 'b' then
605 Position := Last + 1;
608 if Pic.Floater = '#' and then Currency_Pos /= 0 then
612 -- No trailing digits, but now J may need to stick in a currency
615 if Pic.Start_Currency = Invalid_Position then
616 Position := Answer'Last + 1;
618 Position := Pic.Start_Currency;
622 for J in Position .. Answer'Last loop
624 if Pic.Start_Currency /= Invalid_Position and then
625 Answer (Pic.Start_Currency) = '#' then
629 -- Note: There are some weird cases J can imagine with 'b' or '#'
630 -- in currency strings where the following code will cause
631 -- glitches. The trick is to tell when the character in the
632 -- answer should be checked, and when to look at the original
633 -- string. Some other time. RIE 11/26/96 ???
637 Answer (J) := Fill_Character;
643 if Currency_Pos > Currency_Symbol'Length then
647 Answer (J) := Currency_Symbol (Currency_Pos);
648 Currency_Pos := Currency_Pos + 1;
656 Answer (J) := Fill_Character;
662 if Currency_Pos > Currency_Symbol'Length then
665 Answer (J) := Currency_Symbol (Currency_Pos);
666 Currency_Pos := Currency_Pos + 1;
680 -- Now get rid of Blank_when_Zero and complete Star fill.
682 if Zero and Pic.Blank_When_Zero then
684 -- Value is zero, and blank it.
689 Last := Last - 1 + Currency_Symbol'Length;
692 if Pic.Radix_Position /= Invalid_Position and then
693 Answer (Pic.Radix_Position) = 'V' then
697 return String' (1 .. Last => ' ');
699 elsif Zero and Pic.Star_Fill then
703 Last := Last - 1 + Currency_Symbol'Length;
706 if Pic.Radix_Position /= Invalid_Position then
708 if Answer (Pic.Radix_Position) = 'V' then
712 if Pic.Radix_Position > Pic.Start_Currency then
713 return String' (1 .. Pic.Radix_Position - 1 => '*') &
715 String' (Pic.Radix_Position + 1 .. Last => '*');
721 Pic.Radix_Position + Currency_Symbol'Length - 2 =>
724 (Pic.Radix_Position + Currency_Symbol'Length .. Last
729 return String' (1 .. Pic.Radix_Position - 1 => '*') &
731 String' (Pic.Radix_Position + 1 .. Last => '*');
735 return String' (1 .. Last => '*');
738 -- This was once a simple return statement, now there are nine
739 -- different return cases. Not to mention the five above to deal
740 -- with zeros. Why not split things out?
742 -- Processing the radix and sign expansion separately
743 -- would require lots of copying--the string and some of its
744 -- indices--without really simplifying the logic. The cases are:
746 -- 1) Expand $, replace '.' with Radix_Point
747 -- 2) No currency expansion, replace '.' with Radix_Point
748 -- 3) Expand $, radix blanked
749 -- 4) No currency expansion, radix blanked
751 -- 6) Expand $, Elide V
752 -- 7) Elide V, Expand $ (Two cases depending on order.)
753 -- 8) No radix, expand $
754 -- 9) No radix, no currency expansion
756 if Pic.Radix_Position /= Invalid_Position then
758 if Answer (Pic.Radix_Position) = '.' then
759 Answer (Pic.Radix_Position) := Radix_Point;
763 -- 1) Expand $, replace '.' with Radix_Point
765 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
766 Answer (Currency_Pos + 1 .. Answer'Last);
769 -- 2) No currency expansion, replace '.' with Radix_Point
774 elsif Answer (Pic.Radix_Position) = ' ' then -- blanked radix.
777 -- 3) Expand $, radix blanked
779 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
780 Answer (Currency_Pos + 1 .. Answer'Last);
783 -- 4) No expansion, radix blanked
795 return Answer (1 .. Pic.Radix_Position - 1) &
796 Answer (Pic.Radix_Position + 1 .. Answer'Last);
798 elsif Currency_Pos < Pic.Radix_Position then
800 -- 6) Expand $, Elide V
802 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
803 Answer (Currency_Pos + 1 .. Pic.Radix_Position - 1) &
804 Answer (Pic.Radix_Position + 1 .. Answer'Last);
807 -- 7) Elide V, Expand $
809 return Answer (1 .. Pic.Radix_Position - 1) &
810 Answer (Pic.Radix_Position + 1 .. Currency_Pos - 1) &
812 Answer (Currency_Pos + 1 .. Answer'Last);
818 -- 8) No radix, expand $
820 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
821 Answer (Currency_Pos + 1 .. Answer'Last);
824 -- 9) No radix, no currency expansion
831 -------------------------
832 -- Parse_Number_String --
833 -------------------------
835 function Parse_Number_String (Str : String) return Number_Attributes is
836 Answer : Number_Attributes;
839 for J in Str'Range loop
847 -- Decide if this is the start of a number.
848 -- If so, figure out which one...
850 if Answer.Has_Fraction then
851 Answer.End_Of_Fraction := J;
853 if Answer.Start_Of_Int = Invalid_Position then
855 Answer.Start_Of_Int := J;
857 Answer.End_Of_Int := J;
862 -- Only count a zero before the decimal point if it follows a
863 -- non-zero digit. After the decimal point, zeros will be
864 -- counted if followed by a non-zero digit.
866 if not Answer.Has_Fraction then
867 if Answer.Start_Of_Int /= Invalid_Position then
868 Answer.End_Of_Int := J;
876 Answer.Negative := True;
880 -- Close integer, start fraction
882 if Answer.Has_Fraction then
886 -- Two decimal points is a no-no.
888 Answer.Has_Fraction := True;
889 Answer.End_Of_Fraction := J;
891 -- Could leave this at Invalid_Position, but this seems the
892 -- right way to indicate a null range...
894 Answer.Start_Of_Fraction := J + 1;
895 Answer.End_Of_Int := J - 1;
898 raise Picture_Error; -- can this happen? probably not!
902 if Answer.Start_Of_Int = Invalid_Position then
903 Answer.Start_Of_Int := Answer.End_Of_Int + 1;
906 -- No significant (intger) digits needs a null range.
910 end Parse_Number_String;
916 -- The following ensures that we return B and not b being careful not
917 -- to break things which expect lower case b for blank. See CXF3A02.
919 function Pic_String (Pic : in Picture) return String is
920 Temp : String (1 .. Pic.Contents.Picture.Length) :=
921 Pic.Contents.Picture.Expanded;
923 for J in Temp'Range loop
924 if Temp (J) = 'b' then Temp (J) := 'B'; end if;
934 procedure Precalculate (Pic : in out Format_Record) is
936 Computed_BWZ : Boolean := True;
937 Debug : Boolean := False;
939 type Legality is (Okay, Reject);
940 State : Legality := Reject;
941 -- Start in reject, which will reject null strings.
943 Index : Pic_Index := Pic.Picture.Expanded'First;
945 function At_End return Boolean;
946 pragma Inline (At_End);
948 procedure Set_State (L : Legality);
949 pragma Inline (Set_State);
951 function Look return Character;
952 pragma Inline (Look);
954 function Is_Insert return Boolean;
955 pragma Inline (Is_Insert);
958 pragma Inline (Skip);
960 procedure Debug_Start (Name : String);
961 pragma Inline (Debug_Start);
963 procedure Debug_Integer (Value : in Integer; S : String);
964 pragma Inline (Debug_Integer);
966 procedure Trailing_Currency;
967 procedure Trailing_Bracket;
968 procedure Number_Fraction;
969 procedure Number_Completion;
970 procedure Number_Fraction_Or_Bracket;
971 procedure Number_Fraction_Or_Z_Fill;
972 procedure Zero_Suppression;
973 procedure Floating_Bracket;
974 procedure Number_Fraction_Or_Star_Fill;
975 procedure Star_Suppression;
976 procedure Number_Fraction_Or_Dollar;
977 procedure Leading_Dollar;
978 procedure Number_Fraction_Or_Pound;
979 procedure Leading_Pound;
981 procedure Floating_Plus;
982 procedure Floating_Minus;
983 procedure Picture_Plus;
984 procedure Picture_Minus;
985 procedure Picture_Bracket;
987 procedure Optional_RHS_Sign;
988 procedure Picture_String;
994 function At_End return Boolean is
996 return Index > Pic.Picture.Length;
1003 procedure Debug_Integer (Value : in Integer; S : String) is
1004 use Ada.Text_IO; -- needed for >
1007 if Debug and then Value > 0 then
1008 if Ada.Text_IO.Col > 70 - S'Length then
1009 Ada.Text_IO.New_Line;
1012 Ada.Text_IO.Put (' ' & S & Integer'Image (Value) & ',');
1020 procedure Debug_Start (Name : String) is
1023 Ada.Text_IO.Put_Line (" In " & Name & '.');
1027 ----------------------
1028 -- Floating_Bracket --
1029 ----------------------
1031 -- Note that Floating_Bracket is only called with an acceptable
1032 -- prefix. But we don't set Okay, because we must end with a '>'.
1034 procedure Floating_Bracket is
1036 Debug_Start ("Floating_Bracket");
1038 Pic.End_Float := Index;
1039 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1041 -- First bracket wasn't counted...
1052 when '_' | '0' | '/' =>
1053 Pic.End_Float := Index;
1057 Pic.End_Float := Index;
1058 Pic.Picture.Expanded (Index) := 'b';
1062 Pic.End_Float := Index;
1063 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1075 when 'V' | 'v' | '.' =>
1076 Pic.Radix_Position := Index;
1078 Number_Fraction_Or_Bracket;
1085 end Floating_Bracket;
1088 --------------------
1089 -- Floating_Minus --
1090 --------------------
1092 procedure Floating_Minus is
1094 Debug_Start ("Floating_Minus");
1102 when '_' | '0' | '/' =>
1103 Pic.End_Float := Index;
1107 Pic.End_Float := Index;
1108 Pic.Picture.Expanded (Index) := 'b';
1112 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1113 Pic.End_Float := Index;
1120 when '.' | 'V' | 'v' =>
1121 Pic.Radix_Position := Index;
1124 while Is_Insert loop
1141 Pic.Max_Trailing_Digits :=
1142 Pic.Max_Trailing_Digits + 1;
1143 Pic.End_Float := Index;
1146 when '_' | '0' | '/' =>
1150 Pic.Picture.Expanded (Index) := 'b';
1175 procedure Floating_Plus is
1177 Debug_Start ("Floating_Plus");
1185 when '_' | '0' | '/' =>
1186 Pic.End_Float := Index;
1190 Pic.End_Float := Index;
1191 Pic.Picture.Expanded (Index) := 'b';
1195 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1196 Pic.End_Float := Index;
1203 when '.' | 'V' | 'v' =>
1204 Pic.Radix_Position := Index;
1207 while Is_Insert loop
1224 Pic.Max_Trailing_Digits :=
1225 Pic.Max_Trailing_Digits + 1;
1226 Pic.End_Float := Index;
1229 when '_' | '0' | '/' =>
1233 Pic.Picture.Expanded (Index) := 'b';
1259 function Is_Insert return Boolean is
1265 case Pic.Picture.Expanded (Index) is
1267 when '_' | '0' | '/' => return True;
1270 Pic.Picture.Expanded (Index) := 'b'; -- canonical
1273 when others => return False;
1277 --------------------
1278 -- Leading_Dollar --
1279 --------------------
1281 -- Note that Leading_Dollar can be called in either State.
1282 -- It will set state to Okay only if a 9 or (second) $
1285 -- Also notice the tricky bit with State and Zero_Suppression.
1286 -- Zero_Suppression is Picture_Error if a '$' or a '9' has been
1287 -- encountered, exactly the cases where State has been set.
1289 procedure Leading_Dollar is
1291 Debug_Start ("Leading_Dollar");
1293 -- Treat as a floating dollar, and unwind otherwise.
1296 Pic.Start_Currency := Index;
1297 Pic.End_Currency := Index;
1298 Pic.Start_Float := Index;
1299 Pic.End_Float := Index;
1301 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1313 when '_' | '0' | '/' =>
1314 Pic.End_Float := Index;
1317 -- A trailing insertion character is not part of the
1318 -- floating currency, so need to look ahead.
1321 Pic.End_Float := Pic.End_Float - 1;
1325 Pic.End_Float := Index;
1326 Pic.Picture.Expanded (Index) := 'b';
1330 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1332 if State = Okay then
1333 raise Picture_Error;
1335 -- Will overwrite Floater and Start_Float
1341 if State = Okay then
1342 raise Picture_Error;
1344 -- Will overwrite Floater and Start_Float
1350 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1351 Pic.End_Float := Index;
1352 Pic.End_Currency := Index;
1353 Set_State (Okay); Skip;
1356 if State /= Okay then
1358 Pic.Start_Float := Invalid_Position;
1359 Pic.End_Float := Invalid_Position;
1362 -- A single dollar does not a floating make.
1367 when 'V' | 'v' | '.' =>
1368 if State /= Okay then
1370 Pic.Start_Float := Invalid_Position;
1371 Pic.End_Float := Invalid_Position;
1374 -- Only one dollar before the sign is okay,
1375 -- but doesn't float.
1377 Pic.Radix_Position := Index;
1379 Number_Fraction_Or_Dollar;
1393 -- This one is complex! A Leading_Pound can be fixed or floating,
1394 -- but in some cases the decision has to be deferred until we leave
1395 -- this procedure. Also note that Leading_Pound can be called in
1398 -- It will set state to Okay only if a 9 or (second) # is
1401 -- One Last note: In ambiguous cases, the currency is treated as
1402 -- floating unless there is only one '#'.
1404 procedure Leading_Pound is
1406 Inserts : Boolean := False;
1407 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1409 Must_Float : Boolean := False;
1410 -- Set to true if a '#' occurs after an insert.
1413 Debug_Start ("Leading_Pound");
1415 -- Treat as a floating currency. If it isn't, this will be
1416 -- overwritten later.
1420 Pic.Start_Currency := Index;
1421 Pic.End_Currency := Index;
1422 Pic.Start_Float := Index;
1423 Pic.End_Float := Index;
1425 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1428 Pic.Max_Currency_Digits := 1; -- we've seen one.
1439 when '_' | '0' | '/' =>
1440 Pic.End_Float := Index;
1445 Pic.Picture.Expanded (Index) := 'b';
1446 Pic.End_Float := Index;
1451 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1454 raise Picture_Error;
1456 Pic.Max_Leading_Digits := 0;
1458 -- Will overwrite Floater and Start_Float
1465 raise Picture_Error;
1467 Pic.Max_Leading_Digits := 0;
1469 -- Will overwrite Floater and Start_Float
1479 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1480 Pic.End_Float := Index;
1481 Pic.End_Currency := Index;
1486 if State /= Okay then
1488 -- A single '#' doesn't float.
1491 Pic.Start_Float := Invalid_Position;
1492 Pic.End_Float := Invalid_Position;
1498 when 'V' | 'v' | '.' =>
1499 if State /= Okay then
1501 Pic.Start_Float := Invalid_Position;
1502 Pic.End_Float := Invalid_Position;
1505 -- Only one pound before the sign is okay,
1506 -- but doesn't float.
1508 Pic.Radix_Position := Index;
1510 Number_Fraction_Or_Pound;
1523 function Look return Character is
1526 raise Picture_Error;
1529 return Pic.Picture.Expanded (Index);
1538 Debug_Start ("Number");
1543 when '_' | '0' | '/' =>
1547 Pic.Picture.Expanded (Index) := 'b';
1551 Computed_BWZ := False;
1552 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1556 when '.' | 'V' | 'v' =>
1557 Pic.Radix_Position := Index;
1571 -- Will return in Okay state if a '9' was seen.
1576 -----------------------
1577 -- Number_Completion --
1578 -----------------------
1580 procedure Number_Completion is
1582 Debug_Start ("Number_Completion");
1584 while not At_End loop
1587 when '_' | '0' | '/' =>
1591 Pic.Picture.Expanded (Index) := 'b';
1595 Computed_BWZ := False;
1596 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1600 when 'V' | 'v' | '.' =>
1601 Pic.Radix_Position := Index;
1610 end Number_Completion;
1612 ---------------------
1613 -- Number_Fraction --
1614 ---------------------
1616 procedure Number_Fraction is
1618 -- Note that number fraction can be called in either State.
1619 -- It will set state to Valid only if a 9 is encountered.
1621 Debug_Start ("Number_Fraction");
1629 when '_' | '0' | '/' =>
1633 Pic.Picture.Expanded (Index) := 'b';
1637 Computed_BWZ := False;
1638 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1639 Set_State (Okay); Skip;
1645 end Number_Fraction;
1647 --------------------------------
1648 -- Number_Fraction_Or_Bracket --
1649 --------------------------------
1651 procedure Number_Fraction_Or_Bracket is
1653 Debug_Start ("Number_Fraction_Or_Bracket");
1662 when '_' | '0' | '/' => Skip;
1665 Pic.Picture.Expanded (Index) := 'b';
1669 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1670 Pic.End_Float := Index;
1679 when '_' | '0' | '/' =>
1683 Pic.Picture.Expanded (Index) := 'b';
1687 Pic.Max_Trailing_Digits :=
1688 Pic.Max_Trailing_Digits + 1;
1689 Pic.End_Float := Index;
1702 end Number_Fraction_Or_Bracket;
1704 -------------------------------
1705 -- Number_Fraction_Or_Dollar --
1706 -------------------------------
1708 procedure Number_Fraction_Or_Dollar is
1710 Debug_Start ("Number_Fraction_Or_Dollar");
1718 when '_' | '0' | '/' =>
1722 Pic.Picture.Expanded (Index) := 'b';
1726 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1727 Pic.End_Float := Index;
1736 when '_' | '0' | '/' =>
1740 Pic.Picture.Expanded (Index) := 'b';
1744 Pic.Max_Trailing_Digits :=
1745 Pic.Max_Trailing_Digits + 1;
1746 Pic.End_Float := Index;
1759 end Number_Fraction_Or_Dollar;
1761 ------------------------------
1762 -- Number_Fraction_Or_Pound --
1763 ------------------------------
1765 procedure Number_Fraction_Or_Pound is
1774 when '_' | '0' | '/' =>
1778 Pic.Picture.Expanded (Index) := 'b';
1782 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1783 Pic.End_Float := Index;
1793 when '_' | '0' | '/' =>
1797 Pic.Picture.Expanded (Index) := 'b';
1801 Pic.Max_Trailing_Digits :=
1802 Pic.Max_Trailing_Digits + 1;
1803 Pic.End_Float := Index;
1818 end Number_Fraction_Or_Pound;
1820 ----------------------------------
1821 -- Number_Fraction_Or_Star_Fill --
1822 ----------------------------------
1824 procedure Number_Fraction_Or_Star_Fill is
1826 Debug_Start ("Number_Fraction_Or_Star_Fill");
1835 when '_' | '0' | '/' =>
1839 Pic.Picture.Expanded (Index) := 'b';
1843 Pic.Star_Fill := True;
1844 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1845 Pic.End_Float := Index;
1855 when '_' | '0' | '/' =>
1859 Pic.Picture.Expanded (Index) := 'b';
1863 Pic.Star_Fill := True;
1864 Pic.Max_Trailing_Digits :=
1865 Pic.Max_Trailing_Digits + 1;
1866 Pic.End_Float := Index;
1880 end Number_Fraction_Or_Star_Fill;
1882 -------------------------------
1883 -- Number_Fraction_Or_Z_Fill --
1884 -------------------------------
1886 procedure Number_Fraction_Or_Z_Fill is
1888 Debug_Start ("Number_Fraction_Or_Z_Fill");
1897 when '_' | '0' | '/' =>
1901 Pic.Picture.Expanded (Index) := 'b';
1905 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1906 Pic.End_Float := Index;
1907 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1918 when '_' | '0' | '/' =>
1922 Pic.Picture.Expanded (Index) := 'b';
1926 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1928 Pic.Max_Trailing_Digits :=
1929 Pic.Max_Trailing_Digits + 1;
1930 Pic.End_Float := Index;
1943 end Number_Fraction_Or_Z_Fill;
1945 -----------------------
1946 -- Optional_RHS_Sign --
1947 -----------------------
1949 procedure Optional_RHS_Sign is
1951 Debug_Start ("Optional_RHS_Sign");
1960 Pic.Sign_Position := Index;
1965 Pic.Sign_Position := Index;
1966 Pic.Picture.Expanded (Index) := 'C';
1969 if Look = 'R' or Look = 'r' then
1970 Pic.Second_Sign := Index;
1971 Pic.Picture.Expanded (Index) := 'R';
1975 raise Picture_Error;
1981 Pic.Sign_Position := Index;
1982 Pic.Picture.Expanded (Index) := 'D';
1985 if Look = 'B' or Look = 'b' then
1986 Pic.Second_Sign := Index;
1987 Pic.Picture.Expanded (Index) := 'B';
1991 raise Picture_Error;
1997 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
1998 Pic.Second_Sign := Index;
2002 raise Picture_Error;
2009 end Optional_RHS_Sign;
2015 -- Note that Picture can be called in either State.
2017 -- It will set state to Valid only if a 9 is encountered or floating
2018 -- currency is called.
2020 procedure Picture is
2022 Debug_Start ("Picture");
2031 when '_' | '0' | '/' =>
2035 Pic.Picture.Expanded (Index) := 'b';
2047 Computed_BWZ := False;
2049 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2052 when 'V' | 'v' | '.' =>
2053 Pic.Radix_Position := Index;
2066 ---------------------
2067 -- Picture_Bracket --
2068 ---------------------
2070 procedure Picture_Bracket is
2072 Pic.Sign_Position := Index;
2073 Debug_Start ("Picture_Bracket");
2074 Pic.Sign_Position := Index;
2076 -- Treat as a floating sign, and unwind otherwise.
2079 Pic.Start_Float := Index;
2080 Pic.End_Float := Index;
2082 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2085 Skip; -- Known Bracket
2090 when '_' | '0' | '/' =>
2091 Pic.End_Float := Index;
2095 Pic.End_Float := Index;
2096 Pic.Picture.Expanded (Index) := 'b';
2100 Set_State (Okay); -- "<<>" is enough.
2106 when '$' | '#' | '9' | '*' =>
2107 if State /= Okay then
2109 Pic.Start_Float := Invalid_Position;
2110 Pic.End_Float := Invalid_Position;
2118 when '.' | 'V' | 'v' =>
2119 if State /= Okay then
2121 Pic.Start_Float := Invalid_Position;
2122 Pic.End_Float := Invalid_Position;
2125 -- Don't assume that state is okay, haven't seen a digit
2132 raise Picture_Error;
2136 end Picture_Bracket;
2142 procedure Picture_Minus is
2144 Debug_Start ("Picture_Minus");
2146 Pic.Sign_Position := Index;
2148 -- Treat as a floating sign, and unwind otherwise.
2151 Pic.Start_Float := Index;
2152 Pic.End_Float := Index;
2154 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2157 Skip; -- Known Minus
2162 when '_' | '0' | '/' =>
2163 Pic.End_Float := Index;
2167 Pic.End_Float := Index;
2168 Pic.Picture.Expanded (Index) := 'b';
2172 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2173 Pic.End_Float := Index;
2175 Set_State (Okay); -- "-- " is enough.
2180 when '$' | '#' | '9' | '*' =>
2181 if State /= Okay then
2183 Pic.Start_Float := Invalid_Position;
2184 Pic.End_Float := Invalid_Position;
2193 -- Can't have Z and a floating sign.
2195 if State = Okay then
2199 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2205 when '.' | 'V' | 'v' =>
2206 if State /= Okay then
2208 Pic.Start_Float := Invalid_Position;
2209 Pic.End_Float := Invalid_Position;
2212 -- Don't assume that state is okay, haven't seen a digit.
2228 procedure Picture_Plus is
2230 Debug_Start ("Picture_Plus");
2231 Pic.Sign_Position := Index;
2233 -- Treat as a floating sign, and unwind otherwise.
2236 Pic.Start_Float := Index;
2237 Pic.End_Float := Index;
2239 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2247 when '_' | '0' | '/' =>
2248 Pic.End_Float := Index;
2252 Pic.End_Float := Index;
2253 Pic.Picture.Expanded (Index) := 'b';
2257 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2258 Pic.End_Float := Index;
2260 Set_State (Okay); -- "++" is enough.
2265 when '$' | '#' | '9' | '*' =>
2266 if State /= Okay then
2268 Pic.Start_Float := Invalid_Position;
2269 Pic.End_Float := Invalid_Position;
2277 if State = Okay then
2281 -- Can't have Z and a floating sign.
2283 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2285 -- '+Z' is acceptable
2294 when '.' | 'V' | 'v' =>
2295 if State /= Okay then
2297 Pic.Start_Float := Invalid_Position;
2298 Pic.End_Float := Invalid_Position;
2301 -- Don't assume that state is okay, haven't seen a digit.
2313 --------------------
2314 -- Picture_String --
2315 --------------------
2317 procedure Picture_String is
2319 Debug_Start ("Picture_String");
2321 while Is_Insert loop
2341 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2351 when '9' | '.' | 'V' | 'v' =>
2357 raise Picture_Error;
2361 -- Blank when zero either if the PIC does not contain a '9' or if
2362 -- requested by the user and no '*'
2364 Pic.Blank_When_Zero :=
2365 (Computed_BWZ or Pic.Blank_When_Zero) and not Pic.Star_Fill;
2367 -- Star fill if '*' and no '9'.
2369 Pic.Star_Fill := Pic.Star_Fill and Computed_BWZ;
2381 procedure Set_State (L : Legality) is
2383 if Debug then Ada.Text_IO.Put_Line
2384 (" Set state from " & Legality'Image (State) &
2385 " to " & Legality'Image (L));
2397 if Debug then Ada.Text_IO.Put_Line
2398 (" Skip " & Pic.Picture.Expanded (Index));
2404 ----------------------
2405 -- Star_Suppression --
2406 ----------------------
2408 procedure Star_Suppression is
2410 Debug_Start ("Star_Suppression");
2412 Pic.Start_Float := Index;
2413 Pic.End_Float := Index;
2414 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2417 -- Even a single * is a valid picture
2419 Pic.Star_Fill := True;
2429 when '_' | '0' | '/' =>
2430 Pic.End_Float := Index;
2434 Pic.End_Float := Index;
2435 Pic.Picture.Expanded (Index) := 'b';
2439 Pic.End_Float := Index;
2440 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2441 Set_State (Okay); Skip;
2448 when '.' | 'V' | 'v' =>
2449 Pic.Radix_Position := Index;
2451 Number_Fraction_Or_Star_Fill;
2459 when others => raise Picture_Error;
2462 end Star_Suppression;
2464 ----------------------
2465 -- Trailing_Bracket --
2466 ----------------------
2468 procedure Trailing_Bracket is
2470 Debug_Start ("Trailing_Bracket");
2473 Pic.Second_Sign := Index;
2476 raise Picture_Error;
2478 end Trailing_Bracket;
2480 -----------------------
2481 -- Trailing_Currency --
2482 -----------------------
2484 procedure Trailing_Currency is
2486 Debug_Start ("Trailing_Currency");
2493 Pic.Start_Currency := Index;
2494 Pic.End_Currency := Index;
2498 while not At_End and then Look = '#' loop
2499 if Pic.Start_Currency = Invalid_Position then
2500 Pic.Start_Currency := Index;
2503 Pic.End_Currency := Index;
2514 when '_' | '0' | '/' => Skip;
2517 Pic.Picture.Expanded (Index) := 'b';
2520 when others => return;
2523 end Trailing_Currency;
2525 ----------------------
2526 -- Zero_Suppression --
2527 ----------------------
2529 procedure Zero_Suppression is
2531 Debug_Start ("Zero_Suppression");
2534 Pic.Start_Float := Index;
2535 Pic.End_Float := Index;
2536 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2537 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2542 -- Even a single Z is a valid picture
2550 when '_' | '0' | '/' =>
2551 Pic.End_Float := Index;
2555 Pic.End_Float := Index;
2556 Pic.Picture.Expanded (Index) := 'b';
2560 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2562 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2563 Pic.End_Float := Index;
2572 when '.' | 'V' | 'v' =>
2573 Pic.Radix_Position := Index;
2575 Number_Fraction_Or_Z_Fill;
2587 end Zero_Suppression;
2589 -- Start of processing for Precalculate
2595 Ada.Text_IO.New_Line;
2596 Ada.Text_IO.Put (" Picture : """ &
2597 Pic.Picture.Expanded (1 .. Pic.Picture.Length) & """,");
2598 Ada.Text_IO.Put (" Floater : '" & Pic.Floater & "',");
2601 if State = Reject then
2602 raise Picture_Error;
2605 Debug_Integer (Pic.Radix_Position, "Radix Positon : ");
2606 Debug_Integer (Pic.Sign_Position, "Sign Positon : ");
2607 Debug_Integer (Pic.Second_Sign, "Second Sign : ");
2608 Debug_Integer (Pic.Start_Float, "Start Float : ");
2609 Debug_Integer (Pic.End_Float, "End Float : ");
2610 Debug_Integer (Pic.Start_Currency, "Start Currency : ");
2611 Debug_Integer (Pic.End_Currency, "End Currency : ");
2612 Debug_Integer (Pic.Max_Leading_Digits, "Max Leading Digits : ");
2613 Debug_Integer (Pic.Max_Trailing_Digits, "Max Trailing Digits : ");
2616 Ada.Text_IO.New_Line;
2621 when Constraint_Error =>
2623 -- To deal with special cases like null strings.
2625 raise Picture_Error;
2634 (Pic_String : in String;
2635 Blank_When_Zero : in Boolean := False)
2642 Item : constant String := Expand (Pic_String);
2645 Result.Contents.Picture := (Item'Length, Item);
2646 Result.Contents.Original_BWZ := Blank_When_Zero;
2647 Result.Contents.Blank_When_Zero := Blank_When_Zero;
2648 Precalculate (Result.Contents);
2654 raise Picture_Error;
2663 (Pic_String : in String;
2664 Blank_When_Zero : in Boolean := False)
2669 Expanded_Pic : constant String := Expand (Pic_String);
2670 -- Raises Picture_Error if Item not well-formed
2672 Format_Rec : Format_Record;
2675 Format_Rec.Picture := (Expanded_Pic'Length, Expanded_Pic);
2676 Format_Rec.Blank_When_Zero := Blank_When_Zero;
2677 Format_Rec.Original_BWZ := Blank_When_Zero;
2678 Precalculate (Format_Rec);
2680 -- False only if Blank_When_0 is True but the pic string has a '*'
2682 return not Blank_When_Zero or
2683 Strings_Fixed.Index (Expanded_Pic, "*") = 0;
2687 when others => return False;
2691 --------------------
2692 -- Decimal_Output --
2693 --------------------
2695 package body Decimal_Output is
2704 Currency : in String := Default_Currency;
2705 Fill : in Character := Default_Fill;
2706 Separator : in Character := Default_Separator;
2707 Radix_Mark : in Character := Default_Radix_Mark)
2711 return Format_Number
2712 (Pic.Contents, Num'Image (Item),
2713 Currency, Fill, Separator, Radix_Mark);
2722 Currency : in String := Default_Currency)
2725 Picstr : constant String := Pic_String (Pic);
2726 V_Adjust : Integer := 0;
2727 Cur_Adjust : Integer := 0;
2730 -- Check if Picstr has 'V' or '$'
2732 -- If 'V', then length is 1 less than otherwise
2734 -- If '$', then length is Currency'Length-1 more than otherwise
2736 -- This should use the string handling package ???
2738 for J in Picstr'Range loop
2739 if Picstr (J) = 'V' then
2742 elsif Picstr (J) = '$' then
2743 Cur_Adjust := Currency'Length - 1;
2747 return Picstr'Length - V_Adjust + Cur_Adjust;
2755 (File : in Text_IO.File_Type;
2758 Currency : in String := Default_Currency;
2759 Fill : in Character := Default_Fill;
2760 Separator : in Character := Default_Separator;
2761 Radix_Mark : in Character := Default_Radix_Mark)
2764 Text_IO.Put (File, Image (Item, Pic,
2765 Currency, Fill, Separator, Radix_Mark));
2771 Currency : in String := Default_Currency;
2772 Fill : in Character := Default_Fill;
2773 Separator : in Character := Default_Separator;
2774 Radix_Mark : in Character := Default_Radix_Mark)
2777 Text_IO.Put (Image (Item, Pic,
2778 Currency, Fill, Separator, Radix_Mark));
2785 Currency : in String := Default_Currency;
2786 Fill : in Character := Default_Fill;
2787 Separator : in Character := Default_Separator;
2788 Radix_Mark : in Character := Default_Radix_Mark)
2790 Result : constant String :=
2791 Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
2794 if Result'Length > To'Length then
2795 raise Text_IO.Layout_Error;
2797 Strings_Fixed.Move (Source => Result, Target => To,
2798 Justify => Strings.Right);
2809 Currency : in String := Default_Currency)
2814 Temp : constant String := Image (Item, Pic, Currency);
2815 pragma Warnings (Off, Temp);
2821 when Layout_Error => return False;
2827 end Ada.Text_IO.Editing;