2009-10-01 Tobias Burnus <burnus@net-b.de>
[official-gcc/alias-decl.git] / gcc / ada / a-wtedit.adb
blobdbe09a9df153032c435533bc3542dfb307057058
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . W I D E _ T E X T _ I O . E D I T I N G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
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
50 ---------------------
51 -- Blank_When_Zero --
52 ---------------------
54 function Blank_When_Zero (Pic : Picture) return Boolean is
55 begin
56 return Pic.Contents.Original_BWZ;
57 end Blank_When_Zero;
59 --------------------
60 -- Decimal_Output --
61 --------------------
63 package body Decimal_Output is
65 -----------
66 -- Image --
67 -----------
69 function Image
70 (Item : Num;
71 Pic : Picture;
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
77 begin
78 return Format_Number
79 (Pic.Contents, Num'Image (Item),
80 Currency, Fill, Separator, Radix_Mark);
81 end Image;
83 ------------
84 -- Length --
85 ------------
87 function Length
88 (Pic : Picture;
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;
95 begin
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
106 V_Adjust := -1;
108 elsif Picstr (J) = '$' then
109 Cur_Adjust := Currency'Length - 1;
110 end if;
111 end loop;
113 return Picstr'Length - V_Adjust + Cur_Adjust;
114 end Length;
116 ---------
117 -- Put --
118 ---------
120 procedure Put
121 (File : Wide_Text_IO.File_Type;
122 Item : Num;
123 Pic : Picture;
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)
129 begin
130 Wide_Text_IO.Put (File, Image (Item, Pic,
131 Currency, Fill, Separator, Radix_Mark));
132 end Put;
134 procedure Put
135 (Item : Num;
136 Pic : Picture;
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)
142 begin
143 Wide_Text_IO.Put (Image (Item, Pic,
144 Currency, Fill, Separator, Radix_Mark));
145 end Put;
147 procedure Put
148 (To : out Wide_String;
149 Item : Num;
150 Pic : Picture;
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);
159 begin
160 if Result'Length > To'Length then
161 raise Wide_Text_IO.Layout_Error;
162 else
163 Strings_Wide_Fixed.Move (Source => Result, Target => To,
164 Justify => Strings.Right);
165 end if;
166 end Put;
168 -----------
169 -- Valid --
170 -----------
172 function Valid
173 (Item : Num;
174 Pic : Picture;
175 Currency : Wide_String := Default_Currency) return Boolean
177 begin
178 declare
179 Temp : constant Wide_String := Image (Item, Pic, Currency);
180 pragma Warnings (Off, Temp);
181 begin
182 return True;
183 end;
185 exception
186 when Layout_Error => return False;
188 end Valid;
189 end Decimal_Output;
191 ------------
192 -- Expand --
193 ------------
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;
199 Count : Natural;
200 Last : Integer;
202 begin
203 if Picture'Length < 1 then
204 raise Picture_Error;
205 end if;
207 if Picture (Picture'First) = '(' then
208 raise Picture_Error;
209 end if;
211 loop
212 case Picture (Picture_Index) is
214 when '(' =>
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;
223 Count := 0;
225 if Picture (Last) not in '0' .. '9' then
226 raise Picture_Error;
227 end if;
229 Count := Character'Pos (Picture (Last)) - Character'Pos ('0');
230 Last := Last + 1;
232 loop
233 if Last > Picture'Last then
234 raise Picture_Error;
235 end if;
237 if Picture (Last) = '_' then
238 if Picture (Last - 1) = '_' then
239 raise Picture_Error;
240 end if;
242 elsif Picture (Last) = ')' then
243 exit;
245 elsif Picture (Last) not in '0' .. '9' then
246 raise Picture_Error;
248 else
249 Count := Count * 10
250 + Character'Pos (Picture (Last)) -
251 Character'Pos ('0');
252 end if;
254 Last := Last + 1;
255 end loop;
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);
263 end loop;
265 Result_Index := Result_Index + Count - 1;
267 -- Last was a ')' throw it away too
269 Picture_Index := Last + 1;
271 when ')' =>
272 raise Picture_Error;
274 when others =>
275 Result (Result_Index) := Picture (Picture_Index);
276 Picture_Index := Picture_Index + 1;
277 Result_Index := Result_Index + 1;
279 end case;
281 exit when Picture_Index > Picture'Last;
282 end loop;
284 return Result (1 .. Result_Index - 1);
286 exception
287 when others =>
288 raise Picture_Error;
289 end Expand;
291 -------------------
292 -- Format_Number --
293 -------------------
295 function Format_Number
296 (Pic : Format_Record;
297 Number : String;
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);
304 Position : Integer;
305 Rounded : String := Number;
307 Sign_Position : Integer := Pic.Sign_Position; -- may float.
309 Answer : Wide_String (1 .. Pic.Picture.Length);
310 Last : Integer;
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
319 begin
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
325 declare
326 R : constant Integer :=
327 (Attrs.End_Of_Fraction - Attrs.Start_Of_Fraction + 1)
328 - Pic.Max_Trailing_Digits;
329 R_Pos : Integer;
331 begin
332 if R > 0 then
333 R_Pos := Rounded'Length - R;
335 if Rounded (R_Pos + 1) > '4' then
337 if Rounded (R_Pos) = '.' then
338 R_Pos := R_Pos - 1;
339 end if;
341 if Rounded (R_Pos) /= '9' then
342 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
343 else
344 Rounded (R_Pos) := '0';
345 R_Pos := R_Pos - 1;
347 while R_Pos > 1 loop
348 if Rounded (R_Pos) = '.' then
349 R_Pos := R_Pos - 1;
350 end if;
352 if Rounded (R_Pos) /= '9' then
353 Rounded (R_Pos) := Character'Succ (Rounded (R_Pos));
354 exit;
355 else
356 Rounded (R_Pos) := '0';
357 R_Pos := R_Pos - 1;
358 end if;
359 end loop;
361 -- The rounding may add a digit in front. Either the
362 -- leading blank or the sign (already captured) can be
363 -- overwritten.
365 if R_Pos = 1 then
366 Rounded (R_Pos) := '1';
367 Attrs.Start_Of_Int := Attrs.Start_Of_Int - 1;
368 end if;
369 end if;
370 end if;
371 end if;
372 end;
373 end if;
375 for J in Answer'Range loop
376 Answer (J) := To_Wide (Pic.Picture.Expanded (J));
377 end loop;
379 if Pic.Start_Currency /= Invalid_Position then
380 Dollar := Answer (Pic.Start_Currency) = '$';
381 end if;
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.
386 Last := 1;
387 loop
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
394 when '_' =>
395 Answer (Last) := Separator_Character;
397 when 'b' =>
398 Answer (Last) := ' ';
400 when others =>
401 null;
403 end case;
405 exit when Last = Answer'Last;
407 Last := Last + 1;
408 end loop;
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
418 exit;
419 end if;
421 case Answer (J) is
423 when '_' =>
424 Answer (J) := Separator_Character;
426 when 'b' =>
427 Answer (J) := ' ';
429 when '9' =>
430 exit;
432 when others =>
433 null;
435 end case;
436 end loop;
438 -- Non-floating sign
440 if Pic.Start_Currency /= -1
441 and then Answer (Pic.Start_Currency) = '#'
442 and then Pic.Floater /= '#'
443 then
444 if Currency_Symbol'Length >
445 Pic.End_Currency - Pic.Start_Currency + 1
446 then
447 raise Picture_Error;
449 elsif Currency_Symbol'Length =
450 Pic.End_Currency - Pic.Start_Currency + 1
451 then
452 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
453 Currency_Symbol;
455 elsif Pic.Radix_Position = Invalid_Position
456 or else Pic.Start_Currency < Pic.Radix_Position
457 then
458 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
459 (others => ' ');
460 Answer (Pic.End_Currency - Currency_Symbol'Length + 1 ..
461 Pic.End_Currency) := Currency_Symbol;
463 else
464 Answer (Pic.Start_Currency .. Pic.End_Currency) :=
465 (others => ' ');
466 Answer (Pic.Start_Currency ..
467 Pic.Start_Currency + Currency_Symbol'Length - 1) :=
468 Currency_Symbol;
469 end if;
470 end if;
472 -- Fill in leading digits
474 if Attrs.End_Of_Int - Attrs.Start_Of_Int + 1 >
475 Pic.Max_Leading_Digits
476 then
477 raise Layout_Error;
478 end if;
480 if Pic.Radix_Position = Invalid_Position then
481 Position := Answer'Last;
482 else
483 Position := Pic.Radix_Position - 1;
484 end if;
486 for J in reverse Attrs.Start_Of_Int .. Attrs.End_Of_Int loop
488 while Answer (Position) /= '9'
489 and then
490 Answer (Position) /= Pic.Floater
491 loop
492 if Answer (Position) = '_' then
493 Answer (Position) := Separator_Character;
495 elsif Answer (Position) = 'b' then
496 Answer (Position) := ' ';
497 end if;
499 Position := Position - 1;
500 end loop;
502 Answer (Position) := To_Wide (Rounded (J));
504 if Rounded (J) /= '0' then
505 Zero := False;
506 end if;
508 Position := Position - 1;
509 end loop;
511 -- Do lead float
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
524 Answer (J) := '0';
526 elsif Answer (J) = '_' then
527 Answer (J) := Separator_Character;
529 elsif Answer (J) = 'b' then
530 Answer (J) := ' ';
532 end if;
534 end loop;
536 elsif Pic.Floater = '<'
537 or else
538 Pic.Floater = '+'
539 or else
540 Pic.Floater = '-'
541 then
542 for J in Pic.End_Float .. Position loop -- May be null range
543 if Answer (J) = '9' then
544 Answer (J) := '0';
546 elsif Answer (J) = '_' then
547 Answer (J) := Separator_Character;
549 elsif Answer (J) = 'b' then
550 Answer (J) := ' ';
552 end if;
553 end loop;
555 if Position > Pic.End_Float then
556 Position := Pic.End_Float;
557 end if;
559 for J in Pic.Start_Float .. Position - 1 loop
560 Answer (J) := ' ';
561 end 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
570 Answer (J) := '0';
572 elsif Answer (J) = '_' then
573 Answer (J) := ' '; -- no separator before leftmost digit
575 elsif Answer (J) = 'b' then
576 Answer (J) := ' ';
577 end if;
578 end loop;
580 if Position > Pic.End_Float then
581 Position := Pic.End_Float;
582 end if;
584 for J in Pic.Start_Float .. Position - 1 loop
585 Answer (J) := ' ';
586 end 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
595 Answer (J) := '0';
597 elsif Answer (J) = '_' then
598 Answer (J) := Separator_Character;
600 elsif Answer (J) = 'b' then
601 Answer (J) := '*';
602 end if;
603 end loop;
605 if Position > Pic.End_Float then
606 Position := Pic.End_Float;
607 end if;
609 for J in Pic.Start_Float .. Position loop
610 Answer (J) := '*';
611 end loop;
613 else
614 if Pic.Floater = '#' then
615 Currency_Pos := Currency_Symbol'Length;
616 end if;
618 for J in reverse Pic.Start_Float .. Position loop
619 case Answer (J) is
621 when '*' =>
622 Answer (J) := Fill_Character;
624 when 'Z' | 'b' | '/' | '0' =>
625 Answer (J) := ' ';
627 when '9' =>
628 Answer (J) := '0';
630 when '.' | 'V' | 'v' | '<' | '$' | '+' | '-' =>
631 null;
633 when '#' =>
634 if Currency_Pos = 0 then
635 Answer (J) := ' ';
636 else
637 Answer (J) := Currency_Symbol (Currency_Pos);
638 Currency_Pos := Currency_Pos - 1;
639 end if;
641 when '_' =>
643 case Pic.Floater is
645 when '*' =>
646 Answer (J) := Fill_Character;
648 when 'Z' | 'b' =>
649 Answer (J) := ' ';
651 when '#' =>
652 if Currency_Pos = 0 then
653 Answer (J) := ' ';
655 else
656 Answer (J) := Currency_Symbol (Currency_Pos);
657 Currency_Pos := Currency_Pos - 1;
658 end if;
660 when others =>
661 null;
663 end case;
665 when others =>
666 null;
668 end case;
669 end loop;
671 if Pic.Floater = '#' and then Currency_Pos /= 0 then
672 raise Layout_Error;
673 end if;
674 end if;
676 -- Do sign
678 if Sign_Position = Invalid_Position then
679 if Attrs.Negative then
680 raise Layout_Error;
681 end if;
683 else
684 if Attrs.Negative then
685 case Answer (Sign_Position) is
686 when 'C' | 'D' | '-' =>
687 null;
689 when '+' =>
690 Answer (Sign_Position) := '-';
692 when '<' =>
693 Answer (Sign_Position) := '(';
694 Answer (Pic.Second_Sign) := ')';
696 when others =>
697 raise Picture_Error;
699 end case;
701 else -- positive
703 case Answer (Sign_Position) is
705 when '-' =>
706 Answer (Sign_Position) := ' ';
708 when '<' | 'C' | 'D' =>
709 Answer (Sign_Position) := ' ';
710 Answer (Pic.Second_Sign) := ' ';
712 when '+' =>
713 null;
715 when others =>
716 raise Picture_Error;
718 end case;
719 end if;
720 end if;
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 else Answer (J) = Pic.Floater then
733 Answer (J) := To_Wide (Rounded (Position));
735 if Rounded (Position) /= '0' then
736 Zero := False;
737 end if;
739 Position := Position + 1;
740 Last := J + 1;
742 -- Used up fraction but remember place in Answer
744 exit when Position > Attrs.End_Of_Fraction;
746 elsif Answer (J) = 'b' then
747 Answer (J) := ' ';
749 elsif Answer (J) = '_' then
750 Answer (J) := Separator_Character;
752 end if;
754 Last := J + 1;
755 end loop;
757 Position := Last;
759 else
760 Position := Pic.Radix_Position + 1;
761 end if;
763 -- Now fill remaining 9's with zeros and _ with separators
765 Last := Answer'Last;
767 for J in Position .. Last loop
768 if Answer (J) = '9' then
769 Answer (J) := '0';
771 elsif Answer (J) = Pic.Floater then
772 Answer (J) := '0';
774 elsif Answer (J) = '_' then
775 Answer (J) := Separator_Character;
777 elsif Answer (J) = 'b' then
778 Answer (J) := ' ';
780 end if;
781 end loop;
783 Position := Last + 1;
785 else
786 if Pic.Floater = '#' and then Currency_Pos /= 0 then
787 raise Layout_Error;
788 end if;
790 -- No trailing digits, but now J may need to stick in a currency
791 -- symbol or sign.
793 if Pic.Start_Currency = Invalid_Position then
794 Position := Answer'Last + 1;
795 else
796 Position := Pic.Start_Currency;
797 end if;
798 end if;
800 for J in Position .. Answer'Last loop
802 if Pic.Start_Currency /= Invalid_Position and then
803 Answer (Pic.Start_Currency) = '#' then
804 Currency_Pos := 1;
805 end if;
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 ???
813 case Answer (J) is
814 when '*' =>
815 Answer (J) := Fill_Character;
817 when 'b' =>
818 Answer (J) := ' ';
820 when '#' =>
821 if Currency_Pos > Currency_Symbol'Length then
822 Answer (J) := ' ';
824 else
825 Answer (J) := Currency_Symbol (Currency_Pos);
826 Currency_Pos := Currency_Pos + 1;
827 end if;
829 when '_' =>
831 case Pic.Floater is
833 when '*' =>
834 Answer (J) := Fill_Character;
836 when 'Z' | 'z' =>
837 Answer (J) := ' ';
839 when '#' =>
840 if Currency_Pos > Currency_Symbol'Length then
841 Answer (J) := ' ';
842 else
843 Answer (J) := Currency_Symbol (Currency_Pos);
844 Currency_Pos := Currency_Pos + 1;
845 end if;
847 when others =>
848 null;
850 end case;
852 when others =>
853 exit;
855 end case;
856 end loop;
858 -- Now get rid of Blank_when_Zero and complete Star fill
860 if Zero and then Pic.Blank_When_Zero then
862 -- Value is zero, and blank it
864 Last := Answer'Last;
866 if Dollar then
867 Last := Last - 1 + Currency_Symbol'Length;
868 end if;
870 if Pic.Radix_Position /= Invalid_Position and then
871 Answer (Pic.Radix_Position) = 'V' then
872 Last := Last - 1;
873 end if;
875 return Wide_String'(1 .. Last => ' ');
877 elsif Zero and then Pic.Star_Fill then
878 Last := Answer'Last;
880 if Dollar then
881 Last := Last - 1 + Currency_Symbol'Length;
882 end if;
884 if Pic.Radix_Position /= Invalid_Position then
886 if Answer (Pic.Radix_Position) = 'V' then
887 Last := Last - 1;
889 elsif Dollar then
890 if Pic.Radix_Position > Pic.Start_Currency then
891 return Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
892 Radix_Point &
893 Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
895 else
896 return
897 Wide_String'
898 (1 ..
899 Pic.Radix_Position + Currency_Symbol'Length - 2
900 => '*') &
901 Radix_Point &
902 Wide_String'
903 (Pic.Radix_Position + Currency_Symbol'Length .. Last
904 => '*');
905 end if;
907 else
908 return
909 Wide_String'(1 .. Pic.Radix_Position - 1 => '*') &
910 Radix_Point &
911 Wide_String'(Pic.Radix_Position + 1 .. Last => '*');
912 end if;
913 end if;
915 return Wide_String'(1 .. Last => '*');
916 end if;
918 -- This was once a simple return statement, now there are nine
919 -- different return cases. Not to mention the five above to deal
920 -- with zeros. Why not split things out?
922 -- Processing the radix and sign expansion separately
923 -- would require lots of copying--the string and some of its
924 -- indicies--without really simplifying the logic. The cases are:
926 -- 1) Expand $, replace '.' with Radix_Point
927 -- 2) No currency expansion, replace '.' with Radix_Point
928 -- 3) Expand $, radix blanked
929 -- 4) No currency expansion, radix blanked
930 -- 5) Elide V
931 -- 6) Expand $, Elide V
932 -- 7) Elide V, Expand $ (Two cases depending on order.)
933 -- 8) No radix, expand $
934 -- 9) No radix, no currency expansion
936 if Pic.Radix_Position /= Invalid_Position then
938 if Answer (Pic.Radix_Position) = '.' then
939 Answer (Pic.Radix_Position) := Radix_Point;
941 if Dollar then
943 -- 1) Expand $, replace '.' with Radix_Point
945 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
946 Answer (Currency_Pos + 1 .. Answer'Last);
948 else
949 -- 2) No currency expansion, replace '.' with Radix_Point
951 return Answer;
952 end if;
954 elsif Answer (Pic.Radix_Position) = ' ' then -- blanked radix.
955 if Dollar then
957 -- 3) Expand $, radix blanked
959 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
960 Answer (Currency_Pos + 1 .. Answer'Last);
962 else
963 -- 4) No expansion, radix blanked
965 return Answer;
966 end if;
968 -- V cases
970 else
971 if not Dollar then
973 -- 5) Elide V
975 return Answer (1 .. Pic.Radix_Position - 1) &
976 Answer (Pic.Radix_Position + 1 .. Answer'Last);
978 elsif Currency_Pos < Pic.Radix_Position then
980 -- 6) Expand $, Elide V
982 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
983 Answer (Currency_Pos + 1 .. Pic.Radix_Position - 1) &
984 Answer (Pic.Radix_Position + 1 .. Answer'Last);
986 else
987 -- 7) Elide V, Expand $
989 return Answer (1 .. Pic.Radix_Position - 1) &
990 Answer (Pic.Radix_Position + 1 .. Currency_Pos - 1) &
991 Currency_Symbol &
992 Answer (Currency_Pos + 1 .. Answer'Last);
993 end if;
994 end if;
996 elsif Dollar then
998 -- 8) No radix, expand $
1000 return Answer (1 .. Currency_Pos - 1) & Currency_Symbol &
1001 Answer (Currency_Pos + 1 .. Answer'Last);
1003 else
1004 -- 9) No radix, no currency expansion
1006 return Answer;
1007 end if;
1008 end Format_Number;
1010 -------------------------
1011 -- Parse_Number_String --
1012 -------------------------
1014 function Parse_Number_String (Str : String) return Number_Attributes is
1015 Answer : Number_Attributes;
1017 begin
1018 for J in Str'Range loop
1019 case Str (J) is
1021 when ' ' =>
1022 null; -- ignore
1024 when '1' .. '9' =>
1026 -- Decide if this is the start of a number.
1027 -- If so, figure out which one...
1029 if Answer.Has_Fraction then
1030 Answer.End_Of_Fraction := J;
1031 else
1032 if Answer.Start_Of_Int = Invalid_Position then
1033 -- start integer
1034 Answer.Start_Of_Int := J;
1035 end if;
1036 Answer.End_Of_Int := J;
1037 end if;
1039 when '0' =>
1041 -- Only count a zero before the decimal point if it follows a
1042 -- non-zero digit. After the decimal point, zeros will be
1043 -- counted if followed by a non-zero digit.
1045 if not Answer.Has_Fraction then
1046 if Answer.Start_Of_Int /= Invalid_Position then
1047 Answer.End_Of_Int := J;
1048 end if;
1049 end if;
1051 when '-' =>
1053 -- Set negative
1055 Answer.Negative := True;
1057 when '.' =>
1059 -- Close integer, start fraction
1061 if Answer.Has_Fraction then
1062 raise Picture_Error;
1063 end if;
1065 -- Two decimal points is a no-no
1067 Answer.Has_Fraction := True;
1068 Answer.End_Of_Fraction := J;
1070 -- Could leave this at Invalid_Position, but this seems the
1071 -- right way to indicate a null range...
1073 Answer.Start_Of_Fraction := J + 1;
1074 Answer.End_Of_Int := J - 1;
1076 when others =>
1077 raise Picture_Error; -- can this happen? probably not!
1078 end case;
1079 end loop;
1081 if Answer.Start_Of_Int = Invalid_Position then
1082 Answer.Start_Of_Int := Answer.End_Of_Int + 1;
1083 end if;
1085 -- No significant (intger) digits needs a null range
1087 return Answer;
1088 end Parse_Number_String;
1090 ----------------
1091 -- Pic_String --
1092 ----------------
1094 -- The following ensures that we return B and not b being careful not
1095 -- to break things which expect lower case b for blank. See CXF3A02.
1097 function Pic_String (Pic : Picture) return String is
1098 Temp : String (1 .. Pic.Contents.Picture.Length) :=
1099 Pic.Contents.Picture.Expanded;
1100 begin
1101 for J in Temp'Range loop
1102 if Temp (J) = 'b' then
1103 Temp (J) := 'B';
1104 end if;
1105 end loop;
1107 return Temp;
1108 end Pic_String;
1110 ------------------
1111 -- Precalculate --
1112 ------------------
1114 procedure Precalculate (Pic : in out Format_Record) is
1116 Computed_BWZ : Boolean := True;
1118 type Legality is (Okay, Reject);
1119 State : Legality := Reject;
1120 -- Start in reject, which will reject null strings
1122 Index : Pic_Index := Pic.Picture.Expanded'First;
1124 function At_End return Boolean;
1125 pragma Inline (At_End);
1127 procedure Set_State (L : Legality);
1128 pragma Inline (Set_State);
1130 function Look return Character;
1131 pragma Inline (Look);
1133 function Is_Insert return Boolean;
1134 pragma Inline (Is_Insert);
1136 procedure Skip;
1137 pragma Inline (Skip);
1139 procedure Trailing_Currency;
1140 procedure Trailing_Bracket;
1141 procedure Number_Fraction;
1142 procedure Number_Completion;
1143 procedure Number_Fraction_Or_Bracket;
1144 procedure Number_Fraction_Or_Z_Fill;
1145 procedure Zero_Suppression;
1146 procedure Floating_Bracket;
1147 procedure Number_Fraction_Or_Star_Fill;
1148 procedure Star_Suppression;
1149 procedure Number_Fraction_Or_Dollar;
1150 procedure Leading_Dollar;
1151 procedure Number_Fraction_Or_Pound;
1152 procedure Leading_Pound;
1153 procedure Picture;
1154 procedure Floating_Plus;
1155 procedure Floating_Minus;
1156 procedure Picture_Plus;
1157 procedure Picture_Minus;
1158 procedure Picture_Bracket;
1159 procedure Number;
1160 procedure Optional_RHS_Sign;
1161 procedure Picture_String;
1163 ------------
1164 -- At_End --
1165 ------------
1167 function At_End return Boolean is
1168 begin
1169 return Index > Pic.Picture.Length;
1170 end At_End;
1172 ----------------------
1173 -- Floating_Bracket --
1174 ----------------------
1176 -- Note that Floating_Bracket is only called with an acceptable
1177 -- prefix. But we don't set Okay, because we must end with a '>'.
1179 procedure Floating_Bracket is
1180 begin
1181 Pic.Floater := '<';
1182 Pic.End_Float := Index;
1183 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1185 -- First bracket wasn't counted...
1187 Skip; -- known '<'
1189 loop
1190 if At_End then
1191 return;
1192 end if;
1194 case Look is
1196 when '_' | '0' | '/' =>
1197 Pic.End_Float := Index;
1198 Skip;
1200 when 'B' | 'b' =>
1201 Pic.End_Float := Index;
1202 Pic.Picture.Expanded (Index) := 'b';
1203 Skip;
1205 when '<' =>
1206 Pic.End_Float := Index;
1207 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1208 Skip;
1210 when '9' =>
1211 Number_Completion;
1213 when '$' =>
1214 Leading_Dollar;
1216 when '#' =>
1217 Leading_Pound;
1219 when 'V' | 'v' | '.' =>
1220 Pic.Radix_Position := Index;
1221 Skip;
1222 Number_Fraction_Or_Bracket;
1223 return;
1225 when others =>
1226 return;
1227 end case;
1228 end loop;
1229 end Floating_Bracket;
1231 --------------------
1232 -- Floating_Minus --
1233 --------------------
1235 procedure Floating_Minus is
1236 begin
1237 loop
1238 if At_End then
1239 return;
1240 end if;
1242 case Look is
1243 when '_' | '0' | '/' =>
1244 Pic.End_Float := Index;
1245 Skip;
1247 when 'B' | 'b' =>
1248 Pic.End_Float := Index;
1249 Pic.Picture.Expanded (Index) := 'b';
1250 Skip;
1252 when '-' =>
1253 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1254 Pic.End_Float := Index;
1255 Skip;
1257 when '9' =>
1258 Number_Completion;
1259 return;
1261 when '.' | 'V' | 'v' =>
1262 Pic.Radix_Position := Index;
1263 Skip; -- Radix
1265 while Is_Insert loop
1266 Skip;
1267 end loop;
1269 if At_End then
1270 return;
1271 end if;
1273 if Look = '-' then
1274 loop
1275 if At_End then
1276 return;
1277 end if;
1279 case Look is
1281 when '-' =>
1282 Pic.Max_Trailing_Digits :=
1283 Pic.Max_Trailing_Digits + 1;
1284 Pic.End_Float := Index;
1285 Skip;
1287 when '_' | '0' | '/' =>
1288 Skip;
1290 when 'B' | 'b' =>
1291 Pic.Picture.Expanded (Index) := 'b';
1292 Skip;
1294 when others =>
1295 return;
1297 end case;
1298 end loop;
1300 else
1301 Number_Completion;
1302 end if;
1304 return;
1306 when others =>
1307 return;
1308 end case;
1309 end loop;
1310 end Floating_Minus;
1312 -------------------
1313 -- Floating_Plus --
1314 -------------------
1316 procedure Floating_Plus is
1317 begin
1318 loop
1319 if At_End then
1320 return;
1321 end if;
1323 case Look is
1324 when '_' | '0' | '/' =>
1325 Pic.End_Float := Index;
1326 Skip;
1328 when 'B' | 'b' =>
1329 Pic.End_Float := Index;
1330 Pic.Picture.Expanded (Index) := 'b';
1331 Skip;
1333 when '+' =>
1334 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1335 Pic.End_Float := Index;
1336 Skip;
1338 when '9' =>
1339 Number_Completion;
1340 return;
1342 when '.' | 'V' | 'v' =>
1343 Pic.Radix_Position := Index;
1344 Skip; -- Radix
1346 while Is_Insert loop
1347 Skip;
1348 end loop;
1350 if At_End then
1351 return;
1352 end if;
1354 if Look = '+' then
1355 loop
1356 if At_End then
1357 return;
1358 end if;
1360 case Look is
1362 when '+' =>
1363 Pic.Max_Trailing_Digits :=
1364 Pic.Max_Trailing_Digits + 1;
1365 Pic.End_Float := Index;
1366 Skip;
1368 when '_' | '0' | '/' =>
1369 Skip;
1371 when 'B' | 'b' =>
1372 Pic.Picture.Expanded (Index) := 'b';
1373 Skip;
1375 when others =>
1376 return;
1378 end case;
1379 end loop;
1381 else
1382 Number_Completion;
1383 end if;
1385 return;
1387 when others =>
1388 return;
1390 end case;
1391 end loop;
1392 end Floating_Plus;
1394 ---------------
1395 -- Is_Insert --
1396 ---------------
1398 function Is_Insert return Boolean is
1399 begin
1400 if At_End then
1401 return False;
1402 end if;
1404 case Pic.Picture.Expanded (Index) is
1406 when '_' | '0' | '/' => return True;
1408 when 'B' | 'b' =>
1409 Pic.Picture.Expanded (Index) := 'b'; -- canonical
1410 return True;
1412 when others => return False;
1413 end case;
1414 end Is_Insert;
1416 --------------------
1417 -- Leading_Dollar --
1418 --------------------
1420 -- Note that Leading_Dollar can be called in either State.
1421 -- It will set state to Okay only if a 9 or (second) $
1422 -- 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
1429 begin
1430 -- Treat as a floating dollar, and unwind otherwise
1432 Pic.Floater := '$';
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"
1439 -- currency place.
1441 Skip; -- known '$'
1443 loop
1444 if At_End then
1445 return;
1446 end if;
1448 case Look is
1450 when '_' | '0' | '/' =>
1451 Pic.End_Float := Index;
1452 Skip;
1454 -- A trailing insertion character is not part of the
1455 -- floating currency, so need to look ahead.
1457 if Look /= '$' then
1458 Pic.End_Float := Pic.End_Float - 1;
1459 end if;
1461 when 'B' | 'b' =>
1462 Pic.End_Float := Index;
1463 Pic.Picture.Expanded (Index) := 'b';
1464 Skip;
1466 when 'Z' | 'z' =>
1467 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1469 if State = Okay then
1470 raise Picture_Error;
1471 else
1472 -- Will overwrite Floater and Start_Float
1474 Zero_Suppression;
1475 end if;
1477 when '*' =>
1478 if State = Okay then
1479 raise Picture_Error;
1480 else
1481 -- Will overwrite Floater and Start_Float
1483 Star_Suppression;
1484 end if;
1486 when '$' =>
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;
1492 when '9' =>
1493 if State /= Okay then
1494 Pic.Floater := '!';
1495 Pic.Start_Float := Invalid_Position;
1496 Pic.End_Float := Invalid_Position;
1497 end if;
1499 -- A single dollar does not a floating make
1501 Number_Completion;
1502 return;
1504 when 'V' | 'v' | '.' =>
1505 if State /= Okay then
1506 Pic.Floater := '!';
1507 Pic.Start_Float := Invalid_Position;
1508 Pic.End_Float := Invalid_Position;
1509 end if;
1511 -- Only one dollar before the sign is okay, but doesn't
1512 -- float.
1514 Pic.Radix_Position := Index;
1515 Skip;
1516 Number_Fraction_Or_Dollar;
1517 return;
1519 when others =>
1520 return;
1522 end case;
1523 end loop;
1524 end Leading_Dollar;
1526 -------------------
1527 -- Leading_Pound --
1528 -------------------
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
1533 -- either State.
1535 -- It will set state to Okay only if a 9 or (second) # is
1536 -- encountered.
1538 -- One Last note: In ambiguous cases, the currency is treated as
1539 -- floating unless there is only one '#'.
1541 procedure Leading_Pound is
1543 Inserts : Boolean := False;
1544 -- Set to True if a '_', '0', '/', 'B', or 'b' is encountered
1546 Must_Float : Boolean := False;
1547 -- Set to true if a '#' occurs after an insert
1549 begin
1550 -- Treat as a floating currency. If it isn't, this will be
1551 -- overwritten later.
1553 Pic.Floater := '#';
1555 Pic.Start_Currency := Index;
1556 Pic.End_Currency := Index;
1557 Pic.Start_Float := Index;
1558 Pic.End_Float := Index;
1560 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
1561 -- currency place.
1563 Pic.Max_Currency_Digits := 1; -- we've seen one.
1565 Skip; -- known '#'
1567 loop
1568 if At_End then
1569 return;
1570 end if;
1572 case Look is
1574 when '_' | '0' | '/' =>
1575 Pic.End_Float := Index;
1576 Inserts := True;
1577 Skip;
1579 when 'B' | 'b' =>
1580 Pic.Picture.Expanded (Index) := 'b';
1581 Pic.End_Float := Index;
1582 Inserts := True;
1583 Skip;
1585 when 'Z' | 'z' =>
1586 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
1588 if Must_Float then
1589 raise Picture_Error;
1590 else
1591 Pic.Max_Leading_Digits := 0;
1593 -- Will overwrite Floater and Start_Float
1595 Zero_Suppression;
1596 end if;
1598 when '*' =>
1599 if Must_Float then
1600 raise Picture_Error;
1601 else
1602 Pic.Max_Leading_Digits := 0;
1604 -- Will overwrite Floater and Start_Float
1606 Star_Suppression;
1607 end if;
1609 when '#' =>
1610 if Inserts then
1611 Must_Float := True;
1612 end if;
1614 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1615 Pic.End_Float := Index;
1616 Pic.End_Currency := Index;
1617 Set_State (Okay);
1618 Skip;
1620 when '9' =>
1621 if State /= Okay then
1623 -- A single '#' doesn't float
1625 Pic.Floater := '!';
1626 Pic.Start_Float := Invalid_Position;
1627 Pic.End_Float := Invalid_Position;
1628 end if;
1630 Number_Completion;
1631 return;
1633 when 'V' | 'v' | '.' =>
1634 if State /= Okay then
1635 Pic.Floater := '!';
1636 Pic.Start_Float := Invalid_Position;
1637 Pic.End_Float := Invalid_Position;
1638 end if;
1640 -- Only one pound before the sign is okay, but doesn't
1641 -- float.
1643 Pic.Radix_Position := Index;
1644 Skip;
1645 Number_Fraction_Or_Pound;
1646 return;
1648 when others =>
1649 return;
1650 end case;
1651 end loop;
1652 end Leading_Pound;
1654 ----------
1655 -- Look --
1656 ----------
1658 function Look return Character is
1659 begin
1660 if At_End then
1661 raise Picture_Error;
1662 end if;
1664 return Pic.Picture.Expanded (Index);
1665 end Look;
1667 ------------
1668 -- Number --
1669 ------------
1671 procedure Number is
1672 begin
1673 loop
1675 case Look is
1676 when '_' | '0' | '/' =>
1677 Skip;
1679 when 'B' | 'b' =>
1680 Pic.Picture.Expanded (Index) := 'b';
1681 Skip;
1683 when '9' =>
1684 Computed_BWZ := False;
1685 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1686 Set_State (Okay);
1687 Skip;
1689 when '.' | 'V' | 'v' =>
1690 Pic.Radix_Position := Index;
1691 Skip;
1692 Number_Fraction;
1693 return;
1695 when others =>
1696 return;
1698 end case;
1700 if At_End then
1701 return;
1702 end if;
1704 -- Will return in Okay state if a '9' was seen
1706 end loop;
1707 end Number;
1709 -----------------------
1710 -- Number_Completion --
1711 -----------------------
1713 procedure Number_Completion is
1714 begin
1715 while not At_End loop
1716 case Look is
1718 when '_' | '0' | '/' =>
1719 Skip;
1721 when 'B' | 'b' =>
1722 Pic.Picture.Expanded (Index) := 'b';
1723 Skip;
1725 when '9' =>
1726 Computed_BWZ := False;
1727 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
1728 Set_State (Okay);
1729 Skip;
1731 when 'V' | 'v' | '.' =>
1732 Pic.Radix_Position := Index;
1733 Skip;
1734 Number_Fraction;
1735 return;
1737 when others =>
1738 return;
1739 end case;
1740 end loop;
1741 end Number_Completion;
1743 ---------------------
1744 -- Number_Fraction --
1745 ---------------------
1747 procedure Number_Fraction is
1748 begin
1749 -- Note that number fraction can be called in either State.
1750 -- It will set state to Valid only if a 9 is encountered.
1752 loop
1753 if At_End then
1754 return;
1755 end if;
1757 case Look is
1758 when '_' | '0' | '/' =>
1759 Skip;
1761 when 'B' | 'b' =>
1762 Pic.Picture.Expanded (Index) := 'b';
1763 Skip;
1765 when '9' =>
1766 Computed_BWZ := False;
1767 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1768 Set_State (Okay); Skip;
1770 when others =>
1771 return;
1772 end case;
1773 end loop;
1774 end Number_Fraction;
1776 --------------------------------
1777 -- Number_Fraction_Or_Bracket --
1778 --------------------------------
1780 procedure Number_Fraction_Or_Bracket is
1781 begin
1782 loop
1783 if At_End then
1784 return;
1785 end if;
1787 case Look is
1789 when '_' | '0' | '/' => Skip;
1791 when 'B' | 'b' =>
1792 Pic.Picture.Expanded (Index) := 'b';
1793 Skip;
1795 when '<' =>
1796 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1797 Pic.End_Float := Index;
1798 Skip;
1800 loop
1801 if At_End then
1802 return;
1803 end if;
1805 case Look is
1806 when '_' | '0' | '/' =>
1807 Skip;
1809 when 'B' | 'b' =>
1810 Pic.Picture.Expanded (Index) := 'b';
1811 Skip;
1813 when '<' =>
1814 Pic.Max_Trailing_Digits :=
1815 Pic.Max_Trailing_Digits + 1;
1816 Pic.End_Float := Index;
1817 Skip;
1819 when others =>
1820 return;
1821 end case;
1822 end loop;
1824 when others =>
1825 Number_Fraction;
1826 return;
1827 end case;
1828 end loop;
1829 end Number_Fraction_Or_Bracket;
1831 -------------------------------
1832 -- Number_Fraction_Or_Dollar --
1833 -------------------------------
1835 procedure Number_Fraction_Or_Dollar is
1836 begin
1837 loop
1838 if At_End then
1839 return;
1840 end if;
1842 case Look is
1843 when '_' | '0' | '/' =>
1844 Skip;
1846 when 'B' | 'b' =>
1847 Pic.Picture.Expanded (Index) := 'b';
1848 Skip;
1850 when '$' =>
1851 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1852 Pic.End_Float := Index;
1853 Skip;
1855 loop
1856 if At_End then
1857 return;
1858 end if;
1860 case Look is
1861 when '_' | '0' | '/' =>
1862 Skip;
1864 when 'B' | 'b' =>
1865 Pic.Picture.Expanded (Index) := 'b';
1866 Skip;
1868 when '$' =>
1869 Pic.Max_Trailing_Digits :=
1870 Pic.Max_Trailing_Digits + 1;
1871 Pic.End_Float := Index;
1872 Skip;
1874 when others =>
1875 return;
1876 end case;
1877 end loop;
1879 when others =>
1880 Number_Fraction;
1881 return;
1882 end case;
1883 end loop;
1884 end Number_Fraction_Or_Dollar;
1886 ------------------------------
1887 -- Number_Fraction_Or_Pound --
1888 ------------------------------
1890 procedure Number_Fraction_Or_Pound is
1891 begin
1892 loop
1893 if At_End then
1894 return;
1895 end if;
1897 case Look is
1899 when '_' | '0' | '/' =>
1900 Skip;
1902 when 'B' | 'b' =>
1903 Pic.Picture.Expanded (Index) := 'b';
1904 Skip;
1906 when '#' =>
1907 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1908 Pic.End_Float := Index;
1909 Skip;
1911 loop
1912 if At_End then
1913 return;
1914 end if;
1916 case Look is
1918 when '_' | '0' | '/' =>
1919 Skip;
1921 when 'B' | 'b' =>
1922 Pic.Picture.Expanded (Index) := 'b';
1923 Skip;
1925 when '#' =>
1926 Pic.Max_Trailing_Digits :=
1927 Pic.Max_Trailing_Digits + 1;
1928 Pic.End_Float := Index;
1929 Skip;
1931 when others =>
1932 return;
1934 end case;
1935 end loop;
1937 when others =>
1938 Number_Fraction;
1939 return;
1941 end case;
1942 end loop;
1943 end Number_Fraction_Or_Pound;
1945 ----------------------------------
1946 -- Number_Fraction_Or_Star_Fill --
1947 ----------------------------------
1949 procedure Number_Fraction_Or_Star_Fill is
1950 begin
1951 loop
1952 if At_End then
1953 return;
1954 end if;
1956 case Look is
1958 when '_' | '0' | '/' =>
1959 Skip;
1961 when 'B' | 'b' =>
1962 Pic.Picture.Expanded (Index) := 'b';
1963 Skip;
1965 when '*' =>
1966 Pic.Star_Fill := True;
1967 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
1968 Pic.End_Float := Index;
1969 Skip;
1971 loop
1972 if At_End then
1973 return;
1974 end if;
1976 case Look is
1978 when '_' | '0' | '/' =>
1979 Skip;
1981 when 'B' | 'b' =>
1982 Pic.Picture.Expanded (Index) := 'b';
1983 Skip;
1985 when '*' =>
1986 Pic.Star_Fill := True;
1987 Pic.Max_Trailing_Digits :=
1988 Pic.Max_Trailing_Digits + 1;
1989 Pic.End_Float := Index;
1990 Skip;
1992 when others =>
1993 return;
1994 end case;
1995 end loop;
1997 when others =>
1998 Number_Fraction;
1999 return;
2001 end case;
2002 end loop;
2003 end Number_Fraction_Or_Star_Fill;
2005 -------------------------------
2006 -- Number_Fraction_Or_Z_Fill --
2007 -------------------------------
2009 procedure Number_Fraction_Or_Z_Fill is
2010 begin
2011 loop
2012 if At_End then
2013 return;
2014 end if;
2016 case Look is
2018 when '_' | '0' | '/' =>
2019 Skip;
2021 when 'B' | 'b' =>
2022 Pic.Picture.Expanded (Index) := 'b';
2023 Skip;
2025 when 'Z' | 'z' =>
2026 Pic.Max_Trailing_Digits := Pic.Max_Trailing_Digits + 1;
2027 Pic.End_Float := Index;
2028 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2030 Skip;
2032 loop
2033 if At_End then
2034 return;
2035 end if;
2037 case Look is
2039 when '_' | '0' | '/' =>
2040 Skip;
2042 when 'B' | 'b' =>
2043 Pic.Picture.Expanded (Index) := 'b';
2044 Skip;
2046 when 'Z' | 'z' =>
2047 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2049 Pic.Max_Trailing_Digits :=
2050 Pic.Max_Trailing_Digits + 1;
2051 Pic.End_Float := Index;
2052 Skip;
2054 when others =>
2055 return;
2056 end case;
2057 end loop;
2059 when others =>
2060 Number_Fraction;
2061 return;
2062 end case;
2063 end loop;
2064 end Number_Fraction_Or_Z_Fill;
2066 -----------------------
2067 -- Optional_RHS_Sign --
2068 -----------------------
2070 procedure Optional_RHS_Sign is
2071 begin
2072 if At_End then
2073 return;
2074 end if;
2076 case Look is
2078 when '+' | '-' =>
2079 Pic.Sign_Position := Index;
2080 Skip;
2081 return;
2083 when 'C' | 'c' =>
2084 Pic.Sign_Position := Index;
2085 Pic.Picture.Expanded (Index) := 'C';
2086 Skip;
2088 if Look = 'R' or else Look = 'r' then
2089 Pic.Second_Sign := Index;
2090 Pic.Picture.Expanded (Index) := 'R';
2091 Skip;
2093 else
2094 raise Picture_Error;
2095 end if;
2097 return;
2099 when 'D' | 'd' =>
2100 Pic.Sign_Position := Index;
2101 Pic.Picture.Expanded (Index) := 'D';
2102 Skip;
2104 if Look = 'B' or else Look = 'b' then
2105 Pic.Second_Sign := Index;
2106 Pic.Picture.Expanded (Index) := 'B';
2107 Skip;
2109 else
2110 raise Picture_Error;
2111 end if;
2113 return;
2115 when '>' =>
2116 if Pic.Picture.Expanded (Pic.Sign_Position) = '<' then
2117 Pic.Second_Sign := Index;
2118 Skip;
2120 else
2121 raise Picture_Error;
2122 end if;
2124 when others =>
2125 return;
2127 end case;
2128 end Optional_RHS_Sign;
2130 -------------
2131 -- Picture --
2132 -------------
2134 -- Note that Picture can be called in either State
2136 -- It will set state to Valid only if a 9 is encountered or floating
2137 -- currency is called.
2139 procedure Picture is
2140 begin
2141 loop
2142 if At_End then
2143 return;
2144 end if;
2146 case Look is
2148 when '_' | '0' | '/' =>
2149 Skip;
2151 when 'B' | 'b' =>
2152 Pic.Picture.Expanded (Index) := 'b';
2153 Skip;
2155 when '$' =>
2156 Leading_Dollar;
2157 return;
2159 when '#' =>
2160 Leading_Pound;
2161 return;
2163 when '9' =>
2164 Computed_BWZ := False;
2165 Set_State (Okay);
2166 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2167 Skip;
2169 when 'V' | 'v' | '.' =>
2170 Pic.Radix_Position := Index;
2171 Skip;
2172 Number_Fraction;
2173 Trailing_Currency;
2174 return;
2176 when others =>
2177 return;
2179 end case;
2180 end loop;
2181 end Picture;
2183 ---------------------
2184 -- Picture_Bracket --
2185 ---------------------
2187 procedure Picture_Bracket is
2188 begin
2189 Pic.Sign_Position := Index;
2190 Pic.Sign_Position := Index;
2192 -- Treat as a floating sign, and unwind otherwise
2194 Pic.Floater := '<';
2195 Pic.Start_Float := Index;
2196 Pic.End_Float := Index;
2198 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2199 -- sign place.
2201 Skip; -- Known Bracket
2203 loop
2204 case Look is
2206 when '_' | '0' | '/' =>
2207 Pic.End_Float := Index;
2208 Skip;
2210 when 'B' | 'b' =>
2211 Pic.End_Float := Index;
2212 Pic.Picture.Expanded (Index) := 'b';
2213 Skip;
2215 when '<' =>
2216 Set_State (Okay); -- "<<>" is enough.
2217 Floating_Bracket;
2218 Trailing_Currency;
2219 Trailing_Bracket;
2220 return;
2222 when '$' | '#' | '9' | '*' =>
2223 if State /= Okay then
2224 Pic.Floater := '!';
2225 Pic.Start_Float := Invalid_Position;
2226 Pic.End_Float := Invalid_Position;
2227 end if;
2229 Picture;
2230 Trailing_Bracket;
2231 Set_State (Okay);
2232 return;
2234 when '.' | 'V' | 'v' =>
2235 if State /= Okay then
2236 Pic.Floater := '!';
2237 Pic.Start_Float := Invalid_Position;
2238 Pic.End_Float := Invalid_Position;
2239 end if;
2241 -- Don't assume that state is okay, haven't seen a digit
2243 Picture;
2244 Trailing_Bracket;
2245 return;
2247 when others =>
2248 raise Picture_Error;
2250 end case;
2251 end loop;
2252 end Picture_Bracket;
2254 -------------------
2255 -- Picture_Minus --
2256 -------------------
2258 procedure Picture_Minus is
2259 begin
2260 Pic.Sign_Position := Index;
2262 -- Treat as a floating sign, and unwind otherwise
2264 Pic.Floater := '-';
2265 Pic.Start_Float := Index;
2266 Pic.End_Float := Index;
2268 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2269 -- sign place.
2271 Skip; -- Known Minus
2273 loop
2274 case Look is
2276 when '_' | '0' | '/' =>
2277 Pic.End_Float := Index;
2278 Skip;
2280 when 'B' | 'b' =>
2281 Pic.End_Float := Index;
2282 Pic.Picture.Expanded (Index) := 'b';
2283 Skip;
2285 when '-' =>
2286 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2287 Pic.End_Float := Index;
2288 Skip;
2289 Set_State (Okay); -- "-- " is enough
2290 Floating_Minus;
2291 Trailing_Currency;
2292 return;
2294 when '$' | '#' | '9' | '*' =>
2295 if State /= Okay then
2296 Pic.Floater := '!';
2297 Pic.Start_Float := Invalid_Position;
2298 Pic.End_Float := Invalid_Position;
2299 end if;
2301 Picture;
2302 Set_State (Okay);
2303 return;
2305 when 'Z' | 'z' =>
2307 -- Can't have Z and a floating sign
2309 if State = Okay then
2310 Set_State (Reject);
2311 end if;
2313 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2314 Zero_Suppression;
2315 Trailing_Currency;
2316 Optional_RHS_Sign;
2317 return;
2319 when '.' | 'V' | 'v' =>
2320 if State /= Okay then
2321 Pic.Floater := '!';
2322 Pic.Start_Float := Invalid_Position;
2323 Pic.End_Float := Invalid_Position;
2324 end if;
2326 -- Don't assume that state is okay, haven't seen a digit
2328 Picture;
2329 return;
2331 when others =>
2332 return;
2334 end case;
2335 end loop;
2336 end Picture_Minus;
2338 ------------------
2339 -- Picture_Plus --
2340 ------------------
2342 procedure Picture_Plus is
2343 begin
2344 Pic.Sign_Position := Index;
2346 -- Treat as a floating sign, and unwind otherwise
2348 Pic.Floater := '+';
2349 Pic.Start_Float := Index;
2350 Pic.End_Float := Index;
2352 -- Don't increment Pic.Max_Leading_Digits, we need one "real"
2353 -- sign place.
2355 Skip; -- Known Plus
2357 loop
2358 case Look is
2360 when '_' | '0' | '/' =>
2361 Pic.End_Float := Index;
2362 Skip;
2364 when 'B' | 'b' =>
2365 Pic.End_Float := Index;
2366 Pic.Picture.Expanded (Index) := 'b';
2367 Skip;
2369 when '+' =>
2370 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2371 Pic.End_Float := Index;
2372 Skip;
2373 Set_State (Okay); -- "++" is enough
2374 Floating_Plus;
2375 Trailing_Currency;
2376 return;
2378 when '$' | '#' | '9' | '*' =>
2379 if State /= Okay then
2380 Pic.Floater := '!';
2381 Pic.Start_Float := Invalid_Position;
2382 Pic.End_Float := Invalid_Position;
2383 end if;
2385 Picture;
2386 Set_State (Okay);
2387 return;
2389 when 'Z' | 'z' =>
2390 if State = Okay then
2391 Set_State (Reject);
2392 end if;
2394 -- Can't have Z and a floating sign
2396 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2398 -- '+Z' is acceptable
2400 Set_State (Okay);
2402 Zero_Suppression;
2403 Trailing_Currency;
2404 Optional_RHS_Sign;
2405 return;
2407 when '.' | 'V' | 'v' =>
2408 if State /= Okay then
2409 Pic.Floater := '!';
2410 Pic.Start_Float := Invalid_Position;
2411 Pic.End_Float := Invalid_Position;
2412 end if;
2414 -- Don't assume that state is okay, haven't seen a digit
2416 Picture;
2417 return;
2419 when others =>
2420 return;
2422 end case;
2423 end loop;
2424 end Picture_Plus;
2426 --------------------
2427 -- Picture_String --
2428 --------------------
2430 procedure Picture_String is
2431 begin
2432 while Is_Insert loop
2433 Skip;
2434 end loop;
2436 case Look is
2438 when '$' | '#' =>
2439 Picture;
2440 Optional_RHS_Sign;
2442 when '+' =>
2443 Picture_Plus;
2445 when '-' =>
2446 Picture_Minus;
2448 when '<' =>
2449 Picture_Bracket;
2451 when 'Z' | 'z' =>
2452 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2453 Zero_Suppression;
2454 Trailing_Currency;
2455 Optional_RHS_Sign;
2457 when '*' =>
2458 Star_Suppression;
2459 Trailing_Currency;
2460 Optional_RHS_Sign;
2462 when '9' | '.' | 'V' | 'v' =>
2463 Number;
2464 Trailing_Currency;
2465 Optional_RHS_Sign;
2467 when others =>
2468 raise Picture_Error;
2470 end case;
2472 -- Blank when zero either if the PIC does not contain a '9' or if
2473 -- requested by the user and no '*'.
2475 Pic.Blank_When_Zero :=
2476 (Computed_BWZ or else Pic.Blank_When_Zero)
2477 and then not Pic.Star_Fill;
2479 -- Star fill if '*' and no '9'
2481 Pic.Star_Fill := Pic.Star_Fill and then Computed_BWZ;
2483 if not At_End then
2484 Set_State (Reject);
2485 end if;
2487 end Picture_String;
2489 ---------------
2490 -- Set_State --
2491 ---------------
2493 procedure Set_State (L : Legality) is
2494 begin
2495 State := L;
2496 end Set_State;
2498 ----------
2499 -- Skip --
2500 ----------
2502 procedure Skip is
2503 begin
2504 Index := Index + 1;
2505 end Skip;
2507 ----------------------
2508 -- Star_Suppression --
2509 ----------------------
2511 procedure Star_Suppression is
2512 begin
2513 Pic.Floater := '*';
2514 Pic.Start_Float := Index;
2515 Pic.End_Float := Index;
2516 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2517 Set_State (Okay);
2519 -- Even a single * is a valid picture
2521 Pic.Star_Fill := True;
2522 Skip; -- Known *
2524 loop
2525 if At_End then
2526 return;
2527 end if;
2529 case Look is
2531 when '_' | '0' | '/' =>
2532 Pic.End_Float := Index;
2533 Skip;
2535 when 'B' | 'b' =>
2536 Pic.End_Float := Index;
2537 Pic.Picture.Expanded (Index) := 'b';
2538 Skip;
2540 when '*' =>
2541 Pic.End_Float := Index;
2542 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2543 Set_State (Okay); Skip;
2545 when '9' =>
2546 Set_State (Okay);
2547 Number_Completion;
2548 return;
2550 when '.' | 'V' | 'v' =>
2551 Pic.Radix_Position := Index;
2552 Skip;
2553 Number_Fraction_Or_Star_Fill;
2554 return;
2556 when '#' | '$' =>
2557 Trailing_Currency;
2558 Set_State (Okay);
2559 return;
2561 when others => raise Picture_Error;
2562 end case;
2563 end loop;
2564 end Star_Suppression;
2566 ----------------------
2567 -- Trailing_Bracket --
2568 ----------------------
2570 procedure Trailing_Bracket is
2571 begin
2572 if Look = '>' then
2573 Pic.Second_Sign := Index;
2574 Skip;
2575 else
2576 raise Picture_Error;
2577 end if;
2578 end Trailing_Bracket;
2580 -----------------------
2581 -- Trailing_Currency --
2582 -----------------------
2584 procedure Trailing_Currency is
2585 begin
2586 if At_End then
2587 return;
2588 end if;
2590 if Look = '$' then
2591 Pic.Start_Currency := Index;
2592 Pic.End_Currency := Index;
2593 Skip;
2595 else
2596 while not At_End and then Look = '#' loop
2597 if Pic.Start_Currency = Invalid_Position then
2598 Pic.Start_Currency := Index;
2599 end if;
2601 Pic.End_Currency := Index;
2602 Skip;
2603 end loop;
2604 end if;
2606 loop
2607 if At_End then
2608 return;
2609 end if;
2611 case Look is
2612 when '_' | '0' | '/' => Skip;
2614 when 'B' | 'b' =>
2615 Pic.Picture.Expanded (Index) := 'b';
2616 Skip;
2618 when others => return;
2619 end case;
2620 end loop;
2621 end Trailing_Currency;
2623 ----------------------
2624 -- Zero_Suppression --
2625 ----------------------
2627 procedure Zero_Suppression is
2628 begin
2629 Pic.Floater := 'Z';
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
2635 Skip; -- Known Z
2637 loop
2638 -- Even a single Z is a valid picture
2640 if At_End then
2641 Set_State (Okay);
2642 return;
2643 end if;
2645 case Look is
2646 when '_' | '0' | '/' =>
2647 Pic.End_Float := Index;
2648 Skip;
2650 when 'B' | 'b' =>
2651 Pic.End_Float := Index;
2652 Pic.Picture.Expanded (Index) := 'b';
2653 Skip;
2655 when 'Z' | 'z' =>
2656 Pic.Picture.Expanded (Index) := 'Z'; -- consistency
2658 Pic.Max_Leading_Digits := Pic.Max_Leading_Digits + 1;
2659 Pic.End_Float := Index;
2660 Set_State (Okay);
2661 Skip;
2663 when '9' =>
2664 Set_State (Okay);
2665 Number_Completion;
2666 return;
2668 when '.' | 'V' | 'v' =>
2669 Pic.Radix_Position := Index;
2670 Skip;
2671 Number_Fraction_Or_Z_Fill;
2672 return;
2674 when '#' | '$' =>
2675 Trailing_Currency;
2676 Set_State (Okay);
2677 return;
2679 when others =>
2680 return;
2681 end case;
2682 end loop;
2683 end Zero_Suppression;
2685 -- Start of processing for Precalculate
2687 begin
2688 Picture_String;
2690 if State = Reject then
2691 raise Picture_Error;
2692 end if;
2694 exception
2696 when Constraint_Error =>
2698 -- To deal with special cases like null strings
2700 raise Picture_Error;
2702 end Precalculate;
2704 ----------------
2705 -- To_Picture --
2706 ----------------
2708 function To_Picture
2709 (Pic_String : String;
2710 Blank_When_Zero : Boolean := False) return Picture
2712 Result : Picture;
2714 begin
2715 declare
2716 Item : constant String := Expand (Pic_String);
2718 begin
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);
2723 return Result;
2724 end;
2726 exception
2727 when others =>
2728 raise Picture_Error;
2730 end To_Picture;
2732 -------------
2733 -- To_Wide --
2734 -------------
2736 function To_Wide (C : Character) return Wide_Character is
2737 begin
2738 return Wide_Character'Val (Character'Pos (C));
2739 end To_Wide;
2741 -----------
2742 -- Valid --
2743 -----------
2745 function Valid
2746 (Pic_String : String;
2747 Blank_When_Zero : Boolean := False) return Boolean
2749 begin
2750 declare
2751 Expanded_Pic : constant String := Expand (Pic_String);
2752 -- Raises Picture_Error if Item not well-formed
2754 Format_Rec : Format_Record;
2756 begin
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;
2766 end;
2768 exception
2769 when others => return False;
2770 end Valid;
2772 end Ada.Wide_Text_IO.Editing;