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-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_Fixed;
35 package body Ada.Wide_Text_IO.Editing is
37 package Strings renames Ada.Strings;
38 package Strings_Fixed renames Ada.Strings.Fixed;
39 package Strings_Wide_Fixed renames Ada.Strings.Wide_Fixed;
40 package Wide_Text_IO renames Ada.Wide_Text_IO;
42 -----------------------
43 -- Local_Subprograms --
44 -----------------------
46 function To_Wide (C : Character) return Wide_Character;
47 pragma Inline (To_Wide);
48 -- Convert Character to corresponding 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_String := Default_Currency;
73 Fill : Wide_Character := Default_Fill;
74 Separator : Wide_Character := Default_Separator;
75 Radix_Mark : Wide_Character := Default_Radix_Mark) return Wide_String
79 (Pic.Contents, Num'Image (Item),
80 Currency, Fill, Separator, Radix_Mark);
89 Currency : Wide_String := Default_Currency) return Natural
91 Picstr : constant String := Pic_String (Pic);
92 V_Adjust : Integer := 0;
93 Cur_Adjust : Integer := 0;
96 -- Check if Picstr has 'V' or '$'
98 -- If 'V', then length is 1 less than otherwise
100 -- If '$', then length is Currency'Length-1 more than otherwise
102 -- This should use the string handling package ???
104 for J in Picstr'Range loop
105 if Picstr (J) = 'V' then
108 elsif Picstr (J) = '$' then
109 Cur_Adjust := Currency'Length - 1;
113 return Picstr'Length - V_Adjust + Cur_Adjust;
121 (File : Wide_Text_IO.File_Type;
124 Currency : Wide_String := Default_Currency;
125 Fill : Wide_Character := Default_Fill;
126 Separator : Wide_Character := Default_Separator;
127 Radix_Mark : Wide_Character := Default_Radix_Mark)
130 Wide_Text_IO.Put (File, Image (Item, Pic,
131 Currency, Fill, Separator, Radix_Mark));
137 Currency : Wide_String := Default_Currency;
138 Fill : Wide_Character := Default_Fill;
139 Separator : Wide_Character := Default_Separator;
140 Radix_Mark : Wide_Character := Default_Radix_Mark)
143 Wide_Text_IO.Put (Image (Item, Pic,
144 Currency, Fill, Separator, Radix_Mark));
148 (To : out Wide_String;
151 Currency : Wide_String := Default_Currency;
152 Fill : Wide_Character := Default_Fill;
153 Separator : Wide_Character := Default_Separator;
154 Radix_Mark : Wide_Character := Default_Radix_Mark)
156 Result : constant Wide_String :=
157 Image (Item, Pic, Currency, Fill, Separator, Radix_Mark);
160 if Result'Length > To'Length then
161 raise Wide_Text_IO.Layout_Error;
163 Strings_Wide_Fixed.Move (Source => Result, Target => To,
164 Justify => Strings.Right);
175 Currency : Wide_String := Default_Currency) return Boolean
179 Temp : constant Wide_String := Image (Item, Pic, Currency);
180 pragma Warnings (Off, Temp);
186 when Layout_Error => return False;
195 function Expand (Picture : String) return String is
196 Result : String (1 .. MAX_PICSIZE);
197 Picture_Index : Integer := Picture'First;
198 Result_Index : Integer := Result'First;
203 if Picture'Length < 1 then
207 if Picture (Picture'First) = '(' then
212 case Picture (Picture_Index) is
216 -- We now need to scan out the count after a left paren. In
217 -- the non-wide version we used Integer_IO.Get, but that is
218 -- not convenient here, since we don't want to drag in normal
219 -- Text_IO just for this purpose. So we do the scan ourselves,
220 -- with the normal validity checks.
222 Last := Picture_Index + 1;
225 if Picture (Last) not in '0' .. '9' then
229 Count := Character'Pos (Picture (Last)) - Character'Pos ('0');
233 if Last > Picture'Last then
237 if Picture (Last) = '_' then
238 if Picture (Last - 1) = '_' then
242 elsif Picture (Last) = ')' then
245 elsif Picture (Last) not in '0' .. '9' then
250 + Character'Pos (Picture (Last)) -
257 -- In what follows note that one copy of the repeated
258 -- character has already been made, so a count of one is
259 -- no-op, and a count of zero erases a character.
261 for J in 2 .. Count loop
262 Result (Result_Index + J - 2) := Picture (Picture_Index - 1);
265 Result_Index := Result_Index + Count - 1;
267 -- Last was a ')' throw it away too
269 Picture_Index := Last + 1;
275 Result (Result_Index) := Picture (Picture_Index);
276 Picture_Index := Picture_Index + 1;
277 Result_Index := Result_Index + 1;
281 exit when Picture_Index > Picture'Last;
284 return Result (1 .. Result_Index - 1);
295 function Format_Number
296 (Pic : Format_Record;
298 Currency_Symbol : Wide_String;
299 Fill_Character : Wide_Character;
300 Separator_Character : Wide_Character;
301 Radix_Point : Wide_Character) return Wide_String
303 Attrs : Number_Attributes := Parse_Number_String (Number);
305 Rounded : String := Number;
307 Sign_Position : Integer := Pic.Sign_Position; -- may float.
309 Answer : Wide_String (1 .. Pic.Picture.Length);
311 Currency_Pos : Integer := Pic.Start_Currency;
313 Dollar : Boolean := False;
314 -- Overridden immediately if necessary
316 Zero : Boolean := True;
317 -- Set to False when a non-zero digit is output
321 -- If the picture has fewer decimal places than the number, the image
322 -- must be rounded according to the usual rules.
324 if Attrs.Has_Fraction then
326 R : constant Integer :=
327 (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
328 - Pic.Max_Trailing_Digits;
333 R_Pos := Rounded'Length - R;
335 if Rounded (R_Pos + 1) > '4' then
337 if Rounded (R_Pos) = '.' then
341 if Rounded (R_Pos) /= '9' then
342 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
344 Rounded (R_Pos) := '0';
348 if Rounded (R_Pos) = '.' then
352 if Rounded (R_Pos) /= '9' then
353 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
356 Rounded (R_Pos) := '0';
361 -- The rounding may add a digit in front. Either the
362 -- leading blank or the sign (already captured) can be
366 Rounded (R_Pos) := '1';
367 Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
375 for J in Answer'Range loop
376 Answer (J) := To_Wide (Pic.Picture.Expanded (J));
379 if Pic.Start_Currency /= Invalid_Position then
380 Dollar := Answer (Pic.Start_Currency) = '$';
383 -- Fix up "direct inserts" outside the playing field. Set up as one
384 -- loop to do the beginning, one (reverse) loop to do the end.
388 exit when Last = Pic.Start_Float;
389 exit when Last = Pic.Radix_Position;
390 exit when Answer (Last) = '9';
392 case Answer (Last) is
395 Answer (Last) := Separator_Character;
398 Answer (Last) := ' ';
405 exit when Last = Answer'Last;
410 -- Now for the end...
412 for J in reverse Last .. Answer'Last loop
413 exit when J = Pic.Radix_Position;
415 -- Do this test First, Separator_Character can equal Pic.Floater
417 if Answer (J) = Pic.Floater then
424 Answer (J) := Separator_Character;
440 if Pic.Start_Currency /= -1
441 and then Answer (Pic.Start_Currency) = '#'
442 and then Pic.Floater /= '#'
444 if Currency_Symbol'Length >
445 Pic.End_Currency - Pic.Start_Currency + 1
449 elsif Currency_Symbol'Length =
450 Pic.End_Currency - Pic.Start_Currency + 1
452 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
455 elsif Pic.Radix_Position = Invalid_Position
456 or else Pic.Start_Currency < Pic.Radix_Position
458 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
460 Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
461 Pic.End_Currency) := Currency_Symbol;
464 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
466 Answer (Pic.Start_Currency ..
467 Pic.Start_Currency + Currency_Symbol'Length - 1) :=
472 -- Fill in leading digits
474 if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
475 Pic.Max_Leading_Digits
481 (if Pic.Radix_Position = Invalid_Position then Answer'Last
482 else Pic.Radix_Position - 1);
484 for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
485 while Answer (Position) /= '9'
487 Answer (Position) /= Pic.Floater
489 if Answer (Position) = '_' then
490 Answer (Position) := Separator_Character;
491 elsif Answer (Position) = 'b' then
492 Answer (Position) := ' ';
495 Position := Position - 1;
498 Answer (Position) := To_Wide (Rounded (J));
500 if Rounded (J) /= '0' then
504 Position := Position - 1;
509 if Pic.Start_Float = Invalid_Position then
511 -- No leading floats, but need to change '9' to '0', '_' to
512 -- Separator_Character and 'b' to ' '.
514 for J in Last .. Position loop
516 -- Last set when fixing the "uninteresting" leaders above.
517 -- Don't duplicate the work.
519 if Answer (J) = '9' then
522 elsif Answer (J) = '_' then
523 Answer (J) := Separator_Character;
525 elsif Answer (J) = 'b' then
532 elsif Pic.Floater = '<'
538 for J in Pic.End_Float .. Position loop -- May be null range
539 if Answer (J) = '9' then
542 elsif Answer (J) = '_' then
543 Answer (J) := Separator_Character;
545 elsif Answer (J) = 'b' then
551 if Position > Pic.End_Float then
552 Position := Pic.End_Float;
555 for J in Pic.Start_Float .. Position - 1 loop
559 Answer (Position) := Pic.Floater;
560 Sign_Position := Position;
562 elsif Pic.Floater = '$' then
564 for J in Pic.End_Float .. Position loop -- May be null range
565 if Answer (J) = '9' then
568 elsif Answer (J) = '_' then
569 Answer (J) := ' '; -- no separator before leftmost digit
571 elsif Answer (J) = 'b' then
576 if Position > Pic.End_Float then
577 Position := Pic.End_Float;
580 for J in Pic.Start_Float .. Position - 1 loop
584 Answer (Position) := Pic.Floater;
585 Currency_Pos := Position;
587 elsif Pic.Floater = '*' then
589 for J in Pic.End_Float .. Position loop -- May be null range
590 if Answer (J) = '9' then
593 elsif Answer (J) = '_' then
594 Answer (J) := Separator_Character;
596 elsif Answer (J) = 'b' then
601 if Position > Pic.End_Float then
602 Position := Pic.End_Float;
605 for J in Pic.Start_Float .. Position loop
610 if Pic.Floater = '#' then
611 Currency_Pos := Currency_Symbol'Length;
614 for J in reverse Pic.Start_Float .. Position loop
618 Answer (J) := Fill_Character;
620 when 'Z' | 'b' | '/' | '0' =>
626 when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
630 if Currency_Pos = 0 then
633 Answer (J) := Currency_Symbol (Currency_Pos);
634 Currency_Pos := Currency_Pos - 1;
642 Answer (J) := Fill_Character;
648 if Currency_Pos = 0 then
652 Answer (J) := Currency_Symbol (Currency_Pos);
653 Currency_Pos := Currency_Pos - 1;
667 if Pic.Floater = '#' and then Currency_Pos /= 0 then
674 if Sign_Position = Invalid_Position then
675 if Attrs.Negative then
680 if Attrs.Negative then
681 case Answer (Sign_Position) is
682 when 'C' | 'D' | '-' =>
686 Answer (Sign_Position) := '-';
689 Answer (Sign_Position) := '(';
690 Answer (Pic.Second_Sign) := ')';
699 case Answer (Sign_Position) is
702 Answer (Sign_Position) := ' ';
704 when '<' | 'C' | 'D' =>
705 Answer (Sign_Position) := ' ';
706 Answer (Pic.Second_Sign) := ' ';
718 -- Fill in trailing digits
720 if Pic.Max_Trailing_Digits > 0 then
722 if Attrs.Has_Fraction then
723 Position := Attrs.Start_Of_Fraction;
724 Last := Pic.Radix_Position + 1;
726 for J in Last .. Answer'Last loop
728 if Answer (J) = '9' or else Answer (J) = Pic.Floater then
729 Answer (J) := To_Wide (Rounded (Position));
731 if Rounded (Position) /= '0' then
735 Position := Position + 1;
738 -- Used up fraction but remember place in Answer
740 exit when Position > Attrs.End_Of_Fraction;
742 elsif Answer (J) = 'b' then
745 elsif Answer (J) = '_' then
746 Answer (J) := Separator_Character;
756 Position := Pic.Radix_Position + 1;
759 -- Now fill remaining 9's with zeros and _ with separators
763 for J in Position .. Last loop
764 if Answer (J) = '9' then
767 elsif Answer (J) = Pic.Floater then
770 elsif Answer (J) = '_' then
771 Answer (J) := Separator_Character;
773 elsif Answer (J) = 'b' then
779 Position := Last + 1;
782 if Pic.Floater = '#' and then Currency_Pos /= 0 then
786 -- No trailing digits, but now J may need to stick in a currency
790 (if Pic.Start_Currency = Invalid_Position then Answer'Last + 1
791 else Pic.Start_Currency);
794 for J in Position .. Answer'Last loop
795 if Pic.Start_Currency /= Invalid_Position and then
796 Answer (Pic.Start_Currency) = '#' then
800 -- Note: There are some weird cases J can imagine with 'b' or '#' in
801 -- currency strings where the following code will cause glitches. The
802 -- trick is to tell when the character in the answer should be
803 -- checked, and when to look at the original string. Some other time.
808 Answer (J) := Fill_Character;
814 if Currency_Pos > Currency_Symbol'Length then
818 Answer (J) := Currency_Symbol (Currency_Pos);
819 Currency_Pos := Currency_Pos + 1;
827 Answer (J) := Fill_Character;
833 if Currency_Pos > Currency_Symbol'Length then
836 Answer (J) := Currency_Symbol (Currency_Pos);
837 Currency_Pos := Currency_Pos + 1;
851 -- Now get rid of Blank_when_Zero and complete Star fill
853 if Zero and then Pic.Blank_When_Zero then
855 -- Value is zero, and blank it
860 Last := Last - 1 + Currency_Symbol'Length;
863 if Pic.Radix_Position /= Invalid_Position and then
864 Answer (Pic.Radix_Position) = 'V' then
868 return Wide_String'(1 .. Last => ' ');
870 elsif Zero and then Pic.Star_Fill then
874 Last := Last - 1 + Currency_Symbol'Length;
877 if Pic.Radix_Position /= Invalid_Position then
879 if Answer (Pic.Radix_Position) = 'V' then
883 if Pic.Radix_Position > Pic.Start_Currency then
884 return Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
886 Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
892 Pic.Radix_Position + Currency_Symbol'Length - 2
896 (Pic.Radix_Position + Currency_Symbol'Length .. Last
902 Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
904 Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
908 return Wide_String'(1 .. Last => '*');
911 -- This was once a simple return statement, now there are nine
912 -- different return cases. Not to mention the five above to deal
913 -- with zeros. Why not split things out?
915 -- Processing the radix and sign expansion separately
916 -- would require lots of copying--the string and some of its
917 -- indicies--without really simplifying the logic. The cases are:
919 -- 1) Expand $, replace '.' with Radix_Point
920 -- 2) No currency expansion, replace '.' with Radix_Point
921 -- 3) Expand $, radix blanked
922 -- 4) No currency expansion, radix blanked
924 -- 6) Expand $, Elide V
925 -- 7) Elide V, Expand $ (Two cases depending on order.)
926 -- 8) No radix, expand $
927 -- 9) No radix, no currency expansion
929 if Pic.Radix_Position /= Invalid_Position then
931 if Answer (Pic.Radix_Position) = '.' then
932 Answer (Pic.Radix_Position) := Radix_Point;
936 -- 1) Expand $, replace '.' with Radix_Point
939 Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
940 Answer (Currency_Pos + 1 .. Answer'Last);
943 -- 2) No currency expansion, replace '.' with Radix_Point
948 elsif Answer (Pic.Radix_Position) = ' ' then -- blanked radix.
951 -- 3) Expand $, radix blanked
953 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
954 Answer (Currency_Pos + 1 .. Answer'Last);
957 -- 4) No expansion, radix blanked
969 return Answer (1 .. Pic.Radix_Position - 1) &
970 Answer (Pic.Radix_Position + 1 .. Answer'Last);
972 elsif Currency_Pos < Pic.Radix_Position then
974 -- 6) Expand $, Elide V
976 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
977 Answer (Currency_Pos + 1 .. Pic.Radix_Position - 1) &
978 Answer (Pic.Radix_Position + 1 .. Answer'Last);
981 -- 7) Elide V, Expand $
983 return Answer (1 .. Pic.Radix_Position - 1) &
984 Answer (Pic.Radix_Position + 1 .. Currency_Pos - 1) &
986 Answer (Currency_Pos + 1 .. Answer'Last);
992 -- 8) No radix, expand $
994 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
995 Answer (Currency_Pos + 1 .. Answer'Last);
998 -- 9) No radix, no currency expansion
1004 -------------------------
1005 -- Parse_Number_String --
1006 -------------------------
1008 function Parse_Number_String (Str : String) return Number_Attributes is
1009 Answer : Number_Attributes;
1012 for J in Str'Range loop
1020 -- Decide if this is the start of a number.
1021 -- If so, figure out which one...
1023 if Answer.Has_Fraction then
1024 Answer.End_Of_Fraction := J;
1026 if Answer.Start_Of_Int = Invalid_Position then
1028 Answer.Start_Of_Int := J;
1030 Answer.End_Of_Int := J;
1035 -- Only count a zero before the decimal point if it follows a
1036 -- non-zero digit. After the decimal point, zeros will be
1037 -- counted if followed by a non-zero digit.
1039 if not Answer.Has_Fraction then
1040 if Answer.Start_Of_Int /= Invalid_Position then
1041 Answer.End_Of_Int := J;
1049 Answer.Negative := True;
1053 -- Close integer, start fraction
1055 if Answer.Has_Fraction then
1056 raise Picture_Error;
1059 -- Two decimal points is a no-no
1061 Answer.Has_Fraction := True;
1062 Answer.End_Of_Fraction := J;
1064 -- Could leave this at Invalid_Position, but this seems the
1065 -- right way to indicate a null range...
1067 Answer.Start_Of_Fraction := J + 1;
1068 Answer.End_Of_Int := J - 1;
1071 raise Picture_Error; -- can this happen? probably not!
1075 if Answer.Start_Of_Int = Invalid_Position then
1076 Answer.Start_Of_Int := Answer.End_Of_Int + 1;
1079 -- No significant (intger) digits needs a null range
1082 end Parse_Number_String;
1088 -- The following ensures that we return B and not b being careful not
1089 -- to break things which expect lower case b for blank. See CXF3A02.
1091 function Pic_String (Pic : Picture) return String is
1092 Temp : String (1 .. Pic.Contents.Picture.Length) :=
1093 Pic.Contents.Picture.Expanded;
1095 for J in Temp'Range loop
1096 if Temp (J) = 'b' then
1108 procedure Precalculate (Pic : in out Format_Record) is
1110 Computed_BWZ : Boolean := True;
1112 type Legality is (Okay, Reject);
1113 State : Legality := Reject;
1114 -- Start in reject, which will reject null strings
1116 Index : Pic_Index := Pic.Picture.Expanded'First;
1118 function At_End return Boolean;
1119 pragma Inline (At_End);
1121 procedure Set_State (L : Legality);
1122 pragma Inline (Set_State);
1124 function Look return Character;
1125 pragma Inline (Look);
1127 function Is_Insert return Boolean;
1128 pragma Inline (Is_Insert);
1131 pragma Inline (Skip);
1133 procedure Trailing_Currency;
1134 procedure Trailing_Bracket;
1135 procedure Number_Fraction;
1136 procedure Number_Completion;
1137 procedure Number_Fraction_Or_Bracket;
1138 procedure Number_Fraction_Or_Z_Fill;
1139 procedure Zero_Suppression;
1140 procedure Floating_Bracket;
1141 procedure Number_Fraction_Or_Star_Fill;
1142 procedure Star_Suppression;
1143 procedure Number_Fraction_Or_Dollar;
1144 procedure Leading_Dollar;
1145 procedure Number_Fraction_Or_Pound;
1146 procedure Leading_Pound;
1148 procedure Floating_Plus;
1149 procedure Floating_Minus;
1150 procedure Picture_Plus;
1151 procedure Picture_Minus;
1152 procedure Picture_Bracket;
1154 procedure Optional_RHS_Sign;
1155 procedure Picture_String;
1161 function At_End return Boolean is
1163 return Index > Pic.Picture.Length;
1166 ----------------------
1167 -- Floating_Bracket --
1168 ----------------------
1170 -- Note that Floating_Bracket is only called with an acceptable
1171 -- prefix. But we don't set Okay, because we must end with a '>'.
1173 procedure Floating_Bracket is
1176 Pic.End_Float := Index;
1177 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1179 -- First bracket wasn't counted...
1190 when '_' | '0' | '/' =>
1191 Pic.End_Float := Index;
1195 Pic.End_Float := Index;
1196 Pic.Picture.Expanded (Index) := 'b';
1200 Pic.End_Float := Index;
1201 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1213 when 'V' | 'v' | '.' =>
1214 Pic.Radix_Position := Index;
1216 Number_Fraction_Or_Bracket;
1223 end Floating_Bracket;
1225 --------------------
1226 -- Floating_Minus --
1227 --------------------
1229 procedure Floating_Minus is
1237 when '_' | '0' | '/' =>
1238 Pic.End_Float := Index;
1242 Pic.End_Float := Index;
1243 Pic.Picture.Expanded (Index) := 'b';
1247 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1248 Pic.End_Float := Index;
1255 when '.' | 'V' | 'v' =>
1256 Pic.Radix_Position := Index;
1259 while Is_Insert loop
1276 Pic.Max_Trailing_Digits :=
1277 Pic.Max_Trailing_Digits + 1;
1278 Pic.End_Float := Index;
1281 when '_' | '0' | '/' =>
1285 Pic.Picture.Expanded (Index) := 'b';
1310 procedure Floating_Plus is
1318 when '_' | '0' | '/' =>
1319 Pic.End_Float := Index;
1323 Pic.End_Float := Index;
1324 Pic.Picture.Expanded (Index) := 'b';
1328 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1329 Pic.End_Float := Index;
1336 when '.' | 'V' | 'v' =>
1337 Pic.Radix_Position := Index;
1340 while Is_Insert loop
1357 Pic.Max_Trailing_Digits :=
1358 Pic.Max_Trailing_Digits + 1;
1359 Pic.End_Float := Index;
1362 when '_' | '0' | '/' =>
1366 Pic.Picture.Expanded (Index) := 'b';
1392 function Is_Insert return Boolean is
1398 case Pic.Picture.Expanded (Index) is
1400 when '_' | '0' | '/' => return True;
1403 Pic.Picture.Expanded (Index) := 'b'; -- canonical
1406 when others => return False;
1410 --------------------
1411 -- Leading_Dollar --
1412 --------------------
1414 -- Note that Leading_Dollar can be called in either State.
1415 -- It will set state to Okay only if a 9 or (second) $
1418 -- Also notice the tricky bit with State and Zero_Suppression.
1419 -- Zero_Suppression is Picture_Error if a '$' or a '9' has been
1420 -- encountered, exactly the cases where State has been set.
1422 procedure Leading_Dollar is
1424 -- Treat as a floating dollar, and unwind otherwise
1427 Pic.Start_Currency := Index;
1428 Pic.End_Currency := Index;
1429 Pic.Start_Float := Index;
1430 Pic.End_Float := Index;
1432 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1444 when '_' | '0' | '/' =>
1445 Pic.End_Float := Index;
1448 -- A trailing insertion character is not part of the
1449 -- floating currency, so need to look ahead.
1452 Pic.End_Float := Pic.End_Float - 1;
1456 Pic.End_Float := Index;
1457 Pic.Picture.Expanded (Index) := 'b';
1461 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1463 if State = Okay then
1464 raise Picture_Error;
1466 -- Will overwrite Floater and Start_Float
1472 if State = Okay then
1473 raise Picture_Error;
1475 -- Will overwrite Floater and Start_Float
1481 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1482 Pic.End_Float := Index;
1483 Pic.End_Currency := Index;
1484 Set_State (Okay); Skip;
1487 if State /= Okay then
1489 Pic.Start_Float := Invalid_Position;
1490 Pic.End_Float := Invalid_Position;
1493 -- A single dollar does not a floating make
1498 when 'V' | 'v' | '.' =>
1499 if State /= Okay then
1501 Pic.Start_Float := Invalid_Position;
1502 Pic.End_Float := Invalid_Position;
1505 -- Only one dollar before the sign is okay, but doesn't
1508 Pic.Radix_Position := Index;
1510 Number_Fraction_Or_Dollar;
1524 -- This one is complex! A Leading_Pound can be fixed or floating,
1525 -- but in some cases the decision has to be deferred until we leave
1526 -- this procedure. Also note that Leading_Pound can be called in
1529 -- It will set state to Okay only if a 9 or (second) # is
1532 -- One Last note: In ambiguous cases, the currency is treated as
1533 -- floating unless there is only one '#'.
1535 procedure Leading_Pound is
1537 Inserts : Boolean := False;
1538 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1540 Must_Float : Boolean := False;
1541 -- Set to true if a '#' occurs after an insert
1544 -- Treat as a floating currency. If it isn't, this will be
1545 -- overwritten later.
1549 Pic.Start_Currency := Index;
1550 Pic.End_Currency := Index;
1551 Pic.Start_Float := Index;
1552 Pic.End_Float := Index;
1554 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1557 Pic.Max_Currency_Digits := 1; -- we've seen one.
1568 when '_' | '0' | '/' =>
1569 Pic.End_Float := Index;
1574 Pic.Picture.Expanded (Index) := 'b';
1575 Pic.End_Float := Index;
1580 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1583 raise Picture_Error;
1585 Pic.Max_Leading_Digits := 0;
1587 -- Will overwrite Floater and Start_Float
1594 raise Picture_Error;
1596 Pic.Max_Leading_Digits := 0;
1598 -- Will overwrite Floater and Start_Float
1608 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1609 Pic.End_Float := Index;
1610 Pic.End_Currency := Index;
1615 if State /= Okay then
1617 -- A single '#' doesn't float
1620 Pic.Start_Float := Invalid_Position;
1621 Pic.End_Float := Invalid_Position;
1627 when 'V' | 'v' | '.' =>
1628 if State /= Okay then
1630 Pic.Start_Float := Invalid_Position;
1631 Pic.End_Float := Invalid_Position;
1634 -- Only one pound before the sign is okay, but doesn't
1637 Pic.Radix_Position := Index;
1639 Number_Fraction_Or_Pound;
1652 function Look return Character is
1655 raise Picture_Error;
1658 return Pic.Picture.Expanded (Index);
1670 when '_' | '0' | '/' =>
1674 Pic.Picture.Expanded (Index) := 'b';
1678 Computed_BWZ := False;
1679 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1683 when '.' | 'V' | 'v' =>
1684 Pic.Radix_Position := Index;
1698 -- Will return in Okay state if a '9' was seen
1703 -----------------------
1704 -- Number_Completion --
1705 -----------------------
1707 procedure Number_Completion is
1709 while not At_End loop
1712 when '_' | '0' | '/' =>
1716 Pic.Picture.Expanded (Index) := 'b';
1720 Computed_BWZ := False;
1721 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1725 when 'V' | 'v' | '.' =>
1726 Pic.Radix_Position := Index;
1735 end Number_Completion;
1737 ---------------------
1738 -- Number_Fraction --
1739 ---------------------
1741 procedure Number_Fraction is
1743 -- Note that number fraction can be called in either State.
1744 -- It will set state to Valid only if a 9 is encountered.
1752 when '_' | '0' | '/' =>
1756 Pic.Picture.Expanded (Index) := 'b';
1760 Computed_BWZ := False;
1761 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1762 Set_State (Okay); Skip;
1768 end Number_Fraction;
1770 --------------------------------
1771 -- Number_Fraction_Or_Bracket --
1772 --------------------------------
1774 procedure Number_Fraction_Or_Bracket is
1783 when '_' | '0' | '/' => Skip;
1786 Pic.Picture.Expanded (Index) := 'b';
1790 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1791 Pic.End_Float := Index;
1800 when '_' | '0' | '/' =>
1804 Pic.Picture.Expanded (Index) := 'b';
1808 Pic.Max_Trailing_Digits :=
1809 Pic.Max_Trailing_Digits + 1;
1810 Pic.End_Float := Index;
1823 end Number_Fraction_Or_Bracket;
1825 -------------------------------
1826 -- Number_Fraction_Or_Dollar --
1827 -------------------------------
1829 procedure Number_Fraction_Or_Dollar 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;
1855 when '_' | '0' | '/' =>
1859 Pic.Picture.Expanded (Index) := 'b';
1863 Pic.Max_Trailing_Digits :=
1864 Pic.Max_Trailing_Digits + 1;
1865 Pic.End_Float := Index;
1878 end Number_Fraction_Or_Dollar;
1880 ------------------------------
1881 -- Number_Fraction_Or_Pound --
1882 ------------------------------
1884 procedure Number_Fraction_Or_Pound is
1893 when '_' | '0' | '/' =>
1897 Pic.Picture.Expanded (Index) := 'b';
1901 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1902 Pic.End_Float := Index;
1912 when '_' | '0' | '/' =>
1916 Pic.Picture.Expanded (Index) := 'b';
1920 Pic.Max_Trailing_Digits :=
1921 Pic.Max_Trailing_Digits + 1;
1922 Pic.End_Float := Index;
1937 end Number_Fraction_Or_Pound;
1939 ----------------------------------
1940 -- Number_Fraction_Or_Star_Fill --
1941 ----------------------------------
1943 procedure Number_Fraction_Or_Star_Fill is
1952 when '_' | '0' | '/' =>
1956 Pic.Picture.Expanded (Index) := 'b';
1960 Pic.Star_Fill := True;
1961 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1962 Pic.End_Float := Index;
1972 when '_' | '0' | '/' =>
1976 Pic.Picture.Expanded (Index) := 'b';
1980 Pic.Star_Fill := True;
1981 Pic.Max_Trailing_Digits :=
1982 Pic.Max_Trailing_Digits + 1;
1983 Pic.End_Float := Index;
1997 end Number_Fraction_Or_Star_Fill;
1999 -------------------------------
2000 -- Number_Fraction_Or_Z_Fill --
2001 -------------------------------
2003 procedure Number_Fraction_Or_Z_Fill is
2012 when '_' | '0' | '/' =>
2016 Pic.Picture.Expanded (Index) := 'b';
2020 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
2021 Pic.End_Float := Index;
2022 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2033 when '_' | '0' | '/' =>
2037 Pic.Picture.Expanded (Index) := 'b';
2041 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2043 Pic.Max_Trailing_Digits :=
2044 Pic.Max_Trailing_Digits + 1;
2045 Pic.End_Float := Index;
2058 end Number_Fraction_Or_Z_Fill;
2060 -----------------------
2061 -- Optional_RHS_Sign --
2062 -----------------------
2064 procedure Optional_RHS_Sign is
2073 Pic.Sign_Position := Index;
2078 Pic.Sign_Position := Index;
2079 Pic.Picture.Expanded (Index) := 'C';
2082 if Look = 'R' or else Look = 'r' then
2083 Pic.Second_Sign := Index;
2084 Pic.Picture.Expanded (Index) := 'R';
2088 raise Picture_Error;
2094 Pic.Sign_Position := Index;
2095 Pic.Picture.Expanded (Index) := 'D';
2098 if Look = 'B' or else Look = 'b' then
2099 Pic.Second_Sign := Index;
2100 Pic.Picture.Expanded (Index) := 'B';
2104 raise Picture_Error;
2110 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2111 Pic.Second_Sign := Index;
2115 raise Picture_Error;
2122 end Optional_RHS_Sign;
2128 -- Note that Picture can be called in either State
2130 -- It will set state to Valid only if a 9 is encountered or floating
2131 -- currency is called.
2133 procedure Picture is
2142 when '_' | '0' | '/' =>
2146 Pic.Picture.Expanded (Index) := 'b';
2158 Computed_BWZ := False;
2160 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2163 when 'V' | 'v' | '.' =>
2164 Pic.Radix_Position := Index;
2177 ---------------------
2178 -- Picture_Bracket --
2179 ---------------------
2181 procedure Picture_Bracket is
2183 Pic.Sign_Position := Index;
2184 Pic.Sign_Position := Index;
2186 -- Treat as a floating sign, and unwind otherwise
2189 Pic.Start_Float := Index;
2190 Pic.End_Float := Index;
2192 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2195 Skip; -- Known Bracket
2200 when '_' | '0' | '/' =>
2201 Pic.End_Float := Index;
2205 Pic.End_Float := Index;
2206 Pic.Picture.Expanded (Index) := 'b';
2210 Set_State (Okay); -- "<<>" is enough.
2216 when '$' | '#' | '9' | '*' =>
2217 if State /= Okay then
2219 Pic.Start_Float := Invalid_Position;
2220 Pic.End_Float := Invalid_Position;
2228 when '.' | 'V' | 'v' =>
2229 if State /= Okay then
2231 Pic.Start_Float := Invalid_Position;
2232 Pic.End_Float := Invalid_Position;
2235 -- Don't assume that state is okay, haven't seen a digit
2242 raise Picture_Error;
2246 end Picture_Bracket;
2252 procedure Picture_Minus is
2254 Pic.Sign_Position := Index;
2256 -- Treat as a floating sign, and unwind otherwise
2259 Pic.Start_Float := Index;
2260 Pic.End_Float := Index;
2262 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2265 Skip; -- Known Minus
2270 when '_' | '0' | '/' =>
2271 Pic.End_Float := Index;
2275 Pic.End_Float := Index;
2276 Pic.Picture.Expanded (Index) := 'b';
2280 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2281 Pic.End_Float := Index;
2283 Set_State (Okay); -- "-- " is enough
2288 when '$' | '#' | '9' | '*' =>
2289 if State /= Okay then
2291 Pic.Start_Float := Invalid_Position;
2292 Pic.End_Float := Invalid_Position;
2301 -- Can't have Z and a floating sign
2303 if State = Okay then
2307 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2313 when '.' | 'V' | 'v' =>
2314 if State /= Okay then
2316 Pic.Start_Float := Invalid_Position;
2317 Pic.End_Float := Invalid_Position;
2320 -- Don't assume that state is okay, haven't seen a digit
2336 procedure Picture_Plus is
2338 Pic.Sign_Position := Index;
2340 -- Treat as a floating sign, and unwind otherwise
2343 Pic.Start_Float := Index;
2344 Pic.End_Float := Index;
2346 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2354 when '_' | '0' | '/' =>
2355 Pic.End_Float := Index;
2359 Pic.End_Float := Index;
2360 Pic.Picture.Expanded (Index) := 'b';
2364 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2365 Pic.End_Float := Index;
2367 Set_State (Okay); -- "++" is enough
2372 when '$' | '#' | '9' | '*' =>
2373 if State /= Okay then
2375 Pic.Start_Float := Invalid_Position;
2376 Pic.End_Float := Invalid_Position;
2384 if State = Okay then
2388 -- Can't have Z and a floating sign
2390 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2392 -- '+Z' is acceptable
2401 when '.' | 'V' | 'v' =>
2402 if State /= Okay then
2404 Pic.Start_Float := Invalid_Position;
2405 Pic.End_Float := Invalid_Position;
2408 -- Don't assume that state is okay, haven't seen a digit
2420 --------------------
2421 -- Picture_String --
2422 --------------------
2424 procedure Picture_String is
2426 while Is_Insert loop
2446 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2456 when '9' | '.' | 'V' | 'v' =>
2462 raise Picture_Error;
2466 -- Blank when zero either if the PIC does not contain a '9' or if
2467 -- requested by the user and no '*'.
2469 Pic.Blank_When_Zero :=
2470 (Computed_BWZ or else Pic.Blank_When_Zero)
2471 and then not Pic.Star_Fill;
2473 -- Star fill if '*' and no '9'
2475 Pic.Star_Fill := Pic.Star_Fill and then Computed_BWZ;
2487 procedure Set_State (L : Legality) is
2501 ----------------------
2502 -- Star_Suppression --
2503 ----------------------
2505 procedure Star_Suppression is
2508 Pic.Start_Float := Index;
2509 Pic.End_Float := Index;
2510 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2513 -- Even a single * is a valid picture
2515 Pic.Star_Fill := True;
2525 when '_' | '0' | '/' =>
2526 Pic.End_Float := Index;
2530 Pic.End_Float := Index;
2531 Pic.Picture.Expanded (Index) := 'b';
2535 Pic.End_Float := Index;
2536 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2537 Set_State (Okay); Skip;
2544 when '.' | 'V' | 'v' =>
2545 Pic.Radix_Position := Index;
2547 Number_Fraction_Or_Star_Fill;
2555 when others => raise Picture_Error;
2558 end Star_Suppression;
2560 ----------------------
2561 -- Trailing_Bracket --
2562 ----------------------
2564 procedure Trailing_Bracket is
2567 Pic.Second_Sign := Index;
2570 raise Picture_Error;
2572 end Trailing_Bracket;
2574 -----------------------
2575 -- Trailing_Currency --
2576 -----------------------
2578 procedure Trailing_Currency is
2585 Pic.Start_Currency := Index;
2586 Pic.End_Currency := Index;
2590 while not At_End and then Look = '#' loop
2591 if Pic.Start_Currency = Invalid_Position then
2592 Pic.Start_Currency := Index;
2595 Pic.End_Currency := Index;
2606 when '_' | '0' | '/' => Skip;
2609 Pic.Picture.Expanded (Index) := 'b';
2612 when others => return;
2615 end Trailing_Currency;
2617 ----------------------
2618 -- Zero_Suppression --
2619 ----------------------
2621 procedure Zero_Suppression is
2624 Pic.Start_Float := Index;
2625 Pic.End_Float := Index;
2626 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2627 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2632 -- Even a single Z is a valid picture
2640 when '_' | '0' | '/' =>
2641 Pic.End_Float := Index;
2645 Pic.End_Float := Index;
2646 Pic.Picture.Expanded (Index) := 'b';
2650 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2652 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2653 Pic.End_Float := Index;
2662 when '.' | 'V' | 'v' =>
2663 Pic.Radix_Position := Index;
2665 Number_Fraction_Or_Z_Fill;
2677 end Zero_Suppression;
2679 -- Start of processing for Precalculate
2684 if State = Reject then
2685 raise Picture_Error;
2690 when Constraint_Error =>
2692 -- To deal with special cases like null strings
2694 raise Picture_Error;
2703 (Pic_String : String;
2704 Blank_When_Zero : Boolean := False) return Picture
2710 Item : constant String := Expand (Pic_String);
2713 Result.Contents.Picture := (Item'Length, Item);
2714 Result.Contents.Original_BWZ := Blank_When_Zero;
2715 Result.Contents.Blank_When_Zero := Blank_When_Zero;
2716 Precalculate (Result.Contents);
2722 raise Picture_Error;
2730 function To_Wide (C : Character) return Wide_Character is
2732 return Wide_Character'Val (Character'Pos (C));
2740 (Pic_String : String;
2741 Blank_When_Zero : Boolean := False) return Boolean
2745 Expanded_Pic : constant String := Expand (Pic_String);
2746 -- Raises Picture_Error if Item not well-formed
2748 Format_Rec : Format_Record;
2751 Format_Rec.Picture := (Expanded_Pic'Length, Expanded_Pic);
2752 Format_Rec.Blank_When_Zero := Blank_When_Zero;
2753 Format_Rec.Original_BWZ := Blank_When_Zero;
2754 Precalculate (Format_Rec);
2756 -- False only if Blank_When_0 is True but the pic string has a '*'
2758 return not Blank_When_Zero
2759 or else Strings_Fixed.Index (Expanded_Pic, "*") = 0;
2763 when others => return False;
2766 end Ada.Wide_Text_IO.Editing;