OSDN Git Service

2008-04-08 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-imgdec.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                        S Y S T E M . I M G _ D E C                       --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, 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 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with System.Img_Int; use System.Img_Int;
35
36 package body System.Img_Dec is
37
38    -------------------
39    -- Image_Decimal --
40    -------------------
41
42    procedure Image_Decimal
43      (V     : Integer;
44       S     : in out String;
45       P     : out Natural;
46       Scale : Integer)
47    is
48       pragma Assert (S'First = 1);
49
50    begin
51       --  Add space at start for non-negative numbers
52
53       if V >= 0 then
54          S (1) := ' ';
55          P := 1;
56       else
57          P := 0;
58       end if;
59
60       Set_Image_Decimal (V, S, P, Scale, 1, Integer'Max (1, Scale), 0);
61    end Image_Decimal;
62
63    ------------------------
64    -- Set_Decimal_Digits --
65    ------------------------
66
67    procedure Set_Decimal_Digits
68      (Digs  : in out String;
69       NDigs : Natural;
70       S     : out String;
71       P     : in out Natural;
72       Scale : Integer;
73       Fore  : Natural;
74       Aft   : Natural;
75       Exp   : Natural)
76    is
77       Minus : constant Boolean := (Digs (Digs'First) = '-');
78       --  Set True if input is negative
79
80       Zero : Boolean := (Digs (Digs'First + 1) = '0');
81       --  Set True if input is exactly zero (only case when a leading zero
82       --  is permitted in the input string given to this procedure). This
83       --  flag can get set later if rounding causes the value to become zero.
84
85       FD : Natural := 2;
86       --  First digit position of digits remaining to be processed
87
88       LD : Natural := NDigs;
89       --  Last digit position of digits remaining to be processed
90
91       ND : Natural := NDigs - 1;
92       --  Number of digits remaining to be processed (LD - FD + 1)
93
94       Digits_Before_Point : Integer := ND - Scale;
95       --  Number of digits before decimal point in the input value. This
96       --  value can be negative if the input value is less than 0.1, so
97       --  it is an indication of the current exponent. Digits_Before_Point
98       --  is adjusted if the rounding step generates an extra digit.
99
100       Digits_After_Point : constant Natural := Integer'Max (1, Aft);
101       --  Digit positions after decimal point in result string
102
103       Expon : Integer;
104       --  Integer value of exponent
105
106       procedure Round (N : Natural);
107       --  Round the number in Digs. N is the position of the last digit to be
108       --  retained in the rounded position (rounding is based on Digs (N + 1)
109       --  FD, LD, ND are reset as necessary if required. Note that if the
110       --  result value rounds up (e.g. 9.99 => 10.0), an extra digit can be
111       --  placed in the sign position as a result of the rounding, this is
112       --  the case in which FD is adjusted.
113
114       procedure Set (C : Character);
115       pragma Inline (Set);
116       --  Sets character C in output buffer
117
118       procedure Set_Blanks_And_Sign (N : Integer);
119       --  Sets leading blanks and minus sign if needed. N is the number of
120       --  positions to be filled (a minus sign is output even if N is zero
121       --  or negative, For a positive value, if N is non-positive, then
122       --  a leading blank is filled.
123
124       procedure Set_Digits (S, E : Natural);
125       pragma Inline (Set_Digits);
126       --  Set digits S through E from Digs, no effect if S > E
127
128       procedure Set_Zeroes (N : Integer);
129       pragma Inline (Set_Zeroes);
130       --  Set N zeroes, no effect if N is negative
131
132       -----------
133       -- Round --
134       -----------
135
136       procedure Round (N : Natural) is
137          D : Character;
138
139       begin
140          --  Nothing to do if rounding at or past last digit
141
142          if N >= LD then
143             return;
144
145          --  Cases of rounding before the initial digit
146
147          elsif N < FD then
148
149             --  The result is zero, unless we are rounding just before
150             --  the first digit, and the first digit is five or more.
151
152             if N = 1 and then Digs (Digs'First + 1) >= '5' then
153                Digs (Digs'First) := '1';
154             else
155                Digs (Digs'First) := '0';
156                Zero := True;
157             end if;
158
159             Digits_Before_Point := Digits_Before_Point + 1;
160             FD := 1;
161             LD := 1;
162             ND := 1;
163
164          --  Normal case of rounding an existing digit
165
166          else
167             LD := N;
168             ND := LD - 1;
169
170             if Digs (N + 1) >= '5' then
171                for J in reverse 2 .. N loop
172                   D := Character'Succ (Digs (J));
173
174                   if D <= '9' then
175                      Digs (J) := D;
176                      return;
177                   else
178                      Digs (J) := '0';
179                   end if;
180                end loop;
181
182                --  Here the rounding overflows into the sign position. That's
183                --  OK, because we already captured the value of the sign and
184                --  we are in any case destroying the value in the Digs buffer
185
186                Digs (Digs'First) := '1';
187                FD := 1;
188                ND := ND + 1;
189                Digits_Before_Point := Digits_Before_Point + 1;
190             end if;
191          end if;
192       end Round;
193
194       ---------
195       -- Set --
196       ---------
197
198       procedure Set (C : Character) is
199       begin
200          P := P + 1;
201          S (P) := C;
202       end Set;
203
204       -------------------------
205       -- Set_Blanks_And_Sign --
206       -------------------------
207
208       procedure Set_Blanks_And_Sign (N : Integer) is
209          W : Integer := N;
210
211       begin
212          if Minus then
213             W := W - 1;
214
215             for J in 1 .. W loop
216                Set (' ');
217             end loop;
218
219             Set ('-');
220
221          else
222             for J in 1 .. W loop
223                Set (' ');
224             end loop;
225          end if;
226       end Set_Blanks_And_Sign;
227
228       ----------------
229       -- Set_Digits --
230       ----------------
231
232       procedure Set_Digits (S, E : Natural) is
233       begin
234          for J in S .. E loop
235             Set (Digs (J));
236          end loop;
237       end Set_Digits;
238
239       ----------------
240       -- Set_Zeroes --
241       ----------------
242
243       procedure Set_Zeroes (N : Integer) is
244       begin
245          for J in 1 .. N loop
246             Set ('0');
247          end loop;
248       end Set_Zeroes;
249
250    --  Start of processing for Set_Decimal_Digits
251
252    begin
253       --  Case of exponent given
254
255       if Exp > 0 then
256          Set_Blanks_And_Sign (Fore - 1);
257          Round (Digits_After_Point + 2);
258          Set (Digs (FD));
259          FD := FD + 1;
260          ND := ND - 1;
261          Set ('.');
262
263          if ND >= Digits_After_Point then
264             Set_Digits (FD, FD + Digits_After_Point - 1);
265          else
266             Set_Digits (FD, LD);
267             Set_Zeroes (Digits_After_Point - ND);
268          end if;
269
270          --  Calculate exponent. The number of digits before the decimal point
271          --  in the input is Digits_Before_Point, and the number of digits
272          --  before the decimal point in the output is 1, so we can get the
273          --  exponent as the difference between these two values. The one
274          --  exception is for the value zero, which by convention has an
275          --  exponent of +0.
276
277          if Zero then
278             Expon := 0;
279          else
280             Expon := Digits_Before_Point - 1;
281          end if;
282
283          Set ('E');
284          ND := 0;
285
286          if Expon >= 0 then
287             Set ('+');
288             Set_Image_Integer (Expon, Digs, ND);
289          else
290             Set ('-');
291             Set_Image_Integer (-Expon, Digs, ND);
292          end if;
293
294          Set_Zeroes (Exp - ND - 1);
295          Set_Digits (1, ND);
296          return;
297
298       --  Case of no exponent given. To make these cases clear, we use
299       --  examples. For all the examples, we assume Fore = 2, Aft = 3.
300       --  A P in the example input string is an implied zero position,
301       --  not included in the input string.
302
303       else
304          --  Round at correct position
305          --    Input: 4PP      => unchanged
306          --    Input: 400.03   => unchanged
307          --    Input  3.4567   => 3.457
308          --    Input: 9.9999   => 10.000
309          --    Input: 0.PPP5   => 0.001
310          --    Input: 0.PPP4   => 0
311          --    Input: 0.00003  => 0
312
313          Round (LD - (Scale - Digits_After_Point));
314
315          --  No digits before point in input
316          --    Input: .123   Output: 0.123
317          --    Input: .PP3   Output: 0.003
318
319          if Digits_Before_Point <= 0 then
320             Set_Blanks_And_Sign (Fore - 1);
321             Set ('0');
322             Set ('.');
323             Set_Zeroes (-Digits_Before_Point);
324             Set_Digits (FD, LD);
325             Set_Zeroes (Digits_After_Point - Scale);
326
327          --  At least one digit before point in input
328
329          else
330             --  Less digits in input than are needed before point
331             --    Input: 1PP  Output: 100.000
332
333             if ND < Digits_Before_Point then
334
335                --  Special case, if the input is the single digit 0, then we
336                --  do not want 000.000, but instead 0.000.
337
338                if ND = 1 and then Digs (FD) = '0' then
339                   Set_Blanks_And_Sign (Fore - 1);
340                   Set ('0');
341
342                --  Normal case where we need to output scaling zeroes
343
344                else
345                   Set_Blanks_And_Sign (Fore - Digits_Before_Point);
346                   Set_Digits (FD, LD);
347                   Set_Zeroes (Digits_Before_Point - ND);
348                end if;
349
350                --  Set period and zeroes after the period
351
352                Set ('.');
353                Set_Zeroes (Digits_After_Point);
354
355             --  Input has full amount of digits before decimal point
356
357             else
358                Set_Blanks_And_Sign (Fore - Digits_Before_Point);
359                Set_Digits (FD, FD + Digits_Before_Point - 1);
360                Set ('.');
361                Set_Digits (FD + Digits_Before_Point, LD);
362                Set_Zeroes (Digits_After_Point - (ND - Digits_Before_Point));
363             end if;
364          end if;
365       end if;
366    end Set_Decimal_Digits;
367
368    -----------------------
369    -- Set_Image_Decimal --
370    -----------------------
371
372    procedure Set_Image_Decimal
373      (V     : Integer;
374       S     : in out String;
375       P     : in out Natural;
376       Scale : Integer;
377       Fore  : Natural;
378       Aft   : Natural;
379       Exp   : Natural)
380    is
381       Digs : String := Integer'Image (V);
382       --  Sign and digits of decimal value
383
384    begin
385       Set_Decimal_Digits (Digs, Digs'Length, S, P, Scale, Fore, Aft, Exp);
386    end Set_Image_Decimal;
387
388 end System.Img_Dec;