1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . T E X T _ I O . E D I T I N G --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada.Strings.Fixed;
33 package body Ada.Text_IO.Editing is
35 package Strings renames Ada.Strings;
36 package Strings_Fixed renames Ada.Strings.Fixed;
37 package Text_IO renames Ada.Text_IO;
43 function Blank_When_Zero (Pic : Picture) return Boolean is
45 return Pic.Contents.Original_BWZ;
52 function Expand (Picture : String) return String is
53 Result : String (1 .. MAX_PICSIZE);
54 Picture_Index : Integer := Picture'First;
55 Result_Index : Integer := Result'First;
59 package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
62 if Picture'Length < 1 then
66 if Picture (Picture'First) = '(' then
71 case Picture (Picture_Index) is
74 Int_IO.Get (Picture (Picture_Index + 1 .. Picture'Last),
77 if Picture (Last + 1) /= ')' then
81 -- In what follows note that one copy of the repeated
82 -- character has already been made, so a count of one is a
83 -- no-op, and a count of zero erases a character.
85 if Result_Index + Count - 2 > Result'Last then
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 if Result_Index > Result'Last then
107 Result (Result_Index) := Picture (Picture_Index);
108 Picture_Index := Picture_Index + 1;
109 Result_Index := Result_Index + 1;
113 exit when Picture_Index > Picture'Last;
116 return Result (1 .. Result_Index - 1);
127 function Format_Number
128 (Pic : Format_Record;
130 Currency_Symbol : String;
131 Fill_Character : Character;
132 Separator_Character : Character;
133 Radix_Point : Character) return String
135 Attrs : Number_Attributes := Parse_Number_String (Number);
137 Rounded : String := Number;
139 Sign_Position : Integer := Pic.Sign_Position; -- may float.
141 Answer : String (1 .. Pic.Picture.Length) := Pic.Picture.Expanded;
143 Currency_Pos : Integer := Pic.Start_Currency;
144 In_Currency : Boolean := False;
146 Dollar : Boolean := False;
147 -- Overridden immediately if necessary
149 Zero : Boolean := True;
150 -- Set to False when a non-zero digit is output
154 -- If the picture has fewer decimal places than the number, the image
155 -- must be rounded according to the usual rules.
157 if Attrs.Has_Fraction then
159 R : constant Integer :=
160 (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
161 - Pic.Max_Trailing_Digits;
166 R_Pos := Attrs.End_Of_Fraction - R;
168 if Rounded (R_Pos + 1) > '4' then
170 if Rounded (R_Pos) = '.' then
174 if Rounded (R_Pos) /= '9' then
175 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
177 Rounded (R_Pos) := '0';
181 if Rounded (R_Pos) = '.' then
185 if Rounded (R_Pos) /= '9' then
186 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
189 Rounded (R_Pos) := '0';
194 -- The rounding may add a digit in front. Either the
195 -- leading blank or the sign (already captured) can
199 Rounded (R_Pos) := '1';
200 Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
208 if Pic.Start_Currency /= Invalid_Position then
209 Dollar := Answer (Pic.Start_Currency) = '$';
212 -- Fix up "direct inserts" outside the playing field. Set up as one
213 -- loop to do the beginning, one (reverse) loop to do the end.
217 exit when Last = Pic.Start_Float;
218 exit when Last = Pic.Radix_Position;
219 exit when Answer (Last) = '9';
221 case Answer (Last) is
224 Answer (Last) := Separator_Character;
227 Answer (Last) := ' ';
234 exit when Last = Answer'Last;
239 -- Now for the end...
241 for J in reverse Last .. Answer'Last loop
242 exit when J = Pic.Radix_Position;
244 -- Do this test First, Separator_Character can equal Pic.Floater
246 if Answer (J) = Pic.Floater then
253 Answer (J) := Separator_Character;
269 if Pic.Start_Currency /= -1
270 and then Answer (Pic.Start_Currency) = '#'
271 and then Pic.Floater /= '#'
273 if Currency_Symbol'Length >
274 Pic.End_Currency - Pic.Start_Currency + 1
278 elsif Currency_Symbol'Length =
279 Pic.End_Currency - Pic.Start_Currency + 1
281 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
284 elsif Pic.Radix_Position = Invalid_Position
285 or else Pic.Start_Currency < Pic.Radix_Position
287 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
289 Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
290 Pic.End_Currency) := Currency_Symbol;
293 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
295 Answer (Pic.Start_Currency ..
296 Pic.Start_Currency + Currency_Symbol'Length - 1) :=
301 -- Fill in leading digits
303 if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
304 Pic.Max_Leading_Digits
306 raise Ada.Text_IO.Layout_Error;
309 if Pic.Radix_Position = Invalid_Position then
310 Position := Answer'Last;
312 Position := Pic.Radix_Position - 1;
315 for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
317 while Answer (Position) /= '9'
318 and Answer (Position) /= Pic.Floater
320 if Answer (Position) = '_' then
321 Answer (Position) := Separator_Character;
323 elsif Answer (Position) = 'b' then
324 Answer (Position) := ' ';
327 Position := Position - 1;
330 Answer (Position) := Rounded (J);
332 if Rounded (J) /= '0' then
336 Position := Position - 1;
341 if Pic.Start_Float = Invalid_Position then
343 -- No leading floats, but need to change '9' to '0', '_' to
344 -- Separator_Character and 'b' to ' '.
346 for J in Last .. Position loop
348 -- Last set when fixing the "uninteresting" leaders above.
349 -- Don't duplicate the work.
351 if Answer (J) = '9' then
354 elsif Answer (J) = '_' then
355 Answer (J) := Separator_Character;
357 elsif Answer (J) = 'b' then
362 elsif Pic.Floater = '<'
368 for J in Pic.End_Float .. Position loop -- May be null range.
369 if Answer (J) = '9' then
372 elsif Answer (J) = '_' then
373 Answer (J) := Separator_Character;
375 elsif Answer (J) = 'b' then
380 if Position > Pic.End_Float then
381 Position := Pic.End_Float;
384 for J in Pic.Start_Float .. Position - 1 loop
388 Answer (Position) := Pic.Floater;
389 Sign_Position := Position;
391 elsif Pic.Floater = '$' then
393 for J in Pic.End_Float .. Position loop -- May be null range.
394 if Answer (J) = '9' then
397 elsif Answer (J) = '_' then
398 Answer (J) := ' '; -- no separators before leftmost digit.
400 elsif Answer (J) = 'b' then
405 if Position > Pic.End_Float then
406 Position := Pic.End_Float;
409 for J in Pic.Start_Float .. Position - 1 loop
413 Answer (Position) := Pic.Floater;
414 Currency_Pos := Position;
416 elsif Pic.Floater = '*' then
418 for J in Pic.End_Float .. Position loop -- May be null range.
419 if Answer (J) = '9' then
422 elsif Answer (J) = '_' then
423 Answer (J) := Separator_Character;
425 elsif Answer (J) = 'b' then
426 Answer (J) := Fill_Character;
430 if Position > Pic.End_Float then
431 Position := Pic.End_Float;
434 for J in Pic.Start_Float .. Position loop
435 Answer (J) := Fill_Character;
439 if Pic.Floater = '#' then
440 Currency_Pos := Currency_Symbol'Length;
444 for J in reverse Pic.Start_Float .. Position loop
448 Answer (J) := Fill_Character;
451 if In_Currency and then Currency_Pos > 0 then
452 Answer (J) := Currency_Symbol (Currency_Pos);
453 Currency_Pos := Currency_Pos - 1;
464 when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
468 if Currency_Pos = 0 then
471 Answer (J) := Currency_Symbol (Currency_Pos);
472 Currency_Pos := Currency_Pos - 1;
480 Answer (J) := Fill_Character;
486 if Currency_Pos = 0 then
490 Answer (J) := Currency_Symbol (Currency_Pos);
491 Currency_Pos := Currency_Pos - 1;
505 if Pic.Floater = '#' and then Currency_Pos /= 0 then
506 raise Ada.Text_IO.Layout_Error;
512 if Sign_Position = Invalid_Position then
513 if Attrs.Negative then
514 raise Ada.Text_IO.Layout_Error;
518 if Attrs.Negative then
519 case Answer (Sign_Position) is
520 when 'C' | 'D' | '-' =>
524 Answer (Sign_Position) := '-';
527 Answer (Sign_Position) := '(';
528 Answer (Pic.Second_Sign) := ')';
537 case Answer (Sign_Position) is
540 Answer (Sign_Position) := ' ';
542 when '<' | 'C' | 'D' =>
543 Answer (Sign_Position) := ' ';
544 Answer (Pic.Second_Sign) := ' ';
556 -- Fill in trailing digits
558 if Pic.Max_Trailing_Digits > 0 then
560 if Attrs.Has_Fraction then
561 Position := Attrs.Start_Of_Fraction;
562 Last := Pic.Radix_Position + 1;
564 for J in Last .. Answer'Last loop
566 if Answer (J) = '9' or Answer (J) = Pic.Floater then
567 Answer (J) := Rounded (Position);
569 if Rounded (Position) /= '0' then
573 Position := Position + 1;
576 -- Used up fraction but remember place in Answer
578 exit when Position > Attrs.End_Of_Fraction;
580 elsif Answer (J) = 'b' then
583 elsif Answer (J) = '_' then
584 Answer (J) := Separator_Character;
594 Position := Pic.Radix_Position + 1;
597 -- Now fill remaining 9's with zeros and _ with separators
601 for J in Position .. Last loop
602 if Answer (J) = '9' then
605 elsif Answer (J) = Pic.Floater then
608 elsif Answer (J) = '_' then
609 Answer (J) := Separator_Character;
611 elsif Answer (J) = 'b' then
617 Position := Last + 1;
620 if Pic.Floater = '#' and then Currency_Pos /= 0 then
621 raise Ada.Text_IO.Layout_Error;
624 -- No trailing digits, but now J may need to stick in a currency
627 if Pic.Start_Currency = Invalid_Position then
628 Position := Answer'Last + 1;
630 Position := Pic.Start_Currency;
634 for J in Position .. Answer'Last loop
636 if Pic.Start_Currency /= Invalid_Position and then
637 Answer (Pic.Start_Currency) = '#' then
643 Answer (J) := Fill_Character;
647 Answer (J) := Currency_Symbol (Currency_Pos);
648 Currency_Pos := Currency_Pos + 1;
650 if Currency_Pos > Currency_Symbol'Length then
651 In_Currency := False;
656 if Currency_Pos > Currency_Symbol'Length then
661 Answer (J) := Currency_Symbol (Currency_Pos);
662 Currency_Pos := Currency_Pos + 1;
664 if Currency_Pos > Currency_Symbol'Length then
665 In_Currency := False;
670 Answer (J) := Currency_Symbol (Currency_Pos);
671 Currency_Pos := Currency_Pos + 1;
676 Answer (J) := Fill_Character;
682 if Currency_Pos > Currency_Symbol'Length then
685 Answer (J) := Currency_Symbol (Currency_Pos);
686 Currency_Pos := Currency_Pos + 1;
700 -- Now get rid of Blank_when_Zero and complete Star fill
702 if Zero and Pic.Blank_When_Zero then
704 -- Value is zero, and blank it
709 Last := Last - 1 + Currency_Symbol'Length;
712 if Pic.Radix_Position /= Invalid_Position and then
713 Answer (Pic.Radix_Position) = 'V' then
717 return String'(1 .. Last => ' ');
719 elsif Zero and Pic.Star_Fill then
723 Last := Last - 1 + Currency_Symbol'Length;
726 if Pic.Radix_Position /= Invalid_Position then
728 if Answer (Pic.Radix_Position) = 'V' then
732 if Pic.Radix_Position > Pic.Start_Currency then
733 return String'(1 .. Pic.Radix_Position - 1 => '*') &
735 String'(Pic.Radix_Position + 1 .. Last => '*');
741 Pic.Radix_Position + Currency_Symbol'Length - 2 =>
744 (Pic.Radix_Position + Currency_Symbol'Length .. Last
749 return String'(1 .. Pic.Radix_Position - 1 => '*') &
751 String'(Pic.Radix_Position + 1 .. Last => '*');
755 return String'(1 .. Last => '*');
758 -- This was once a simple return statement, now there are nine
759 -- different return cases. Not to mention the five above to deal
760 -- with zeros. Why not split things out?
762 -- Processing the radix and sign expansion separately
763 -- would require lots of copying--the string and some of its
764 -- indicies--without really simplifying the logic. The cases are:
766 -- 1) Expand $, replace '.' with Radix_Point
767 -- 2) No currency expansion, replace '.' with Radix_Point
768 -- 3) Expand $, radix blanked
769 -- 4) No currency expansion, radix blanked
771 -- 6) Expand $, Elide V
772 -- 7) Elide V, Expand $ (Two cases depending on order.)
773 -- 8) No radix, expand $
774 -- 9) No radix, no currency expansion
776 if Pic.Radix_Position /= Invalid_Position then
778 if Answer (Pic.Radix_Position) = '.' then
779 Answer (Pic.Radix_Position) := Radix_Point;
783 -- 1) Expand $, replace '.' with Radix_Point
785 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
786 Answer (Currency_Pos + 1 .. Answer'Last);
789 -- 2) No currency expansion, replace '.' with Radix_Point
794 elsif Answer (Pic.Radix_Position) = ' ' then -- blanked radix.
797 -- 3) Expand $, radix blanked
799 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
800 Answer (Currency_Pos + 1 .. Answer'Last);
803 -- 4) No expansion, radix blanked
815 return Answer (1 .. Pic.Radix_Position - 1) &
816 Answer (Pic.Radix_Position + 1 .. Answer'Last);
818 elsif Currency_Pos < Pic.Radix_Position then
820 -- 6) Expand $, Elide V
822 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
823 Answer (Currency_Pos + 1 .. Pic.Radix_Position - 1) &
824 Answer (Pic.Radix_Position + 1 .. Answer'Last);
827 -- 7) Elide V, Expand $
829 return Answer (1 .. Pic.Radix_Position - 1) &
830 Answer (Pic.Radix_Position + 1 .. Currency_Pos - 1) &
832 Answer (Currency_Pos + 1 .. Answer'Last);
838 -- 8) No radix, expand $
840 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
841 Answer (Currency_Pos + 1 .. Answer'Last);
844 -- 9) No radix, no currency expansion
850 -------------------------
851 -- Parse_Number_String --
852 -------------------------
854 function Parse_Number_String (Str : String) return Number_Attributes is
855 Answer : Number_Attributes;
858 for J in Str'Range loop
866 -- Decide if this is the start of a number.
867 -- If so, figure out which one...
869 if Answer.Has_Fraction then
870 Answer.End_Of_Fraction := J;
872 if Answer.Start_Of_Int = Invalid_Position then
874 Answer.Start_Of_Int := J;
876 Answer.End_Of_Int := J;
881 -- Only count a zero before the decimal point if it follows a
882 -- non-zero digit. After the decimal point, zeros will be
883 -- counted if followed by a non-zero digit.
885 if not Answer.Has_Fraction then
886 if Answer.Start_Of_Int /= Invalid_Position then
887 Answer.End_Of_Int := J;
895 Answer.Negative := True;
899 -- Close integer, start fraction
901 if Answer.Has_Fraction then
905 -- Two decimal points is a no-no
907 Answer.Has_Fraction := True;
908 Answer.End_Of_Fraction := J;
910 -- Could leave this at Invalid_Position, but this seems the
911 -- right way to indicate a null range...
913 Answer.Start_Of_Fraction := J + 1;
914 Answer.End_Of_Int := J - 1;
917 raise Picture_Error; -- can this happen? probably not!
921 if Answer.Start_Of_Int = Invalid_Position then
922 Answer.Start_Of_Int := Answer.End_Of_Int + 1;
925 -- No significant (integer) digits needs a null range
928 end Parse_Number_String;
934 -- The following ensures that we return B and not b being careful not
935 -- to break things which expect lower case b for blank. See CXF3A02.
937 function Pic_String (Pic : Picture) return String is
938 Temp : String (1 .. Pic.Contents.Picture.Length) :=
939 Pic.Contents.Picture.Expanded;
941 for J in Temp'Range loop
942 if Temp (J) = 'b' then
954 procedure Precalculate (Pic : in out Format_Record) is
955 Debug : constant Boolean := False;
956 -- Set True to generate debug output
958 Computed_BWZ : Boolean := True;
960 type Legality is (Okay, Reject);
962 State : Legality := Reject;
963 -- Start in reject, which will reject null strings
965 Index : Pic_Index := Pic.Picture.Expanded'First;
967 function At_End return Boolean;
968 pragma Inline (At_End);
970 procedure Set_State (L : Legality);
971 pragma Inline (Set_State);
973 function Look return Character;
974 pragma Inline (Look);
976 function Is_Insert return Boolean;
977 pragma Inline (Is_Insert);
980 pragma Inline (Skip);
982 procedure Debug_Start (Name : String);
983 pragma Inline (Debug_Start);
985 procedure Debug_Integer (Value : Integer; S : String);
986 pragma Inline (Debug_Integer);
988 procedure Trailing_Currency;
989 procedure Trailing_Bracket;
990 procedure Number_Fraction;
991 procedure Number_Completion;
992 procedure Number_Fraction_Or_Bracket;
993 procedure Number_Fraction_Or_Z_Fill;
994 procedure Zero_Suppression;
995 procedure Floating_Bracket;
996 procedure Number_Fraction_Or_Star_Fill;
997 procedure Star_Suppression;
998 procedure Number_Fraction_Or_Dollar;
999 procedure Leading_Dollar;
1000 procedure Number_Fraction_Or_Pound;
1001 procedure Leading_Pound;
1003 procedure Floating_Plus;
1004 procedure Floating_Minus;
1005 procedure Picture_Plus;
1006 procedure Picture_Minus;
1007 procedure Picture_Bracket;
1009 procedure Optional_RHS_Sign;
1010 procedure Picture_String;
1011 procedure Set_Debug;
1017 function At_End return Boolean is
1019 Debug_Start ("At_End");
1020 return Index > Pic.Picture.Length;
1027 -- Needed to have a procedure to pass to pragma Debug
1029 procedure Set_Debug is
1031 -- Uncomment this line and make Debug a variable to enable debug
1042 procedure Debug_Integer (Value : Integer; S : String) is
1043 use Ada.Text_IO; -- needed for >
1046 if Debug and then Value > 0 then
1047 if Ada.Text_IO.Col > 70 - S'Length then
1048 Ada.Text_IO.New_Line;
1051 Ada.Text_IO.Put (' ' & S & Integer'Image (Value) & ',');
1059 procedure Debug_Start (Name : String) is
1062 Ada.Text_IO.Put_Line (" In " & Name & '.');
1066 ----------------------
1067 -- Floating_Bracket --
1068 ----------------------
1070 -- Note that Floating_Bracket is only called with an acceptable
1071 -- prefix. But we don't set Okay, because we must end with a '>'.
1073 procedure Floating_Bracket is
1075 Debug_Start ("Floating_Bracket");
1077 -- Two different floats not allowed
1079 if Pic.Floater /= '!' and then Pic.Floater /= '<' then
1080 raise Picture_Error;
1086 Pic.End_Float := Index;
1087 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1089 -- First bracket wasn't counted...
1100 when '_' | '0' | '/' =>
1101 Pic.End_Float := Index;
1105 Pic.End_Float := Index;
1106 Pic.Picture.Expanded (Index) := 'b';
1110 Pic.End_Float := Index;
1111 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1123 when 'V' | 'v' | '.' =>
1124 Pic.Radix_Position := Index;
1126 Number_Fraction_Or_Bracket;
1133 end Floating_Bracket;
1135 --------------------
1136 -- Floating_Minus --
1137 --------------------
1139 procedure Floating_Minus is
1141 Debug_Start ("Floating_Minus");
1149 when '_' | '0' | '/' =>
1150 Pic.End_Float := Index;
1154 Pic.End_Float := Index;
1155 Pic.Picture.Expanded (Index) := 'b';
1159 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1160 Pic.End_Float := Index;
1167 when '.' | 'V' | 'v' =>
1168 Pic.Radix_Position := Index;
1171 while Is_Insert loop
1188 Pic.Max_Trailing_Digits :=
1189 Pic.Max_Trailing_Digits + 1;
1190 Pic.End_Float := Index;
1193 when '_' | '0' | '/' =>
1197 Pic.Picture.Expanded (Index) := 'b';
1222 procedure Floating_Plus is
1224 Debug_Start ("Floating_Plus");
1232 when '_' | '0' | '/' =>
1233 Pic.End_Float := Index;
1237 Pic.End_Float := Index;
1238 Pic.Picture.Expanded (Index) := 'b';
1242 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1243 Pic.End_Float := Index;
1250 when '.' | 'V' | 'v' =>
1251 Pic.Radix_Position := Index;
1254 while Is_Insert loop
1271 Pic.Max_Trailing_Digits :=
1272 Pic.Max_Trailing_Digits + 1;
1273 Pic.End_Float := Index;
1276 when '_' | '0' | '/' =>
1280 Pic.Picture.Expanded (Index) := 'b';
1306 function Is_Insert return Boolean is
1312 case Pic.Picture.Expanded (Index) is
1314 when '_' | '0' | '/' => return True;
1317 Pic.Picture.Expanded (Index) := 'b'; -- canonical
1320 when others => return False;
1324 --------------------
1325 -- Leading_Dollar --
1326 --------------------
1328 -- Note that Leading_Dollar can be called in either State.
1329 -- It will set state to Okay only if a 9 or (second) $
1332 -- Also notice the tricky bit with State and Zero_Suppression.
1333 -- Zero_Suppression is Picture_Error if a '$' or a '9' has been
1334 -- encountered, exactly the cases where State has been set.
1336 procedure Leading_Dollar is
1338 Debug_Start ("Leading_Dollar");
1340 -- Treat as a floating dollar, and unwind otherwise
1342 if Pic.Floater /= '!' and then Pic.Floater /= '$' then
1344 -- Two floats not allowed
1346 raise Picture_Error;
1352 Pic.Start_Currency := Index;
1353 Pic.End_Currency := Index;
1354 Pic.Start_Float := Index;
1355 Pic.End_Float := Index;
1357 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1369 when '_' | '0' | '/' =>
1370 Pic.End_Float := Index;
1373 -- A trailing insertion character is not part of the
1374 -- floating currency, so need to look ahead.
1377 Pic.End_Float := Pic.End_Float - 1;
1381 Pic.End_Float := Index;
1382 Pic.Picture.Expanded (Index) := 'b';
1386 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1388 if State = Okay then
1389 raise Picture_Error;
1391 -- Overwrite Floater and Start_Float
1394 Pic.Start_Float := Index;
1399 if State = Okay then
1400 raise Picture_Error;
1402 -- Overwrite Floater and Start_Float
1405 Pic.Start_Float := Index;
1410 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1411 Pic.End_Float := Index;
1412 Pic.End_Currency := Index;
1413 Set_State (Okay); Skip;
1416 if State /= Okay then
1418 Pic.Start_Float := Invalid_Position;
1419 Pic.End_Float := Invalid_Position;
1422 -- A single dollar does not a floating make
1427 when 'V' | 'v' | '.' =>
1428 if State /= Okay then
1430 Pic.Start_Float := Invalid_Position;
1431 Pic.End_Float := Invalid_Position;
1434 -- Only one dollar before the sign is okay, but doesn't
1437 Pic.Radix_Position := Index;
1439 Number_Fraction_Or_Dollar;
1453 -- This one is complex! A Leading_Pound can be fixed or floating,
1454 -- but in some cases the decision has to be deferred until we leave
1455 -- this procedure. Also note that Leading_Pound can be called in
1458 -- It will set state to Okay only if a 9 or (second) # is
1461 -- One Last note: In ambiguous cases, the currency is treated as
1462 -- floating unless there is only one '#'.
1464 procedure Leading_Pound is
1466 Inserts : Boolean := False;
1467 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1469 Must_Float : Boolean := False;
1470 -- Set to true if a '#' occurs after an insert
1473 Debug_Start ("Leading_Pound");
1475 -- Treat as a floating currency. If it isn't, this will be
1476 -- overwritten later.
1478 if Pic.Floater /= '!' and then Pic.Floater /= '#' then
1480 -- Two floats not allowed
1482 raise Picture_Error;
1488 Pic.Start_Currency := Index;
1489 Pic.End_Currency := Index;
1490 Pic.Start_Float := Index;
1491 Pic.End_Float := Index;
1493 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1496 Pic.Max_Currency_Digits := 1; -- we've seen one.
1507 when '_' | '0' | '/' =>
1508 Pic.End_Float := Index;
1513 Pic.Picture.Expanded (Index) := 'b';
1514 Pic.End_Float := Index;
1519 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1522 raise Picture_Error;
1524 Pic.Max_Leading_Digits := 0;
1526 -- Overwrite Floater and Start_Float
1529 Pic.Start_Float := Index;
1535 raise Picture_Error;
1537 Pic.Max_Leading_Digits := 0;
1539 -- Overwrite Floater and Start_Float
1541 Pic.Start_Float := Index;
1550 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1551 Pic.End_Float := Index;
1552 Pic.End_Currency := Index;
1557 if State /= Okay then
1559 -- A single '#' doesn't float
1562 Pic.Start_Float := Invalid_Position;
1563 Pic.End_Float := Invalid_Position;
1569 when 'V' | 'v' | '.' =>
1570 if State /= Okay then
1572 Pic.Start_Float := Invalid_Position;
1573 Pic.End_Float := Invalid_Position;
1576 -- Only one pound before the sign is okay, but doesn't
1579 Pic.Radix_Position := Index;
1581 Number_Fraction_Or_Pound;
1594 function Look return Character is
1597 raise Picture_Error;
1600 return Pic.Picture.Expanded (Index);
1609 Debug_Start ("Number");
1614 when '_' | '0' | '/' =>
1618 Pic.Picture.Expanded (Index) := 'b';
1622 Computed_BWZ := False;
1623 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1627 when '.' | 'V' | 'v' =>
1628 Pic.Radix_Position := Index;
1642 -- Will return in Okay state if a '9' was seen
1647 -----------------------
1648 -- Number_Completion --
1649 -----------------------
1651 procedure Number_Completion is
1653 Debug_Start ("Number_Completion");
1655 while not At_End loop
1658 when '_' | '0' | '/' =>
1662 Pic.Picture.Expanded (Index) := 'b';
1666 Computed_BWZ := False;
1667 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1671 when 'V' | 'v' | '.' =>
1672 Pic.Radix_Position := Index;
1681 end Number_Completion;
1683 ---------------------
1684 -- Number_Fraction --
1685 ---------------------
1687 procedure Number_Fraction is
1689 -- Note that number fraction can be called in either State.
1690 -- It will set state to Valid only if a 9 is encountered.
1692 Debug_Start ("Number_Fraction");
1700 when '_' | '0' | '/' =>
1704 Pic.Picture.Expanded (Index) := 'b';
1708 Computed_BWZ := False;
1709 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1710 Set_State (Okay); Skip;
1716 end Number_Fraction;
1718 --------------------------------
1719 -- Number_Fraction_Or_Bracket --
1720 --------------------------------
1722 procedure Number_Fraction_Or_Bracket is
1724 Debug_Start ("Number_Fraction_Or_Bracket");
1733 when '_' | '0' | '/' => Skip;
1736 Pic.Picture.Expanded (Index) := 'b';
1740 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1741 Pic.End_Float := Index;
1750 when '_' | '0' | '/' =>
1754 Pic.Picture.Expanded (Index) := 'b';
1758 Pic.Max_Trailing_Digits :=
1759 Pic.Max_Trailing_Digits + 1;
1760 Pic.End_Float := Index;
1773 end Number_Fraction_Or_Bracket;
1775 -------------------------------
1776 -- Number_Fraction_Or_Dollar --
1777 -------------------------------
1779 procedure Number_Fraction_Or_Dollar is
1781 Debug_Start ("Number_Fraction_Or_Dollar");
1789 when '_' | '0' | '/' =>
1793 Pic.Picture.Expanded (Index) := 'b';
1797 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1798 Pic.End_Float := Index;
1807 when '_' | '0' | '/' =>
1811 Pic.Picture.Expanded (Index) := 'b';
1815 Pic.Max_Trailing_Digits :=
1816 Pic.Max_Trailing_Digits + 1;
1817 Pic.End_Float := Index;
1830 end Number_Fraction_Or_Dollar;
1832 ------------------------------
1833 -- Number_Fraction_Or_Pound --
1834 ------------------------------
1836 procedure Number_Fraction_Or_Pound is
1845 when '_' | '0' | '/' =>
1849 Pic.Picture.Expanded (Index) := 'b';
1853 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1854 Pic.End_Float := Index;
1864 when '_' | '0' | '/' =>
1868 Pic.Picture.Expanded (Index) := 'b';
1872 Pic.Max_Trailing_Digits :=
1873 Pic.Max_Trailing_Digits + 1;
1874 Pic.End_Float := Index;
1889 end Number_Fraction_Or_Pound;
1891 ----------------------------------
1892 -- Number_Fraction_Or_Star_Fill --
1893 ----------------------------------
1895 procedure Number_Fraction_Or_Star_Fill is
1897 Debug_Start ("Number_Fraction_Or_Star_Fill");
1906 when '_' | '0' | '/' =>
1910 Pic.Picture.Expanded (Index) := 'b';
1914 Pic.Star_Fill := True;
1915 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1916 Pic.End_Float := Index;
1926 when '_' | '0' | '/' =>
1930 Pic.Picture.Expanded (Index) := 'b';
1934 Pic.Star_Fill := True;
1935 Pic.Max_Trailing_Digits :=
1936 Pic.Max_Trailing_Digits + 1;
1937 Pic.End_Float := Index;
1951 end Number_Fraction_Or_Star_Fill;
1953 -------------------------------
1954 -- Number_Fraction_Or_Z_Fill --
1955 -------------------------------
1957 procedure Number_Fraction_Or_Z_Fill is
1959 Debug_Start ("Number_Fraction_Or_Z_Fill");
1968 when '_' | '0' | '/' =>
1972 Pic.Picture.Expanded (Index) := 'b';
1976 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1977 Pic.End_Float := Index;
1978 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1989 when '_' | '0' | '/' =>
1993 Pic.Picture.Expanded (Index) := 'b';
1997 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1999 Pic.Max_Trailing_Digits :=
2000 Pic.Max_Trailing_Digits + 1;
2001 Pic.End_Float := Index;
2014 end Number_Fraction_Or_Z_Fill;
2016 -----------------------
2017 -- Optional_RHS_Sign --
2018 -----------------------
2020 procedure Optional_RHS_Sign is
2022 Debug_Start ("Optional_RHS_Sign");
2031 Pic.Sign_Position := Index;
2036 Pic.Sign_Position := Index;
2037 Pic.Picture.Expanded (Index) := 'C';
2040 if Look = 'R' or Look = 'r' then
2041 Pic.Second_Sign := Index;
2042 Pic.Picture.Expanded (Index) := 'R';
2046 raise Picture_Error;
2052 Pic.Sign_Position := Index;
2053 Pic.Picture.Expanded (Index) := 'D';
2056 if Look = 'B' or Look = 'b' then
2057 Pic.Second_Sign := Index;
2058 Pic.Picture.Expanded (Index) := 'B';
2062 raise Picture_Error;
2068 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2069 Pic.Second_Sign := Index;
2073 raise Picture_Error;
2080 end Optional_RHS_Sign;
2086 -- Note that Picture can be called in either State
2088 -- It will set state to Valid only if a 9 is encountered or floating
2089 -- currency is called.
2091 procedure Picture is
2093 Debug_Start ("Picture");
2102 when '_' | '0' | '/' =>
2106 Pic.Picture.Expanded (Index) := 'b';
2118 Computed_BWZ := False;
2120 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2123 when 'V' | 'v' | '.' =>
2124 Pic.Radix_Position := Index;
2137 ---------------------
2138 -- Picture_Bracket --
2139 ---------------------
2141 procedure Picture_Bracket is
2143 Pic.Sign_Position := Index;
2144 Debug_Start ("Picture_Bracket");
2145 Pic.Sign_Position := Index;
2147 -- Treat as a floating sign, and unwind otherwise
2150 Pic.Start_Float := Index;
2151 Pic.End_Float := Index;
2153 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2156 Skip; -- Known Bracket
2161 when '_' | '0' | '/' =>
2162 Pic.End_Float := Index;
2166 Pic.End_Float := Index;
2167 Pic.Picture.Expanded (Index) := 'b';
2171 Set_State (Okay); -- "<<>" is enough.
2177 when '$' | '#' | '9' | '*' =>
2178 if State /= Okay then
2180 Pic.Start_Float := Invalid_Position;
2181 Pic.End_Float := Invalid_Position;
2189 when '.' | 'V' | 'v' =>
2190 if State /= Okay then
2192 Pic.Start_Float := Invalid_Position;
2193 Pic.End_Float := Invalid_Position;
2196 -- Don't assume that state is okay, haven't seen a digit
2203 raise Picture_Error;
2207 end Picture_Bracket;
2213 procedure Picture_Minus is
2215 Debug_Start ("Picture_Minus");
2217 Pic.Sign_Position := Index;
2219 -- Treat as a floating sign, and unwind otherwise
2222 Pic.Start_Float := Index;
2223 Pic.End_Float := Index;
2225 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2228 Skip; -- Known Minus
2233 when '_' | '0' | '/' =>
2234 Pic.End_Float := Index;
2238 Pic.End_Float := Index;
2239 Pic.Picture.Expanded (Index) := 'b';
2243 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2244 Pic.End_Float := Index;
2246 Set_State (Okay); -- "-- " is enough.
2251 when '$' | '#' | '9' | '*' =>
2252 if State /= Okay then
2254 Pic.Start_Float := Invalid_Position;
2255 Pic.End_Float := Invalid_Position;
2264 -- Can't have Z and a floating sign
2266 if State = Okay then
2270 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2276 when '.' | 'V' | 'v' =>
2277 if State /= Okay then
2279 Pic.Start_Float := Invalid_Position;
2280 Pic.End_Float := Invalid_Position;
2283 -- Don't assume that state is okay, haven't seen a digit
2299 procedure Picture_Plus is
2301 Debug_Start ("Picture_Plus");
2302 Pic.Sign_Position := Index;
2304 -- Treat as a floating sign, and unwind otherwise
2307 Pic.Start_Float := Index;
2308 Pic.End_Float := Index;
2310 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2318 when '_' | '0' | '/' =>
2319 Pic.End_Float := Index;
2323 Pic.End_Float := Index;
2324 Pic.Picture.Expanded (Index) := 'b';
2328 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2329 Pic.End_Float := Index;
2331 Set_State (Okay); -- "++" is enough
2336 when '$' | '#' | '9' | '*' =>
2337 if State /= Okay then
2339 Pic.Start_Float := Invalid_Position;
2340 Pic.End_Float := Invalid_Position;
2348 if State = Okay then
2352 -- Can't have Z and a floating sign
2354 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2356 -- '+Z' is acceptable
2360 -- Overwrite Floater and Start_Float
2363 Pic.Start_Float := Index;
2370 when '.' | 'V' | 'v' =>
2371 if State /= Okay then
2373 Pic.Start_Float := Invalid_Position;
2374 Pic.End_Float := Invalid_Position;
2377 -- Don't assume that state is okay, haven't seen a digit
2389 --------------------
2390 -- Picture_String --
2391 --------------------
2393 procedure Picture_String is
2395 Debug_Start ("Picture_String");
2397 while Is_Insert loop
2417 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2427 when '9' | '.' | 'V' | 'v' =>
2433 raise Picture_Error;
2437 -- Blank when zero either if the PIC does not contain a '9' or if
2438 -- requested by the user and no '*'.
2440 Pic.Blank_When_Zero :=
2441 (Computed_BWZ or Pic.Blank_When_Zero) and not Pic.Star_Fill;
2443 -- Star fill if '*' and no '9'
2445 Pic.Star_Fill := Pic.Star_Fill and Computed_BWZ;
2457 procedure Set_State (L : Legality) is
2460 Ada.Text_IO.Put_Line
2461 (" Set state from " & Legality'Image (State)
2462 & " to " & Legality'Image (L));
2475 Ada.Text_IO.Put_Line (" Skip " & Pic.Picture.Expanded (Index));
2481 ----------------------
2482 -- Star_Suppression --
2483 ----------------------
2485 procedure Star_Suppression is
2487 Debug_Start ("Star_Suppression");
2489 if Pic.Floater /= '!' and then Pic.Floater /= '*' then
2491 -- Two floats not allowed
2493 raise Picture_Error;
2499 Pic.Start_Float := Index;
2500 Pic.End_Float := Index;
2501 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2504 -- Even a single * is a valid picture
2506 Pic.Star_Fill := True;
2516 when '_' | '0' | '/' =>
2517 Pic.End_Float := Index;
2521 Pic.End_Float := Index;
2522 Pic.Picture.Expanded (Index) := 'b';
2526 Pic.End_Float := Index;
2527 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2528 Set_State (Okay); Skip;
2535 when '.' | 'V' | 'v' =>
2536 Pic.Radix_Position := Index;
2538 Number_Fraction_Or_Star_Fill;
2542 if Pic.Max_Currency_Digits > 0 then
2543 raise Picture_Error;
2546 -- Cannot have leading and trailing currency
2552 when others => raise Picture_Error;
2555 end Star_Suppression;
2557 ----------------------
2558 -- Trailing_Bracket --
2559 ----------------------
2561 procedure Trailing_Bracket is
2563 Debug_Start ("Trailing_Bracket");
2566 Pic.Second_Sign := Index;
2569 raise Picture_Error;
2571 end Trailing_Bracket;
2573 -----------------------
2574 -- Trailing_Currency --
2575 -----------------------
2577 procedure Trailing_Currency is
2579 Debug_Start ("Trailing_Currency");
2586 Pic.Start_Currency := Index;
2587 Pic.End_Currency := Index;
2591 while not At_End and then Look = '#' loop
2592 if Pic.Start_Currency = Invalid_Position then
2593 Pic.Start_Currency := Index;
2596 Pic.End_Currency := Index;
2607 when '_' | '0' | '/' => Skip;
2610 Pic.Picture.Expanded (Index) := 'b';
2613 when others => return;
2616 end Trailing_Currency;
2618 ----------------------
2619 -- Zero_Suppression --
2620 ----------------------
2622 procedure Zero_Suppression is
2624 Debug_Start ("Zero_Suppression");
2627 Pic.Start_Float := Index;
2628 Pic.End_Float := Index;
2629 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2630 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2635 -- Even a single Z is a valid picture
2643 when '_' | '0' | '/' =>
2644 Pic.End_Float := Index;
2648 Pic.End_Float := Index;
2649 Pic.Picture.Expanded (Index) := 'b';
2653 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2655 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2656 Pic.End_Float := Index;
2665 when '.' | 'V' | 'v' =>
2666 Pic.Radix_Position := Index;
2668 Number_Fraction_Or_Z_Fill;
2680 end Zero_Suppression;
2682 -- Start of processing for Precalculate
2685 pragma Debug (Set_Debug);
2690 Ada.Text_IO.New_Line;
2691 Ada.Text_IO.Put (" Picture : """ &
2692 Pic.Picture.Expanded (1 .. Pic.Picture.Length) & """,");
2693 Ada.Text_IO.Put (" Floater : '" & Pic.Floater & "',");
2696 if State = Reject then
2697 raise Picture_Error;
2700 Debug_Integer (Pic.Radix_Position, "Radix Positon : ");
2701 Debug_Integer (Pic.Sign_Position, "Sign Positon : ");
2702 Debug_Integer (Pic.Second_Sign, "Second Sign : ");
2703 Debug_Integer (Pic.Start_Float, "Start Float : ");
2704 Debug_Integer (Pic.End_Float, "End Float : ");
2705 Debug_Integer (Pic.Start_Currency, "Start Currency : ");
2706 Debug_Integer (Pic.End_Currency, "End Currency : ");
2707 Debug_Integer (Pic.Max_Leading_Digits, "Max Leading Digits : ");
2708 Debug_Integer (Pic.Max_Trailing_Digits, "Max Trailing Digits : ");
2711 Ada.Text_IO.New_Line;
2716 when Constraint_Error =>
2718 -- To deal with special cases like null strings
2720 raise Picture_Error;
2728 (Pic_String : String;
2729 Blank_When_Zero : Boolean := False) return Picture
2735 Item : constant String := Expand (Pic_String);
2738 Result.Contents.Picture := (Item'Length, Item);
2739 Result.Contents.Original_BWZ := Blank_When_Zero;
2740 Result.Contents.Blank_When_Zero := Blank_When_Zero;
2741 Precalculate (Result.Contents);
2747 raise Picture_Error;
2755 (Pic_String : String;
2756 Blank_When_Zero : Boolean := False) return Boolean
2760 Expanded_Pic : constant String := Expand (Pic_String);
2761 -- Raises Picture_Error if Item not well-formed
2763 Format_Rec : Format_Record;
2766 Format_Rec.Picture := (Expanded_Pic'Length, Expanded_Pic);
2767 Format_Rec.Blank_When_Zero := Blank_When_Zero;
2768 Format_Rec.Original_BWZ := Blank_When_Zero;
2769 Precalculate (Format_Rec);
2771 -- False only if Blank_When_Zero is True but the pic string has a '*'
2773 return not Blank_When_Zero
2774 or else Strings_Fixed.Index (Expanded_Pic, "*") = 0;
2778 when others => return False;
2781 --------------------
2782 -- Decimal_Output --
2783 --------------------
2785 package body Decimal_Output is
2794 Currency : String := Default_Currency;
2795 Fill : Character := Default_Fill;
2796 Separator : Character := Default_Separator;
2797 Radix_Mark : Character := Default_Radix_Mark) return String
2800 return Format_Number
2801 (Pic.Contents, Num'Image (Item),
2802 Currency, Fill, Separator, Radix_Mark);
2811 Currency : String := Default_Currency) return Natural
2813 Picstr : constant String := Pic_String (Pic);
2814 V_Adjust : Integer := 0;
2815 Cur_Adjust : Integer := 0;
2818 -- Check if Picstr has 'V' or '$'
2820 -- If 'V', then length is 1 less than otherwise
2822 -- If '$', then length is Currency'Length-1 more than otherwise
2824 -- This should use the string handling package ???
2826 for J in Picstr'Range loop
2827 if Picstr (J) = 'V' then
2830 elsif Picstr (J) = '$' then
2831 Cur_Adjust := Currency'Length - 1;
2835 return Picstr'Length - V_Adjust + Cur_Adjust;
2843 (File : Text_IO.File_Type;
2846 Currency : String := Default_Currency;
2847 Fill : Character := Default_Fill;
2848 Separator : Character := Default_Separator;
2849 Radix_Mark : Character := Default_Radix_Mark)
2852 Text_IO.Put (File, Image (Item, Pic,
2853 Currency, Fill, Separator, Radix_Mark));
2859 Currency : String := Default_Currency;
2860 Fill : Character := Default_Fill;
2861 Separator : Character := Default_Separator;
2862 Radix_Mark : Character := Default_Radix_Mark)
2865 Text_IO.Put (Image (Item, Pic,
2866 Currency, Fill, Separator, Radix_Mark));
2873 Currency : String := Default_Currency;
2874 Fill : Character := Default_Fill;
2875 Separator : Character := Default_Separator;
2876 Radix_Mark : Character := Default_Radix_Mark)
2878 Result : constant String :=
2879 Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
2882 if Result'Length > To'Length then
2883 raise Ada.Text_IO.Layout_Error;
2885 Strings_Fixed.Move (Source => Result, Target => To,
2886 Justify => Strings.Right);
2897 Currency : String := Default_Currency) return Boolean
2901 Temp : constant String := Image (Item, Pic, Currency);
2902 pragma Warnings (Off, Temp);
2908 when Ada.Text_IO.Layout_Error => return False;
2913 end Ada.Text_IO.Editing;