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-2007, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Strings.Fixed;
35 package body Ada.Text_IO.Editing is
37 package Strings renames Ada.Strings;
38 package Strings_Fixed renames Ada.Strings.Fixed;
39 package Text_IO renames Ada.Text_IO;
45 function Blank_When_Zero (Pic : Picture) return Boolean is
47 return Pic.Contents.Original_BWZ;
54 function Expand (Picture : String) return String is
55 Result : String (1 .. MAX_PICSIZE);
56 Picture_Index : Integer := Picture'First;
57 Result_Index : Integer := Result'First;
61 package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
64 if Picture'Length < 1 then
68 if Picture (Picture'First) = '(' then
73 case Picture (Picture_Index) is
76 Int_IO.Get (Picture (Picture_Index + 1 .. Picture'Last),
79 if Picture (Last + 1) /= ')' then
83 -- In what follows note that one copy of the repeated
84 -- character has already been made, so a count of one is a
85 -- no-op, and a count of zero erases a character.
87 for J in 2 .. Count loop
88 Result (Result_Index + J - 2) := Picture (Picture_Index - 1);
91 Result_Index := Result_Index + Count - 1;
93 -- Last + 1 was a ')' throw it away too
95 Picture_Index := Last + 2;
101 Result (Result_Index) := Picture (Picture_Index);
102 Picture_Index := Picture_Index + 1;
103 Result_Index := Result_Index + 1;
107 exit when Picture_Index > Picture'Last;
110 return Result (1 .. Result_Index - 1);
121 function Format_Number
122 (Pic : Format_Record;
124 Currency_Symbol : String;
125 Fill_Character : Character;
126 Separator_Character : Character;
127 Radix_Point : Character) return String
129 Attrs : Number_Attributes := Parse_Number_String (Number);
131 Rounded : String := Number;
133 Sign_Position : Integer := Pic.Sign_Position; -- may float.
135 Answer : String (1 .. Pic.Picture.Length) := Pic.Picture.Expanded;
137 Currency_Pos : Integer := Pic.Start_Currency;
138 In_Currency : Boolean := False;
140 Dollar : Boolean := False;
141 -- Overridden immediately if necessary
143 Zero : Boolean := True;
144 -- Set to False when a non-zero digit is output
148 -- If the picture has fewer decimal places than the number, the image
149 -- must be rounded according to the usual rules.
151 if Attrs.Has_Fraction then
153 R : constant Integer :=
154 (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
155 - Pic.Max_Trailing_Digits;
160 R_Pos := Attrs.End_Of_Fraction - R;
162 if Rounded (R_Pos + 1) > '4' then
164 if Rounded (R_Pos) = '.' then
168 if Rounded (R_Pos) /= '9' then
169 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
171 Rounded (R_Pos) := '0';
175 if Rounded (R_Pos) = '.' then
179 if Rounded (R_Pos) /= '9' then
180 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
183 Rounded (R_Pos) := '0';
188 -- The rounding may add a digit in front. Either the
189 -- leading blank or the sign (already captured) can
193 Rounded (R_Pos) := '1';
194 Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
202 if Pic.Start_Currency /= Invalid_Position then
203 Dollar := Answer (Pic.Start_Currency) = '$';
206 -- Fix up "direct inserts" outside the playing field. Set up as one
207 -- loop to do the beginning, one (reverse) loop to do the end.
211 exit when Last = Pic.Start_Float;
212 exit when Last = Pic.Radix_Position;
213 exit when Answer (Last) = '9';
215 case Answer (Last) is
218 Answer (Last) := Separator_Character;
221 Answer (Last) := ' ';
228 exit when Last = Answer'Last;
233 -- Now for the end...
235 for J in reverse Last .. Answer'Last loop
236 exit when J = Pic.Radix_Position;
238 -- Do this test First, Separator_Character can equal Pic.Floater
240 if Answer (J) = Pic.Floater then
247 Answer (J) := Separator_Character;
263 if Pic.Start_Currency /= -1
264 and then Answer (Pic.Start_Currency) = '#'
265 and then Pic.Floater /= '#'
267 if Currency_Symbol'Length >
268 Pic.End_Currency - Pic.Start_Currency + 1
272 elsif Currency_Symbol'Length =
273 Pic.End_Currency - Pic.Start_Currency + 1
275 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
278 elsif Pic.Radix_Position = Invalid_Position
279 or else Pic.Start_Currency < Pic.Radix_Position
281 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
283 Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
284 Pic.End_Currency) := Currency_Symbol;
287 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
289 Answer (Pic.Start_Currency ..
290 Pic.Start_Currency + Currency_Symbol'Length - 1) :=
295 -- Fill in leading digits
297 if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
298 Pic.Max_Leading_Digits
300 raise Ada.Text_IO.Layout_Error;
303 if Pic.Radix_Position = Invalid_Position then
304 Position := Answer'Last;
306 Position := Pic.Radix_Position - 1;
309 for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
311 while Answer (Position) /= '9'
312 and Answer (Position) /= Pic.Floater
314 if Answer (Position) = '_' then
315 Answer (Position) := Separator_Character;
317 elsif Answer (Position) = 'b' then
318 Answer (Position) := ' ';
321 Position := Position - 1;
324 Answer (Position) := Rounded (J);
326 if Rounded (J) /= '0' then
330 Position := Position - 1;
335 if Pic.Start_Float = Invalid_Position then
337 -- No leading floats, but need to change '9' to '0', '_' to
338 -- Separator_Character and 'b' to ' '.
340 for J in Last .. Position loop
342 -- Last set when fixing the "uninteresting" leaders above.
343 -- Don't duplicate the work.
345 if Answer (J) = '9' then
348 elsif Answer (J) = '_' then
349 Answer (J) := Separator_Character;
351 elsif Answer (J) = 'b' then
356 elsif Pic.Floater = '<'
362 for J in Pic.End_Float .. Position loop -- May be null range.
363 if Answer (J) = '9' then
366 elsif Answer (J) = '_' then
367 Answer (J) := Separator_Character;
369 elsif Answer (J) = 'b' then
374 if Position > Pic.End_Float then
375 Position := Pic.End_Float;
378 for J in Pic.Start_Float .. Position - 1 loop
382 Answer (Position) := Pic.Floater;
383 Sign_Position := Position;
385 elsif Pic.Floater = '$' then
387 for J in Pic.End_Float .. Position loop -- May be null range.
388 if Answer (J) = '9' then
391 elsif Answer (J) = '_' then
392 Answer (J) := ' '; -- no separators before leftmost digit.
394 elsif Answer (J) = 'b' then
399 if Position > Pic.End_Float then
400 Position := Pic.End_Float;
403 for J in Pic.Start_Float .. Position - 1 loop
407 Answer (Position) := Pic.Floater;
408 Currency_Pos := Position;
410 elsif Pic.Floater = '*' then
412 for J in Pic.End_Float .. Position loop -- May be null range.
413 if Answer (J) = '9' then
416 elsif Answer (J) = '_' then
417 Answer (J) := Separator_Character;
419 elsif Answer (J) = 'b' then
420 Answer (J) := Fill_Character;
424 if Position > Pic.End_Float then
425 Position := Pic.End_Float;
428 for J in Pic.Start_Float .. Position loop
429 Answer (J) := Fill_Character;
433 if Pic.Floater = '#' then
434 Currency_Pos := Currency_Symbol'Length;
438 for J in reverse Pic.Start_Float .. Position loop
442 Answer (J) := Fill_Character;
445 if In_Currency and then Currency_Pos > 0 then
446 Answer (J) := Currency_Symbol (Currency_Pos);
447 Currency_Pos := Currency_Pos - 1;
458 when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
462 if Currency_Pos = 0 then
465 Answer (J) := Currency_Symbol (Currency_Pos);
466 Currency_Pos := Currency_Pos - 1;
474 Answer (J) := Fill_Character;
480 if Currency_Pos = 0 then
484 Answer (J) := Currency_Symbol (Currency_Pos);
485 Currency_Pos := Currency_Pos - 1;
499 if Pic.Floater = '#' and then Currency_Pos /= 0 then
500 raise Ada.Text_IO.Layout_Error;
506 if Sign_Position = Invalid_Position then
507 if Attrs.Negative then
508 raise Ada.Text_IO.Layout_Error;
512 if Attrs.Negative then
513 case Answer (Sign_Position) is
514 when 'C' | 'D' | '-' =>
518 Answer (Sign_Position) := '-';
521 Answer (Sign_Position) := '(';
522 Answer (Pic.Second_Sign) := ')';
531 case Answer (Sign_Position) is
534 Answer (Sign_Position) := ' ';
536 when '<' | 'C' | 'D' =>
537 Answer (Sign_Position) := ' ';
538 Answer (Pic.Second_Sign) := ' ';
550 -- Fill in trailing digits
552 if Pic.Max_Trailing_Digits > 0 then
554 if Attrs.Has_Fraction then
555 Position := Attrs.Start_Of_Fraction;
556 Last := Pic.Radix_Position + 1;
558 for J in Last .. Answer'Last loop
560 if Answer (J) = '9' or Answer (J) = Pic.Floater then
561 Answer (J) := Rounded (Position);
563 if Rounded (Position) /= '0' then
567 Position := Position + 1;
570 -- Used up fraction but remember place in Answer
572 exit when Position > Attrs.End_Of_Fraction;
574 elsif Answer (J) = 'b' then
577 elsif Answer (J) = '_' then
578 Answer (J) := Separator_Character;
588 Position := Pic.Radix_Position + 1;
591 -- Now fill remaining 9's with zeros and _ with separators
595 for J in Position .. Last loop
596 if Answer (J) = '9' then
599 elsif Answer (J) = Pic.Floater then
602 elsif Answer (J) = '_' then
603 Answer (J) := Separator_Character;
605 elsif Answer (J) = 'b' then
611 Position := Last + 1;
614 if Pic.Floater = '#' and then Currency_Pos /= 0 then
615 raise Ada.Text_IO.Layout_Error;
618 -- No trailing digits, but now J may need to stick in a currency
621 if Pic.Start_Currency = Invalid_Position then
622 Position := Answer'Last + 1;
624 Position := Pic.Start_Currency;
628 for J in Position .. Answer'Last loop
630 if Pic.Start_Currency /= Invalid_Position and then
631 Answer (Pic.Start_Currency) = '#' then
637 Answer (J) := Fill_Character;
641 Answer (J) := Currency_Symbol (Currency_Pos);
642 Currency_Pos := Currency_Pos + 1;
644 if Currency_Pos > Currency_Symbol'Length then
645 In_Currency := False;
650 if Currency_Pos > Currency_Symbol'Length then
655 Answer (J) := Currency_Symbol (Currency_Pos);
656 Currency_Pos := Currency_Pos + 1;
658 if Currency_Pos > Currency_Symbol'Length then
659 In_Currency := False;
664 Answer (J) := Currency_Symbol (Currency_Pos);
665 Currency_Pos := Currency_Pos + 1;
670 Answer (J) := Fill_Character;
676 if Currency_Pos > Currency_Symbol'Length then
679 Answer (J) := Currency_Symbol (Currency_Pos);
680 Currency_Pos := Currency_Pos + 1;
694 -- Now get rid of Blank_when_Zero and complete Star fill
696 if Zero and Pic.Blank_When_Zero then
698 -- Value is zero, and blank it
703 Last := Last - 1 + Currency_Symbol'Length;
706 if Pic.Radix_Position /= Invalid_Position and then
707 Answer (Pic.Radix_Position) = 'V' then
711 return String'(1 .. Last => ' ');
713 elsif Zero and Pic.Star_Fill then
717 Last := Last - 1 + Currency_Symbol'Length;
720 if Pic.Radix_Position /= Invalid_Position then
722 if Answer (Pic.Radix_Position) = 'V' then
726 if Pic.Radix_Position > Pic.Start_Currency then
727 return String'(1 .. Pic.Radix_Position - 1 => '*') &
729 String'(Pic.Radix_Position + 1 .. Last => '*');
735 Pic.Radix_Position + Currency_Symbol'Length - 2 =>
738 (Pic.Radix_Position + Currency_Symbol'Length .. Last
743 return String'(1 .. Pic.Radix_Position - 1 => '*') &
745 String'(Pic.Radix_Position + 1 .. Last => '*');
749 return String'(1 .. Last => '*');
752 -- This was once a simple return statement, now there are nine
753 -- different return cases. Not to mention the five above to deal
754 -- with zeros. Why not split things out?
756 -- Processing the radix and sign expansion separately
757 -- would require lots of copying--the string and some of its
758 -- indicies--without really simplifying the logic. The cases are:
760 -- 1) Expand $, replace '.' with Radix_Point
761 -- 2) No currency expansion, replace '.' with Radix_Point
762 -- 3) Expand $, radix blanked
763 -- 4) No currency expansion, radix blanked
765 -- 6) Expand $, Elide V
766 -- 7) Elide V, Expand $ (Two cases depending on order.)
767 -- 8) No radix, expand $
768 -- 9) No radix, no currency expansion
770 if Pic.Radix_Position /= Invalid_Position then
772 if Answer (Pic.Radix_Position) = '.' then
773 Answer (Pic.Radix_Position) := Radix_Point;
777 -- 1) Expand $, replace '.' with Radix_Point
779 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
780 Answer (Currency_Pos + 1 .. Answer'Last);
783 -- 2) No currency expansion, replace '.' with Radix_Point
788 elsif Answer (Pic.Radix_Position) = ' ' then -- blanked radix.
791 -- 3) Expand $, radix blanked
793 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
794 Answer (Currency_Pos + 1 .. Answer'Last);
797 -- 4) No expansion, radix blanked
809 return Answer (1 .. Pic.Radix_Position - 1) &
810 Answer (Pic.Radix_Position + 1 .. Answer'Last);
812 elsif Currency_Pos < Pic.Radix_Position then
814 -- 6) Expand $, Elide V
816 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
817 Answer (Currency_Pos + 1 .. Pic.Radix_Position - 1) &
818 Answer (Pic.Radix_Position + 1 .. Answer'Last);
821 -- 7) Elide V, Expand $
823 return Answer (1 .. Pic.Radix_Position - 1) &
824 Answer (Pic.Radix_Position + 1 .. Currency_Pos - 1) &
826 Answer (Currency_Pos + 1 .. Answer'Last);
832 -- 8) No radix, expand $
834 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
835 Answer (Currency_Pos + 1 .. Answer'Last);
838 -- 9) No radix, no currency expansion
844 -------------------------
845 -- Parse_Number_String --
846 -------------------------
848 function Parse_Number_String (Str : String) return Number_Attributes is
849 Answer : Number_Attributes;
852 for J in Str'Range loop
860 -- Decide if this is the start of a number.
861 -- If so, figure out which one...
863 if Answer.Has_Fraction then
864 Answer.End_Of_Fraction := J;
866 if Answer.Start_Of_Int = Invalid_Position then
868 Answer.Start_Of_Int := J;
870 Answer.End_Of_Int := J;
875 -- Only count a zero before the decimal point if it follows a
876 -- non-zero digit. After the decimal point, zeros will be
877 -- counted if followed by a non-zero digit.
879 if not Answer.Has_Fraction then
880 if Answer.Start_Of_Int /= Invalid_Position then
881 Answer.End_Of_Int := J;
889 Answer.Negative := True;
893 -- Close integer, start fraction
895 if Answer.Has_Fraction then
899 -- Two decimal points is a no-no
901 Answer.Has_Fraction := True;
902 Answer.End_Of_Fraction := J;
904 -- Could leave this at Invalid_Position, but this seems the
905 -- right way to indicate a null range...
907 Answer.Start_Of_Fraction := J + 1;
908 Answer.End_Of_Int := J - 1;
911 raise Picture_Error; -- can this happen? probably not!
915 if Answer.Start_Of_Int = Invalid_Position then
916 Answer.Start_Of_Int := Answer.End_Of_Int + 1;
919 -- No significant (integer) digits needs a null range
922 end Parse_Number_String;
928 -- The following ensures that we return B and not b being careful not
929 -- to break things which expect lower case b for blank. See CXF3A02.
931 function Pic_String (Pic : Picture) return String is
932 Temp : String (1 .. Pic.Contents.Picture.Length) :=
933 Pic.Contents.Picture.Expanded;
935 for J in Temp'Range loop
936 if Temp (J) = 'b' then
948 procedure Precalculate (Pic : in out Format_Record) is
949 Debug : constant Boolean := False;
950 -- Set True to generate debug output
952 Computed_BWZ : Boolean := True;
954 type Legality is (Okay, Reject);
956 State : Legality := Reject;
957 -- Start in reject, which will reject null strings
959 Index : Pic_Index := Pic.Picture.Expanded'First;
961 function At_End return Boolean;
962 pragma Inline (At_End);
964 procedure Set_State (L : Legality);
965 pragma Inline (Set_State);
967 function Look return Character;
968 pragma Inline (Look);
970 function Is_Insert return Boolean;
971 pragma Inline (Is_Insert);
974 pragma Inline (Skip);
976 procedure Debug_Start (Name : String);
977 pragma Inline (Debug_Start);
979 procedure Debug_Integer (Value : Integer; S : String);
980 pragma Inline (Debug_Integer);
982 procedure Trailing_Currency;
983 procedure Trailing_Bracket;
984 procedure Number_Fraction;
985 procedure Number_Completion;
986 procedure Number_Fraction_Or_Bracket;
987 procedure Number_Fraction_Or_Z_Fill;
988 procedure Zero_Suppression;
989 procedure Floating_Bracket;
990 procedure Number_Fraction_Or_Star_Fill;
991 procedure Star_Suppression;
992 procedure Number_Fraction_Or_Dollar;
993 procedure Leading_Dollar;
994 procedure Number_Fraction_Or_Pound;
995 procedure Leading_Pound;
997 procedure Floating_Plus;
998 procedure Floating_Minus;
999 procedure Picture_Plus;
1000 procedure Picture_Minus;
1001 procedure Picture_Bracket;
1003 procedure Optional_RHS_Sign;
1004 procedure Picture_String;
1005 procedure Set_Debug;
1011 function At_End return Boolean is
1013 Debug_Start ("At_End");
1014 return Index > Pic.Picture.Length;
1021 -- Needed to have a procedure to pass to pragma Debug
1023 procedure Set_Debug is
1025 -- Uncomment this line and make Debug a variable to enable debug
1036 procedure Debug_Integer (Value : Integer; S : String) is
1037 use Ada.Text_IO; -- needed for >
1040 if Debug and then Value > 0 then
1041 if Ada.Text_IO.Col > 70 - S'Length then
1042 Ada.Text_IO.New_Line;
1045 Ada.Text_IO.Put (' ' & S & Integer'Image (Value) & ',');
1053 procedure Debug_Start (Name : String) is
1056 Ada.Text_IO.Put_Line (" In " & Name & '.');
1060 ----------------------
1061 -- Floating_Bracket --
1062 ----------------------
1064 -- Note that Floating_Bracket is only called with an acceptable
1065 -- prefix. But we don't set Okay, because we must end with a '>'.
1067 procedure Floating_Bracket is
1069 Debug_Start ("Floating_Bracket");
1071 -- Two different floats not allowed
1073 if Pic.Floater /= '!' and then Pic.Floater /= '<' then
1074 raise Picture_Error;
1080 Pic.End_Float := Index;
1081 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1083 -- First bracket wasn't counted...
1094 when '_' | '0' | '/' =>
1095 Pic.End_Float := Index;
1099 Pic.End_Float := Index;
1100 Pic.Picture.Expanded (Index) := 'b';
1104 Pic.End_Float := Index;
1105 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1117 when 'V' | 'v' | '.' =>
1118 Pic.Radix_Position := Index;
1120 Number_Fraction_Or_Bracket;
1127 end Floating_Bracket;
1129 --------------------
1130 -- Floating_Minus --
1131 --------------------
1133 procedure Floating_Minus is
1135 Debug_Start ("Floating_Minus");
1143 when '_' | '0' | '/' =>
1144 Pic.End_Float := Index;
1148 Pic.End_Float := Index;
1149 Pic.Picture.Expanded (Index) := 'b';
1153 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1154 Pic.End_Float := Index;
1161 when '.' | 'V' | 'v' =>
1162 Pic.Radix_Position := Index;
1165 while Is_Insert loop
1182 Pic.Max_Trailing_Digits :=
1183 Pic.Max_Trailing_Digits + 1;
1184 Pic.End_Float := Index;
1187 when '_' | '0' | '/' =>
1191 Pic.Picture.Expanded (Index) := 'b';
1216 procedure Floating_Plus is
1218 Debug_Start ("Floating_Plus");
1226 when '_' | '0' | '/' =>
1227 Pic.End_Float := Index;
1231 Pic.End_Float := Index;
1232 Pic.Picture.Expanded (Index) := 'b';
1236 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1237 Pic.End_Float := Index;
1244 when '.' | 'V' | 'v' =>
1245 Pic.Radix_Position := Index;
1248 while Is_Insert loop
1265 Pic.Max_Trailing_Digits :=
1266 Pic.Max_Trailing_Digits + 1;
1267 Pic.End_Float := Index;
1270 when '_' | '0' | '/' =>
1274 Pic.Picture.Expanded (Index) := 'b';
1300 function Is_Insert return Boolean is
1306 case Pic.Picture.Expanded (Index) is
1308 when '_' | '0' | '/' => return True;
1311 Pic.Picture.Expanded (Index) := 'b'; -- canonical
1314 when others => return False;
1318 --------------------
1319 -- Leading_Dollar --
1320 --------------------
1322 -- Note that Leading_Dollar can be called in either State.
1323 -- It will set state to Okay only if a 9 or (second) $
1326 -- Also notice the tricky bit with State and Zero_Suppression.
1327 -- Zero_Suppression is Picture_Error if a '$' or a '9' has been
1328 -- encountered, exactly the cases where State has been set.
1330 procedure Leading_Dollar is
1332 Debug_Start ("Leading_Dollar");
1334 -- Treat as a floating dollar, and unwind otherwise
1336 if Pic.Floater /= '!' and then Pic.Floater /= '$' then
1338 -- Two floats not allowed
1340 raise Picture_Error;
1346 Pic.Start_Currency := Index;
1347 Pic.End_Currency := Index;
1348 Pic.Start_Float := Index;
1349 Pic.End_Float := Index;
1351 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1363 when '_' | '0' | '/' =>
1364 Pic.End_Float := Index;
1367 -- A trailing insertion character is not part of the
1368 -- floating currency, so need to look ahead.
1371 Pic.End_Float := Pic.End_Float - 1;
1375 Pic.End_Float := Index;
1376 Pic.Picture.Expanded (Index) := 'b';
1380 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1382 if State = Okay then
1383 raise Picture_Error;
1385 -- Overwrite Floater and Start_Float
1388 Pic.Start_Float := Index;
1393 if State = Okay then
1394 raise Picture_Error;
1396 -- Overwrite Floater and Start_Float
1399 Pic.Start_Float := Index;
1404 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1405 Pic.End_Float := Index;
1406 Pic.End_Currency := Index;
1407 Set_State (Okay); Skip;
1410 if State /= Okay then
1412 Pic.Start_Float := Invalid_Position;
1413 Pic.End_Float := Invalid_Position;
1416 -- A single dollar does not a floating make
1421 when 'V' | 'v' | '.' =>
1422 if State /= Okay then
1424 Pic.Start_Float := Invalid_Position;
1425 Pic.End_Float := Invalid_Position;
1428 -- Only one dollar before the sign is okay, but doesn't
1431 Pic.Radix_Position := Index;
1433 Number_Fraction_Or_Dollar;
1447 -- This one is complex! A Leading_Pound can be fixed or floating,
1448 -- but in some cases the decision has to be deferred until we leave
1449 -- this procedure. Also note that Leading_Pound can be called in
1452 -- It will set state to Okay only if a 9 or (second) # is
1455 -- One Last note: In ambiguous cases, the currency is treated as
1456 -- floating unless there is only one '#'.
1458 procedure Leading_Pound is
1460 Inserts : Boolean := False;
1461 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1463 Must_Float : Boolean := False;
1464 -- Set to true if a '#' occurs after an insert
1467 Debug_Start ("Leading_Pound");
1469 -- Treat as a floating currency. If it isn't, this will be
1470 -- overwritten later.
1472 if Pic.Floater /= '!' and then Pic.Floater /= '#' then
1474 -- Two floats not allowed
1476 raise Picture_Error;
1482 Pic.Start_Currency := Index;
1483 Pic.End_Currency := Index;
1484 Pic.Start_Float := Index;
1485 Pic.End_Float := Index;
1487 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1490 Pic.Max_Currency_Digits := 1; -- we've seen one.
1501 when '_' | '0' | '/' =>
1502 Pic.End_Float := Index;
1507 Pic.Picture.Expanded (Index) := 'b';
1508 Pic.End_Float := Index;
1513 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1516 raise Picture_Error;
1518 Pic.Max_Leading_Digits := 0;
1520 -- Overwrite Floater and Start_Float
1523 Pic.Start_Float := Index;
1529 raise Picture_Error;
1531 Pic.Max_Leading_Digits := 0;
1533 -- Overwrite Floater and Start_Float
1535 Pic.Start_Float := Index;
1544 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1545 Pic.End_Float := Index;
1546 Pic.End_Currency := Index;
1551 if State /= Okay then
1553 -- A single '#' doesn't float
1556 Pic.Start_Float := Invalid_Position;
1557 Pic.End_Float := Invalid_Position;
1563 when 'V' | 'v' | '.' =>
1564 if State /= Okay then
1566 Pic.Start_Float := Invalid_Position;
1567 Pic.End_Float := Invalid_Position;
1570 -- Only one pound before the sign is okay, but doesn't
1573 Pic.Radix_Position := Index;
1575 Number_Fraction_Or_Pound;
1588 function Look return Character is
1591 raise Picture_Error;
1594 return Pic.Picture.Expanded (Index);
1603 Debug_Start ("Number");
1608 when '_' | '0' | '/' =>
1612 Pic.Picture.Expanded (Index) := 'b';
1616 Computed_BWZ := False;
1617 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1621 when '.' | 'V' | 'v' =>
1622 Pic.Radix_Position := Index;
1636 -- Will return in Okay state if a '9' was seen
1641 -----------------------
1642 -- Number_Completion --
1643 -----------------------
1645 procedure Number_Completion is
1647 Debug_Start ("Number_Completion");
1649 while not At_End loop
1652 when '_' | '0' | '/' =>
1656 Pic.Picture.Expanded (Index) := 'b';
1660 Computed_BWZ := False;
1661 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1665 when 'V' | 'v' | '.' =>
1666 Pic.Radix_Position := Index;
1675 end Number_Completion;
1677 ---------------------
1678 -- Number_Fraction --
1679 ---------------------
1681 procedure Number_Fraction is
1683 -- Note that number fraction can be called in either State.
1684 -- It will set state to Valid only if a 9 is encountered.
1686 Debug_Start ("Number_Fraction");
1694 when '_' | '0' | '/' =>
1698 Pic.Picture.Expanded (Index) := 'b';
1702 Computed_BWZ := False;
1703 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1704 Set_State (Okay); Skip;
1710 end Number_Fraction;
1712 --------------------------------
1713 -- Number_Fraction_Or_Bracket --
1714 --------------------------------
1716 procedure Number_Fraction_Or_Bracket is
1718 Debug_Start ("Number_Fraction_Or_Bracket");
1727 when '_' | '0' | '/' => Skip;
1730 Pic.Picture.Expanded (Index) := 'b';
1734 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1735 Pic.End_Float := Index;
1744 when '_' | '0' | '/' =>
1748 Pic.Picture.Expanded (Index) := 'b';
1752 Pic.Max_Trailing_Digits :=
1753 Pic.Max_Trailing_Digits + 1;
1754 Pic.End_Float := Index;
1767 end Number_Fraction_Or_Bracket;
1769 -------------------------------
1770 -- Number_Fraction_Or_Dollar --
1771 -------------------------------
1773 procedure Number_Fraction_Or_Dollar is
1775 Debug_Start ("Number_Fraction_Or_Dollar");
1783 when '_' | '0' | '/' =>
1787 Pic.Picture.Expanded (Index) := 'b';
1791 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1792 Pic.End_Float := Index;
1801 when '_' | '0' | '/' =>
1805 Pic.Picture.Expanded (Index) := 'b';
1809 Pic.Max_Trailing_Digits :=
1810 Pic.Max_Trailing_Digits + 1;
1811 Pic.End_Float := Index;
1824 end Number_Fraction_Or_Dollar;
1826 ------------------------------
1827 -- Number_Fraction_Or_Pound --
1828 ------------------------------
1830 procedure Number_Fraction_Or_Pound is
1839 when '_' | '0' | '/' =>
1843 Pic.Picture.Expanded (Index) := 'b';
1847 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1848 Pic.End_Float := Index;
1858 when '_' | '0' | '/' =>
1862 Pic.Picture.Expanded (Index) := 'b';
1866 Pic.Max_Trailing_Digits :=
1867 Pic.Max_Trailing_Digits + 1;
1868 Pic.End_Float := Index;
1883 end Number_Fraction_Or_Pound;
1885 ----------------------------------
1886 -- Number_Fraction_Or_Star_Fill --
1887 ----------------------------------
1889 procedure Number_Fraction_Or_Star_Fill is
1891 Debug_Start ("Number_Fraction_Or_Star_Fill");
1900 when '_' | '0' | '/' =>
1904 Pic.Picture.Expanded (Index) := 'b';
1908 Pic.Star_Fill := True;
1909 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1910 Pic.End_Float := Index;
1920 when '_' | '0' | '/' =>
1924 Pic.Picture.Expanded (Index) := 'b';
1928 Pic.Star_Fill := True;
1929 Pic.Max_Trailing_Digits :=
1930 Pic.Max_Trailing_Digits + 1;
1931 Pic.End_Float := Index;
1945 end Number_Fraction_Or_Star_Fill;
1947 -------------------------------
1948 -- Number_Fraction_Or_Z_Fill --
1949 -------------------------------
1951 procedure Number_Fraction_Or_Z_Fill is
1953 Debug_Start ("Number_Fraction_Or_Z_Fill");
1962 when '_' | '0' | '/' =>
1966 Pic.Picture.Expanded (Index) := 'b';
1970 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1971 Pic.End_Float := Index;
1972 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1983 when '_' | '0' | '/' =>
1987 Pic.Picture.Expanded (Index) := 'b';
1991 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1993 Pic.Max_Trailing_Digits :=
1994 Pic.Max_Trailing_Digits + 1;
1995 Pic.End_Float := Index;
2008 end Number_Fraction_Or_Z_Fill;
2010 -----------------------
2011 -- Optional_RHS_Sign --
2012 -----------------------
2014 procedure Optional_RHS_Sign is
2016 Debug_Start ("Optional_RHS_Sign");
2025 Pic.Sign_Position := Index;
2030 Pic.Sign_Position := Index;
2031 Pic.Picture.Expanded (Index) := 'C';
2034 if Look = 'R' or Look = 'r' then
2035 Pic.Second_Sign := Index;
2036 Pic.Picture.Expanded (Index) := 'R';
2040 raise Picture_Error;
2046 Pic.Sign_Position := Index;
2047 Pic.Picture.Expanded (Index) := 'D';
2050 if Look = 'B' or Look = 'b' then
2051 Pic.Second_Sign := Index;
2052 Pic.Picture.Expanded (Index) := 'B';
2056 raise Picture_Error;
2062 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2063 Pic.Second_Sign := Index;
2067 raise Picture_Error;
2074 end Optional_RHS_Sign;
2080 -- Note that Picture can be called in either State
2082 -- It will set state to Valid only if a 9 is encountered or floating
2083 -- currency is called.
2085 procedure Picture is
2087 Debug_Start ("Picture");
2096 when '_' | '0' | '/' =>
2100 Pic.Picture.Expanded (Index) := 'b';
2112 Computed_BWZ := False;
2114 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2117 when 'V' | 'v' | '.' =>
2118 Pic.Radix_Position := Index;
2131 ---------------------
2132 -- Picture_Bracket --
2133 ---------------------
2135 procedure Picture_Bracket is
2137 Pic.Sign_Position := Index;
2138 Debug_Start ("Picture_Bracket");
2139 Pic.Sign_Position := Index;
2141 -- Treat as a floating sign, and unwind otherwise
2144 Pic.Start_Float := Index;
2145 Pic.End_Float := Index;
2147 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2150 Skip; -- Known Bracket
2155 when '_' | '0' | '/' =>
2156 Pic.End_Float := Index;
2160 Pic.End_Float := Index;
2161 Pic.Picture.Expanded (Index) := 'b';
2165 Set_State (Okay); -- "<<>" is enough.
2171 when '$' | '#' | '9' | '*' =>
2172 if State /= Okay then
2174 Pic.Start_Float := Invalid_Position;
2175 Pic.End_Float := Invalid_Position;
2183 when '.' | 'V' | 'v' =>
2184 if State /= Okay then
2186 Pic.Start_Float := Invalid_Position;
2187 Pic.End_Float := Invalid_Position;
2190 -- Don't assume that state is okay, haven't seen a digit
2197 raise Picture_Error;
2201 end Picture_Bracket;
2207 procedure Picture_Minus is
2209 Debug_Start ("Picture_Minus");
2211 Pic.Sign_Position := Index;
2213 -- Treat as a floating sign, and unwind otherwise
2216 Pic.Start_Float := Index;
2217 Pic.End_Float := Index;
2219 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2222 Skip; -- Known Minus
2227 when '_' | '0' | '/' =>
2228 Pic.End_Float := Index;
2232 Pic.End_Float := Index;
2233 Pic.Picture.Expanded (Index) := 'b';
2237 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2238 Pic.End_Float := Index;
2240 Set_State (Okay); -- "-- " is enough.
2245 when '$' | '#' | '9' | '*' =>
2246 if State /= Okay then
2248 Pic.Start_Float := Invalid_Position;
2249 Pic.End_Float := Invalid_Position;
2258 -- Can't have Z and a floating sign
2260 if State = Okay then
2264 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2270 when '.' | 'V' | 'v' =>
2271 if State /= Okay then
2273 Pic.Start_Float := Invalid_Position;
2274 Pic.End_Float := Invalid_Position;
2277 -- Don't assume that state is okay, haven't seen a digit
2293 procedure Picture_Plus is
2295 Debug_Start ("Picture_Plus");
2296 Pic.Sign_Position := Index;
2298 -- Treat as a floating sign, and unwind otherwise
2301 Pic.Start_Float := Index;
2302 Pic.End_Float := Index;
2304 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2312 when '_' | '0' | '/' =>
2313 Pic.End_Float := Index;
2317 Pic.End_Float := Index;
2318 Pic.Picture.Expanded (Index) := 'b';
2322 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2323 Pic.End_Float := Index;
2325 Set_State (Okay); -- "++" is enough
2330 when '$' | '#' | '9' | '*' =>
2331 if State /= Okay then
2333 Pic.Start_Float := Invalid_Position;
2334 Pic.End_Float := Invalid_Position;
2342 if State = Okay then
2346 -- Can't have Z and a floating sign
2348 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2350 -- '+Z' is acceptable
2354 -- Overwrite Floater and Start_Float
2357 Pic.Start_Float := Index;
2364 when '.' | 'V' | 'v' =>
2365 if State /= Okay then
2367 Pic.Start_Float := Invalid_Position;
2368 Pic.End_Float := Invalid_Position;
2371 -- Don't assume that state is okay, haven't seen a digit
2383 --------------------
2384 -- Picture_String --
2385 --------------------
2387 procedure Picture_String is
2389 Debug_Start ("Picture_String");
2391 while Is_Insert loop
2411 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2421 when '9' | '.' | 'V' | 'v' =>
2427 raise Picture_Error;
2431 -- Blank when zero either if the PIC does not contain a '9' or if
2432 -- requested by the user and no '*'.
2434 Pic.Blank_When_Zero :=
2435 (Computed_BWZ or Pic.Blank_When_Zero) and not Pic.Star_Fill;
2437 -- Star fill if '*' and no '9'
2439 Pic.Star_Fill := Pic.Star_Fill and Computed_BWZ;
2451 procedure Set_State (L : Legality) is
2454 Ada.Text_IO.Put_Line
2455 (" Set state from " & Legality'Image (State)
2456 & " to " & Legality'Image (L));
2469 Ada.Text_IO.Put_Line (" Skip " & Pic.Picture.Expanded (Index));
2475 ----------------------
2476 -- Star_Suppression --
2477 ----------------------
2479 procedure Star_Suppression is
2481 Debug_Start ("Star_Suppression");
2483 if Pic.Floater /= '!' and then Pic.Floater /= '*' then
2485 -- Two floats not allowed
2487 raise Picture_Error;
2493 Pic.Start_Float := Index;
2494 Pic.End_Float := Index;
2495 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2498 -- Even a single * is a valid picture
2500 Pic.Star_Fill := True;
2510 when '_' | '0' | '/' =>
2511 Pic.End_Float := Index;
2515 Pic.End_Float := Index;
2516 Pic.Picture.Expanded (Index) := 'b';
2520 Pic.End_Float := Index;
2521 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2522 Set_State (Okay); Skip;
2529 when '.' | 'V' | 'v' =>
2530 Pic.Radix_Position := Index;
2532 Number_Fraction_Or_Star_Fill;
2536 if Pic.Max_Currency_Digits > 0 then
2537 raise Picture_Error;
2540 -- Cannot have leading and trailing currency
2546 when others => raise Picture_Error;
2549 end Star_Suppression;
2551 ----------------------
2552 -- Trailing_Bracket --
2553 ----------------------
2555 procedure Trailing_Bracket is
2557 Debug_Start ("Trailing_Bracket");
2560 Pic.Second_Sign := Index;
2563 raise Picture_Error;
2565 end Trailing_Bracket;
2567 -----------------------
2568 -- Trailing_Currency --
2569 -----------------------
2571 procedure Trailing_Currency is
2573 Debug_Start ("Trailing_Currency");
2580 Pic.Start_Currency := Index;
2581 Pic.End_Currency := Index;
2585 while not At_End and then Look = '#' loop
2586 if Pic.Start_Currency = Invalid_Position then
2587 Pic.Start_Currency := Index;
2590 Pic.End_Currency := Index;
2601 when '_' | '0' | '/' => Skip;
2604 Pic.Picture.Expanded (Index) := 'b';
2607 when others => return;
2610 end Trailing_Currency;
2612 ----------------------
2613 -- Zero_Suppression --
2614 ----------------------
2616 procedure Zero_Suppression is
2618 Debug_Start ("Zero_Suppression");
2621 Pic.Start_Float := Index;
2622 Pic.End_Float := Index;
2623 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2624 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2629 -- Even a single Z is a valid picture
2637 when '_' | '0' | '/' =>
2638 Pic.End_Float := Index;
2642 Pic.End_Float := Index;
2643 Pic.Picture.Expanded (Index) := 'b';
2647 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2649 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2650 Pic.End_Float := Index;
2659 when '.' | 'V' | 'v' =>
2660 Pic.Radix_Position := Index;
2662 Number_Fraction_Or_Z_Fill;
2674 end Zero_Suppression;
2676 -- Start of processing for Precalculate
2679 pragma Debug (Set_Debug);
2684 Ada.Text_IO.New_Line;
2685 Ada.Text_IO.Put (" Picture : """ &
2686 Pic.Picture.Expanded (1 .. Pic.Picture.Length) & """,");
2687 Ada.Text_IO.Put (" Floater : '" & Pic.Floater & "',");
2690 if State = Reject then
2691 raise Picture_Error;
2694 Debug_Integer (Pic.Radix_Position, "Radix Positon : ");
2695 Debug_Integer (Pic.Sign_Position, "Sign Positon : ");
2696 Debug_Integer (Pic.Second_Sign, "Second Sign : ");
2697 Debug_Integer (Pic.Start_Float, "Start Float : ");
2698 Debug_Integer (Pic.End_Float, "End Float : ");
2699 Debug_Integer (Pic.Start_Currency, "Start Currency : ");
2700 Debug_Integer (Pic.End_Currency, "End Currency : ");
2701 Debug_Integer (Pic.Max_Leading_Digits, "Max Leading Digits : ");
2702 Debug_Integer (Pic.Max_Trailing_Digits, "Max Trailing Digits : ");
2705 Ada.Text_IO.New_Line;
2710 when Constraint_Error =>
2712 -- To deal with special cases like null strings
2714 raise Picture_Error;
2722 (Pic_String : String;
2723 Blank_When_Zero : Boolean := False) return Picture
2729 Item : constant String := Expand (Pic_String);
2732 Result.Contents.Picture := (Item'Length, Item);
2733 Result.Contents.Original_BWZ := Blank_When_Zero;
2734 Result.Contents.Blank_When_Zero := Blank_When_Zero;
2735 Precalculate (Result.Contents);
2741 raise Picture_Error;
2749 (Pic_String : String;
2750 Blank_When_Zero : Boolean := False) return Boolean
2754 Expanded_Pic : constant String := Expand (Pic_String);
2755 -- Raises Picture_Error if Item not well-formed
2757 Format_Rec : Format_Record;
2760 Format_Rec.Picture := (Expanded_Pic'Length, Expanded_Pic);
2761 Format_Rec.Blank_When_Zero := Blank_When_Zero;
2762 Format_Rec.Original_BWZ := Blank_When_Zero;
2763 Precalculate (Format_Rec);
2765 -- False only if Blank_When_Zero is True but the pic string has a '*'
2767 return not Blank_When_Zero
2768 or else Strings_Fixed.Index (Expanded_Pic, "*") = 0;
2772 when others => return False;
2775 --------------------
2776 -- Decimal_Output --
2777 --------------------
2779 package body Decimal_Output is
2788 Currency : String := Default_Currency;
2789 Fill : Character := Default_Fill;
2790 Separator : Character := Default_Separator;
2791 Radix_Mark : Character := Default_Radix_Mark) return String
2794 return Format_Number
2795 (Pic.Contents, Num'Image (Item),
2796 Currency, Fill, Separator, Radix_Mark);
2805 Currency : String := Default_Currency) return Natural
2807 Picstr : constant String := Pic_String (Pic);
2808 V_Adjust : Integer := 0;
2809 Cur_Adjust : Integer := 0;
2812 -- Check if Picstr has 'V' or '$'
2814 -- If 'V', then length is 1 less than otherwise
2816 -- If '$', then length is Currency'Length-1 more than otherwise
2818 -- This should use the string handling package ???
2820 for J in Picstr'Range loop
2821 if Picstr (J) = 'V' then
2824 elsif Picstr (J) = '$' then
2825 Cur_Adjust := Currency'Length - 1;
2829 return Picstr'Length - V_Adjust + Cur_Adjust;
2837 (File : Text_IO.File_Type;
2840 Currency : String := Default_Currency;
2841 Fill : Character := Default_Fill;
2842 Separator : Character := Default_Separator;
2843 Radix_Mark : Character := Default_Radix_Mark)
2846 Text_IO.Put (File, Image (Item, Pic,
2847 Currency, Fill, Separator, Radix_Mark));
2853 Currency : String := Default_Currency;
2854 Fill : Character := Default_Fill;
2855 Separator : Character := Default_Separator;
2856 Radix_Mark : Character := Default_Radix_Mark)
2859 Text_IO.Put (Image (Item, Pic,
2860 Currency, Fill, Separator, Radix_Mark));
2867 Currency : String := Default_Currency;
2868 Fill : Character := Default_Fill;
2869 Separator : Character := Default_Separator;
2870 Radix_Mark : Character := Default_Radix_Mark)
2872 Result : constant String :=
2873 Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
2876 if Result'Length > To'Length then
2877 raise Ada.Text_IO.Layout_Error;
2879 Strings_Fixed.Move (Source => Result, Target => To,
2880 Justify => Strings.Right);
2891 Currency : String := Default_Currency) return Boolean
2895 Temp : constant String := Image (Item, Pic, Currency);
2896 pragma Warnings (Off, Temp);
2902 when Ada.Text_IO.Layout_Error => return False;
2907 end Ada.Text_IO.Editing;