Debian Codegraph @

gcc/ada/libgnat/s-imagew.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . I M A G E _ W --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 ------------------------------------------------------------------------------
31
32 package body System.Image_W is
33
34 -----------------------------
35 -- Set_Image_Width_Integer --
36 -----------------------------
37
38 procedure Set_Image_Width_Integer
39 (V : Int;
40 W : Integer;
41 S : out String;
42 P : in out Natural)
43 is
44 Start : Natural;
45
46 begin
47 -- Positive case can just use the unsigned circuit directly
48
49 if V >= 0 then
50 Set_Image_Width_Unsigned (Uns (V), W, S, P);
51
52 -- Negative case has to set a minus sign. Note also that we have to be
53 -- careful not to generate overflow with the largest negative number.
54
55 else
56 P := P + 1;
57 S (P) := ' ';
58 Start := P;
59
60 declare
61 pragma Suppress (Overflow_Check);
62 pragma Suppress (Range_Check);
63 begin
64 Set_Image_Width_Unsigned (Uns (-V), W - 1, S, P);
65 end;
66
67 -- Set minus sign in last leading blank location. Because of the
68 -- code above, there must be at least one such location.
69
70 while S (Start + 1) = ' ' loop
71 Start := Start + 1;
72 end loop;
73
74 S (Start) := '-';
75 end if;
76
77 end Set_Image_Width_Integer;
78
79 ------------------------------
80 -- Set_Image_Width_Unsigned --
81 ------------------------------
82
83 procedure Set_Image_Width_Unsigned
84 (V : Uns;
85 W : Integer;
86 S : out String;
87 P : in out Natural)
88 is
89 Start : constant Natural := P + 1;
90 Nb_Digits : Natural := 1;
91 T : Uns := V;
92
93 begin
94
95 -- First we compute the number of characters needed for representing
96 -- the number.
97 loop
98 T := T / 10;
99 exit when T = 0;
100 Nb_Digits := Nb_Digits + 1;
101 end loop;
102
103 P := Start;
104
105 -- Pad S with spaces up to W reduced by Nb_Digits
106 while P < Start + W - Nb_Digits loop
107 S (P) := ' ';
108 P := P + 1;
109 end loop;
110
111 -- We now populate digits from the end of the value to the beginning
112 T := V;
113 for J in reverse P .. P + Nb_Digits - 1 loop
114 S (J) := Character'Val (T mod 10 + Character'Pos ('0'));
115 T := T / 10;
116 end loop;
117
118 P := P + Nb_Digits - 1;
119
120 end Set_Image_Width_Unsigned;
121
122 end System.Image_W;