1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ W I D E _ T E X T _ I O . E D I T I N G --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada.Strings.Fixed;
33 with Ada.Strings.Wide_Wide_Fixed;
35 package body Ada.Wide_Wide_Text_IO.Editing is
37 package Strings renames Ada.Strings;
38 package Strings_Fixed renames Ada.Strings.Fixed;
39 package Strings_Wide_Wide_Fixed renames Ada.Strings.Wide_Wide_Fixed;
40 package Wide_Wide_Text_IO renames Ada.Wide_Wide_Text_IO;
42 -----------------------
43 -- Local_Subprograms --
44 -----------------------
46 function To_Wide (C : Character) return Wide_Wide_Character;
47 pragma Inline (To_Wide);
48 -- Convert Character to corresponding Wide_Wide_Character
54 function Blank_When_Zero (Pic : Picture) return Boolean is
56 return Pic.Contents.Original_BWZ;
63 package body Decimal_Output is
72 Currency : Wide_Wide_String := Default_Currency;
73 Fill : Wide_Wide_Character := Default_Fill;
74 Separator : Wide_Wide_Character := Default_Separator;
75 Radix_Mark : Wide_Wide_Character := Default_Radix_Mark)
76 return Wide_Wide_String
80 (Pic.Contents, Num'Image (Item),
81 Currency, Fill, Separator, Radix_Mark);
90 Currency : Wide_Wide_String := Default_Currency) return Natural
92 Picstr : constant String := Pic_String (Pic);
93 V_Adjust : Integer := 0;
94 Cur_Adjust : Integer := 0;
97 -- Check if Picstr has 'V' or '$'
99 -- If 'V', then length is 1 less than otherwise
101 -- If '$', then length is Currency'Length-1 more than otherwise
103 -- This should use the string handling package ???
105 for J in Picstr'Range loop
106 if Picstr (J) = 'V' then
109 elsif Picstr (J) = '$' then
110 Cur_Adjust := Currency'Length - 1;
114 return Picstr'Length - V_Adjust + Cur_Adjust;
122 (File : Wide_Wide_Text_IO.File_Type;
125 Currency : Wide_Wide_String := Default_Currency;
126 Fill : Wide_Wide_Character := Default_Fill;
127 Separator : Wide_Wide_Character := Default_Separator;
128 Radix_Mark : Wide_Wide_Character := Default_Radix_Mark)
131 Wide_Wide_Text_IO.Put (File, Image (Item, Pic,
132 Currency, Fill, Separator, Radix_Mark));
138 Currency : Wide_Wide_String := Default_Currency;
139 Fill : Wide_Wide_Character := Default_Fill;
140 Separator : Wide_Wide_Character := Default_Separator;
141 Radix_Mark : Wide_Wide_Character := Default_Radix_Mark)
144 Wide_Wide_Text_IO.Put (Image (Item, Pic,
145 Currency, Fill, Separator, Radix_Mark));
149 (To : out Wide_Wide_String;
152 Currency : Wide_Wide_String := Default_Currency;
153 Fill : Wide_Wide_Character := Default_Fill;
154 Separator : Wide_Wide_Character := Default_Separator;
155 Radix_Mark : Wide_Wide_Character := Default_Radix_Mark)
157 Result : constant Wide_Wide_String :=
158 Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
161 if Result'Length > To'Length then
162 raise Wide_Wide_Text_IO.Layout_Error;
164 Strings_Wide_Wide_Fixed.Move (Source => Result, Target => To,
165 Justify => Strings.Right);
176 Currency : Wide_Wide_String := Default_Currency) return Boolean
180 Temp : constant Wide_Wide_String := Image (Item, Pic, Currency);
181 pragma Warnings (Off, Temp);
187 when Layout_Error => return False;
196 function Expand (Picture : String) return String is
197 Result : String (1 .. MAX_PICSIZE);
198 Picture_Index : Integer := Picture'First;
199 Result_Index : Integer := Result'First;
204 if Picture'Length < 1 then
208 if Picture (Picture'First) = '(' then
213 case Picture (Picture_Index) is
217 -- We now need to scan out the count after a left paren. In
218 -- the non-wide version we used Integer_IO.Get, but that is
219 -- not convenient here, since we don't want to drag in normal
220 -- Text_IO just for this purpose. So we do the scan ourselves,
221 -- with the normal validity checks.
223 Last := Picture_Index + 1;
226 if Picture (Last) not in '0' .. '9' then
230 Count := Character'Pos (Picture (Last)) - Character'Pos ('0');
234 if Last > Picture'Last then
238 if Picture (Last) = '_' then
239 if Picture (Last - 1) = '_' then
243 elsif Picture (Last) = ')' then
246 elsif Picture (Last) not in '0' .. '9' then
251 + Character'Pos (Picture (Last)) -
258 -- In what follows note that one copy of the repeated
259 -- character has already been made, so a count of one is
260 -- no-op, and a count of zero erases a character.
262 for J in 2 .. Count loop
263 Result (Result_Index + J - 2) := Picture (Picture_Index - 1);
266 Result_Index := Result_Index + Count - 1;
268 -- Last was a ')' throw it away too
270 Picture_Index := Last + 1;
276 Result (Result_Index) := Picture (Picture_Index);
277 Picture_Index := Picture_Index + 1;
278 Result_Index := Result_Index + 1;
282 exit when Picture_Index > Picture'Last;
285 return Result (1 .. Result_Index - 1);
296 function Format_Number
297 (Pic : Format_Record;
299 Currency_Symbol : Wide_Wide_String;
300 Fill_Character : Wide_Wide_Character;
301 Separator_Character : Wide_Wide_Character;
302 Radix_Point : Wide_Wide_Character) return Wide_Wide_String
304 Attrs : Number_Attributes := Parse_Number_String (Number);
306 Rounded : String := Number;
308 Sign_Position : Integer := Pic.Sign_Position; -- may float.
310 Answer : Wide_Wide_String (1 .. Pic.Picture.Length);
312 Currency_Pos : Integer := Pic.Start_Currency;
314 Dollar : Boolean := False;
315 -- Overridden immediately if necessary
317 Zero : Boolean := True;
318 -- Set to False when a non-zero digit is output
322 -- If the picture has fewer decimal places than the number, the image
323 -- must be rounded according to the usual rules.
325 if Attrs.Has_Fraction then
327 R : constant Integer :=
328 (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
329 - Pic.Max_Trailing_Digits;
334 R_Pos := Rounded'Length - R;
336 if Rounded (R_Pos + 1) > '4' then
338 if Rounded (R_Pos) = '.' then
342 if Rounded (R_Pos) /= '9' then
343 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
345 Rounded (R_Pos) := '0';
349 if Rounded (R_Pos) = '.' then
353 if Rounded (R_Pos) /= '9' then
354 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
357 Rounded (R_Pos) := '0';
362 -- The rounding may add a digit in front. Either the
363 -- leading blank or the sign (already captured) can be
367 Rounded (R_Pos) := '1';
368 Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
376 for J in Answer'Range loop
377 Answer (J) := To_Wide (Pic.Picture.Expanded (J));
380 if Pic.Start_Currency /= Invalid_Position then
381 Dollar := Answer (Pic.Start_Currency) = '$';
384 -- Fix up "direct inserts" outside the playing field. Set up as one
385 -- loop to do the beginning, one (reverse) loop to do the end.
389 exit when Last = Pic.Start_Float;
390 exit when Last = Pic.Radix_Position;
391 exit when Answer (Last) = '9';
393 case Answer (Last) is
396 Answer (Last) := Separator_Character;
399 Answer (Last) := ' ';
406 exit when Last = Answer'Last;
411 -- Now for the end...
413 for J in reverse Last .. Answer'Last loop
414 exit when J = Pic.Radix_Position;
416 -- Do this test First, Separator_Character can equal Pic.Floater
418 if Answer (J) = Pic.Floater then
425 Answer (J) := Separator_Character;
441 if Pic.Start_Currency /= -1
442 and then Answer (Pic.Start_Currency) = '#'
443 and then Pic.Floater /= '#'
445 if Currency_Symbol'Length >
446 Pic.End_Currency - Pic.Start_Currency + 1
450 elsif Currency_Symbol'Length =
451 Pic.End_Currency - Pic.Start_Currency + 1
453 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
456 elsif Pic.Radix_Position = Invalid_Position
457 or else Pic.Start_Currency < Pic.Radix_Position
459 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
461 Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
462 Pic.End_Currency) := Currency_Symbol;
465 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
467 Answer (Pic.Start_Currency ..
468 Pic.Start_Currency + Currency_Symbol'Length - 1) :=
473 -- Fill in leading digits
475 if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
476 Pic.Max_Leading_Digits
481 if Pic.Radix_Position = Invalid_Position then
482 Position := Answer'Last;
484 Position := Pic.Radix_Position - 1;
487 for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
489 while Answer (Position) /= '9'
490 and Answer (Position) /= Pic.Floater
492 if Answer (Position) = '_' then
493 Answer (Position) := Separator_Character;
495 elsif Answer (Position) = 'b' then
496 Answer (Position) := ' ';
499 Position := Position - 1;
502 Answer (Position) := To_Wide (Rounded (J));
504 if Rounded (J) /= '0' then
508 Position := Position - 1;
513 if Pic.Start_Float = Invalid_Position then
515 -- No leading floats, but need to change '9' to '0', '_' to
516 -- Separator_Character and 'b' to ' '.
518 for J in Last .. Position loop
520 -- Last set when fixing the "uninteresting" leaders above.
521 -- Don't duplicate the work.
523 if Answer (J) = '9' then
526 elsif Answer (J) = '_' then
527 Answer (J) := Separator_Character;
529 elsif Answer (J) = 'b' then
536 elsif Pic.Floater = '<'
542 for J in Pic.End_Float .. Position loop -- May be null range
543 if Answer (J) = '9' then
546 elsif Answer (J) = '_' then
547 Answer (J) := Separator_Character;
549 elsif Answer (J) = 'b' then
555 if Position > Pic.End_Float then
556 Position := Pic.End_Float;
559 for J in Pic.Start_Float .. Position - 1 loop
563 Answer (Position) := Pic.Floater;
564 Sign_Position := Position;
566 elsif Pic.Floater = '$' then
568 for J in Pic.End_Float .. Position loop -- May be null range
569 if Answer (J) = '9' then
572 elsif Answer (J) = '_' then
573 Answer (J) := ' '; -- no separator before leftmost digit
575 elsif Answer (J) = 'b' then
580 if Position > Pic.End_Float then
581 Position := Pic.End_Float;
584 for J in Pic.Start_Float .. Position - 1 loop
588 Answer (Position) := Pic.Floater;
589 Currency_Pos := Position;
591 elsif Pic.Floater = '*' then
593 for J in Pic.End_Float .. Position loop -- May be null range
594 if Answer (J) = '9' then
597 elsif Answer (J) = '_' then
598 Answer (J) := Separator_Character;
600 elsif Answer (J) = 'b' then
605 if Position > Pic.End_Float then
606 Position := Pic.End_Float;
609 for J in Pic.Start_Float .. Position loop
614 if Pic.Floater = '#' then
615 Currency_Pos := Currency_Symbol'Length;
618 for J in reverse Pic.Start_Float .. Position loop
622 Answer (J) := Fill_Character;
624 when 'Z' | 'b' | '/' | '0' =>
630 when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
634 if Currency_Pos = 0 then
637 Answer (J) := Currency_Symbol (Currency_Pos);
638 Currency_Pos := Currency_Pos - 1;
646 Answer (J) := Fill_Character;
652 if Currency_Pos = 0 then
656 Answer (J) := Currency_Symbol (Currency_Pos);
657 Currency_Pos := Currency_Pos - 1;
671 if Pic.Floater = '#' and then Currency_Pos /= 0 then
678 if Sign_Position = Invalid_Position then
679 if Attrs.Negative then
684 if Attrs.Negative then
685 case Answer (Sign_Position) is
686 when 'C' | 'D' | '-' =>
690 Answer (Sign_Position) := '-';
693 Answer (Sign_Position) := '(';
694 Answer (Pic.Second_Sign) := ')';
703 case Answer (Sign_Position) is
706 Answer (Sign_Position) := ' ';
708 when '<' | 'C' | 'D' =>
709 Answer (Sign_Position) := ' ';
710 Answer (Pic.Second_Sign) := ' ';
722 -- Fill in trailing digits
724 if Pic.Max_Trailing_Digits > 0 then
726 if Attrs.Has_Fraction then
727 Position := Attrs.Start_Of_Fraction;
728 Last := Pic.Radix_Position + 1;
730 for J in Last .. Answer'Last loop
732 if Answer (J) = '9' or Answer (J) = Pic.Floater then
733 Answer (J) := To_Wide (Rounded (Position));
735 if Rounded (Position) /= '0' then
739 Position := Position + 1;
742 -- Used up fraction but remember place in Answer
744 exit when Position > Attrs.End_Of_Fraction;
746 elsif Answer (J) = 'b' then
749 elsif Answer (J) = '_' then
750 Answer (J) := Separator_Character;
760 Position := Pic.Radix_Position + 1;
763 -- Now fill remaining 9's with zeros and _ with separators
767 for J in Position .. Last loop
768 if Answer (J) = '9' then
771 elsif Answer (J) = Pic.Floater then
774 elsif Answer (J) = '_' then
775 Answer (J) := Separator_Character;
777 elsif Answer (J) = 'b' then
783 Position := Last + 1;
786 if Pic.Floater = '#' and then Currency_Pos /= 0 then
790 -- No trailing digits, but now J may need to stick in a currency
793 if Pic.Start_Currency = Invalid_Position then
794 Position := Answer'Last + 1;
796 Position := Pic.Start_Currency;
800 for J in Position .. Answer'Last loop
802 if Pic.Start_Currency /= Invalid_Position and then
803 Answer (Pic.Start_Currency) = '#' then
807 -- Note: There are some weird cases J can imagine with 'b' or '#'
808 -- in currency strings where the following code will cause
809 -- glitches. The trick is to tell when the character in the
810 -- answer should be checked, and when to look at the original
811 -- string. Some other time. RIE 11/26/96 ???
815 Answer (J) := Fill_Character;
821 if Currency_Pos > Currency_Symbol'Length then
825 Answer (J) := Currency_Symbol (Currency_Pos);
826 Currency_Pos := Currency_Pos + 1;
834 Answer (J) := Fill_Character;
840 if Currency_Pos > Currency_Symbol'Length then
843 Answer (J) := Currency_Symbol (Currency_Pos);
844 Currency_Pos := Currency_Pos + 1;
858 -- Now get rid of Blank_when_Zero and complete Star fill
860 if Zero and Pic.Blank_When_Zero then
862 -- Value is zero, and blank it
867 Last := Last - 1 + Currency_Symbol'Length;
870 if Pic.Radix_Position /= Invalid_Position and then
871 Answer (Pic.Radix_Position) = 'V' then
875 return Wide_Wide_String'(1 .. Last => ' ');
877 elsif Zero and Pic.Star_Fill then
881 Last := Last - 1 + Currency_Symbol'Length;
884 if Pic.Radix_Position /= Invalid_Position then
886 if Answer (Pic.Radix_Position) = 'V' then
890 if Pic.Radix_Position > Pic.Start_Currency then
892 Wide_Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
894 Wide_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_Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
912 Wide_Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
916 return Wide_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. It will set
1422 -- state to Okay only if a 9 or (second) is encountered.
1424 -- Also notice the tricky bit with State and Zero_Suppression.
1425 -- Zero_Suppression is Picture_Error if a '$' or a '9' has been
1426 -- encountered, exactly the cases where State has been set.
1428 procedure Leading_Dollar is
1430 -- Treat as a floating dollar, and unwind otherwise
1433 Pic.Start_Currency := Index;
1434 Pic.End_Currency := Index;
1435 Pic.Start_Float := Index;
1436 Pic.End_Float := Index;
1438 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1450 when '_' | '0' | '/' =>
1451 Pic.End_Float := Index;
1454 -- A trailing insertion character is not part of the
1455 -- floating currency, so need to look ahead.
1458 Pic.End_Float := Pic.End_Float - 1;
1462 Pic.End_Float := Index;
1463 Pic.Picture.Expanded (Index) := 'b';
1467 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1469 if State = Okay then
1470 raise Picture_Error;
1472 -- Will overwrite Floater and Start_Float
1478 if State = Okay then
1479 raise Picture_Error;
1481 -- Will overwrite Floater and Start_Float
1487 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1488 Pic.End_Float := Index;
1489 Pic.End_Currency := Index;
1490 Set_State (Okay); Skip;
1493 if State /= Okay then
1495 Pic.Start_Float := Invalid_Position;
1496 Pic.End_Float := Invalid_Position;
1499 -- A single dollar does not a floating make
1504 when 'V' | 'v' | '.' =>
1505 if State /= Okay then
1507 Pic.Start_Float := Invalid_Position;
1508 Pic.End_Float := Invalid_Position;
1511 -- Only one dollar before the sign is okay, but doesn't
1514 Pic.Radix_Position := Index;
1516 Number_Fraction_Or_Dollar;
1530 -- This one is complex! A Leading_Pound can be fixed or floating,
1531 -- but in some cases the decision has to be deferred until we leave
1532 -- this procedure. Also note that Leading_Pound can be called in
1535 -- It will set state to Okay only if a 9 or (second) # is encountered
1537 -- One Last note: In ambiguous cases, the currency is treated as
1538 -- floating unless there is only one '#'.
1540 procedure Leading_Pound is
1542 Inserts : Boolean := False;
1543 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1545 Must_Float : Boolean := False;
1546 -- Set to true if a '#' occurs after an insert
1549 -- Treat as a floating currency. If it isn't, this will be
1550 -- overwritten later.
1554 Pic.Start_Currency := Index;
1555 Pic.End_Currency := Index;
1556 Pic.Start_Float := Index;
1557 Pic.End_Float := Index;
1559 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1562 Pic.Max_Currency_Digits := 1; -- we've seen one.
1573 when '_' | '0' | '/' =>
1574 Pic.End_Float := Index;
1579 Pic.Picture.Expanded (Index) := 'b';
1580 Pic.End_Float := Index;
1585 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1588 raise Picture_Error;
1590 Pic.Max_Leading_Digits := 0;
1592 -- Will overwrite Floater and Start_Float
1599 raise Picture_Error;
1601 Pic.Max_Leading_Digits := 0;
1603 -- Will overwrite Floater and Start_Float
1613 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1614 Pic.End_Float := Index;
1615 Pic.End_Currency := Index;
1620 if State /= Okay then
1622 -- A single '#' doesn't float
1625 Pic.Start_Float := Invalid_Position;
1626 Pic.End_Float := Invalid_Position;
1632 when 'V' | 'v' | '.' =>
1633 if State /= Okay then
1635 Pic.Start_Float := Invalid_Position;
1636 Pic.End_Float := Invalid_Position;
1639 -- Only one pound before the sign is okay, but doesn't
1642 Pic.Radix_Position := Index;
1644 Number_Fraction_Or_Pound;
1657 function Look return Character is
1660 raise Picture_Error;
1663 return Pic.Picture.Expanded (Index);
1675 when '_' | '0' | '/' =>
1679 Pic.Picture.Expanded (Index) := 'b';
1683 Computed_BWZ := False;
1684 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1688 when '.' | 'V' | 'v' =>
1689 Pic.Radix_Position := Index;
1703 -- Will return in Okay state if a '9' was seen
1708 -----------------------
1709 -- Number_Completion --
1710 -----------------------
1712 procedure Number_Completion is
1714 while not At_End loop
1717 when '_' | '0' | '/' =>
1721 Pic.Picture.Expanded (Index) := 'b';
1725 Computed_BWZ := False;
1726 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1730 when 'V' | 'v' | '.' =>
1731 Pic.Radix_Position := Index;
1740 end Number_Completion;
1742 ---------------------
1743 -- Number_Fraction --
1744 ---------------------
1746 procedure Number_Fraction is
1748 -- Note that number fraction can be called in either State.
1749 -- It will set state to Valid only if a 9 is encountered.
1757 when '_' | '0' | '/' =>
1761 Pic.Picture.Expanded (Index) := 'b';
1765 Computed_BWZ := False;
1766 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1767 Set_State (Okay); Skip;
1773 end Number_Fraction;
1775 --------------------------------
1776 -- Number_Fraction_Or_Bracket --
1777 --------------------------------
1779 procedure Number_Fraction_Or_Bracket is
1788 when '_' | '0' | '/' => Skip;
1791 Pic.Picture.Expanded (Index) := 'b';
1795 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1796 Pic.End_Float := Index;
1805 when '_' | '0' | '/' =>
1809 Pic.Picture.Expanded (Index) := 'b';
1813 Pic.Max_Trailing_Digits :=
1814 Pic.Max_Trailing_Digits + 1;
1815 Pic.End_Float := Index;
1828 end Number_Fraction_Or_Bracket;
1830 -------------------------------
1831 -- Number_Fraction_Or_Dollar --
1832 -------------------------------
1834 procedure Number_Fraction_Or_Dollar is
1842 when '_' | '0' | '/' =>
1846 Pic.Picture.Expanded (Index) := 'b';
1850 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1851 Pic.End_Float := Index;
1860 when '_' | '0' | '/' =>
1864 Pic.Picture.Expanded (Index) := 'b';
1868 Pic.Max_Trailing_Digits :=
1869 Pic.Max_Trailing_Digits + 1;
1870 Pic.End_Float := Index;
1883 end Number_Fraction_Or_Dollar;
1885 ------------------------------
1886 -- Number_Fraction_Or_Pound --
1887 ------------------------------
1889 procedure Number_Fraction_Or_Pound is
1898 when '_' | '0' | '/' =>
1902 Pic.Picture.Expanded (Index) := 'b';
1906 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1907 Pic.End_Float := Index;
1917 when '_' | '0' | '/' =>
1921 Pic.Picture.Expanded (Index) := 'b';
1925 Pic.Max_Trailing_Digits :=
1926 Pic.Max_Trailing_Digits + 1;
1927 Pic.End_Float := Index;
1942 end Number_Fraction_Or_Pound;
1944 ----------------------------------
1945 -- Number_Fraction_Or_Star_Fill --
1946 ----------------------------------
1948 procedure Number_Fraction_Or_Star_Fill is
1957 when '_' | '0' | '/' =>
1961 Pic.Picture.Expanded (Index) := 'b';
1965 Pic.Star_Fill := True;
1966 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1967 Pic.End_Float := Index;
1977 when '_' | '0' | '/' =>
1981 Pic.Picture.Expanded (Index) := 'b';
1985 Pic.Star_Fill := True;
1986 Pic.Max_Trailing_Digits :=
1987 Pic.Max_Trailing_Digits + 1;
1988 Pic.End_Float := Index;
2002 end Number_Fraction_Or_Star_Fill;
2004 -------------------------------
2005 -- Number_Fraction_Or_Z_Fill --
2006 -------------------------------
2008 procedure Number_Fraction_Or_Z_Fill is
2017 when '_' | '0' | '/' =>
2021 Pic.Picture.Expanded (Index) := 'b';
2025 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
2026 Pic.End_Float := Index;
2027 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2038 when '_' | '0' | '/' =>
2042 Pic.Picture.Expanded (Index) := 'b';
2046 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2048 Pic.Max_Trailing_Digits :=
2049 Pic.Max_Trailing_Digits + 1;
2050 Pic.End_Float := Index;
2063 end Number_Fraction_Or_Z_Fill;
2065 -----------------------
2066 -- Optional_RHS_Sign --
2067 -----------------------
2069 procedure Optional_RHS_Sign is
2078 Pic.Sign_Position := Index;
2083 Pic.Sign_Position := Index;
2084 Pic.Picture.Expanded (Index) := 'C';
2087 if Look = 'R' or Look = 'r' then
2088 Pic.Second_Sign := Index;
2089 Pic.Picture.Expanded (Index) := 'R';
2093 raise Picture_Error;
2099 Pic.Sign_Position := Index;
2100 Pic.Picture.Expanded (Index) := 'D';
2103 if Look = 'B' or Look = 'b' then
2104 Pic.Second_Sign := Index;
2105 Pic.Picture.Expanded (Index) := 'B';
2109 raise Picture_Error;
2115 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2116 Pic.Second_Sign := Index;
2120 raise Picture_Error;
2127 end Optional_RHS_Sign;
2133 -- Note that Picture can be called in either State
2135 -- It will set state to Valid only if a 9 is encountered or floating
2136 -- currency is called.
2138 procedure Picture is
2147 when '_' | '0' | '/' =>
2151 Pic.Picture.Expanded (Index) := 'b';
2163 Computed_BWZ := False;
2165 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2168 when 'V' | 'v' | '.' =>
2169 Pic.Radix_Position := Index;
2182 ---------------------
2183 -- Picture_Bracket --
2184 ---------------------
2186 procedure Picture_Bracket is
2188 Pic.Sign_Position := Index;
2189 Pic.Sign_Position := Index;
2191 -- Treat as a floating sign, and unwind otherwise
2194 Pic.Start_Float := Index;
2195 Pic.End_Float := Index;
2197 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2200 Skip; -- Known Bracket
2205 when '_' | '0' | '/' =>
2206 Pic.End_Float := Index;
2210 Pic.End_Float := Index;
2211 Pic.Picture.Expanded (Index) := 'b';
2215 Set_State (Okay); -- "<<>" is enough.
2221 when '$' | '#' | '9' | '*' =>
2222 if State /= Okay then
2224 Pic.Start_Float := Invalid_Position;
2225 Pic.End_Float := Invalid_Position;
2233 when '.' | 'V' | 'v' =>
2234 if State /= Okay then
2236 Pic.Start_Float := Invalid_Position;
2237 Pic.End_Float := Invalid_Position;
2240 -- Don't assume that state is okay, haven't seen a digit
2247 raise Picture_Error;
2251 end Picture_Bracket;
2257 procedure Picture_Minus is
2259 Pic.Sign_Position := Index;
2261 -- Treat as a floating sign, and unwind otherwise
2264 Pic.Start_Float := Index;
2265 Pic.End_Float := Index;
2267 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2270 Skip; -- Known Minus
2275 when '_' | '0' | '/' =>
2276 Pic.End_Float := Index;
2280 Pic.End_Float := Index;
2281 Pic.Picture.Expanded (Index) := 'b';
2285 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2286 Pic.End_Float := Index;
2288 Set_State (Okay); -- "-- " is enough.
2293 when '$' | '#' | '9' | '*' =>
2294 if State /= Okay then
2296 Pic.Start_Float := Invalid_Position;
2297 Pic.End_Float := Invalid_Position;
2306 -- Can't have Z and a floating sign
2308 if State = Okay then
2312 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2318 when '.' | 'V' | 'v' =>
2319 if State /= Okay then
2321 Pic.Start_Float := Invalid_Position;
2322 Pic.End_Float := Invalid_Position;
2325 -- Don't assume that state is okay, haven't seen a digit
2341 procedure Picture_Plus is
2343 Pic.Sign_Position := Index;
2345 -- Treat as a floating sign, and unwind otherwise
2348 Pic.Start_Float := Index;
2349 Pic.End_Float := Index;
2351 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2359 when '_' | '0' | '/' =>
2360 Pic.End_Float := Index;
2364 Pic.End_Float := Index;
2365 Pic.Picture.Expanded (Index) := 'b';
2369 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2370 Pic.End_Float := Index;
2372 Set_State (Okay); -- "++" is enough
2377 when '$' | '#' | '9' | '*' =>
2378 if State /= Okay then
2380 Pic.Start_Float := Invalid_Position;
2381 Pic.End_Float := Invalid_Position;
2389 if State = Okay then
2393 -- Can't have Z and a floating sign
2395 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2397 -- '+Z' is acceptable
2406 when '.' | 'V' | 'v' =>
2407 if State /= Okay then
2409 Pic.Start_Float := Invalid_Position;
2410 Pic.End_Float := Invalid_Position;
2413 -- Don't assume that state is okay, haven't seen a digit
2425 --------------------
2426 -- Picture_String --
2427 --------------------
2429 procedure Picture_String is
2431 while Is_Insert loop
2451 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2461 when '9' | '.' | 'V' | 'v' =>
2467 raise Picture_Error;
2471 -- Blank when zero either if the PIC does not contain a '9' or if
2472 -- requested by the user and no '*'
2474 Pic.Blank_When_Zero :=
2475 (Computed_BWZ or Pic.Blank_When_Zero) and not Pic.Star_Fill;
2477 -- Star fill if '*' and no '9'
2479 Pic.Star_Fill := Pic.Star_Fill and Computed_BWZ;
2491 procedure Set_State (L : Legality) is
2505 ----------------------
2506 -- Star_Suppression --
2507 ----------------------
2509 procedure Star_Suppression is
2512 Pic.Start_Float := Index;
2513 Pic.End_Float := Index;
2514 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2517 -- Even a single * is a valid picture
2519 Pic.Star_Fill := True;
2529 when '_' | '0' | '/' =>
2530 Pic.End_Float := Index;
2534 Pic.End_Float := Index;
2535 Pic.Picture.Expanded (Index) := 'b';
2539 Pic.End_Float := Index;
2540 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2541 Set_State (Okay); Skip;
2548 when '.' | 'V' | 'v' =>
2549 Pic.Radix_Position := Index;
2551 Number_Fraction_Or_Star_Fill;
2559 when others => raise Picture_Error;
2562 end Star_Suppression;
2564 ----------------------
2565 -- Trailing_Bracket --
2566 ----------------------
2568 procedure Trailing_Bracket is
2571 Pic.Second_Sign := Index;
2574 raise Picture_Error;
2576 end Trailing_Bracket;
2578 -----------------------
2579 -- Trailing_Currency --
2580 -----------------------
2582 procedure Trailing_Currency is
2589 Pic.Start_Currency := Index;
2590 Pic.End_Currency := Index;
2594 while not At_End and then Look = '#' loop
2595 if Pic.Start_Currency = Invalid_Position then
2596 Pic.Start_Currency := Index;
2599 Pic.End_Currency := Index;
2610 when '_' | '0' | '/' => Skip;
2613 Pic.Picture.Expanded (Index) := 'b';
2616 when others => return;
2619 end Trailing_Currency;
2621 ----------------------
2622 -- Zero_Suppression --
2623 ----------------------
2625 procedure Zero_Suppression is
2628 Pic.Start_Float := Index;
2629 Pic.End_Float := Index;
2630 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2631 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2636 -- Even a single Z is a valid picture
2644 when '_' | '0' | '/' =>
2645 Pic.End_Float := Index;
2649 Pic.End_Float := Index;
2650 Pic.Picture.Expanded (Index) := 'b';
2654 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2656 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2657 Pic.End_Float := Index;
2666 when '.' | 'V' | 'v' =>
2667 Pic.Radix_Position := Index;
2669 Number_Fraction_Or_Z_Fill;
2681 end Zero_Suppression;
2683 -- Start of processing for Precalculate
2688 if State = Reject then
2689 raise Picture_Error;
2694 when Constraint_Error =>
2696 -- To deal with special cases like null strings
2698 raise Picture_Error;
2707 (Pic_String : String;
2708 Blank_When_Zero : Boolean := False) return Picture
2714 Item : constant String := Expand (Pic_String);
2717 Result.Contents.Picture := (Item'Length, Item);
2718 Result.Contents.Original_BWZ := Blank_When_Zero;
2719 Result.Contents.Blank_When_Zero := Blank_When_Zero;
2720 Precalculate (Result.Contents);
2726 raise Picture_Error;
2734 function To_Wide (C : Character) return Wide_Wide_Character is
2736 return Wide_Wide_Character'Val (Character'Pos (C));
2744 (Pic_String : String;
2745 Blank_When_Zero : Boolean := False) return Boolean
2749 Expanded_Pic : constant String := Expand (Pic_String);
2750 -- Raises Picture_Error if Item not well-formed
2752 Format_Rec : Format_Record;
2755 Format_Rec.Picture := (Expanded_Pic'Length, Expanded_Pic);
2756 Format_Rec.Blank_When_Zero := Blank_When_Zero;
2757 Format_Rec.Original_BWZ := Blank_When_Zero;
2758 Precalculate (Format_Rec);
2760 -- False only if Blank_When_0 is True but the pic string has a '*'
2762 return not Blank_When_Zero
2763 or else Strings_Fixed.Index (Expanded_Pic, "*") = 0;
2767 when others => return False;
2770 end Ada.Wide_Wide_Text_IO.Editing;