1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . F O R M A T T E D _ S T R I N G --
9 -- Copyright (C) 2014, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package add support for formatted string as supported by C printf().
37 -- F : Formatted_String := +"['%c' ; %10d]";
38 -- C : Character := 'v';
45 -- Which will display:
49 -- Each format specifier is: %[flags][width][.precision][length]specifier
52 -- d or i Signed decimal integer
53 -- u Unsigned decimal integer
55 -- x Unsigned hexadecimal integer
56 -- X Unsigned hexadecimal integer (uppercase)
57 -- f Decimal floating point, lowercase
58 -- F Decimal floating point, uppercase
59 -- e Scientific notation (mantissa/exponent), lowercase
60 -- E Scientific notation (mantissa/exponent), uppercase
61 -- g Use the shortest representation: %e or %f
62 -- G Use the shortest representation: %E or %F
64 -- s String of characters
66 -- % A % followed by another % character will write a single %
70 -- - Left-justify within the given field width;
71 -- Right justification is the default.
73 -- + Forces to preceed the result with a plus or minus sign (+ or -)
74 -- even for positive numbers. By default, only negative numbers
75 -- are preceded with a - sign.
77 -- (space) If no sign is going to be written, a blank space is inserted
80 -- # Used with o, x or X specifiers the value is preceeded with
81 -- 0, 0x or 0X respectively for values different than zero.
82 -- Used with a, A, e, E, f, F, g or G it forces the written
83 -- output to contain a decimal point even if no more digits
84 -- follow. By default, if no digits follow, no decimal point is
87 -- ~ As above, but using Ada style based <base>#<number>#
89 -- 0 Left-pads the number with zeroes (0) instead of spaces when
90 -- padding is specified.
93 -- number Minimum number of characters to be printed. If the value to
94 -- be printed is shorter than this number, the result is padded
95 -- with blank spaces. The value is not truncated even if the
98 -- * The width is not specified in the format string, but as an
99 -- additional integer value argument preceding the argument that
100 -- has to be formatted.
102 -- number For integer specifiers (d, i, o, u, x, X): precision specifies
103 -- the minimum number of digits to be written. If the value to be
104 -- written is shorter than this number, the result is padded with
105 -- leading zeros. The value is not truncated even if the result
106 -- is longer. A precision of 0 means that no character is written
109 -- For e, E, f and F specifiers: this is the number of digits to
110 -- be printed after the decimal point (by default, this is 6).
111 -- For g and G specifiers: This is the maximum number of
112 -- significant digits to be printed.
114 -- For s: this is the maximum number of characters to be printed.
115 -- By default all characters are printed until the ending null
116 -- character is encountered.
118 -- If the period is specified without an explicit value for
119 -- precision, 0 is assumed.
121 -- .* The precision is not specified in the format string, but as an
122 -- additional integer value argument preceding the argument that
123 -- has to be formatted.
128 private with Ada
.Finalization
;
129 private with Ada
.Strings
.Unbounded
;
131 package GNAT
.Formatted_String
is
134 type Formatted_String
(<>) is private;
135 -- A format string as defined for printf routine
137 Format_Error
: exception;
138 -- Raised for every mismatch between the parameter and the expected format
139 -- and for malformed format.
141 function "+" (Format
: String) return Formatted_String
;
142 -- Create the format string
144 function "-" (Format
: Formatted_String
) return String;
145 -- Get the result of the formatted string corresponding to the current
146 -- rendering (up to the last parameter formated).
149 (Format
: Formatted_String
;
150 Var
: Character) return Formatted_String
;
151 -- A character, expect a %c
154 (Format
: Formatted_String
;
155 Var
: String) return Formatted_String
;
156 -- A string, expect a %s
159 (Format
: Formatted_String
;
160 Var
: Boolean) return Formatted_String
;
161 -- A boolean image, expect a %s
164 (Format
: Formatted_String
;
165 Var
: Integer) return Formatted_String
;
166 -- An integer, expect a %d, %o, %x, %X
169 (Format
: Formatted_String
;
170 Var
: Long_Integer) return Formatted_String
;
174 (Format
: Formatted_String
;
175 Var
: System
.Address
) return Formatted_String
;
176 -- An address, expect a %p
179 (Format
: Formatted_String
;
180 Var
: Float) return Formatted_String
;
181 -- A float, expect %f, %e, %F, %E, %g, %G
184 (Format
: Formatted_String
;
185 Var
: Long_Float) return Formatted_String
;
189 (Format
: Formatted_String
;
190 Var
: Duration) return Formatted_String
;
196 type Int
is range <>;
201 Base
: Text_IO
.Number_Base
);
203 (Format
: Formatted_String
;
204 Var
: Int
) return Formatted_String
;
205 -- As for Integer above
213 Base
: Text_IO
.Number_Base
);
215 (Format
: Formatted_String
;
216 Var
: Int
) return Formatted_String
;
217 -- As for Integer above
220 type Flt
is digits <>;
226 Exp
: Text_IO
.Field
);
228 (Format
: Formatted_String
;
229 Var
: Flt
) return Formatted_String
;
230 -- As for Float above
233 type Flt
is delta <>;
239 Exp
: Text_IO
.Field
);
240 function Fixed_Format
241 (Format
: Formatted_String
;
242 Var
: Flt
) return Formatted_String
;
243 -- As for Float above
246 type Flt
is delta <> digits <>;
252 Exp
: Text_IO
.Field
);
253 function Decimal_Format
254 (Format
: Formatted_String
;
255 Var
: Flt
) return Formatted_String
;
256 -- As for Float above
261 (Format
: Formatted_String
;
262 Var
: Enum
) return Formatted_String
;
263 -- As for String above, output the string representation of the enumeration
266 use Ada
.Strings
.Unbounded
;
268 type I_Vars
is array (Positive range 1 .. 2) of Integer;
269 -- Used to keep 2 numbers for the possible * for the width and precision
271 type Data
(Size
: Natural) is record
272 Ref_Count
: Natural := 1;
273 Format
: String (1 .. Size
); -- the format string
274 Index
: Positive := 1; -- format index for next value
275 Result
: Unbounded_String
; -- current value
276 Current
: Natural; -- the current format number
277 Stored_Value
: Natural := 0; -- number of stored values in Stack
281 type Data_Access
is access Data
;
283 -- The formatted string record is controlled and do not need an initialize
284 -- as it requires an explit initial value. This is given with "+" and
285 -- properly initialize the record at this point.
287 type Formatted_String
is new Finalization
.Controlled
with record
291 overriding
procedure Adjust
(F
: in out Formatted_String
);
292 overriding
procedure Finalize
(F
: in out Formatted_String
);
294 end GNAT
.Formatted_String
;