6 char *UTS_sprintf_wrap();
20 * d = strtod(argv[2], (char **)0);
26 * Ret = UTS_sprintf_wrap(obuf, Fmt, d);
27 * assert(Ret == obuf);
29 * printf("{%s}\n", obuf);
34 UTS_sprintf_wrap(obuf, fmt, d,
35 a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15)
39 int fmtlen, Width=0, Precision=6, Alt=0, Plus=0, Minus=0,
41 int FmtChar, BaseFmt = 0;
42 char *f = fmt, *AfterWidth = 0, *AfterPrecision = 0;
47 sprintf(obuf, fmt, d, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15);
50 FmtChar = fmt[fmtlen - 1];
55 sprintf(obuf, fmt, d, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15);
64 if(*f == '#') { Alt = 1; ++f; } /* Always has '.' */
65 if(*f == '+') { Plus = 1; ++f; } /* Force explicit sign */
66 if(*f == '-') { Minus = 1; ++f; } /* Left justify */
67 if(*f == '0') { Zero = 1; ++f;} /* Fill using 0s*/
68 if(Dot = strchr(f, '.')) {
69 Precision = strtol(Dot+1, &AfterPrecision, 0);
71 if(!Dot || (Dot && Dot > f)) { /* Next char=='.' => no width*/
72 Width = strtol(f, &AfterWidth, 0);
74 if(Dot) { f = AfterPrecision; }
75 else if(AfterWidth) { f = AfterWidth; }
76 if(*f != FmtChar) goto regular_sprintf;
77 /* It doesn't look like a f.p. sprintf call */
78 /* from Perl_sv_vcatpvfn */
81 return do_efmt(d, obuf, Width, Precision, Alt,
82 Plus, Minus, Zero, FmtChar == 'E');
84 return do_gfmt(d, obuf, Width, Precision, Alt,
85 Plus, Minus, Zero, FmtChar == 'G');
90 sprintf(obuf, fmt, d, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15);
95 do_efmt(d, obuf, Width, Precision, Alt, Plus, Minus, Zero, UpperCase)
105 Ecvt = ecvt( d , Precision+1, &decpt, &sign);
107 /* fprintf(stderr, "decpt=%d, sign=%d\n", decpt, sign); */
110 if(strspn(Ecvt, "0") == len) AllZeroes = 1;
113 if(sign) *ob++ = '-';
114 else if(Plus) *ob++ = '+';
118 if(Precision > 0 || Alt) *ob++ = '.';
119 strcpy(ob, &Ecvt[1]);
121 ob += strlen(ob); /* ADVANCE TO END OF WHAT WE JUST ADDED */
122 *ob++ = UpperCase ? 'E' : 'e';
127 if(E < 0) { *ob++ = '-'; E = -E; }
128 else { *ob++ = '+'; }
130 sprintf(ob, "%.2d", E); /* Too much horsepower used here */
132 if(Width > strlen(obuf)) return Fill(obuf, Width, Minus, Zero);
137 do_gfmt(d, obuf, Width, Precision, Alt, Plus, Minus, Zero, UpperCase)
141 char *Ecvt = gcvt(d, Precision ? Precision : 1, obuf);
142 int len = strlen(obuf);
144 /* gcvt fails (maybe give a warning? For now return empty string): */
145 if(!Ecvt) { *obuf = '\0'; return obuf; }
147 /* printf("Ecvt='%s'\n", Ecvt); */
148 if(Plus && (Ecvt[0] != '-')) {
149 memmove(obuf+1, obuf, len+1); /* "+1" to get '\0' at end */
153 if(Alt && !strchr(Ecvt, '.')) {
154 int LenUpTo_E = strcspn(obuf, "eE");
155 int E_etc_len = strlen(&obuf[LenUpTo_E]);
156 /* ABOVE: Will be 0 if there's no E/e because */
157 /* strcspn will return length of whole string */
160 memmove(obuf+LenUpTo_E+1, obuf+LenUpTo_E, E_etc_len);
161 obuf[LenUpTo_E] = '.';
162 obuf[LenUpTo_E + 1 + E_etc_len ] = '\0';
165 if(UpperCase && (E_loc = strchr(obuf, 'e'))) { *E_loc = 'E'; }
168 return Fill(obuf, Width, Minus, Zero);
174 Fill(obuf, Width, LeftJustify, Zero)
177 int W = strlen(obuf);
178 int diff = Width - W;
179 /* LeftJustify means there was a '-' flag, and in that case, */
180 /* printf man page (UTS4.4) says ignore '0' */
181 char FillChar = (Zero && !LeftJustify) ? '0' : ' ';
183 int LeftFill = ! LeftJustify;
185 if(Width <= W) return obuf;
188 memmove(obuf+diff, obuf, W+1); /* "+1" to get '\0' at end */
189 for(i=0 ; i < diff ; ++i) { obuf[i] = FillChar; }
191 for(i=W ; i < Width ; ++i)