Debian Codegraph @

gcc/ada/libgnat/g-forstr.ads

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