1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright © 2026 Mikhail Hogrefe
//
// Uses code adopted from the GNU MP Library.
//
// Copyright © 1993-2019 Free Software Foundation, Inc.
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crateNatural;
use String;
use Vec;
use max;
use fail_on_untested_path;
use ToStringBase;
use ;
// Appends the formatted value, the sign of which is `neg` and the absolute value of which is `num`,
// or `num / den` when `den` is present, to `out` according to `spec`. Returns `None` when the total
// width would exceed `i64::MAX`, or would not fit in a `usize`.
//
// This is `__gmp_doprnt_integer` from `printf/doprnti.c`, GMP 6.3.0, together with the
// `mpz_get_str` and `mpq_get_str` calls from the `Z` and `Q` argument cases of `__gmp_doprnt`. As
// there, the digit string of a rational is the numerator's and denominator's digits joined by a
// slash, the field width, precision, and numerator base prefix treat it as a unit, and a `#` prefix
// is inserted separately after the slash. (GMP documents the influence of the precision on a
// rational as undefined; this port reproduces what its code does, padding the numerator so that the
// whole string reaches the precision.)
// Formats a value with the sign `neg` and absolute value `num`, or `num / den` when `den` is
// present, for a single parsed conversion, which must be an integer conversion (`d`, `i`, `u`, `o`,
// `x`, or `X`); the caller is responsible for having checked the specification's type character.
// This function is public so that other Malachite crates can call it; it is not part of the public
// API.
//
// This is `__gmp_doprnt_integer` from `printf/doprnti.c`, GMP 6.3.0, together with the
// `mpz_get_str` and `mpq_get_str` calls from the `Z` and `Q` argument cases of `__gmp_doprnt`.
// Formats a value with the sign `neg` and absolute value `num`, or `num / den` when `den` is
// present, according to a GMP-style format string containing a single `%Z` (or `%Q`, per
// `type_chr`) conversion; the engine behind [`format_natural_str`], `format_integer_str`, and
// malachite-q's `format_rational_str`. This function is public so that other Malachite crates can
// call it; it is not part of the public API.
//
// This is `__gmp_doprnt` from `printf/doprnt.c`, GMP 6.3.0, restricted to a format string whose
// only conversion is a `%Z` or `%Q` integer conversion.
/// Formats a [`Natural`] according to a GMP-style `printf` format string, for strict compatibility
/// with GMP's `gmp_printf` family.
///
/// The format string should contain a single conversion consuming the [`Natural`], written
/// `%[flags][width][.precision]Z[conv]`, with any surrounding literal text (a literal `%` is
/// written `%%`). The pieces are:
/// - **flags**: any of `-` (left-justify within the field), `+` (always show a sign), space (show a
/// space before the value), `#` (alternate form: prefix hexadecimal output with `0x` or `0X` and
/// octal output with `0`, unless the digits already begin with a zero), and `0` (pad the field
/// with leading zeros). The `'` flag is accepted but, as in GMP, has no effect on GMP types.
/// - **width**: the minimum field width, as a decimal integer.
/// - **precision**: following a `.`, the minimum number of digits, the value being padded with
/// leading zeros to reach it; a zero value formatted with a precision of 0 produces no digits at
/// all. By default all necessary digits are printed.
/// - **`Z`**: marks the argument as a multiple-precision integer (GMP's type character).
/// - **conv**: the conversion — `d`, `i`, or `u` (decimal; for a [`Natural`] all three are the
/// same), `o` (octal), or `x`/`X` (lowercase/uppercase hexadecimal).
///
/// Returns [`None`] when the format string is not a single well-formed `%Z` integer conversion: for
/// instance if it uses `*` for the width or precision (which would need an integer argument that
/// this single-value entry point does not supply), contains no `%Z` conversion or more than one,
/// contains a conversion of any other type, or requests a width or precision beyond `i32::MAX` (the
/// range of the C `int` GMP itself stores them in).
///
/// # Worst-case complexity
/// $T(n) = O(n (\log n)^2 \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(), p, w)`, with
/// `p` and `w` the precision and field width requested by the format string.
///
/// # Examples
/// ```
/// use malachite_nz::natural::Natural;
/// use malachite_nz::natural::conversion::string::format_natural::format_natural_str;
///
/// let x = Natural::from(255u32);
/// assert_eq!(format_natural_str(&x, "%Zd"), Some("255".to_string()));
/// assert_eq!(format_natural_str(&x, "%#Zx"), Some("0xff".to_string()));
/// assert_eq!(format_natural_str(&x, "%#ZX"), Some("0XFF".to_string()));
/// assert_eq!(format_natural_str(&x, "%#Zo"), Some("0377".to_string()));
/// assert_eq!(format_natural_str(&x, "%8Zd"), Some(" 255".to_string()));
/// assert_eq!(
/// format_natural_str(&x, "%-8Zd|"),
/// Some("255 |".to_string())
/// );
/// assert_eq!(
/// format_natural_str(&x, "%08Zd"),
/// Some("00000255".to_string())
/// );
/// assert_eq!(format_natural_str(&x, "%.6Zd"), Some("000255".to_string()));
/// assert_eq!(format_natural_str(&x, "%+Zd"), Some("+255".to_string()));
/// assert_eq!(
/// format_natural_str(&x, "x = %Zd!"),
/// Some("x = 255!".to_string())
/// );
/// assert_eq!(
/// format_natural_str(&x, "100%% of %Zd"),
/// Some("100% of 255".to_string())
/// );
///
/// // a zero value with an explicit precision of 0 produces no digits
/// assert_eq!(
/// format_natural_str(&Natural::from(0u32), "%.0Zd"),
/// Some("".to_string())
/// );
///
/// // invalid or unsupported format strings
/// assert_eq!(format_natural_str(&x, "%d"), None);
/// assert_eq!(format_natural_str(&x, "%*Zd"), None);
/// assert_eq!(format_natural_str(&x, "%Zd %Zd"), None);
/// ```
///
/// This is `gmp_snprintf` from `printf/snprintf.c`, GMP 6.3.0, where the format string contains a
/// single `%Z` integer conversion and the buffer is always large enough.