annotate gcc/ada/libgnat/g-forstr.ads @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . F O R M A T T E D _ S T R I N G --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
9 -- Copyright (C) 2014-2019, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This package add support for formatted string as supported by C printf()
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -- A simple usage is:
kono
parents:
diff changeset
35 --
kono
parents:
diff changeset
36 -- Put_Line (-(+"%s" & "a string"));
kono
parents:
diff changeset
37 --
kono
parents:
diff changeset
38 -- or with a constant for the format:
kono
parents:
diff changeset
39 --
kono
parents:
diff changeset
40 -- declare
kono
parents:
diff changeset
41 -- Format : constant Formatted_String := +"%s";
kono
parents:
diff changeset
42 -- begin
kono
parents:
diff changeset
43 -- Put_Line (-(Format & "a string"));
kono
parents:
diff changeset
44 -- end;
kono
parents:
diff changeset
45 --
kono
parents:
diff changeset
46 -- Finally a more complex example:
kono
parents:
diff changeset
47 --
kono
parents:
diff changeset
48 -- declare
kono
parents:
diff changeset
49 -- F : Formatted_String := +"['%c' ; %10d]";
kono
parents:
diff changeset
50 -- C : Character := 'v';
kono
parents:
diff changeset
51 -- I : Integer := 98;
kono
parents:
diff changeset
52 -- begin
kono
parents:
diff changeset
53 -- F := F & C & I;
kono
parents:
diff changeset
54 -- Put_Line (-F);
kono
parents:
diff changeset
55 -- end;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 -- Which will display:
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 -- ['v' ; 98]
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 -- Each format specifier is: %[flags][width][.precision][length]specifier
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 -- Specifiers:
kono
parents:
diff changeset
64 -- d or i Signed decimal integer
kono
parents:
diff changeset
65 -- u Unsigned decimal integer
kono
parents:
diff changeset
66 -- o Unsigned octal
kono
parents:
diff changeset
67 -- x Unsigned hexadecimal integer
kono
parents:
diff changeset
68 -- X Unsigned hexadecimal integer (uppercase)
kono
parents:
diff changeset
69 -- f Decimal floating point, lowercase
kono
parents:
diff changeset
70 -- F Decimal floating point, uppercase
kono
parents:
diff changeset
71 -- e Scientific notation (mantissa/exponent), lowercase
kono
parents:
diff changeset
72 -- E Scientific notation (mantissa/exponent), uppercase
kono
parents:
diff changeset
73 -- g Use the shortest representation: %e or %f
kono
parents:
diff changeset
74 -- G Use the shortest representation: %E or %F
kono
parents:
diff changeset
75 -- c Character
kono
parents:
diff changeset
76 -- s String of characters
kono
parents:
diff changeset
77 -- p Pointer address
kono
parents:
diff changeset
78 -- % A % followed by another % character will write a single %
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 -- Flags:
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 -- - Left-justify within the given field width;
kono
parents:
diff changeset
83 -- Right justification is the default.
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 -- + Forces to preceed the result with a plus or minus sign (+ or -)
kono
parents:
diff changeset
86 -- even for positive numbers. By default, only negative numbers
kono
parents:
diff changeset
87 -- are preceded with a - sign.
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 -- (space) If no sign is going to be written, a blank space is inserted
kono
parents:
diff changeset
90 -- before the value.
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 -- # Used with o, x or X specifiers the value is preceeded with
kono
parents:
diff changeset
93 -- 0, 0x or 0X respectively for values different than zero.
kono
parents:
diff changeset
94 -- Used with a, A, e, E, f, F, g or G it forces the written
kono
parents:
diff changeset
95 -- output to contain a decimal point even if no more digits
kono
parents:
diff changeset
96 -- follow. By default, if no digits follow, no decimal point is
kono
parents:
diff changeset
97 -- written.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 -- ~ As above, but using Ada style based <base>#<number>#
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 -- 0 Left-pads the number with zeroes (0) instead of spaces when
kono
parents:
diff changeset
102 -- padding is specified.
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 -- Width:
kono
parents:
diff changeset
105 -- number Minimum number of characters to be printed. If the value to
kono
parents:
diff changeset
106 -- be printed is shorter than this number, the result is padded
kono
parents:
diff changeset
107 -- with blank spaces. The value is not truncated even if the
kono
parents:
diff changeset
108 -- result is larger.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 -- * The width is not specified in the format string, but as an
kono
parents:
diff changeset
111 -- additional integer value argument preceding the argument that
kono
parents:
diff changeset
112 -- has to be formatted.
kono
parents:
diff changeset
113 -- Precision:
kono
parents:
diff changeset
114 -- number For integer specifiers (d, i, o, u, x, X): precision specifies
kono
parents:
diff changeset
115 -- the minimum number of digits to be written. If the value to be
kono
parents:
diff changeset
116 -- written is shorter than this number, the result is padded with
kono
parents:
diff changeset
117 -- leading zeros. The value is not truncated even if the result
kono
parents:
diff changeset
118 -- is longer. A precision of 0 means that no character is written
kono
parents:
diff changeset
119 -- for the value 0.
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 -- For e, E, f and F specifiers: this is the number of digits to
kono
parents:
diff changeset
122 -- be printed after the decimal point (by default, this is 6).
kono
parents:
diff changeset
123 -- For g and G specifiers: This is the maximum number of
kono
parents:
diff changeset
124 -- significant digits to be printed.
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 -- For s: this is the maximum number of characters to be printed.
kono
parents:
diff changeset
127 -- By default all characters are printed until the ending null
kono
parents:
diff changeset
128 -- character is encountered.
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 -- If the period is specified without an explicit value for
kono
parents:
diff changeset
131 -- precision, 0 is assumed.
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 -- .* The precision is not specified in the format string, but as an
kono
parents:
diff changeset
134 -- additional integer value argument preceding the argument that
kono
parents:
diff changeset
135 -- has to be formatted.
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 with Ada.Text_IO;
kono
parents:
diff changeset
138 with System;
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 private with Ada.Finalization;
kono
parents:
diff changeset
141 private with Ada.Strings.Unbounded;
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 package GNAT.Formatted_String is
kono
parents:
diff changeset
144 use Ada;
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 type Formatted_String (<>) is private;
kono
parents:
diff changeset
147 -- A format string as defined for printf routine. This string is the
kono
parents:
diff changeset
148 -- actual format for all the parameters added with the "&" routines below.
kono
parents:
diff changeset
149 -- Note that a Formatted_String object can't be reused as it serves as
kono
parents:
diff changeset
150 -- recipient for the final result. That is, each use of "&" will build
kono
parents:
diff changeset
151 -- incrementally the final result string which can be retrieved with
kono
parents:
diff changeset
152 -- the "-" routine below.
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 Format_Error : exception;
kono
parents:
diff changeset
155 -- Raised for every mismatch between the parameter and the expected format
kono
parents:
diff changeset
156 -- and for malformed format.
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 function "+" (Format : String) return Formatted_String;
kono
parents:
diff changeset
159 -- Create the format string
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 function "-" (Format : Formatted_String) return String;
kono
parents:
diff changeset
162 -- Get the result of the formatted string corresponding to the current
kono
parents:
diff changeset
163 -- rendering (up to the last parameter formated).
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 function "&"
kono
parents:
diff changeset
166 (Format : Formatted_String;
kono
parents:
diff changeset
167 Var : Character) return Formatted_String;
kono
parents:
diff changeset
168 -- A character, expect a %c
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 function "&"
kono
parents:
diff changeset
171 (Format : Formatted_String;
kono
parents:
diff changeset
172 Var : String) return Formatted_String;
kono
parents:
diff changeset
173 -- A string, expect a %s
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 function "&"
kono
parents:
diff changeset
176 (Format : Formatted_String;
kono
parents:
diff changeset
177 Var : Boolean) return Formatted_String;
kono
parents:
diff changeset
178 -- A boolean image, expect a %s
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 function "&"
kono
parents:
diff changeset
181 (Format : Formatted_String;
kono
parents:
diff changeset
182 Var : Integer) return Formatted_String;
kono
parents:
diff changeset
183 -- An integer, expect a %d, %o, %x, %X
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 function "&"
kono
parents:
diff changeset
186 (Format : Formatted_String;
kono
parents:
diff changeset
187 Var : Long_Integer) return Formatted_String;
kono
parents:
diff changeset
188 -- As above
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 function "&"
kono
parents:
diff changeset
191 (Format : Formatted_String;
kono
parents:
diff changeset
192 Var : System.Address) return Formatted_String;
kono
parents:
diff changeset
193 -- An address, expect a %p
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 function "&"
kono
parents:
diff changeset
196 (Format : Formatted_String;
kono
parents:
diff changeset
197 Var : Float) return Formatted_String;
kono
parents:
diff changeset
198 -- A float, expect %f, %e, %F, %E, %g, %G
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 function "&"
kono
parents:
diff changeset
201 (Format : Formatted_String;
kono
parents:
diff changeset
202 Var : Long_Float) return Formatted_String;
kono
parents:
diff changeset
203 -- As above
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 function "&"
kono
parents:
diff changeset
206 (Format : Formatted_String;
kono
parents:
diff changeset
207 Var : Duration) return Formatted_String;
kono
parents:
diff changeset
208 -- As above
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 -- Some generics
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 generic
kono
parents:
diff changeset
213 type Int is range <>;
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 with procedure Put
kono
parents:
diff changeset
216 (To : out String;
kono
parents:
diff changeset
217 Item : Int;
kono
parents:
diff changeset
218 Base : Text_IO.Number_Base);
kono
parents:
diff changeset
219 function Int_Format
kono
parents:
diff changeset
220 (Format : Formatted_String;
kono
parents:
diff changeset
221 Var : Int) return Formatted_String;
kono
parents:
diff changeset
222 -- As for Integer above
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 generic
kono
parents:
diff changeset
225 type Int is mod <>;
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 with procedure Put
kono
parents:
diff changeset
228 (To : out String;
kono
parents:
diff changeset
229 Item : Int;
kono
parents:
diff changeset
230 Base : Text_IO.Number_Base);
kono
parents:
diff changeset
231 function Mod_Format
kono
parents:
diff changeset
232 (Format : Formatted_String;
kono
parents:
diff changeset
233 Var : Int) return Formatted_String;
kono
parents:
diff changeset
234 -- As for Integer above
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 generic
kono
parents:
diff changeset
237 type Flt is digits <>;
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 with procedure Put
kono
parents:
diff changeset
240 (To : out String;
kono
parents:
diff changeset
241 Item : Flt;
kono
parents:
diff changeset
242 Aft : Text_IO.Field;
kono
parents:
diff changeset
243 Exp : Text_IO.Field);
kono
parents:
diff changeset
244 function Flt_Format
kono
parents:
diff changeset
245 (Format : Formatted_String;
kono
parents:
diff changeset
246 Var : Flt) return Formatted_String;
kono
parents:
diff changeset
247 -- As for Float above
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 generic
kono
parents:
diff changeset
250 type Flt is delta <>;
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 with procedure Put
kono
parents:
diff changeset
253 (To : out String;
kono
parents:
diff changeset
254 Item : Flt;
kono
parents:
diff changeset
255 Aft : Text_IO.Field;
kono
parents:
diff changeset
256 Exp : Text_IO.Field);
kono
parents:
diff changeset
257 function Fixed_Format
kono
parents:
diff changeset
258 (Format : Formatted_String;
kono
parents:
diff changeset
259 Var : Flt) return Formatted_String;
kono
parents:
diff changeset
260 -- As for Float above
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 generic
kono
parents:
diff changeset
263 type Flt is delta <> digits <>;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 with procedure Put
kono
parents:
diff changeset
266 (To : out String;
kono
parents:
diff changeset
267 Item : Flt;
kono
parents:
diff changeset
268 Aft : Text_IO.Field;
kono
parents:
diff changeset
269 Exp : Text_IO.Field);
kono
parents:
diff changeset
270 function Decimal_Format
kono
parents:
diff changeset
271 (Format : Formatted_String;
kono
parents:
diff changeset
272 Var : Flt) return Formatted_String;
kono
parents:
diff changeset
273 -- As for Float above
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 generic
kono
parents:
diff changeset
276 type Enum is (<>);
kono
parents:
diff changeset
277 function Enum_Format
kono
parents:
diff changeset
278 (Format : Formatted_String;
kono
parents:
diff changeset
279 Var : Enum) return Formatted_String;
kono
parents:
diff changeset
280 -- As for String above, output the string representation of the enumeration
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 private
kono
parents:
diff changeset
283 use Ada.Strings.Unbounded;
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 type I_Vars is array (Positive range 1 .. 2) of Integer;
kono
parents:
diff changeset
286 -- Used to keep 2 numbers for the possible * for the width and precision
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 type Data (Size : Natural) is record
kono
parents:
diff changeset
289 Ref_Count : Natural := 1;
kono
parents:
diff changeset
290 Index : Positive := 1; -- format index for next value
kono
parents:
diff changeset
291 Result : Unbounded_String; -- current value
kono
parents:
diff changeset
292 Current : Natural; -- the current format number
kono
parents:
diff changeset
293 Stored_Value : Natural := 0; -- number of stored values in Stack
kono
parents:
diff changeset
294 Stack : I_Vars;
kono
parents:
diff changeset
295 Format : String (1 .. Size); -- the format string
kono
parents:
diff changeset
296 end record;
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 type Data_Access is access Data;
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 -- The formatted string record is controlled and do not need an initialize
kono
parents:
diff changeset
301 -- as it requires an explit initial value. This is given with "+" and
kono
parents:
diff changeset
302 -- properly initialize the record at this point.
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 type Formatted_String is new Finalization.Controlled with record
kono
parents:
diff changeset
305 D : Data_Access;
kono
parents:
diff changeset
306 end record;
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 overriding procedure Adjust (F : in out Formatted_String);
kono
parents:
diff changeset
309 overriding procedure Finalize (F : in out Formatted_String);
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 end GNAT.Formatted_String;