1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // iostream.cfa --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Wed May 27 17:56:53 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Sun Jul 30 23:01:00 2023
|
---|
13 | // Update Count : 1406
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "iostream.hfa"
|
---|
17 |
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <stdbool.h> // true/false
|
---|
20 | #include <stdint.h> // UINT64_MAX
|
---|
21 | #include <float.h> // DBL_DIG, LDBL_DIG
|
---|
22 | #include <complex.h> // creal, cimag
|
---|
23 | //#include <string.h> // strlen, strcmp, memcpy
|
---|
24 | extern "C" {
|
---|
25 | extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
|
---|
26 | extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
|
---|
27 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
|
---|
28 | extern void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
|
---|
29 | } // extern "C"
|
---|
30 |
|
---|
31 | #include "math.hfa" // isfinite, floor, ceiling_div
|
---|
32 | #include "bitmanip.hfa" // high1
|
---|
33 |
|
---|
34 | #pragma GCC visibility push(default)
|
---|
35 |
|
---|
36 |
|
---|
37 | // *********************************** ostream ***********************************
|
---|
38 |
|
---|
39 |
|
---|
40 | forall( ostype & | basic_ostream( ostype ) ) {
|
---|
41 | ostype & ?|?( ostype & os, bool b ) {
|
---|
42 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
43 | fmt( os, "%s", b ? "true" : "false" );
|
---|
44 | return os;
|
---|
45 | } // ?|?
|
---|
46 | OSTYPE_VOID_IMPL( bool )
|
---|
47 |
|
---|
48 | ostype & ?|?( ostype & os, char c ) {
|
---|
49 | fmt( os, "%c", c );
|
---|
50 | if ( c == '\n' ) setNL$( os, true );
|
---|
51 | return nosep( os );
|
---|
52 | } // ?|?
|
---|
53 | OSTYPE_VOID_IMPL( char )
|
---|
54 |
|
---|
55 | ostype & ?|?( ostype & os, signed char sc ) {
|
---|
56 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
57 | fmt( os, "%'hhd", sc );
|
---|
58 | return os;
|
---|
59 | } // ?|?
|
---|
60 | OSTYPE_VOID_IMPL( signed char )
|
---|
61 |
|
---|
62 | ostype & ?|?( ostype & os, unsigned char usc ) {
|
---|
63 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
64 | fmt( os, "%'hhu", usc );
|
---|
65 | return os;
|
---|
66 | } // ?|?
|
---|
67 | OSTYPE_VOID_IMPL( unsigned char )
|
---|
68 |
|
---|
69 | ostype & ?|?( ostype & os, short int si ) {
|
---|
70 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
71 | fmt( os, "%'hd", si );
|
---|
72 | return os;
|
---|
73 | } // ?|?
|
---|
74 | OSTYPE_VOID_IMPL( short int )
|
---|
75 |
|
---|
76 | ostype & ?|?( ostype & os, unsigned short int usi ) {
|
---|
77 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
78 | fmt( os, "%'hu", usi );
|
---|
79 | return os;
|
---|
80 | } // ?|?
|
---|
81 | OSTYPE_VOID_IMPL( unsigned short int )
|
---|
82 |
|
---|
83 | ostype & ?|?( ostype & os, int i ) {
|
---|
84 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
85 | fmt( os, "%'d", i );
|
---|
86 | return os;
|
---|
87 | } // ?|?
|
---|
88 | OSTYPE_VOID_IMPL( int )
|
---|
89 |
|
---|
90 | ostype & ?|?( ostype & os, unsigned int ui ) {
|
---|
91 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
92 | fmt( os, "%'u", ui );
|
---|
93 | return os;
|
---|
94 | } // ?|?
|
---|
95 | OSTYPE_VOID_IMPL( unsigned int )
|
---|
96 |
|
---|
97 | ostype & ?|?( ostype & os, long int li ) {
|
---|
98 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
99 | fmt( os, "%'ld", li );
|
---|
100 | return os;
|
---|
101 | } // ?|?
|
---|
102 | OSTYPE_VOID_IMPL( long int )
|
---|
103 |
|
---|
104 | ostype & ?|?( ostype & os, unsigned long int uli ) {
|
---|
105 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
106 | fmt( os, "%'lu", uli );
|
---|
107 | return os;
|
---|
108 | } // ?|?
|
---|
109 | OSTYPE_VOID_IMPL( unsigned long int )
|
---|
110 |
|
---|
111 | ostype & ?|?( ostype & os, long long int lli ) {
|
---|
112 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
113 | fmt( os, "%'lld", lli );
|
---|
114 | return os;
|
---|
115 | } // ?|?
|
---|
116 | OSTYPE_VOID_IMPL( long long int )
|
---|
117 |
|
---|
118 | ostype & ?|?( ostype & os, unsigned long long int ulli ) {
|
---|
119 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
120 | fmt( os, "%'llu", ulli );
|
---|
121 | return os;
|
---|
122 | } // ?|?
|
---|
123 | OSTYPE_VOID_IMPL( unsigned long long int )
|
---|
124 |
|
---|
125 | #if defined( __SIZEOF_INT128__ )
|
---|
126 | // UINT64_MAX 18_446_744_073_709_551_615_ULL
|
---|
127 | #define P10_UINT64 10_000_000_000_000_000_000_ULL // 19 zeroes
|
---|
128 |
|
---|
129 | static inline void base10_128( ostype & os, unsigned int128 val ) {
|
---|
130 | #if defined(__GNUC__) && __GNUC_PREREQ(7,0) // gcc version >= 7
|
---|
131 | if ( val > P10_UINT64 ) {
|
---|
132 | #else
|
---|
133 | if ( (uint64_t)(val >> 64) != 0 || (uint64_t)val > P10_UINT64 ) { // patch gcc 5 & 6 -O3 bug
|
---|
134 | #endif // __GNUC_PREREQ(7,0)
|
---|
135 | base10_128( os, val / P10_UINT64 ); // recursive
|
---|
136 | fmt( os, "%.19lu", (uint64_t)(val % P10_UINT64) );
|
---|
137 | } else {
|
---|
138 | fmt( os, "%lu", (uint64_t)val );
|
---|
139 | } // if
|
---|
140 | } // base10_128
|
---|
141 |
|
---|
142 | static inline void base10_128( ostype & os, int128 val ) {
|
---|
143 | if ( val < 0 ) {
|
---|
144 | fmt( os, "-" ); // leading negative sign
|
---|
145 | val = -val;
|
---|
146 | } // if
|
---|
147 | base10_128( os, (unsigned int128)val ); // print zero/positive value
|
---|
148 | } // base10_128
|
---|
149 |
|
---|
150 | ostype & ?|?( ostype & os, int128 llli ) {
|
---|
151 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
152 | base10_128( os, llli );
|
---|
153 | return os;
|
---|
154 | } // ?|?
|
---|
155 | OSTYPE_VOID_IMPL( int128 )
|
---|
156 |
|
---|
157 | ostype & ?|?( ostype & os, unsigned int128 ullli ) {
|
---|
158 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
159 | base10_128( os, ullli );
|
---|
160 | return os;
|
---|
161 | } // ?|?
|
---|
162 | OSTYPE_VOID_IMPL( unsigned int128 )
|
---|
163 | #endif // __SIZEOF_INT128__
|
---|
164 |
|
---|
165 | #define PRINT_WITH_DP( os, format, val, ... ) \
|
---|
166 | { \
|
---|
167 | enum { size = 48 }; \
|
---|
168 | char buf[size]; \
|
---|
169 | int len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
|
---|
170 | fmt( os, "%s", buf ); \
|
---|
171 | if ( isfinite( val ) ) { /* if number, print decimal point when no fraction or exponent */ \
|
---|
172 | for ( i; 0 ~ @ ) { \
|
---|
173 | if ( i == len ) { fmt( os, "." ); break; } \
|
---|
174 | if ( buf[i] == '.' || buf[i] == 'e' || buf[i] == 'E' ) break; /* decimal point or scientific ? */ \
|
---|
175 | } /* for */ \
|
---|
176 | } /* if */ \
|
---|
177 | }
|
---|
178 |
|
---|
179 | ostype & ?|?( ostype & os, float f ) {
|
---|
180 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
181 | PRINT_WITH_DP( os, "%'g", f );
|
---|
182 | return os;
|
---|
183 | } // ?|?
|
---|
184 | OSTYPE_VOID_IMPL( float )
|
---|
185 |
|
---|
186 | ostype & ?|?( ostype & os, double d ) {
|
---|
187 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
188 | PRINT_WITH_DP( os, "%'.*lg", d, DBL_DIG );
|
---|
189 | return os;
|
---|
190 | } // ?|?
|
---|
191 | OSTYPE_VOID_IMPL( double )
|
---|
192 |
|
---|
193 | ostype & ?|?( ostype & os, long double ld ) {
|
---|
194 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
195 | PRINT_WITH_DP( os, "%'.*Lg", ld, LDBL_DIG );
|
---|
196 | return os;
|
---|
197 | } // ?|?
|
---|
198 | OSTYPE_VOID_IMPL( long double )
|
---|
199 |
|
---|
200 | ostype & ?|?( ostype & os, float _Complex fc ) {
|
---|
201 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
202 | // os | crealf( fc ) | nonl;
|
---|
203 | PRINT_WITH_DP( os, "%'g", crealf( fc ) );
|
---|
204 | PRINT_WITH_DP( os, "%'+g", cimagf( fc ) );
|
---|
205 | fmt( os, "i" );
|
---|
206 | return os;
|
---|
207 | } // ?|?
|
---|
208 | OSTYPE_VOID_IMPL( float _Complex )
|
---|
209 |
|
---|
210 | ostype & ?|?( ostype & os, double _Complex dc ) {
|
---|
211 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
212 | // os | creal( dc ) | nonl;
|
---|
213 | PRINT_WITH_DP( os, "%'.*lg", creal( dc ), DBL_DIG );
|
---|
214 | PRINT_WITH_DP( os, "%'+.*lg", cimag( dc ), DBL_DIG );
|
---|
215 | fmt( os, "i" );
|
---|
216 | return os;
|
---|
217 | } // ?|?
|
---|
218 | OSTYPE_VOID_IMPL( double _Complex )
|
---|
219 |
|
---|
220 | ostype & ?|?( ostype & os, long double _Complex ldc ) {
|
---|
221 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
222 | // os | creall( ldc ) || nonl;
|
---|
223 | PRINT_WITH_DP( os, "%'.*Lg", creall( ldc ), LDBL_DIG );
|
---|
224 | PRINT_WITH_DP( os, "%'+.*Lg", cimagl( ldc ), LDBL_DIG );
|
---|
225 | fmt( os, "i" );
|
---|
226 | return os;
|
---|
227 | } // ?|?
|
---|
228 | OSTYPE_VOID_IMPL( long double _Complex )
|
---|
229 |
|
---|
230 | ostype & ?|?( ostype & os, const char s[] ) {
|
---|
231 | enum { Open = 1, Close, OpenClose };
|
---|
232 | static const unsigned char mask[256] @= { // 256 covers all Latin-1 characters
|
---|
233 | // opening delimiters, no space after
|
---|
234 | ['('] : Open, ['['] : Open, ['{'] : Open,
|
---|
235 | ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
|
---|
236 | [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
|
---|
237 | // closing delimiters, no space before
|
---|
238 | [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
|
---|
239 | ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
|
---|
240 | [')'] : Close, [']'] : Close, ['}'] : Close,
|
---|
241 | // opening-closing delimiters, no space before or after
|
---|
242 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
|
---|
243 | [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
|
---|
244 | }; // mask
|
---|
245 |
|
---|
246 | if ( s == 0p ) { fmt( os, "%s", "0p" ); return os; } // null pointer
|
---|
247 | if ( s[0] == '\0' ) { nosep( os ); return os; } // null string => no leading/trailing separator
|
---|
248 |
|
---|
249 | // first character IS NOT spacing or closing punctuation => add left separator
|
---|
250 | unsigned char ch = s[0]; // must make unsigned
|
---|
251 | if ( sepPrt$( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
|
---|
252 | fmt( os, "%s", sepGetCur$( os ) );
|
---|
253 | } // if
|
---|
254 |
|
---|
255 | // if string starts line, must reset to determine open state because separator is off
|
---|
256 | sepReset$( os ); // reset separator
|
---|
257 |
|
---|
258 | // last character IS spacing or opening punctuation => turn off separator for next item
|
---|
259 | int len = strlen( s );
|
---|
260 | ch = s[len - 1]; // must make unsigned
|
---|
261 | fmt( os, "%s", s ); // fmt resets seperator, but reset it again
|
---|
262 | if ( sepPrt$( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
|
---|
263 | sep( os );
|
---|
264 | } else {
|
---|
265 | nosep( os );
|
---|
266 | } // if
|
---|
267 | if ( ch == '\n' ) setNL$( os, true ); // check *AFTER* sepPrt$ call above as it resets NL flag
|
---|
268 | return os;
|
---|
269 | // return write( os, s, len );
|
---|
270 | } // ?|?
|
---|
271 | OSTYPE_VOID_IMPL( const char * )
|
---|
272 |
|
---|
273 | // ostype & ?|?( ostype & os, const char16_t * s ) {
|
---|
274 | // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
275 | // fmt( os, "%ls", s );
|
---|
276 | // return os;
|
---|
277 | // } // ?|?
|
---|
278 |
|
---|
279 | // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
|
---|
280 | // ostype & ?|?( ostype & os, const char32_t * s ) {
|
---|
281 | // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
282 | // fmt( os, "%ls", s );
|
---|
283 | // return os;
|
---|
284 | // } // ?|?
|
---|
285 | // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
|
---|
286 |
|
---|
287 | // ostype & ?|?( ostype & os, const wchar_t * s ) {
|
---|
288 | // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
289 | // fmt( os, "%ls", s );
|
---|
290 | // return os;
|
---|
291 | // } // ?|?
|
---|
292 |
|
---|
293 | ostype & ?|?( ostype & os, const void * p ) {
|
---|
294 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
295 | fmt( os, "%p", p );
|
---|
296 | return os;
|
---|
297 | } // ?|?
|
---|
298 | OSTYPE_VOID_IMPL( const void * )
|
---|
299 |
|
---|
300 | // manipulators
|
---|
301 | ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
|
---|
302 | return manip( os );
|
---|
303 | } // ?|?
|
---|
304 | void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
|
---|
305 | manip( os );
|
---|
306 | if ( getPrt$( os ) ) ends( os ); // something printed ?
|
---|
307 | setPrt$( os, false ); // turn off
|
---|
308 | } // ?|?
|
---|
309 |
|
---|
310 | ostype & nl( ostype & os ) {
|
---|
311 | (ostype &)(os | '\n');
|
---|
312 | setPrt$( os, false ); // turn off
|
---|
313 | setNL$( os, true );
|
---|
314 | return nosep( os ); // prepare for next line
|
---|
315 | } // nl
|
---|
316 |
|
---|
317 | ostype & nonl( ostype & os ) {
|
---|
318 | setPrt$( os, false ); // turn off
|
---|
319 | return os;
|
---|
320 | } // nonl
|
---|
321 |
|
---|
322 | ostype & nlOn( ostype & os ) {
|
---|
323 | nlOn( os ); // call void returning
|
---|
324 | return os;
|
---|
325 | } // nlOn
|
---|
326 |
|
---|
327 | ostype & nlOff( ostype & os ) {
|
---|
328 | nlOff( os ); // call void returning
|
---|
329 | return os;
|
---|
330 | } // nlOff
|
---|
331 |
|
---|
332 | ostype & sepVal( ostype & os ) {
|
---|
333 | return (ostype &)(os | sepGet( os ));
|
---|
334 | } // sepVal
|
---|
335 |
|
---|
336 | ostype & sepTupleVal( ostype & os ) {
|
---|
337 | return os | sepGetTuple( os );
|
---|
338 | } // sepTupleVal
|
---|
339 |
|
---|
340 | ostype & sep( ostype & os ) {
|
---|
341 | sep( os ); // call void returning
|
---|
342 | return os;
|
---|
343 | } // sep
|
---|
344 |
|
---|
345 | ostype & nosep( ostype & os ) {
|
---|
346 | nosep( os ); // call void returning
|
---|
347 | return os;
|
---|
348 | } // nosep
|
---|
349 |
|
---|
350 | ostype & sepOn( ostype & os ) {
|
---|
351 | sepOn( os ); // call void returning
|
---|
352 | return os;
|
---|
353 | } // sepOn
|
---|
354 |
|
---|
355 | ostype & sepOff( ostype & os ) {
|
---|
356 | sepOff( os ); // call void returning
|
---|
357 | return os;
|
---|
358 | } // sepOff
|
---|
359 | } // distribution
|
---|
360 |
|
---|
361 | // tuples
|
---|
362 | forall( ostype &, T, Params... | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
|
---|
363 | ostype & ?|?( ostype & os, T arg, Params rest ) {
|
---|
364 | (ostype &)(os | arg); // print first argument
|
---|
365 | sepSetCur$( os, sepGetTuple( os ) ); // switch to tuple separator
|
---|
366 | (ostype &)(os | rest); // print remaining arguments
|
---|
367 | sepSetCur$( os, sepGet( os ) ); // switch to regular separator
|
---|
368 | return os;
|
---|
369 | } // ?|?
|
---|
370 | void ?|?( ostype & os, T arg, Params rest ) {
|
---|
371 | // (ostype &)(?|?( os, arg, rest )); ends( os );
|
---|
372 | (ostype &)(os | arg); // print first argument
|
---|
373 | sepSetCur$( os, sepGetTuple( os ) ); // switch to tuple separator
|
---|
374 | (ostype &)(os | rest); // print remaining arguments
|
---|
375 | sepSetCur$( os, sepGet( os ) ); // switch to regular separator
|
---|
376 | ends( os );
|
---|
377 | } // ?|?
|
---|
378 | } // distribution
|
---|
379 |
|
---|
380 | // writes the range [begin, end) to the given stream
|
---|
381 | forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) {
|
---|
382 | void write( iterator_type begin, iterator_type end, ostype & os ) {
|
---|
383 | void print( elt_type i ) { os | i; }
|
---|
384 | for_each( begin, end, print );
|
---|
385 | } // ?|?
|
---|
386 |
|
---|
387 | void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
|
---|
388 | void print( elt_type i ) { os | i; }
|
---|
389 | for_each_reverse( begin, end, print );
|
---|
390 | } // ?|?
|
---|
391 | } // distribution
|
---|
392 |
|
---|
393 | // *********************************** manipulators ***********************************
|
---|
394 |
|
---|
395 | // *********************************** integral ***********************************
|
---|
396 |
|
---|
397 | static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
|
---|
398 | static const char * longbin[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
|
---|
399 |
|
---|
400 | // Default prefix for non-decimal prints is 0b, 0, 0x.
|
---|
401 | #define INTEGRAL_FMT_IMPL( T, IFMTNP, IFMTP ) \
|
---|
402 | forall( ostype & | basic_ostream( ostype ) ) { \
|
---|
403 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
|
---|
404 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); \
|
---|
405 | \
|
---|
406 | if ( f.base == 'b' || f.base == 'B' ) { /* bespoke binary format */ \
|
---|
407 | int bits = high1( f.val ); /* position of most significant bit */ \
|
---|
408 | if ( bits == 0 ) bits = 1; /* 0 value => force one bit to print */ \
|
---|
409 | int spaces; \
|
---|
410 | if ( ! f.flags.left ) { /* right justified ? */ \
|
---|
411 | /* Note, base prefix then zero padding or spacing then prefix. */ \
|
---|
412 | if ( f.flags.pc ) { \
|
---|
413 | spaces = f.wd - f.pc; \
|
---|
414 | if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
|
---|
415 | if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
|
---|
416 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
|
---|
417 | spaces = f.pc - bits; \
|
---|
418 | if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
|
---|
419 | } else { \
|
---|
420 | spaces = f.wd - bits; \
|
---|
421 | if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
|
---|
422 | if ( f.flags.pad0 ) { \
|
---|
423 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
|
---|
424 | if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
|
---|
425 | } else { \
|
---|
426 | if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
|
---|
427 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
|
---|
428 | } /* if */ \
|
---|
429 | } /* if */ \
|
---|
430 | } else { \
|
---|
431 | if ( ! f.flags.nobsdp ) fmt( os, "0%c", f.base ); \
|
---|
432 | if ( f.flags.pc ) { \
|
---|
433 | spaces = f.pc - bits; \
|
---|
434 | if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
|
---|
435 | spaces = f.wd - f.pc; \
|
---|
436 | } else { /* pad0 flag ignored with left flag */ \
|
---|
437 | spaces = f.wd - bits; \
|
---|
438 | } /* if */ \
|
---|
439 | if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
|
---|
440 | } /* if */ \
|
---|
441 | int shift = floor( bits - 1, 4 ); \
|
---|
442 | typeof( f.val ) temp = f.val; \
|
---|
443 | fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \
|
---|
444 | for () { \
|
---|
445 | shift -= 4; \
|
---|
446 | if ( shift < 0 ) break; \
|
---|
447 | temp = f.val; \
|
---|
448 | fmt( os, "%s", longbin[(temp >> shift) & 0xf] ); \
|
---|
449 | } /* for */ \
|
---|
450 | if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \
|
---|
451 | return os; \
|
---|
452 | } /* if */ \
|
---|
453 | \
|
---|
454 | char fmtstr[sizeof(IFMTP)]; /* sizeof includes '\0' */ \
|
---|
455 | if ( ! f.flags.pc ) memcpy( &fmtstr, IFMTNP, sizeof(IFMTNP) ); \
|
---|
456 | else memcpy( &fmtstr, IFMTP, sizeof(IFMTP) ); \
|
---|
457 | int star = 5; /* position before first '*' */ \
|
---|
458 | \
|
---|
459 | /* Insert flags into spaces before '*', from right to left. */ \
|
---|
460 | if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \
|
---|
461 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
|
---|
462 | if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \
|
---|
463 | if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \
|
---|
464 | fmtstr[star] = '\''; star -= 1; /* locale */ \
|
---|
465 | fmtstr[star] = '%'; \
|
---|
466 | \
|
---|
467 | if ( ! f.flags.pc ) { /* no precision */ \
|
---|
468 | fmtstr[sizeof(IFMTNP)-2] = f.base; /* sizeof includes '\0' */ \
|
---|
469 | /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \
|
---|
470 | fmt( os, &fmtstr[star], f.wd, f.val ); \
|
---|
471 | } else { /* precision */ \
|
---|
472 | fmtstr[sizeof(IFMTP)-2] = f.base; /* sizeof includes '\0' */ \
|
---|
473 | /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \
|
---|
474 | fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \
|
---|
475 | } /* if */ \
|
---|
476 | return os; \
|
---|
477 | } /* ?|? */ \
|
---|
478 | OSTYPE_VOID_IMPL( _Ostream_Manip(T) ) \
|
---|
479 | } // distribution
|
---|
480 |
|
---|
481 | INTEGRAL_FMT_IMPL( signed char, " *hh ", " *.*hh " )
|
---|
482 | INTEGRAL_FMT_IMPL( unsigned char, " *hh ", " *.*hh " )
|
---|
483 | INTEGRAL_FMT_IMPL( signed short int, " *h ", " *.*h " )
|
---|
484 | INTEGRAL_FMT_IMPL( unsigned short int, " *h ", " *.*h " )
|
---|
485 | INTEGRAL_FMT_IMPL( signed int, " * ", " *.* " )
|
---|
486 | INTEGRAL_FMT_IMPL( unsigned int, " * ", " *.* " )
|
---|
487 | INTEGRAL_FMT_IMPL( signed long int, " *l ", " *.*l " )
|
---|
488 | INTEGRAL_FMT_IMPL( unsigned long int, " *l ", " *.*l " )
|
---|
489 | INTEGRAL_FMT_IMPL( signed long long int, " *ll ", " *.*ll " )
|
---|
490 | INTEGRAL_FMT_IMPL( unsigned long long int, " *ll ", " *.*ll " )
|
---|
491 |
|
---|
492 |
|
---|
493 | #if defined( __SIZEOF_INT128__ )
|
---|
494 | // Default prefix for non-decimal prints is 0b, 0, 0x.
|
---|
495 | forall( ostype & | basic_ostream( ostype ) )
|
---|
496 | static inline void base_128( ostype & os, unsigned int128 val, unsigned int128 power, _Ostream_Manip(uint64_t) & f, unsigned int maxdig, unsigned int bits, unsigned int cnt = 0 ) {
|
---|
497 | int wd = 1; // f.wd is never 0 because 0 implies left-pad
|
---|
498 | if ( val > power ) { // subdivide value into printable 64-bit values
|
---|
499 | base_128( os, val / power, power, f, maxdig, bits, cnt + 1 ); // recursive
|
---|
500 | f.val = val % power;
|
---|
501 | if ( cnt == 1 && f.flags.left ) { wd = f.wd; f.wd = maxdig; } // copy f.wd and reset for printing middle chunk
|
---|
502 | // printf( "R val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n",
|
---|
503 | // f.val, f.val, f.wd, f.pc, f.base, f.flags.neg, f.flags.pc, f.flags.left, f.flags.nobsdp, f.flags.sign, f.flags.pad0 );
|
---|
504 | (ostype &)(os | f);
|
---|
505 | if ( cnt == 1 ) {
|
---|
506 | if ( f.flags.left ) { wd -= maxdig; f.wd = wd < 0 ? 1 : wd; } // update and restore f.wd for printing end chunk
|
---|
507 | nosep( os ); // no seperator between chunks
|
---|
508 | } // if
|
---|
509 | } else { // print start chunk
|
---|
510 | f.val = val;
|
---|
511 | // f.pc is unsigned => use wd
|
---|
512 | if ( f.flags.pc && f.pc > maxdig * cnt ) { wd = f.pc - maxdig * cnt; f.pc = wd < 0 ? 0 : wd; }
|
---|
513 | else { f.flags.pc = false; f.pc = 0; }
|
---|
514 |
|
---|
515 | if ( ! f.flags.left ) { // right justify
|
---|
516 | wd = f.wd - maxdig * cnt;
|
---|
517 | f.wd = wd < 0 ? 1 : wd;
|
---|
518 | wd = maxdig;
|
---|
519 | } else { // left justify
|
---|
520 | if ( cnt != 0 ) { // value >= 2^64 ?
|
---|
521 | unsigned int dig, bs = 0;
|
---|
522 | // compute size of prefix digits and base
|
---|
523 | if ( f.base == 'd' || f.base == 'u' ) { // no base prefix
|
---|
524 | dig = ceil( log10( f.val ) ); // use floating-point
|
---|
525 | if ( f.base == 'd' && (f.flags.neg || f.flags.sign) ) bs = 1; // sign ?
|
---|
526 | } else {
|
---|
527 | dig = ceiling_div( high1( f.val ), bits );
|
---|
528 | if ( ! f.flags.nobsdp ) { // base prefix ?
|
---|
529 | if ( f.base == 'o' ) {
|
---|
530 | // 0 prefix for octal is not added for precision with leading zero
|
---|
531 | if ( f.pc <= dig ) bs = 1; // 1 character prefix
|
---|
532 | } else bs = 2; // 2 character prefix
|
---|
533 | } // if
|
---|
534 | } // if
|
---|
535 | wd = f.wd - (f.pc > dig ? f.pc : dig) - bs; // precision > leading digits ?
|
---|
536 | if ( wd < 0 ) wd = 1;
|
---|
537 | f.wd = 1;
|
---|
538 | } // if
|
---|
539 | // all manipulators handled implicitly for value < 2^64
|
---|
540 | } // if
|
---|
541 | // prior checks ensure wd not negative
|
---|
542 |
|
---|
543 | if ( f.flags.neg ) f.val = -f.val;
|
---|
544 | // printf( "L val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n",
|
---|
545 | // f.val, f.val, f.wd, f.pc, f.base, f.flags.neg, f.flags.pc, f.flags.left, f.flags.nobsdp, f.flags.sign, f.flags.pad0 );
|
---|
546 | (ostype &)(os | f);
|
---|
547 |
|
---|
548 | // remaining middle and end chunks are padded with 0s on the left
|
---|
549 | if ( ! f.flags.left ) { f.flags.pad0 = true; f.flags.pc = false; } // left pad with 0s
|
---|
550 | else { f.pc = maxdig; f.flags.pc = true; } // left pad with precision
|
---|
551 |
|
---|
552 | if ( cnt != 0 ) nosep( os ); // no seperator between chunks
|
---|
553 | f.wd = wd; // reset f.wd for next chunk
|
---|
554 | f.flags.sign = false; // no leading +/- sign
|
---|
555 | f.flags.nobsdp = true; // no leading base prefix
|
---|
556 | } // if
|
---|
557 | } // base_128
|
---|
558 |
|
---|
559 | #define INTEGRAL_FMT_IMPL128( T ) \
|
---|
560 | forall( ostype & | basic_ostream( ostype ) ) { \
|
---|
561 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
|
---|
562 | _Ostream_Manip(uint64_t) fmt; \
|
---|
563 | fmt.[wd, pc, base, all] = f.[wd, pc, base, all]; \
|
---|
564 | if ( f.base == 'b' | f.base == 'B' ) { \
|
---|
565 | base_128( os, f.val, (unsigned int128)1 << 64, fmt, 64, 1 ); \
|
---|
566 | } else if ( f.base == 'o' ) { \
|
---|
567 | base_128( os, f.val, (unsigned int128)1 << 63, fmt, 21, 3 ); \
|
---|
568 | } else if ( f.base == 'd' || f.base == 'u' ) { \
|
---|
569 | if ( f.base == 'd' && f.val < 0 ) { f.val = -f.val; fmt.flags.neg = true; } \
|
---|
570 | base_128( os, f.val, (unsigned int128)10_000_000_000_000_000_000UL, fmt, 19, 0 ); \
|
---|
571 | } else { \
|
---|
572 | base_128( os, f.val, (unsigned int128)1 << 64, fmt, 16, 4 ); \
|
---|
573 | } /* if */ \
|
---|
574 | return os; \
|
---|
575 | } /* ?|? */ \
|
---|
576 | OSTYPE_VOID_IMPL( _Ostream_Manip(T) ) \
|
---|
577 | } // distribution
|
---|
578 |
|
---|
579 | INTEGRAL_FMT_IMPL128( int128 )
|
---|
580 | INTEGRAL_FMT_IMPL128( unsigned int128 )
|
---|
581 | #endif // __SIZEOF_INT128__
|
---|
582 |
|
---|
583 | // *********************************** floating point ***********************************
|
---|
584 |
|
---|
585 | static const char *suffixes[] = {
|
---|
586 | "y", "z", "a", "f", "p", "n", "u", "m", "",
|
---|
587 | "K", "M", "G", "T", "P", "E", "Z", "Y"
|
---|
588 | };
|
---|
589 | #define SUFFIXES_START (-24) /* Smallest power for which there is a suffix defined. */
|
---|
590 | #define SUFFIXES_END (SUFFIXES_START + (int)((sizeof(suffixes) / sizeof(char *) - 1) * 3))
|
---|
591 |
|
---|
592 | #define PRINT_WITH_DP2( os, format, ... ) \
|
---|
593 | { \
|
---|
594 | if ( ! f.flags.eng ) { \
|
---|
595 | len = snprintf( buf, size, format, ##__VA_ARGS__ ); \
|
---|
596 | if ( isfinite( f.val ) && ( f.pc != 0 || ! f.flags.nobsdp ) ) { /* if number, print decimal point when no fraction or exponent */ \
|
---|
597 | for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \
|
---|
598 | if ( i == len ) { \
|
---|
599 | if ( ! f.flags.left ) { \
|
---|
600 | buf[i] = '.'; buf[i + 1] = '\0'; \
|
---|
601 | if ( buf[0] == ' ' ) bufbeg = 1; /* decimal point within width */ \
|
---|
602 | } else { \
|
---|
603 | for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \
|
---|
604 | buf[i] = '.'; \
|
---|
605 | if ( i == len ) buf[i + 1] = '\0'; \
|
---|
606 | } /* if */ \
|
---|
607 | } /* if */ \
|
---|
608 | } /* if */ \
|
---|
609 | } else { \
|
---|
610 | int exp10, len2; \
|
---|
611 | eng( f.val, f.pc, exp10 ); /* changes arguments */ \
|
---|
612 | /* printf( "%g %d %d %d %s\n", f.val, f.wd, f.pc, exp10, format ); */ \
|
---|
613 | if ( ! f.flags.left && f.wd > 1 ) { \
|
---|
614 | /* Exponent size: 'e', optional minus sign, number of digits: log10(0) => undefined */ \
|
---|
615 | f.wd -= 1 + (exp10 < 0 ? 1 : 0) + lrint( floor( exp10 == 0 ? 0 : log10( abs( exp10 ) ) ) ) + 1; \
|
---|
616 | if ( f.wd < 1 ) f.wd = 1; \
|
---|
617 | } /* if */ \
|
---|
618 | len = snprintf( buf, size, format, ##__VA_ARGS__ ); \
|
---|
619 | if ( f.flags.left ) { \
|
---|
620 | for ( len -= 1; len > 0 && buf[len] == ' '; len -= 1 ); \
|
---|
621 | len += 1; \
|
---|
622 | } /* if */ \
|
---|
623 | if ( ! f.flags.nobsdp || (exp10 < SUFFIXES_START) || (exp10 > SUFFIXES_END) ) { \
|
---|
624 | len2 = snprintf( &buf[len], size - len, "e%d", (int)exp10 /* ambiguity with function exp10 */ ); \
|
---|
625 | } else { \
|
---|
626 | len2 = snprintf( &buf[len], size - len, "%s", suffixes[(exp10 - SUFFIXES_START) / 3] ); \
|
---|
627 | } /* if */ \
|
---|
628 | if ( f.flags.left && len + len2 < f.wd ) buf[len + len2] = ' '; \
|
---|
629 | } /* if */ \
|
---|
630 | fmt( os, "%s", &buf[bufbeg] ); \
|
---|
631 | }
|
---|
632 |
|
---|
633 | #define FLOATING_POINT_FMT_IMPL( T, DFMTNP, DFMTP ) \
|
---|
634 | forall( ostype & | basic_ostream( ostype ) ) { \
|
---|
635 | static void eng( T &value, int & pc, int & exp10 ) { \
|
---|
636 | exp10 = lrint( floor( log10( abs( value ) ) ) ); /* round to desired precision */ \
|
---|
637 | if ( exp10 < 0 ) exp10 -= 2; \
|
---|
638 | exp10 = floor( exp10, 3 ); \
|
---|
639 | value *= pow( 10.0, -exp10 ); \
|
---|
640 | if ( pc <= 3 ) pc = 3; \
|
---|
641 | } /* eng */ \
|
---|
642 | \
|
---|
643 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
|
---|
644 | enum { size = 48 }; \
|
---|
645 | char buf[size]; \
|
---|
646 | int bufbeg = 0, i, len; \
|
---|
647 | \
|
---|
648 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); \
|
---|
649 | char fmtstr[sizeof(DFMTP) + 8]; /* sizeof includes '\0' */ \
|
---|
650 | if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \
|
---|
651 | else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \
|
---|
652 | int star = 5; /* position before first '*' */ \
|
---|
653 | \
|
---|
654 | /* Insert flags into spaces before '*', from right to left. */ \
|
---|
655 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
|
---|
656 | if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \
|
---|
657 | if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \
|
---|
658 | fmtstr[star] = '\''; star -= 1; /* locale */ \
|
---|
659 | fmtstr[star] = '%'; \
|
---|
660 | \
|
---|
661 | if ( ! f.flags.pc ) { /* no precision */ \
|
---|
662 | fmtstr[sizeof(DFMTNP)-2] = f.base; /* sizeof includes '\0' */ \
|
---|
663 | /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star] ); */ \
|
---|
664 | PRINT_WITH_DP2( os, &fmtstr[star], f.wd, f.val ) \
|
---|
665 | } else { /* precision */ \
|
---|
666 | fmtstr[sizeof(DFMTP)-2] = f.base; /* sizeof includes '\0' */ \
|
---|
667 | /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \
|
---|
668 | PRINT_WITH_DP2( os, &fmtstr[star], f.wd, f.pc, f.val ) \
|
---|
669 | } /* if */ \
|
---|
670 | return os; \
|
---|
671 | } /* ?|? */ \
|
---|
672 | \
|
---|
673 | OSTYPE_VOID_IMPL( _Ostream_Manip(T) ) \
|
---|
674 | } // distribution
|
---|
675 |
|
---|
676 | FLOATING_POINT_FMT_IMPL( double, " * ", " *.* " )
|
---|
677 | FLOATING_POINT_FMT_IMPL( long double, " *L ", " *.*L " )
|
---|
678 |
|
---|
679 | // *********************************** character ***********************************
|
---|
680 |
|
---|
681 | forall( ostype & | basic_ostream( ostype ) ) {
|
---|
682 | ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) {
|
---|
683 | if ( f.base != 'c' ) { // bespoke binary/octal/hex format
|
---|
684 | _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} };
|
---|
685 | fmtuc.flags.pc = f.flags.pc;
|
---|
686 | fmtuc.flags.nobsdp = f.flags.nobsdp;
|
---|
687 | // os | fmtuc | nonl;
|
---|
688 | (ostype &)(os | fmtuc);
|
---|
689 | return os;
|
---|
690 | } // if
|
---|
691 |
|
---|
692 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
693 |
|
---|
694 | #define CFMTNP "% * "
|
---|
695 | char fmtstr[sizeof(CFMTNP)]; // sizeof includes '\0'
|
---|
696 | memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) );
|
---|
697 | int star = 1; // position before first '*'
|
---|
698 |
|
---|
699 | // Insert flags into spaces before '*', from right to left.
|
---|
700 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
|
---|
701 | fmtstr[star] = '%';
|
---|
702 |
|
---|
703 | fmtstr[sizeof(CFMTNP)-2] = f.base; // sizeof includes '\0'
|
---|
704 | // printf( "%d %s\n", f.wd, &fmtstr[star] );
|
---|
705 | fmt( os, &fmtstr[star], f.wd, f.val );
|
---|
706 | return os;
|
---|
707 | } // ?|?
|
---|
708 | OSTYPE_VOID_IMPL( _Ostream_Manip(char) )
|
---|
709 | } // distribution
|
---|
710 |
|
---|
711 | // *********************************** C string ***********************************
|
---|
712 |
|
---|
713 | forall( ostype & | basic_ostream( ostype ) ) {
|
---|
714 | ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) {
|
---|
715 | if ( ! f.val ) return os; // null pointer ?
|
---|
716 |
|
---|
717 | if ( f.base != 's' ) { // bespoke binary/octal/hex format
|
---|
718 | _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} };
|
---|
719 | fmtuc.flags.pc = f.flags.pc;
|
---|
720 | fmtuc.flags.nobsdp = f.flags.nobsdp;
|
---|
721 | for ( i; 0 ~ @ : @; f.val[i] != '\0' ) {
|
---|
722 | fmtuc.val = f.val[i];
|
---|
723 | // os | fmtuc | nonl;
|
---|
724 | (ostype &)(os | fmtuc);
|
---|
725 | } // for
|
---|
726 | return os;
|
---|
727 | } // if
|
---|
728 |
|
---|
729 | if ( f.val[0] != '\0' && // null string => no leading separator
|
---|
730 | sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) );
|
---|
731 |
|
---|
732 | #define SFMTNP "% * "
|
---|
733 | #define SFMTP "% *.* "
|
---|
734 | char fmtstr[sizeof(SFMTP)]; // sizeof includes '\0'
|
---|
735 | if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) );
|
---|
736 | else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) );
|
---|
737 | int star = 1; // position before first '*'
|
---|
738 |
|
---|
739 | // Insert flags into spaces before '*', from right to left.
|
---|
740 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
|
---|
741 | fmtstr[star] = '%';
|
---|
742 |
|
---|
743 | if ( ! f.flags.pc ) { // no precision
|
---|
744 | // printf( "%d %s\n", f.wd, &fmtstr[star] );
|
---|
745 | fmtstr[sizeof(SFMTNP)-2] = f.base; // sizeof includes '\0'
|
---|
746 | fmt( os, &fmtstr[star], f.wd, f.val );
|
---|
747 | } else { // precision
|
---|
748 | fmtstr[sizeof(SFMTP)-2] = f.base; // sizeof includes '\0'
|
---|
749 | // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] );
|
---|
750 | fmt( os, &fmtstr[star], f.wd, f.pc, f.val );
|
---|
751 | } // if
|
---|
752 | if ( f.val[0] == '\0' ) { nosep( os ); } // null string => no trailing separator
|
---|
753 | return os;
|
---|
754 | } // ?|?
|
---|
755 | OSTYPE_VOID_IMPL( _Ostream_Manip(const char *) )
|
---|
756 | } // distribution
|
---|
757 |
|
---|
758 |
|
---|
759 | // *********************************** istream ***********************************
|
---|
760 |
|
---|
761 |
|
---|
762 | forall( istype & | basic_istream( istype ) ) {
|
---|
763 | istype & ?|?( istype & is, bool & b ) {
|
---|
764 | char val[6];
|
---|
765 | fmt( is, "%5s", val );
|
---|
766 | if ( strcmp( val, "true" ) == 0 ) b = true;
|
---|
767 | else if ( strcmp( val, "false" ) == 0 ) b = false;
|
---|
768 | else {
|
---|
769 | fprintf( stderr, "invalid Boolean constant\n" );
|
---|
770 | abort(); // cannot use abort stream
|
---|
771 | } // if
|
---|
772 | return is;
|
---|
773 | } // ?|?
|
---|
774 | ISTYPE_VOID_IMPL( bool & )
|
---|
775 |
|
---|
776 | istype & ?|?( istype & is, char & c ) {
|
---|
777 | char temp;
|
---|
778 | for () {
|
---|
779 | fmt( is, "%c", &temp ); // must pass pointer through varg to fmt
|
---|
780 | // do not overwrite parameter with newline unless appropriate
|
---|
781 | if ( temp != '\n' || getANL$( is ) ) { c = temp; break; }
|
---|
782 | if ( eof( is ) ) break;
|
---|
783 | } // for
|
---|
784 | return is;
|
---|
785 | } // ?|?
|
---|
786 | ISTYPE_VOID_IMPL( char & )
|
---|
787 |
|
---|
788 | istype & ?|?( istype & is, signed char & sc ) {
|
---|
789 | fmt( is, "%hhi", &sc );
|
---|
790 | return is;
|
---|
791 | } // ?|?
|
---|
792 | ISTYPE_VOID_IMPL( signed char & )
|
---|
793 |
|
---|
794 | istype & ?|?( istype & is, unsigned char & usc ) {
|
---|
795 | fmt( is, "%hhi", &usc );
|
---|
796 | return is;
|
---|
797 | } // ?|?
|
---|
798 | ISTYPE_VOID_IMPL( unsigned char & )
|
---|
799 |
|
---|
800 | istype & ?|?( istype & is, short int & si ) {
|
---|
801 | fmt( is, "%hi", &si );
|
---|
802 | return is;
|
---|
803 | } // ?|?
|
---|
804 | ISTYPE_VOID_IMPL( short int & )
|
---|
805 |
|
---|
806 | istype & ?|?( istype & is, unsigned short int & usi ) {
|
---|
807 | fmt( is, "%hi", &usi );
|
---|
808 | return is;
|
---|
809 | } // ?|?
|
---|
810 | ISTYPE_VOID_IMPL( unsigned short int & )
|
---|
811 |
|
---|
812 | istype & ?|?( istype & is, int & i ) {
|
---|
813 | fmt( is, "%i", &i );
|
---|
814 | return is;
|
---|
815 | } // ?|?
|
---|
816 | ISTYPE_VOID_IMPL( int & )
|
---|
817 |
|
---|
818 | istype & ?|?( istype & is, unsigned int & ui ) {
|
---|
819 | fmt( is, "%i", &ui );
|
---|
820 | return is;
|
---|
821 | } // ?|?
|
---|
822 | ISTYPE_VOID_IMPL( unsigned int & )
|
---|
823 |
|
---|
824 | istype & ?|?( istype & is, long int & li ) {
|
---|
825 | fmt( is, "%li", &li );
|
---|
826 | return is;
|
---|
827 | } // ?|?
|
---|
828 | ISTYPE_VOID_IMPL( long int & )
|
---|
829 |
|
---|
830 | istype & ?|?( istype & is, unsigned long int & ulli ) {
|
---|
831 | fmt( is, "%li", &ulli );
|
---|
832 | return is;
|
---|
833 | } // ?|?
|
---|
834 | ISTYPE_VOID_IMPL( unsigned long int & )
|
---|
835 |
|
---|
836 | istype & ?|?( istype & is, long long int & lli ) {
|
---|
837 | fmt( is, "%lli", &lli );
|
---|
838 | return is;
|
---|
839 | } // ?|?
|
---|
840 | ISTYPE_VOID_IMPL( long long int & )
|
---|
841 |
|
---|
842 | istype & ?|?( istype & is, unsigned long long int & ulli ) {
|
---|
843 | fmt( is, "%lli", &ulli );
|
---|
844 | return is;
|
---|
845 | } // ?|?
|
---|
846 | ISTYPE_VOID_IMPL( unsigned long long int & )
|
---|
847 |
|
---|
848 | #if defined( __SIZEOF_INT128__ )
|
---|
849 | istype & ?|?( istype & is, int128 & llli ) {
|
---|
850 | return (istype &)(is | (unsigned int128 &)llli);
|
---|
851 | } // ?|?
|
---|
852 | ISTYPE_VOID_IMPL( int128 & )
|
---|
853 |
|
---|
854 | istype & ?|?( istype & is, unsigned int128 & ullli ) {
|
---|
855 | char s[40];
|
---|
856 | bool sign = false;
|
---|
857 |
|
---|
858 | if ( fmt( is, " %[-]", s ) == 1 ) sign = true; // skip whitespace, negative sign ?
|
---|
859 | // If the input is too large, the value returned is undefined. If there is no input, no value is returned
|
---|
860 | if ( fmt( is, "%39[0-9]%*[0-9]", s ) == 1 ) { // take first 39 characters, ignore remaining
|
---|
861 | ullli = 0;
|
---|
862 | for ( i; 0 ~ @ : @; s[i] != '\0' ) {
|
---|
863 | ullli = ullli * 10 + s[i] - '0';
|
---|
864 | } // for
|
---|
865 | if ( sign ) ullli = -ullli;
|
---|
866 | } else if ( sign ) ungetc( is, '-' ); // return minus when no digits
|
---|
867 | return is;
|
---|
868 | } // ?|?
|
---|
869 | ISTYPE_VOID_IMPL( unsigned int128 & )
|
---|
870 | #endif // __SIZEOF_INT128__
|
---|
871 |
|
---|
872 | istype & ?|?( istype & is, float & f ) {
|
---|
873 | fmt( is, "%f", &f );
|
---|
874 | return is;
|
---|
875 | } // ?|?
|
---|
876 | ISTYPE_VOID_IMPL( float & )
|
---|
877 |
|
---|
878 | istype & ?|?( istype & is, double & d ) {
|
---|
879 | fmt( is, "%lf", &d );
|
---|
880 | return is;
|
---|
881 | } // ?|?
|
---|
882 | ISTYPE_VOID_IMPL( double & )
|
---|
883 |
|
---|
884 | istype & ?|?( istype & is, long double & ld ) {
|
---|
885 | fmt( is, "%Lf", &ld );
|
---|
886 | return is;
|
---|
887 | } // ?|?
|
---|
888 | ISTYPE_VOID_IMPL( long double & )
|
---|
889 |
|
---|
890 | istype & ?|?( istype & is, float _Complex & fc ) {
|
---|
891 | float re, im;
|
---|
892 | fmt( is, "%f%fi", &re, &im );
|
---|
893 | fc = re + im * _Complex_I;
|
---|
894 | return is;
|
---|
895 | } // ?|?
|
---|
896 | ISTYPE_VOID_IMPL( float _Complex & )
|
---|
897 |
|
---|
898 | istype & ?|?( istype & is, double _Complex & dc ) {
|
---|
899 | double re, im;
|
---|
900 | fmt( is, "%lf%lfi", &re, &im );
|
---|
901 | dc = re + im * _Complex_I;
|
---|
902 | return is;
|
---|
903 | } // ?|?
|
---|
904 | ISTYPE_VOID_IMPL( double _Complex & )
|
---|
905 |
|
---|
906 | istype & ?|?( istype & is, long double _Complex & ldc ) {
|
---|
907 | long double re, im;
|
---|
908 | fmt( is, "%Lf%Lfi", &re, &im );
|
---|
909 | ldc = re + im * _Complex_I;
|
---|
910 | return is;
|
---|
911 | } // ?|?
|
---|
912 | ISTYPE_VOID_IMPL( long double _Complex & )
|
---|
913 |
|
---|
914 | // istype & ?|?( istype & is, const char fmt[] ) {
|
---|
915 | // fmt( is, fmt, "" );
|
---|
916 | // return is;
|
---|
917 | // } // ?|?
|
---|
918 |
|
---|
919 | istype & ?|?( istype & is, char s[] ) {
|
---|
920 | fmt( is, "%s", s );
|
---|
921 | return is;
|
---|
922 | } // ?|?
|
---|
923 | ISTYPE_VOID_IMPL( char * )
|
---|
924 |
|
---|
925 | // manipulators
|
---|
926 | istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
|
---|
927 | return manip( is );
|
---|
928 | } // ?|?
|
---|
929 | void ?|?( istype & is, istype & (* manip)( istype & ) ) {
|
---|
930 | manip( is ); ends( is );
|
---|
931 | } // ?|?
|
---|
932 |
|
---|
933 | istype & nl( istype & is ) {
|
---|
934 | fmt( is, "%*[^\n]" ); // ignore characters to newline
|
---|
935 | return is;
|
---|
936 | } // nl
|
---|
937 |
|
---|
938 | istype & nlOn( istype & is ) {
|
---|
939 | nlOn( is ); // call void returning
|
---|
940 | return is;
|
---|
941 | } // nlOn
|
---|
942 |
|
---|
943 | istype & nlOff( istype & is ) {
|
---|
944 | nlOff( is ); // call void returning
|
---|
945 | return is;
|
---|
946 | } // nlOff
|
---|
947 | } // distribution
|
---|
948 |
|
---|
949 | // *********************************** manipulators ***********************************
|
---|
950 |
|
---|
951 | forall( istype & | basic_istream( istype ) ) {
|
---|
952 | istype & ?|?( istype & is, _Istream_Cstr f ) {
|
---|
953 | // skip xxx
|
---|
954 | if ( ! f.s ) {
|
---|
955 | // printf( "skip %s %d\n", f.scanset, f.wd );
|
---|
956 | if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments
|
---|
957 | else for ( f.wd ) fmt( is, "%*c" );
|
---|
958 | return is;
|
---|
959 | } // if
|
---|
960 | size_t len = 0;
|
---|
961 | if ( f.scanset ) len = strlen( f.scanset );
|
---|
962 | char fmtstr[len + 16];
|
---|
963 | int start = 1;
|
---|
964 | fmtstr[0] = '%';
|
---|
965 | if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
|
---|
966 | if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
|
---|
967 | // cstr %s, %*s, %ws, %*ws
|
---|
968 | if ( ! f.scanset ) {
|
---|
969 | fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
|
---|
970 | // printf( "cstr %s\n", fmtstr );
|
---|
971 | fmt( is, fmtstr, f.s );
|
---|
972 | return is;
|
---|
973 | } // if
|
---|
974 | // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx]
|
---|
975 | // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
|
---|
976 | fmtstr[start] = '['; start += 1;
|
---|
977 | if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
|
---|
978 | strcpy( &fmtstr[start], f.scanset ); // copy includes '\0'
|
---|
979 | len += start;
|
---|
980 | fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
|
---|
981 | // printf( "incl/excl %s\n", fmtstr );
|
---|
982 | fmt( is, fmtstr, f.s );
|
---|
983 | return is;
|
---|
984 | } // ?|?
|
---|
985 | ISTYPE_VOID_IMPL( _Istream_Cstr )
|
---|
986 |
|
---|
987 | istype & ?|?( istype & is, _Istream_Char f ) {
|
---|
988 | fmt( is, "%*c" ); // argument variable unused
|
---|
989 | return is;
|
---|
990 | } // ?|?
|
---|
991 | ISTYPE_VOID_IMPL( _Istream_Char )
|
---|
992 | } // distribution
|
---|
993 |
|
---|
994 | #define INPUT_FMT_IMPL( T, CODE ) \
|
---|
995 | forall( istype & | basic_istream( istype ) ) { \
|
---|
996 | istype & ?|?( istype & is, _Istream_Manip(T) f ) { \
|
---|
997 | enum { size = 16 }; \
|
---|
998 | char fmtstr[size]; \
|
---|
999 | if ( f.wd == -1 ) { \
|
---|
1000 | snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \
|
---|
1001 | } else { \
|
---|
1002 | snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \
|
---|
1003 | } /* if */ \
|
---|
1004 | /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \
|
---|
1005 | fmt( is, fmtstr, &f.val ); \
|
---|
1006 | return is; \
|
---|
1007 | } /* ?|? */ \
|
---|
1008 | ISTYPE_VOID_IMPL( _Istream_Manip(T) ) \
|
---|
1009 | } // distribution
|
---|
1010 |
|
---|
1011 | INPUT_FMT_IMPL( signed char, "hhi" )
|
---|
1012 | INPUT_FMT_IMPL( unsigned char, "hhi" )
|
---|
1013 | INPUT_FMT_IMPL( signed short int, "hi" )
|
---|
1014 | INPUT_FMT_IMPL( unsigned short int, "hi" )
|
---|
1015 | INPUT_FMT_IMPL( signed int, "i" )
|
---|
1016 | INPUT_FMT_IMPL( unsigned int, "i" )
|
---|
1017 | INPUT_FMT_IMPL( signed long int, "li" )
|
---|
1018 | INPUT_FMT_IMPL( unsigned long int, "li" )
|
---|
1019 | INPUT_FMT_IMPL( signed long long int, "lli" )
|
---|
1020 | INPUT_FMT_IMPL( unsigned long long int, "lli" )
|
---|
1021 |
|
---|
1022 | INPUT_FMT_IMPL( float, "f" )
|
---|
1023 | INPUT_FMT_IMPL( double, "lf" )
|
---|
1024 | INPUT_FMT_IMPL( long double, "Lf" )
|
---|
1025 |
|
---|
1026 | forall( istype & | basic_istream( istype ) ) {
|
---|
1027 | istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
|
---|
1028 | float re, im;
|
---|
1029 | _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore };
|
---|
1030 | is | fmtuc;
|
---|
1031 | &fmtuc.val = &im;
|
---|
1032 | is | fmtuc;
|
---|
1033 | if ( ! fc.ignore ) fc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
|
---|
1034 | return is;
|
---|
1035 | } // ?|?
|
---|
1036 | ISTYPE_VOID_IMPL( _Istream_Manip(float _Complex) )
|
---|
1037 |
|
---|
1038 | istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
|
---|
1039 | double re, im;
|
---|
1040 | _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore };
|
---|
1041 | is | fmtuc;
|
---|
1042 | &fmtuc.val = &im;
|
---|
1043 | is | fmtuc;
|
---|
1044 | if ( ! dc.ignore ) dc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
|
---|
1045 | return is;
|
---|
1046 | } // ?|?
|
---|
1047 | ISTYPE_VOID_IMPL( _Istream_Manip(double _Complex) )
|
---|
1048 |
|
---|
1049 | istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
|
---|
1050 | long double re, im;
|
---|
1051 | _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore };
|
---|
1052 | is | fmtuc;
|
---|
1053 | &fmtuc.val = &im;
|
---|
1054 | is | fmtuc;
|
---|
1055 | if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
|
---|
1056 | return is;
|
---|
1057 | } // ?|?
|
---|
1058 | ISTYPE_VOID_IMPL( _Istream_Manip(long double _Complex) )
|
---|
1059 | } // distribution
|
---|
1060 |
|
---|
1061 | // Local Variables: //
|
---|
1062 | // tab-width: 4 //
|
---|
1063 | // compile-command: "cfa iostream.cfa" //
|
---|
1064 | // End: //
|
---|