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