1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ 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 with Ada.Strings.Wide_Fixed;
37 package body Ada.Wide_Text_IO.Editing is
39 package Strings renames Ada.Strings;
40 package Strings_Fixed renames Ada.Strings.Fixed;
41 package Strings_Wide_Fixed renames Ada.Strings.Wide_Fixed;
42 package Wide_Text_IO renames Ada.Wide_Text_IO;
44 -----------------------
45 -- Local_Subprograms --
46 -----------------------
48 function To_Wide (C : Character) return Wide_Character;
49 pragma Inline (To_Wide);
50 -- Convert Character to corresponding Wide_Character
56 function Blank_When_Zero (Pic : Picture) return Boolean is
58 return Pic.Contents.Original_BWZ;
65 package body Decimal_Output is
74 Currency : Wide_String := Default_Currency;
75 Fill : Wide_Character := Default_Fill;
76 Separator : Wide_Character := Default_Separator;
77 Radix_Mark : Wide_Character := Default_Radix_Mark) return Wide_String
81 (Pic.Contents, Num'Image (Item),
82 Currency, Fill, Separator, Radix_Mark);
91 Currency : Wide_String := Default_Currency) return Natural
93 Picstr : constant String := Pic_String (Pic);
94 V_Adjust : Integer := 0;
95 Cur_Adjust : Integer := 0;
98 -- Check if Picstr has 'V' or '$'
100 -- If 'V', then length is 1 less than otherwise
102 -- If '$', then length is Currency'Length-1 more than otherwise
104 -- This should use the string handling package ???
106 for J in Picstr'Range loop
107 if Picstr (J) = 'V' then
110 elsif Picstr (J) = '$' then
111 Cur_Adjust := Currency'Length - 1;
115 return Picstr'Length - V_Adjust + Cur_Adjust;
123 (File : Wide_Text_IO.File_Type;
126 Currency : Wide_String := Default_Currency;
127 Fill : Wide_Character := Default_Fill;
128 Separator : Wide_Character := Default_Separator;
129 Radix_Mark : Wide_Character := Default_Radix_Mark)
132 Wide_Text_IO.Put (File, Image (Item, Pic,
133 Currency, Fill, Separator, Radix_Mark));
139 Currency : Wide_String := Default_Currency;
140 Fill : Wide_Character := Default_Fill;
141 Separator : Wide_Character := Default_Separator;
142 Radix_Mark : Wide_Character := Default_Radix_Mark)
145 Wide_Text_IO.Put (Image (Item, Pic,
146 Currency, Fill, Separator, Radix_Mark));
150 (To : out Wide_String;
153 Currency : Wide_String := Default_Currency;
154 Fill : Wide_Character := Default_Fill;
155 Separator : Wide_Character := Default_Separator;
156 Radix_Mark : Wide_Character := Default_Radix_Mark)
158 Result : constant Wide_String :=
159 Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
162 if Result'Length > To'Length then
163 raise Wide_Text_IO.Layout_Error;
165 Strings_Wide_Fixed.Move (Source => Result, Target => To,
166 Justify => Strings.Right);
177 Currency : Wide_String := Default_Currency) return Boolean
181 Temp : constant Wide_String := Image (Item, Pic, Currency);
182 pragma Warnings (Off, Temp);
188 when Layout_Error => return False;
197 function Expand (Picture : String) return String is
198 Result : String (1 .. MAX_PICSIZE);
199 Picture_Index : Integer := Picture'First;
200 Result_Index : Integer := Result'First;
205 if Picture'Length < 1 then
209 if Picture (Picture'First) = '(' then
214 case Picture (Picture_Index) is
218 -- We now need to scan out the count after a left paren. In
219 -- the non-wide version we used Integer_IO.Get, but that is
220 -- not convenient here, since we don't want to drag in normal
221 -- Text_IO just for this purpose. So we do the scan ourselves,
222 -- with the normal validity checks.
224 Last := Picture_Index + 1;
227 if Picture (Last) not in '0' .. '9' then
231 Count := Character'Pos (Picture (Last)) - Character'Pos ('0');
235 if Last > Picture'Last then
239 if Picture (Last) = '_' then
240 if Picture (Last - 1) = '_' then
244 elsif Picture (Last) = ')' then
247 elsif Picture (Last) not in '0' .. '9' then
252 + Character'Pos (Picture (Last)) -
259 -- In what follows note that one copy of the repeated
260 -- character has already been made, so a count of one is
261 -- no-op, and a count of zero erases a character.
263 for J in 2 .. Count loop
264 Result (Result_Index + J - 2) := Picture (Picture_Index - 1);
267 Result_Index := Result_Index + Count - 1;
269 -- Last was a ')' throw it away too
271 Picture_Index := Last + 1;
277 Result (Result_Index) := Picture (Picture_Index);
278 Picture_Index := Picture_Index + 1;
279 Result_Index := Result_Index + 1;
283 exit when Picture_Index > Picture'Last;
286 return Result (1 .. Result_Index - 1);
297 function Format_Number
298 (Pic : Format_Record;
300 Currency_Symbol : Wide_String;
301 Fill_Character : Wide_Character;
302 Separator_Character : Wide_Character;
303 Radix_Point : Wide_Character) return Wide_String
305 Attrs : Number_Attributes := Parse_Number_String (Number);
307 Rounded : String := Number;
309 Sign_Position : Integer := Pic.Sign_Position; -- may float.
311 Answer : Wide_String (1 .. Pic.Picture.Length);
313 Currency_Pos : Integer := Pic.Start_Currency;
315 Dollar : Boolean := False;
316 -- Overridden immediately if necessary
318 Zero : Boolean := True;
319 -- Set to False when a non-zero digit is output
323 -- If the picture has fewer decimal places than the number, the image
324 -- must be rounded according to the usual rules.
326 if Attrs.Has_Fraction then
328 R : constant Integer :=
329 (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
330 - Pic.Max_Trailing_Digits;
335 R_Pos := Rounded'Length - R;
337 if Rounded (R_Pos + 1) > '4' then
339 if Rounded (R_Pos) = '.' then
343 if Rounded (R_Pos) /= '9' then
344 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
346 Rounded (R_Pos) := '0';
350 if Rounded (R_Pos) = '.' then
354 if Rounded (R_Pos) /= '9' then
355 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
358 Rounded (R_Pos) := '0';
363 -- The rounding may add a digit in front. Either the
364 -- leading blank or the sign (already captured) can be
368 Rounded (R_Pos) := '1';
369 Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
377 for J in Answer'Range loop
378 Answer (J) := To_Wide (Pic.Picture.Expanded (J));
381 if Pic.Start_Currency /= Invalid_Position then
382 Dollar := Answer (Pic.Start_Currency) = '$';
385 -- Fix up "direct inserts" outside the playing field. Set up as one
386 -- loop to do the beginning, one (reverse) loop to do the end.
390 exit when Last = Pic.Start_Float;
391 exit when Last = Pic.Radix_Position;
392 exit when Answer (Last) = '9';
394 case Answer (Last) is
397 Answer (Last) := Separator_Character;
400 Answer (Last) := ' ';
407 exit when Last = Answer'Last;
412 -- Now for the end...
414 for J in reverse Last .. Answer'Last loop
415 exit when J = Pic.Radix_Position;
417 -- Do this test First, Separator_Character can equal Pic.Floater
419 if Answer (J) = Pic.Floater then
426 Answer (J) := Separator_Character;
442 if Pic.Start_Currency /= -1
443 and then Answer (Pic.Start_Currency) = '#'
444 and then Pic.Floater /= '#'
446 if Currency_Symbol'Length >
447 Pic.End_Currency - Pic.Start_Currency + 1
451 elsif Currency_Symbol'Length =
452 Pic.End_Currency - Pic.Start_Currency + 1
454 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
457 elsif Pic.Radix_Position = Invalid_Position
458 or else Pic.Start_Currency < Pic.Radix_Position
460 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
462 Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
463 Pic.End_Currency) := Currency_Symbol;
466 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
468 Answer (Pic.Start_Currency ..
469 Pic.Start_Currency + Currency_Symbol'Length - 1) :=
474 -- Fill in leading digits
476 if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
477 Pic.Max_Leading_Digits
482 if Pic.Radix_Position = Invalid_Position then
483 Position := Answer'Last;
485 Position := Pic.Radix_Position - 1;
488 for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
490 while Answer (Position) /= '9'
491 and Answer (Position) /= Pic.Floater
493 if Answer (Position) = '_' then
494 Answer (Position) := Separator_Character;
496 elsif Answer (Position) = 'b' then
497 Answer (Position) := ' ';
500 Position := Position - 1;
503 Answer (Position) := To_Wide (Rounded (J));
505 if Rounded (J) /= '0' then
509 Position := Position - 1;
514 if Pic.Start_Float = Invalid_Position then
516 -- No leading floats, but need to change '9' to '0', '_' to
517 -- Separator_Character and 'b' to ' '.
519 for J in Last .. Position loop
521 -- Last set when fixing the "uninteresting" leaders above.
522 -- Don't duplicate the work.
524 if Answer (J) = '9' then
527 elsif Answer (J) = '_' then
528 Answer (J) := Separator_Character;
530 elsif Answer (J) = 'b' then
537 elsif Pic.Floater = '<'
543 for J in Pic.End_Float .. Position loop -- May be null range
544 if Answer (J) = '9' then
547 elsif Answer (J) = '_' then
548 Answer (J) := Separator_Character;
550 elsif Answer (J) = 'b' then
556 if Position > Pic.End_Float then
557 Position := Pic.End_Float;
560 for J in Pic.Start_Float .. Position - 1 loop
564 Answer (Position) := Pic.Floater;
565 Sign_Position := Position;
567 elsif Pic.Floater = '$' then
569 for J in Pic.End_Float .. Position loop -- May be null range
570 if Answer (J) = '9' then
573 elsif Answer (J) = '_' then
574 Answer (J) := ' '; -- no separator before leftmost digit
576 elsif Answer (J) = 'b' then
581 if Position > Pic.End_Float then
582 Position := Pic.End_Float;
585 for J in Pic.Start_Float .. Position - 1 loop
589 Answer (Position) := Pic.Floater;
590 Currency_Pos := Position;
592 elsif Pic.Floater = '*' then
594 for J in Pic.End_Float .. Position loop -- May be null range
595 if Answer (J) = '9' then
598 elsif Answer (J) = '_' then
599 Answer (J) := Separator_Character;
601 elsif Answer (J) = 'b' then
606 if Position > Pic.End_Float then
607 Position := Pic.End_Float;
610 for J in Pic.Start_Float .. Position loop
615 if Pic.Floater = '#' then
616 Currency_Pos := Currency_Symbol'Length;
619 for J in reverse Pic.Start_Float .. Position loop
623 Answer (J) := Fill_Character;
625 when 'Z' | 'b' | '/' | '0' =>
631 when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
635 if Currency_Pos = 0 then
638 Answer (J) := Currency_Symbol (Currency_Pos);
639 Currency_Pos := Currency_Pos - 1;
647 Answer (J) := Fill_Character;
653 if Currency_Pos = 0 then
657 Answer (J) := Currency_Symbol (Currency_Pos);
658 Currency_Pos := Currency_Pos - 1;
672 if Pic.Floater = '#' and then Currency_Pos /= 0 then
679 if Sign_Position = Invalid_Position then
680 if Attrs.Negative then
685 if Attrs.Negative then
686 case Answer (Sign_Position) is
687 when 'C' | 'D' | '-' =>
691 Answer (Sign_Position) := '-';
694 Answer (Sign_Position) := '(';
695 Answer (Pic.Second_Sign) := ')';
704 case Answer (Sign_Position) is
707 Answer (Sign_Position) := ' ';
709 when '<' | 'C' | 'D' =>
710 Answer (Sign_Position) := ' ';
711 Answer (Pic.Second_Sign) := ' ';
723 -- Fill in trailing digits
725 if Pic.Max_Trailing_Digits > 0 then
727 if Attrs.Has_Fraction then
728 Position := Attrs.Start_Of_Fraction;
729 Last := Pic.Radix_Position + 1;
731 for J in Last .. Answer'Last loop
733 if Answer (J) = '9' or Answer (J) = Pic.Floater then
734 Answer (J) := To_Wide (Rounded (Position));
736 if Rounded (Position) /= '0' then
740 Position := Position + 1;
743 -- Used up fraction but remember place in Answer
745 exit when Position > Attrs.End_Of_Fraction;
747 elsif Answer (J) = 'b' then
750 elsif Answer (J) = '_' then
751 Answer (J) := Separator_Character;
761 Position := Pic.Radix_Position + 1;
764 -- Now fill remaining 9's with zeros and _ with separators
768 for J in Position .. Last loop
769 if Answer (J) = '9' then
772 elsif Answer (J) = Pic.Floater then
775 elsif Answer (J) = '_' then
776 Answer (J) := Separator_Character;
778 elsif Answer (J) = 'b' then
784 Position := Last + 1;
787 if Pic.Floater = '#' and then Currency_Pos /= 0 then
791 -- No trailing digits, but now J may need to stick in a currency
794 if Pic.Start_Currency = Invalid_Position then
795 Position := Answer'Last + 1;
797 Position := Pic.Start_Currency;
801 for J in Position .. Answer'Last loop
803 if Pic.Start_Currency /= Invalid_Position and then
804 Answer (Pic.Start_Currency) = '#' then
808 -- Note: There are some weird cases J can imagine with 'b' or '#'
809 -- in currency strings where the following code will cause
810 -- glitches. The trick is to tell when the character in the
811 -- answer should be checked, and when to look at the original
812 -- string. Some other time. RIE 11/26/96 ???
816 Answer (J) := Fill_Character;
822 if Currency_Pos > Currency_Symbol'Length then
826 Answer (J) := Currency_Symbol (Currency_Pos);
827 Currency_Pos := Currency_Pos + 1;
835 Answer (J) := Fill_Character;
841 if Currency_Pos > Currency_Symbol'Length then
844 Answer (J) := Currency_Symbol (Currency_Pos);
845 Currency_Pos := Currency_Pos + 1;
859 -- Now get rid of Blank_when_Zero and complete Star fill
861 if Zero and Pic.Blank_When_Zero then
863 -- Value is zero, and blank it
868 Last := Last - 1 + Currency_Symbol'Length;
871 if Pic.Radix_Position /= Invalid_Position and then
872 Answer (Pic.Radix_Position) = 'V' then
876 return Wide_String'(1 .. Last => ' ');
878 elsif Zero and Pic.Star_Fill then
882 Last := Last - 1 + Currency_Symbol'Length;
885 if Pic.Radix_Position /= Invalid_Position then
887 if Answer (Pic.Radix_Position) = 'V' then
891 if Pic.Radix_Position > Pic.Start_Currency then
892 return Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
894 Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
900 Pic.Radix_Position + Currency_Symbol'Length - 2
904 (Pic.Radix_Position + Currency_Symbol'Length .. Last
910 Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
912 Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
916 return Wide_String'(1 .. Last => '*');
919 -- This was once a simple return statement, now there are nine
920 -- different return cases. Not to mention the five above to deal
921 -- with zeros. Why not split things out?
923 -- Processing the radix and sign expansion separately
924 -- would require lots of copying--the string and some of its
925 -- indicies--without really simplifying the logic. The cases are:
927 -- 1) Expand $, replace '.' with Radix_Point
928 -- 2) No currency expansion, replace '.' with Radix_Point
929 -- 3) Expand $, radix blanked
930 -- 4) No currency expansion, radix blanked
932 -- 6) Expand $, Elide V
933 -- 7) Elide V, Expand $ (Two cases depending on order.)
934 -- 8) No radix, expand $
935 -- 9) No radix, no currency expansion
937 if Pic.Radix_Position /= Invalid_Position then
939 if Answer (Pic.Radix_Position) = '.' then
940 Answer (Pic.Radix_Position) := Radix_Point;
944 -- 1) Expand $, replace '.' with Radix_Point
946 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
947 Answer (Currency_Pos + 1 .. Answer'Last);
950 -- 2) No currency expansion, replace '.' with Radix_Point
955 elsif Answer (Pic.Radix_Position) = ' ' then -- blanked radix.
958 -- 3) Expand $, radix blanked
960 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
961 Answer (Currency_Pos + 1 .. Answer'Last);
964 -- 4) No expansion, radix blanked
976 return Answer (1 .. Pic.Radix_Position - 1) &
977 Answer (Pic.Radix_Position + 1 .. Answer'Last);
979 elsif Currency_Pos < Pic.Radix_Position then
981 -- 6) Expand $, Elide V
983 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
984 Answer (Currency_Pos + 1 .. Pic.Radix_Position - 1) &
985 Answer (Pic.Radix_Position + 1 .. Answer'Last);
988 -- 7) Elide V, Expand $
990 return Answer (1 .. Pic.Radix_Position - 1) &
991 Answer (Pic.Radix_Position + 1 .. Currency_Pos - 1) &
993 Answer (Currency_Pos + 1 .. Answer'Last);
999 -- 8) No radix, expand $
1001 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
1002 Answer (Currency_Pos + 1 .. Answer'Last);
1005 -- 9) No radix, no currency expansion
1011 -------------------------
1012 -- Parse_Number_String --
1013 -------------------------
1015 function Parse_Number_String (Str : String) return Number_Attributes is
1016 Answer : Number_Attributes;
1019 for J in Str'Range loop
1027 -- Decide if this is the start of a number.
1028 -- If so, figure out which one...
1030 if Answer.Has_Fraction then
1031 Answer.End_Of_Fraction := J;
1033 if Answer.Start_Of_Int = Invalid_Position then
1035 Answer.Start_Of_Int := J;
1037 Answer.End_Of_Int := J;
1042 -- Only count a zero before the decimal point if it follows a
1043 -- non-zero digit. After the decimal point, zeros will be
1044 -- counted if followed by a non-zero digit.
1046 if not Answer.Has_Fraction then
1047 if Answer.Start_Of_Int /= Invalid_Position then
1048 Answer.End_Of_Int := J;
1056 Answer.Negative := True;
1060 -- Close integer, start fraction
1062 if Answer.Has_Fraction then
1063 raise Picture_Error;
1066 -- Two decimal points is a no-no
1068 Answer.Has_Fraction := True;
1069 Answer.End_Of_Fraction := J;
1071 -- Could leave this at Invalid_Position, but this seems the
1072 -- right way to indicate a null range...
1074 Answer.Start_Of_Fraction := J + 1;
1075 Answer.End_Of_Int := J - 1;
1078 raise Picture_Error; -- can this happen? probably not!
1082 if Answer.Start_Of_Int = Invalid_Position then
1083 Answer.Start_Of_Int := Answer.End_Of_Int + 1;
1086 -- No significant (intger) digits needs a null range
1089 end Parse_Number_String;
1095 -- The following ensures that we return B and not b being careful not
1096 -- to break things which expect lower case b for blank. See CXF3A02.
1098 function Pic_String (Pic : Picture) return String is
1099 Temp : String (1 .. Pic.Contents.Picture.Length) :=
1100 Pic.Contents.Picture.Expanded;
1102 for J in Temp'Range loop
1103 if Temp (J) = 'b' then
1115 procedure Precalculate (Pic : in out Format_Record) is
1117 Computed_BWZ : Boolean := True;
1119 type Legality is (Okay, Reject);
1120 State : Legality := Reject;
1121 -- Start in reject, which will reject null strings
1123 Index : Pic_Index := Pic.Picture.Expanded'First;
1125 function At_End return Boolean;
1126 pragma Inline (At_End);
1128 procedure Set_State (L : Legality);
1129 pragma Inline (Set_State);
1131 function Look return Character;
1132 pragma Inline (Look);
1134 function Is_Insert return Boolean;
1135 pragma Inline (Is_Insert);
1138 pragma Inline (Skip);
1140 procedure Trailing_Currency;
1141 procedure Trailing_Bracket;
1142 procedure Number_Fraction;
1143 procedure Number_Completion;
1144 procedure Number_Fraction_Or_Bracket;
1145 procedure Number_Fraction_Or_Z_Fill;
1146 procedure Zero_Suppression;
1147 procedure Floating_Bracket;
1148 procedure Number_Fraction_Or_Star_Fill;
1149 procedure Star_Suppression;
1150 procedure Number_Fraction_Or_Dollar;
1151 procedure Leading_Dollar;
1152 procedure Number_Fraction_Or_Pound;
1153 procedure Leading_Pound;
1155 procedure Floating_Plus;
1156 procedure Floating_Minus;
1157 procedure Picture_Plus;
1158 procedure Picture_Minus;
1159 procedure Picture_Bracket;
1161 procedure Optional_RHS_Sign;
1162 procedure Picture_String;
1168 function At_End return Boolean is
1170 return Index > Pic.Picture.Length;
1173 ----------------------
1174 -- Floating_Bracket --
1175 ----------------------
1177 -- Note that Floating_Bracket is only called with an acceptable
1178 -- prefix. But we don't set Okay, because we must end with a '>'.
1180 procedure Floating_Bracket is
1183 Pic.End_Float := Index;
1184 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1186 -- First bracket wasn't counted...
1197 when '_' | '0' | '/' =>
1198 Pic.End_Float := Index;
1202 Pic.End_Float := Index;
1203 Pic.Picture.Expanded (Index) := 'b';
1207 Pic.End_Float := Index;
1208 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1220 when 'V' | 'v' | '.' =>
1221 Pic.Radix_Position := Index;
1223 Number_Fraction_Or_Bracket;
1230 end Floating_Bracket;
1232 --------------------
1233 -- Floating_Minus --
1234 --------------------
1236 procedure Floating_Minus is
1244 when '_' | '0' | '/' =>
1245 Pic.End_Float := Index;
1249 Pic.End_Float := Index;
1250 Pic.Picture.Expanded (Index) := 'b';
1254 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1255 Pic.End_Float := Index;
1262 when '.' | 'V' | 'v' =>
1263 Pic.Radix_Position := Index;
1266 while Is_Insert loop
1283 Pic.Max_Trailing_Digits :=
1284 Pic.Max_Trailing_Digits + 1;
1285 Pic.End_Float := Index;
1288 when '_' | '0' | '/' =>
1292 Pic.Picture.Expanded (Index) := 'b';
1317 procedure Floating_Plus is
1325 when '_' | '0' | '/' =>
1326 Pic.End_Float := Index;
1330 Pic.End_Float := Index;
1331 Pic.Picture.Expanded (Index) := 'b';
1335 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1336 Pic.End_Float := Index;
1343 when '.' | 'V' | 'v' =>
1344 Pic.Radix_Position := Index;
1347 while Is_Insert loop
1364 Pic.Max_Trailing_Digits :=
1365 Pic.Max_Trailing_Digits + 1;
1366 Pic.End_Float := Index;
1369 when '_' | '0' | '/' =>
1373 Pic.Picture.Expanded (Index) := 'b';
1399 function Is_Insert return Boolean is
1405 case Pic.Picture.Expanded (Index) is
1407 when '_' | '0' | '/' => return True;
1410 Pic.Picture.Expanded (Index) := 'b'; -- canonical
1413 when others => return False;
1417 --------------------
1418 -- Leading_Dollar --
1419 --------------------
1421 -- Note that Leading_Dollar can be called in either State.
1422 -- It will set state to Okay only if a 9 or (second) $
1425 -- Also notice the tricky bit with State and Zero_Suppression.
1426 -- Zero_Suppression is Picture_Error if a '$' or a '9' has been
1427 -- encountered, exactly the cases where State has been set.
1429 procedure Leading_Dollar is
1431 -- Treat as a floating dollar, and unwind otherwise
1434 Pic.Start_Currency := Index;
1435 Pic.End_Currency := Index;
1436 Pic.Start_Float := Index;
1437 Pic.End_Float := Index;
1439 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1451 when '_' | '0' | '/' =>
1452 Pic.End_Float := Index;
1455 -- A trailing insertion character is not part of the
1456 -- floating currency, so need to look ahead.
1459 Pic.End_Float := Pic.End_Float - 1;
1463 Pic.End_Float := Index;
1464 Pic.Picture.Expanded (Index) := 'b';
1468 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1470 if State = Okay then
1471 raise Picture_Error;
1473 -- Will overwrite Floater and Start_Float
1479 if State = Okay then
1480 raise Picture_Error;
1482 -- Will overwrite Floater and Start_Float
1488 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1489 Pic.End_Float := Index;
1490 Pic.End_Currency := Index;
1491 Set_State (Okay); Skip;
1494 if State /= Okay then
1496 Pic.Start_Float := Invalid_Position;
1497 Pic.End_Float := Invalid_Position;
1500 -- A single dollar does not a floating make
1505 when 'V' | 'v' | '.' =>
1506 if State /= Okay then
1508 Pic.Start_Float := Invalid_Position;
1509 Pic.End_Float := Invalid_Position;
1512 -- Only one dollar before the sign is okay, but doesn't
1515 Pic.Radix_Position := Index;
1517 Number_Fraction_Or_Dollar;
1531 -- This one is complex! A Leading_Pound can be fixed or floating,
1532 -- but in some cases the decision has to be deferred until we leave
1533 -- this procedure. Also note that Leading_Pound can be called in
1536 -- It will set state to Okay only if a 9 or (second) # is
1539 -- One Last note: In ambiguous cases, the currency is treated as
1540 -- floating unless there is only one '#'.
1542 procedure Leading_Pound is
1544 Inserts : Boolean := False;
1545 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1547 Must_Float : Boolean := False;
1548 -- Set to true if a '#' occurs after an insert
1551 -- Treat as a floating currency. If it isn't, this will be
1552 -- overwritten later.
1556 Pic.Start_Currency := Index;
1557 Pic.End_Currency := Index;
1558 Pic.Start_Float := Index;
1559 Pic.End_Float := Index;
1561 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1564 Pic.Max_Currency_Digits := 1; -- we've seen one.
1575 when '_' | '0' | '/' =>
1576 Pic.End_Float := Index;
1581 Pic.Picture.Expanded (Index) := 'b';
1582 Pic.End_Float := Index;
1587 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1590 raise Picture_Error;
1592 Pic.Max_Leading_Digits := 0;
1594 -- Will overwrite Floater and Start_Float
1601 raise Picture_Error;
1603 Pic.Max_Leading_Digits := 0;
1605 -- Will overwrite Floater and Start_Float
1615 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1616 Pic.End_Float := Index;
1617 Pic.End_Currency := Index;
1622 if State /= Okay then
1624 -- A single '#' doesn't float
1627 Pic.Start_Float := Invalid_Position;
1628 Pic.End_Float := Invalid_Position;
1634 when 'V' | 'v' | '.' =>
1635 if State /= Okay then
1637 Pic.Start_Float := Invalid_Position;
1638 Pic.End_Float := Invalid_Position;
1641 -- Only one pound before the sign is okay, but doesn't
1644 Pic.Radix_Position := Index;
1646 Number_Fraction_Or_Pound;
1659 function Look return Character is
1662 raise Picture_Error;
1665 return Pic.Picture.Expanded (Index);
1677 when '_' | '0' | '/' =>
1681 Pic.Picture.Expanded (Index) := 'b';
1685 Computed_BWZ := False;
1686 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1690 when '.' | 'V' | 'v' =>
1691 Pic.Radix_Position := Index;
1705 -- Will return in Okay state if a '9' was seen
1710 -----------------------
1711 -- Number_Completion --
1712 -----------------------
1714 procedure Number_Completion is
1716 while not At_End loop
1719 when '_' | '0' | '/' =>
1723 Pic.Picture.Expanded (Index) := 'b';
1727 Computed_BWZ := False;
1728 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1732 when 'V' | 'v' | '.' =>
1733 Pic.Radix_Position := Index;
1742 end Number_Completion;
1744 ---------------------
1745 -- Number_Fraction --
1746 ---------------------
1748 procedure Number_Fraction is
1750 -- Note that number fraction can be called in either State.
1751 -- It will set state to Valid only if a 9 is encountered.
1759 when '_' | '0' | '/' =>
1763 Pic.Picture.Expanded (Index) := 'b';
1767 Computed_BWZ := False;
1768 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1769 Set_State (Okay); Skip;
1775 end Number_Fraction;
1777 --------------------------------
1778 -- Number_Fraction_Or_Bracket --
1779 --------------------------------
1781 procedure Number_Fraction_Or_Bracket is
1790 when '_' | '0' | '/' => Skip;
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_Bracket;
1832 -------------------------------
1833 -- Number_Fraction_Or_Dollar --
1834 -------------------------------
1836 procedure Number_Fraction_Or_Dollar is
1844 when '_' | '0' | '/' =>
1848 Pic.Picture.Expanded (Index) := 'b';
1852 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1853 Pic.End_Float := Index;
1862 when '_' | '0' | '/' =>
1866 Pic.Picture.Expanded (Index) := 'b';
1870 Pic.Max_Trailing_Digits :=
1871 Pic.Max_Trailing_Digits + 1;
1872 Pic.End_Float := Index;
1885 end Number_Fraction_Or_Dollar;
1887 ------------------------------
1888 -- Number_Fraction_Or_Pound --
1889 ------------------------------
1891 procedure Number_Fraction_Or_Pound is
1900 when '_' | '0' | '/' =>
1904 Pic.Picture.Expanded (Index) := 'b';
1908 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1909 Pic.End_Float := Index;
1919 when '_' | '0' | '/' =>
1923 Pic.Picture.Expanded (Index) := 'b';
1927 Pic.Max_Trailing_Digits :=
1928 Pic.Max_Trailing_Digits + 1;
1929 Pic.End_Float := Index;
1944 end Number_Fraction_Or_Pound;
1946 ----------------------------------
1947 -- Number_Fraction_Or_Star_Fill --
1948 ----------------------------------
1950 procedure Number_Fraction_Or_Star_Fill is
1959 when '_' | '0' | '/' =>
1963 Pic.Picture.Expanded (Index) := 'b';
1967 Pic.Star_Fill := True;
1968 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1969 Pic.End_Float := Index;
1979 when '_' | '0' | '/' =>
1983 Pic.Picture.Expanded (Index) := 'b';
1987 Pic.Star_Fill := True;
1988 Pic.Max_Trailing_Digits :=
1989 Pic.Max_Trailing_Digits + 1;
1990 Pic.End_Float := Index;
2004 end Number_Fraction_Or_Star_Fill;
2006 -------------------------------
2007 -- Number_Fraction_Or_Z_Fill --
2008 -------------------------------
2010 procedure Number_Fraction_Or_Z_Fill is
2019 when '_' | '0' | '/' =>
2023 Pic.Picture.Expanded (Index) := 'b';
2027 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
2028 Pic.End_Float := Index;
2029 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2040 when '_' | '0' | '/' =>
2044 Pic.Picture.Expanded (Index) := 'b';
2048 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2050 Pic.Max_Trailing_Digits :=
2051 Pic.Max_Trailing_Digits + 1;
2052 Pic.End_Float := Index;
2065 end Number_Fraction_Or_Z_Fill;
2067 -----------------------
2068 -- Optional_RHS_Sign --
2069 -----------------------
2071 procedure Optional_RHS_Sign is
2080 Pic.Sign_Position := Index;
2085 Pic.Sign_Position := Index;
2086 Pic.Picture.Expanded (Index) := 'C';
2089 if Look = 'R' or Look = 'r' then
2090 Pic.Second_Sign := Index;
2091 Pic.Picture.Expanded (Index) := 'R';
2095 raise Picture_Error;
2101 Pic.Sign_Position := Index;
2102 Pic.Picture.Expanded (Index) := 'D';
2105 if Look = 'B' or Look = 'b' then
2106 Pic.Second_Sign := Index;
2107 Pic.Picture.Expanded (Index) := 'B';
2111 raise Picture_Error;
2117 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2118 Pic.Second_Sign := Index;
2122 raise Picture_Error;
2129 end Optional_RHS_Sign;
2135 -- Note that Picture can be called in either State
2137 -- It will set state to Valid only if a 9 is encountered or floating
2138 -- currency is called.
2140 procedure Picture is
2149 when '_' | '0' | '/' =>
2153 Pic.Picture.Expanded (Index) := 'b';
2165 Computed_BWZ := False;
2167 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2170 when 'V' | 'v' | '.' =>
2171 Pic.Radix_Position := Index;
2184 ---------------------
2185 -- Picture_Bracket --
2186 ---------------------
2188 procedure Picture_Bracket is
2190 Pic.Sign_Position := Index;
2191 Pic.Sign_Position := Index;
2193 -- Treat as a floating sign, and unwind otherwise
2196 Pic.Start_Float := Index;
2197 Pic.End_Float := Index;
2199 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2202 Skip; -- Known Bracket
2207 when '_' | '0' | '/' =>
2208 Pic.End_Float := Index;
2212 Pic.End_Float := Index;
2213 Pic.Picture.Expanded (Index) := 'b';
2217 Set_State (Okay); -- "<<>" is enough.
2223 when '$' | '#' | '9' | '*' =>
2224 if State /= Okay then
2226 Pic.Start_Float := Invalid_Position;
2227 Pic.End_Float := Invalid_Position;
2235 when '.' | 'V' | 'v' =>
2236 if State /= Okay then
2238 Pic.Start_Float := Invalid_Position;
2239 Pic.End_Float := Invalid_Position;
2242 -- Don't assume that state is okay, haven't seen a digit
2249 raise Picture_Error;
2253 end Picture_Bracket;
2259 procedure Picture_Minus is
2261 Pic.Sign_Position := Index;
2263 -- Treat as a floating sign, and unwind otherwise
2266 Pic.Start_Float := Index;
2267 Pic.End_Float := Index;
2269 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2272 Skip; -- Known Minus
2277 when '_' | '0' | '/' =>
2278 Pic.End_Float := Index;
2282 Pic.End_Float := Index;
2283 Pic.Picture.Expanded (Index) := 'b';
2287 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2288 Pic.End_Float := Index;
2290 Set_State (Okay); -- "-- " is enough
2295 when '$' | '#' | '9' | '*' =>
2296 if State /= Okay then
2298 Pic.Start_Float := Invalid_Position;
2299 Pic.End_Float := Invalid_Position;
2308 -- Can't have Z and a floating sign
2310 if State = Okay then
2314 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2320 when '.' | 'V' | 'v' =>
2321 if State /= Okay then
2323 Pic.Start_Float := Invalid_Position;
2324 Pic.End_Float := Invalid_Position;
2327 -- Don't assume that state is okay, haven't seen a digit
2343 procedure Picture_Plus is
2345 Pic.Sign_Position := Index;
2347 -- Treat as a floating sign, and unwind otherwise
2350 Pic.Start_Float := Index;
2351 Pic.End_Float := Index;
2353 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2361 when '_' | '0' | '/' =>
2362 Pic.End_Float := Index;
2366 Pic.End_Float := Index;
2367 Pic.Picture.Expanded (Index) := 'b';
2371 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2372 Pic.End_Float := Index;
2374 Set_State (Okay); -- "++" is enough
2379 when '$' | '#' | '9' | '*' =>
2380 if State /= Okay then
2382 Pic.Start_Float := Invalid_Position;
2383 Pic.End_Float := Invalid_Position;
2391 if State = Okay then
2395 -- Can't have Z and a floating sign
2397 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2399 -- '+Z' is acceptable
2408 when '.' | 'V' | 'v' =>
2409 if State /= Okay then
2411 Pic.Start_Float := Invalid_Position;
2412 Pic.End_Float := Invalid_Position;
2415 -- Don't assume that state is okay, haven't seen a digit
2427 --------------------
2428 -- Picture_String --
2429 --------------------
2431 procedure Picture_String is
2433 while Is_Insert loop
2453 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2463 when '9' | '.' | 'V' | 'v' =>
2469 raise Picture_Error;
2473 -- Blank when zero either if the PIC does not contain a '9' or if
2474 -- requested by the user and no '*'
2476 Pic.Blank_When_Zero :=
2477 (Computed_BWZ or Pic.Blank_When_Zero) and not Pic.Star_Fill;
2479 -- Star fill if '*' and no '9'
2481 Pic.Star_Fill := Pic.Star_Fill and Computed_BWZ;
2493 procedure Set_State (L : Legality) is
2507 ----------------------
2508 -- Star_Suppression --
2509 ----------------------
2511 procedure Star_Suppression is
2514 Pic.Start_Float := Index;
2515 Pic.End_Float := Index;
2516 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2519 -- Even a single * is a valid picture
2521 Pic.Star_Fill := True;
2531 when '_' | '0' | '/' =>
2532 Pic.End_Float := Index;
2536 Pic.End_Float := Index;
2537 Pic.Picture.Expanded (Index) := 'b';
2541 Pic.End_Float := Index;
2542 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2543 Set_State (Okay); Skip;
2550 when '.' | 'V' | 'v' =>
2551 Pic.Radix_Position := Index;
2553 Number_Fraction_Or_Star_Fill;
2561 when others => raise Picture_Error;
2564 end Star_Suppression;
2566 ----------------------
2567 -- Trailing_Bracket --
2568 ----------------------
2570 procedure Trailing_Bracket is
2573 Pic.Second_Sign := Index;
2576 raise Picture_Error;
2578 end Trailing_Bracket;
2580 -----------------------
2581 -- Trailing_Currency --
2582 -----------------------
2584 procedure Trailing_Currency is
2591 Pic.Start_Currency := Index;
2592 Pic.End_Currency := Index;
2596 while not At_End and then Look = '#' loop
2597 if Pic.Start_Currency = Invalid_Position then
2598 Pic.Start_Currency := Index;
2601 Pic.End_Currency := Index;
2612 when '_' | '0' | '/' => Skip;
2615 Pic.Picture.Expanded (Index) := 'b';
2618 when others => return;
2621 end Trailing_Currency;
2623 ----------------------
2624 -- Zero_Suppression --
2625 ----------------------
2627 procedure Zero_Suppression is
2630 Pic.Start_Float := Index;
2631 Pic.End_Float := Index;
2632 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2633 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2638 -- Even a single Z is a valid picture
2646 when '_' | '0' | '/' =>
2647 Pic.End_Float := Index;
2651 Pic.End_Float := Index;
2652 Pic.Picture.Expanded (Index) := 'b';
2656 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2658 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2659 Pic.End_Float := Index;
2668 when '.' | 'V' | 'v' =>
2669 Pic.Radix_Position := Index;
2671 Number_Fraction_Or_Z_Fill;
2683 end Zero_Suppression;
2685 -- Start of processing for Precalculate
2690 if State = Reject then
2691 raise Picture_Error;
2696 when Constraint_Error =>
2698 -- To deal with special cases like null strings
2700 raise Picture_Error;
2709 (Pic_String : String;
2710 Blank_When_Zero : Boolean := False) return Picture
2716 Item : constant String := Expand (Pic_String);
2719 Result.Contents.Picture := (Item'Length, Item);
2720 Result.Contents.Original_BWZ := Blank_When_Zero;
2721 Result.Contents.Blank_When_Zero := Blank_When_Zero;
2722 Precalculate (Result.Contents);
2728 raise Picture_Error;
2736 function To_Wide (C : Character) return Wide_Character is
2738 return Wide_Character'Val (Character'Pos (C));
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_0 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 end Ada.Wide_Text_IO.Editing;