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-2006, 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
424 if Position > Pic.End_Float then
425 Position := Pic.End_Float;
428 for J in Pic.Start_Float .. Position loop
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 Temp (J) := 'B'; end if;
946 procedure Precalculate (Pic : in out Format_Record) is
947 Debug : constant Boolean := False;
948 -- Set True to generate debug output
950 Computed_BWZ : Boolean := True;
952 type Legality is (Okay, Reject);
954 State : Legality := Reject;
955 -- Start in reject, which will reject null strings
957 Index : Pic_Index := Pic.Picture.Expanded'First;
959 function At_End return Boolean;
960 pragma Inline (At_End);
962 procedure Set_State (L : Legality);
963 pragma Inline (Set_State);
965 function Look return Character;
966 pragma Inline (Look);
968 function Is_Insert return Boolean;
969 pragma Inline (Is_Insert);
972 pragma Inline (Skip);
974 procedure Debug_Start (Name : String);
975 pragma Inline (Debug_Start);
977 procedure Debug_Integer (Value : Integer; S : String);
978 pragma Inline (Debug_Integer);
980 procedure Trailing_Currency;
981 procedure Trailing_Bracket;
982 procedure Number_Fraction;
983 procedure Number_Completion;
984 procedure Number_Fraction_Or_Bracket;
985 procedure Number_Fraction_Or_Z_Fill;
986 procedure Zero_Suppression;
987 procedure Floating_Bracket;
988 procedure Number_Fraction_Or_Star_Fill;
989 procedure Star_Suppression;
990 procedure Number_Fraction_Or_Dollar;
991 procedure Leading_Dollar;
992 procedure Number_Fraction_Or_Pound;
993 procedure Leading_Pound;
995 procedure Floating_Plus;
996 procedure Floating_Minus;
997 procedure Picture_Plus;
998 procedure Picture_Minus;
999 procedure Picture_Bracket;
1001 procedure Optional_RHS_Sign;
1002 procedure Picture_String;
1003 procedure Set_Debug;
1009 function At_End return Boolean is
1011 Debug_Start ("At_End");
1012 return Index > Pic.Picture.Length;
1019 -- Needed to have a procedure to pass to pragma Debug
1021 procedure Set_Debug is
1023 -- Uncomment this line and make Debug a variable to enable debug
1034 procedure Debug_Integer (Value : Integer; S : String) is
1035 use Ada.Text_IO; -- needed for >
1038 if Debug and then Value > 0 then
1039 if Ada.Text_IO.Col > 70 - S'Length then
1040 Ada.Text_IO.New_Line;
1043 Ada.Text_IO.Put (' ' & S & Integer'Image (Value) & ',');
1051 procedure Debug_Start (Name : String) is
1054 Ada.Text_IO.Put_Line (" In " & Name & '.');
1058 ----------------------
1059 -- Floating_Bracket --
1060 ----------------------
1062 -- Note that Floating_Bracket is only called with an acceptable
1063 -- prefix. But we don't set Okay, because we must end with a '>'.
1065 procedure Floating_Bracket is
1067 Debug_Start ("Floating_Bracket");
1069 -- Two different floats not allowed
1071 if Pic.Floater /= '!' and then Pic.Floater /= '<' then
1072 raise Picture_Error;
1078 Pic.End_Float := Index;
1079 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1081 -- First bracket wasn't counted...
1092 when '_' | '0' | '/' =>
1093 Pic.End_Float := Index;
1097 Pic.End_Float := Index;
1098 Pic.Picture.Expanded (Index) := 'b';
1102 Pic.End_Float := Index;
1103 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1115 when 'V' | 'v' | '.' =>
1116 Pic.Radix_Position := Index;
1118 Number_Fraction_Or_Bracket;
1125 end Floating_Bracket;
1127 --------------------
1128 -- Floating_Minus --
1129 --------------------
1131 procedure Floating_Minus is
1133 Debug_Start ("Floating_Minus");
1141 when '_' | '0' | '/' =>
1142 Pic.End_Float := Index;
1146 Pic.End_Float := Index;
1147 Pic.Picture.Expanded (Index) := 'b';
1151 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1152 Pic.End_Float := Index;
1159 when '.' | 'V' | 'v' =>
1160 Pic.Radix_Position := Index;
1163 while Is_Insert loop
1180 Pic.Max_Trailing_Digits :=
1181 Pic.Max_Trailing_Digits + 1;
1182 Pic.End_Float := Index;
1185 when '_' | '0' | '/' =>
1189 Pic.Picture.Expanded (Index) := 'b';
1214 procedure Floating_Plus is
1216 Debug_Start ("Floating_Plus");
1224 when '_' | '0' | '/' =>
1225 Pic.End_Float := Index;
1229 Pic.End_Float := Index;
1230 Pic.Picture.Expanded (Index) := 'b';
1234 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1235 Pic.End_Float := Index;
1242 when '.' | 'V' | 'v' =>
1243 Pic.Radix_Position := Index;
1246 while Is_Insert loop
1263 Pic.Max_Trailing_Digits :=
1264 Pic.Max_Trailing_Digits + 1;
1265 Pic.End_Float := Index;
1268 when '_' | '0' | '/' =>
1272 Pic.Picture.Expanded (Index) := 'b';
1298 function Is_Insert return Boolean is
1304 case Pic.Picture.Expanded (Index) is
1306 when '_' | '0' | '/' => return True;
1309 Pic.Picture.Expanded (Index) := 'b'; -- canonical
1312 when others => return False;
1316 --------------------
1317 -- Leading_Dollar --
1318 --------------------
1320 -- Note that Leading_Dollar can be called in either State.
1321 -- It will set state to Okay only if a 9 or (second) $
1324 -- Also notice the tricky bit with State and Zero_Suppression.
1325 -- Zero_Suppression is Picture_Error if a '$' or a '9' has been
1326 -- encountered, exactly the cases where State has been set.
1328 procedure Leading_Dollar is
1330 Debug_Start ("Leading_Dollar");
1332 -- Treat as a floating dollar, and unwind otherwise
1334 if Pic.Floater /= '!' and then Pic.Floater /= '$' then
1336 -- Two floats not allowed
1338 raise Picture_Error;
1344 Pic.Start_Currency := Index;
1345 Pic.End_Currency := Index;
1346 Pic.Start_Float := Index;
1347 Pic.End_Float := Index;
1349 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1361 when '_' | '0' | '/' =>
1362 Pic.End_Float := Index;
1365 -- A trailing insertion character is not part of the
1366 -- floating currency, so need to look ahead.
1369 Pic.End_Float := Pic.End_Float - 1;
1373 Pic.End_Float := Index;
1374 Pic.Picture.Expanded (Index) := 'b';
1378 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1380 if State = Okay then
1381 raise Picture_Error;
1383 -- Overwrite Floater and Start_Float
1386 Pic.Start_Float := Index;
1391 if State = Okay then
1392 raise Picture_Error;
1394 -- Overwrite Floater and Start_Float
1397 Pic.Start_Float := Index;
1402 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1403 Pic.End_Float := Index;
1404 Pic.End_Currency := Index;
1405 Set_State (Okay); Skip;
1408 if State /= Okay then
1410 Pic.Start_Float := Invalid_Position;
1411 Pic.End_Float := Invalid_Position;
1414 -- A single dollar does not a floating make
1419 when 'V' | 'v' | '.' =>
1420 if State /= Okay then
1422 Pic.Start_Float := Invalid_Position;
1423 Pic.End_Float := Invalid_Position;
1426 -- Only one dollar before the sign is okay, but doesn't
1429 Pic.Radix_Position := Index;
1431 Number_Fraction_Or_Dollar;
1445 -- This one is complex! A Leading_Pound can be fixed or floating,
1446 -- but in some cases the decision has to be deferred until we leave
1447 -- this procedure. Also note that Leading_Pound can be called in
1450 -- It will set state to Okay only if a 9 or (second) # is
1453 -- One Last note: In ambiguous cases, the currency is treated as
1454 -- floating unless there is only one '#'.
1456 procedure Leading_Pound is
1458 Inserts : Boolean := False;
1459 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1461 Must_Float : Boolean := False;
1462 -- Set to true if a '#' occurs after an insert
1465 Debug_Start ("Leading_Pound");
1467 -- Treat as a floating currency. If it isn't, this will be
1468 -- overwritten later.
1470 if Pic.Floater /= '!' and then Pic.Floater /= '#' then
1472 -- Two floats not allowed
1474 raise Picture_Error;
1480 Pic.Start_Currency := Index;
1481 Pic.End_Currency := Index;
1482 Pic.Start_Float := Index;
1483 Pic.End_Float := Index;
1485 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1488 Pic.Max_Currency_Digits := 1; -- we've seen one.
1499 when '_' | '0' | '/' =>
1500 Pic.End_Float := Index;
1505 Pic.Picture.Expanded (Index) := 'b';
1506 Pic.End_Float := Index;
1511 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1514 raise Picture_Error;
1516 Pic.Max_Leading_Digits := 0;
1518 -- Overwrite Floater and Start_Float
1521 Pic.Start_Float := Index;
1527 raise Picture_Error;
1529 Pic.Max_Leading_Digits := 0;
1531 -- Overwrite Floater and Start_Float
1533 Pic.Start_Float := Index;
1542 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1543 Pic.End_Float := Index;
1544 Pic.End_Currency := Index;
1549 if State /= Okay then
1551 -- A single '#' doesn't float
1554 Pic.Start_Float := Invalid_Position;
1555 Pic.End_Float := Invalid_Position;
1561 when 'V' | 'v' | '.' =>
1562 if State /= Okay then
1564 Pic.Start_Float := Invalid_Position;
1565 Pic.End_Float := Invalid_Position;
1568 -- Only one pound before the sign is okay, but doesn't
1571 Pic.Radix_Position := Index;
1573 Number_Fraction_Or_Pound;
1586 function Look return Character is
1589 raise Picture_Error;
1592 return Pic.Picture.Expanded (Index);
1601 Debug_Start ("Number");
1606 when '_' | '0' | '/' =>
1610 Pic.Picture.Expanded (Index) := 'b';
1614 Computed_BWZ := False;
1615 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1619 when '.' | 'V' | 'v' =>
1620 Pic.Radix_Position := Index;
1634 -- Will return in Okay state if a '9' was seen
1639 -----------------------
1640 -- Number_Completion --
1641 -----------------------
1643 procedure Number_Completion is
1645 Debug_Start ("Number_Completion");
1647 while not At_End loop
1650 when '_' | '0' | '/' =>
1654 Pic.Picture.Expanded (Index) := 'b';
1658 Computed_BWZ := False;
1659 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1663 when 'V' | 'v' | '.' =>
1664 Pic.Radix_Position := Index;
1673 end Number_Completion;
1675 ---------------------
1676 -- Number_Fraction --
1677 ---------------------
1679 procedure Number_Fraction is
1681 -- Note that number fraction can be called in either State.
1682 -- It will set state to Valid only if a 9 is encountered.
1684 Debug_Start ("Number_Fraction");
1692 when '_' | '0' | '/' =>
1696 Pic.Picture.Expanded (Index) := 'b';
1700 Computed_BWZ := False;
1701 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1702 Set_State (Okay); Skip;
1708 end Number_Fraction;
1710 --------------------------------
1711 -- Number_Fraction_Or_Bracket --
1712 --------------------------------
1714 procedure Number_Fraction_Or_Bracket is
1716 Debug_Start ("Number_Fraction_Or_Bracket");
1725 when '_' | '0' | '/' => Skip;
1728 Pic.Picture.Expanded (Index) := 'b';
1732 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1733 Pic.End_Float := Index;
1742 when '_' | '0' | '/' =>
1746 Pic.Picture.Expanded (Index) := 'b';
1750 Pic.Max_Trailing_Digits :=
1751 Pic.Max_Trailing_Digits + 1;
1752 Pic.End_Float := Index;
1765 end Number_Fraction_Or_Bracket;
1767 -------------------------------
1768 -- Number_Fraction_Or_Dollar --
1769 -------------------------------
1771 procedure Number_Fraction_Or_Dollar is
1773 Debug_Start ("Number_Fraction_Or_Dollar");
1781 when '_' | '0' | '/' =>
1785 Pic.Picture.Expanded (Index) := 'b';
1789 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1790 Pic.End_Float := Index;
1799 when '_' | '0' | '/' =>
1803 Pic.Picture.Expanded (Index) := 'b';
1807 Pic.Max_Trailing_Digits :=
1808 Pic.Max_Trailing_Digits + 1;
1809 Pic.End_Float := Index;
1822 end Number_Fraction_Or_Dollar;
1824 ------------------------------
1825 -- Number_Fraction_Or_Pound --
1826 ------------------------------
1828 procedure Number_Fraction_Or_Pound is
1837 when '_' | '0' | '/' =>
1841 Pic.Picture.Expanded (Index) := 'b';
1845 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1846 Pic.End_Float := Index;
1856 when '_' | '0' | '/' =>
1860 Pic.Picture.Expanded (Index) := 'b';
1864 Pic.Max_Trailing_Digits :=
1865 Pic.Max_Trailing_Digits + 1;
1866 Pic.End_Float := Index;
1881 end Number_Fraction_Or_Pound;
1883 ----------------------------------
1884 -- Number_Fraction_Or_Star_Fill --
1885 ----------------------------------
1887 procedure Number_Fraction_Or_Star_Fill is
1889 Debug_Start ("Number_Fraction_Or_Star_Fill");
1898 when '_' | '0' | '/' =>
1902 Pic.Picture.Expanded (Index) := 'b';
1906 Pic.Star_Fill := True;
1907 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1908 Pic.End_Float := Index;
1918 when '_' | '0' | '/' =>
1922 Pic.Picture.Expanded (Index) := 'b';
1926 Pic.Star_Fill := True;
1927 Pic.Max_Trailing_Digits :=
1928 Pic.Max_Trailing_Digits + 1;
1929 Pic.End_Float := Index;
1943 end Number_Fraction_Or_Star_Fill;
1945 -------------------------------
1946 -- Number_Fraction_Or_Z_Fill --
1947 -------------------------------
1949 procedure Number_Fraction_Or_Z_Fill is
1951 Debug_Start ("Number_Fraction_Or_Z_Fill");
1960 when '_' | '0' | '/' =>
1964 Pic.Picture.Expanded (Index) := 'b';
1968 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1969 Pic.End_Float := Index;
1970 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1981 when '_' | '0' | '/' =>
1985 Pic.Picture.Expanded (Index) := 'b';
1989 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1991 Pic.Max_Trailing_Digits :=
1992 Pic.Max_Trailing_Digits + 1;
1993 Pic.End_Float := Index;
2006 end Number_Fraction_Or_Z_Fill;
2008 -----------------------
2009 -- Optional_RHS_Sign --
2010 -----------------------
2012 procedure Optional_RHS_Sign is
2014 Debug_Start ("Optional_RHS_Sign");
2023 Pic.Sign_Position := Index;
2028 Pic.Sign_Position := Index;
2029 Pic.Picture.Expanded (Index) := 'C';
2032 if Look = 'R' or Look = 'r' then
2033 Pic.Second_Sign := Index;
2034 Pic.Picture.Expanded (Index) := 'R';
2038 raise Picture_Error;
2044 Pic.Sign_Position := Index;
2045 Pic.Picture.Expanded (Index) := 'D';
2048 if Look = 'B' or Look = 'b' then
2049 Pic.Second_Sign := Index;
2050 Pic.Picture.Expanded (Index) := 'B';
2054 raise Picture_Error;
2060 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2061 Pic.Second_Sign := Index;
2065 raise Picture_Error;
2072 end Optional_RHS_Sign;
2078 -- Note that Picture can be called in either State
2080 -- It will set state to Valid only if a 9 is encountered or floating
2081 -- currency is called.
2083 procedure Picture is
2085 Debug_Start ("Picture");
2094 when '_' | '0' | '/' =>
2098 Pic.Picture.Expanded (Index) := 'b';
2110 Computed_BWZ := False;
2112 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2115 when 'V' | 'v' | '.' =>
2116 Pic.Radix_Position := Index;
2129 ---------------------
2130 -- Picture_Bracket --
2131 ---------------------
2133 procedure Picture_Bracket is
2135 Pic.Sign_Position := Index;
2136 Debug_Start ("Picture_Bracket");
2137 Pic.Sign_Position := Index;
2139 -- Treat as a floating sign, and unwind otherwise
2142 Pic.Start_Float := Index;
2143 Pic.End_Float := Index;
2145 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2148 Skip; -- Known Bracket
2153 when '_' | '0' | '/' =>
2154 Pic.End_Float := Index;
2158 Pic.End_Float := Index;
2159 Pic.Picture.Expanded (Index) := 'b';
2163 Set_State (Okay); -- "<<>" is enough.
2169 when '$' | '#' | '9' | '*' =>
2170 if State /= Okay then
2172 Pic.Start_Float := Invalid_Position;
2173 Pic.End_Float := Invalid_Position;
2181 when '.' | 'V' | 'v' =>
2182 if State /= Okay then
2184 Pic.Start_Float := Invalid_Position;
2185 Pic.End_Float := Invalid_Position;
2188 -- Don't assume that state is okay, haven't seen a digit
2195 raise Picture_Error;
2199 end Picture_Bracket;
2205 procedure Picture_Minus is
2207 Debug_Start ("Picture_Minus");
2209 Pic.Sign_Position := Index;
2211 -- Treat as a floating sign, and unwind otherwise
2214 Pic.Start_Float := Index;
2215 Pic.End_Float := Index;
2217 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2220 Skip; -- Known Minus
2225 when '_' | '0' | '/' =>
2226 Pic.End_Float := Index;
2230 Pic.End_Float := Index;
2231 Pic.Picture.Expanded (Index) := 'b';
2235 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2236 Pic.End_Float := Index;
2238 Set_State (Okay); -- "-- " is enough.
2243 when '$' | '#' | '9' | '*' =>
2244 if State /= Okay then
2246 Pic.Start_Float := Invalid_Position;
2247 Pic.End_Float := Invalid_Position;
2256 -- Can't have Z and a floating sign
2258 if State = Okay then
2262 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2268 when '.' | 'V' | 'v' =>
2269 if State /= Okay then
2271 Pic.Start_Float := Invalid_Position;
2272 Pic.End_Float := Invalid_Position;
2275 -- Don't assume that state is okay, haven't seen a digit
2291 procedure Picture_Plus is
2293 Debug_Start ("Picture_Plus");
2294 Pic.Sign_Position := Index;
2296 -- Treat as a floating sign, and unwind otherwise
2299 Pic.Start_Float := Index;
2300 Pic.End_Float := Index;
2302 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2310 when '_' | '0' | '/' =>
2311 Pic.End_Float := Index;
2315 Pic.End_Float := Index;
2316 Pic.Picture.Expanded (Index) := 'b';
2320 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2321 Pic.End_Float := Index;
2323 Set_State (Okay); -- "++" is enough
2328 when '$' | '#' | '9' | '*' =>
2329 if State /= Okay then
2331 Pic.Start_Float := Invalid_Position;
2332 Pic.End_Float := Invalid_Position;
2340 if State = Okay then
2344 -- Can't have Z and a floating sign
2346 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2348 -- '+Z' is acceptable
2352 -- Overwrite Floater and Start_Float
2355 Pic.Start_Float := Index;
2362 when '.' | 'V' | 'v' =>
2363 if State /= Okay then
2365 Pic.Start_Float := Invalid_Position;
2366 Pic.End_Float := Invalid_Position;
2369 -- Don't assume that state is okay, haven't seen a digit
2381 --------------------
2382 -- Picture_String --
2383 --------------------
2385 procedure Picture_String is
2387 Debug_Start ("Picture_String");
2389 while Is_Insert loop
2409 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2419 when '9' | '.' | 'V' | 'v' =>
2425 raise Picture_Error;
2429 -- Blank when zero either if the PIC does not contain a '9' or if
2430 -- requested by the user and no '*'.
2432 Pic.Blank_When_Zero :=
2433 (Computed_BWZ or Pic.Blank_When_Zero) and not Pic.Star_Fill;
2435 -- Star fill if '*' and no '9'
2437 Pic.Star_Fill := Pic.Star_Fill and Computed_BWZ;
2449 procedure Set_State (L : Legality) is
2451 if Debug then Ada.Text_IO.Put_Line
2452 (" Set state from " & Legality'Image (State) &
2453 " to " & Legality'Image (L));
2465 if Debug then Ada.Text_IO.Put_Line
2466 (" Skip " & Pic.Picture.Expanded (Index));
2472 ----------------------
2473 -- Star_Suppression --
2474 ----------------------
2476 procedure Star_Suppression is
2478 Debug_Start ("Star_Suppression");
2480 if Pic.Floater /= '!' and then Pic.Floater /= '*' then
2482 -- Two floats not allowed
2484 raise Picture_Error;
2490 Pic.Start_Float := Index;
2491 Pic.End_Float := Index;
2492 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2495 -- Even a single * is a valid picture
2497 Pic.Star_Fill := True;
2507 when '_' | '0' | '/' =>
2508 Pic.End_Float := Index;
2512 Pic.End_Float := Index;
2513 Pic.Picture.Expanded (Index) := 'b';
2517 Pic.End_Float := Index;
2518 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2519 Set_State (Okay); Skip;
2526 when '.' | 'V' | 'v' =>
2527 Pic.Radix_Position := Index;
2529 Number_Fraction_Or_Star_Fill;
2533 if Pic.Max_Currency_Digits > 0 then
2534 raise Picture_Error;
2537 -- Cannot have leading and trailing currency
2543 when others => raise Picture_Error;
2546 end Star_Suppression;
2548 ----------------------
2549 -- Trailing_Bracket --
2550 ----------------------
2552 procedure Trailing_Bracket is
2554 Debug_Start ("Trailing_Bracket");
2557 Pic.Second_Sign := Index;
2560 raise Picture_Error;
2562 end Trailing_Bracket;
2564 -----------------------
2565 -- Trailing_Currency --
2566 -----------------------
2568 procedure Trailing_Currency is
2570 Debug_Start ("Trailing_Currency");
2577 Pic.Start_Currency := Index;
2578 Pic.End_Currency := Index;
2582 while not At_End and then Look = '#' loop
2583 if Pic.Start_Currency = Invalid_Position then
2584 Pic.Start_Currency := Index;
2587 Pic.End_Currency := Index;
2598 when '_' | '0' | '/' => Skip;
2601 Pic.Picture.Expanded (Index) := 'b';
2604 when others => return;
2607 end Trailing_Currency;
2609 ----------------------
2610 -- Zero_Suppression --
2611 ----------------------
2613 procedure Zero_Suppression is
2615 Debug_Start ("Zero_Suppression");
2618 Pic.Start_Float := Index;
2619 Pic.End_Float := Index;
2620 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2621 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2626 -- Even a single Z is a valid picture
2634 when '_' | '0' | '/' =>
2635 Pic.End_Float := Index;
2639 Pic.End_Float := Index;
2640 Pic.Picture.Expanded (Index) := 'b';
2644 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2646 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2647 Pic.End_Float := Index;
2656 when '.' | 'V' | 'v' =>
2657 Pic.Radix_Position := Index;
2659 Number_Fraction_Or_Z_Fill;
2671 end Zero_Suppression;
2673 -- Start of processing for Precalculate
2676 pragma Debug (Set_Debug);
2681 Ada.Text_IO.New_Line;
2682 Ada.Text_IO.Put (" Picture : """ &
2683 Pic.Picture.Expanded (1 .. Pic.Picture.Length) & """,");
2684 Ada.Text_IO.Put (" Floater : '" & Pic.Floater & "',");
2687 if State = Reject then
2688 raise Picture_Error;
2691 Debug_Integer (Pic.Radix_Position, "Radix Positon : ");
2692 Debug_Integer (Pic.Sign_Position, "Sign Positon : ");
2693 Debug_Integer (Pic.Second_Sign, "Second Sign : ");
2694 Debug_Integer (Pic.Start_Float, "Start Float : ");
2695 Debug_Integer (Pic.End_Float, "End Float : ");
2696 Debug_Integer (Pic.Start_Currency, "Start Currency : ");
2697 Debug_Integer (Pic.End_Currency, "End Currency : ");
2698 Debug_Integer (Pic.Max_Leading_Digits, "Max Leading Digits : ");
2699 Debug_Integer (Pic.Max_Trailing_Digits, "Max Trailing Digits : ");
2702 Ada.Text_IO.New_Line;
2707 when Constraint_Error =>
2709 -- To deal with special cases like null strings
2711 raise Picture_Error;
2719 (Pic_String : String;
2720 Blank_When_Zero : Boolean := False) return Picture
2726 Item : constant String := Expand (Pic_String);
2729 Result.Contents.Picture := (Item'Length, Item);
2730 Result.Contents.Original_BWZ := Blank_When_Zero;
2731 Result.Contents.Blank_When_Zero := Blank_When_Zero;
2732 Precalculate (Result.Contents);
2738 raise Picture_Error;
2746 (Pic_String : String;
2747 Blank_When_Zero : Boolean := False) return Boolean
2751 Expanded_Pic : constant String := Expand (Pic_String);
2752 -- Raises Picture_Error if Item not well-formed
2754 Format_Rec : Format_Record;
2757 Format_Rec.Picture := (Expanded_Pic'Length, Expanded_Pic);
2758 Format_Rec.Blank_When_Zero := Blank_When_Zero;
2759 Format_Rec.Original_BWZ := Blank_When_Zero;
2760 Precalculate (Format_Rec);
2762 -- False only if Blank_When_Zero is True but the pic string has a '*'
2764 return not Blank_When_Zero
2765 or else Strings_Fixed.Index (Expanded_Pic, "*") = 0;
2769 when others => return False;
2772 --------------------
2773 -- Decimal_Output --
2774 --------------------
2776 package body Decimal_Output is
2785 Currency : String := Default_Currency;
2786 Fill : Character := Default_Fill;
2787 Separator : Character := Default_Separator;
2788 Radix_Mark : Character := Default_Radix_Mark) return String
2791 return Format_Number
2792 (Pic.Contents, Num'Image (Item),
2793 Currency, Fill, Separator, Radix_Mark);
2802 Currency : String := Default_Currency) return Natural
2804 Picstr : constant String := Pic_String (Pic);
2805 V_Adjust : Integer := 0;
2806 Cur_Adjust : Integer := 0;
2809 -- Check if Picstr has 'V' or '$'
2811 -- If 'V', then length is 1 less than otherwise
2813 -- If '$', then length is Currency'Length-1 more than otherwise
2815 -- This should use the string handling package ???
2817 for J in Picstr'Range loop
2818 if Picstr (J) = 'V' then
2821 elsif Picstr (J) = '$' then
2822 Cur_Adjust := Currency'Length - 1;
2826 return Picstr'Length - V_Adjust + Cur_Adjust;
2834 (File : Text_IO.File_Type;
2837 Currency : String := Default_Currency;
2838 Fill : Character := Default_Fill;
2839 Separator : Character := Default_Separator;
2840 Radix_Mark : Character := Default_Radix_Mark)
2843 Text_IO.Put (File, Image (Item, Pic,
2844 Currency, Fill, Separator, Radix_Mark));
2850 Currency : String := Default_Currency;
2851 Fill : Character := Default_Fill;
2852 Separator : Character := Default_Separator;
2853 Radix_Mark : Character := Default_Radix_Mark)
2856 Text_IO.Put (Image (Item, Pic,
2857 Currency, Fill, Separator, Radix_Mark));
2864 Currency : String := Default_Currency;
2865 Fill : Character := Default_Fill;
2866 Separator : Character := Default_Separator;
2867 Radix_Mark : Character := Default_Radix_Mark)
2869 Result : constant String :=
2870 Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
2873 if Result'Length > To'Length then
2874 raise Ada.Text_IO.Layout_Error;
2876 Strings_Fixed.Move (Source => Result, Target => To,
2877 Justify => Strings.Right);
2888 Currency : String := Default_Currency) return Boolean
2892 Temp : constant String := Image (Item, Pic, Currency);
2893 pragma Warnings (Off, Temp);
2899 when Ada.Text_IO.Layout_Error => return False;
2904 end Ada.Text_IO.Editing;