📄 mprprintf.c
字号:
if (fmt.width < 0) { fmt.width = -fmt.width; fmt.flags |= SPRINTF_LEFT; } } else { while (isdigit((int)c)) { fmt.width = fmt.width * 10 + (c - '0'); c = *spec++; } spec--; } break; case STATE_DOT: fmt.precision = 0; fmt.flags &= ~SPRINTF_LEAD_ZERO; break; case STATE_PRECISION: if (c == '*') { fmt.precision = va_arg(arg, int); } else { while (isdigit((int) c)) { fmt.precision = fmt.precision * 10 + (c - '0'); c = *spec++; } spec--; } break; case STATE_BITS: switch (c) {#if BLD_FEATURE_INT64 case 'L': fmt.flags |= SPRINTF_LONGLONG; /* 64 bit */ break;#endif case 'l': fmt.flags |= SPRINTF_LONG; break; case 'h': fmt.flags |= SPRINTF_SHORT; break; } break; case STATE_TYPE: switch (c) {#if BLD_FEATURE_FLOATING_POINT case 'e': case 'g': case 'f': fmt.radix = 10; outFloat(MPR_LOC_PASS(ctx, loc), &fmt, c, (double) va_arg(arg, double)); break;#endif case 'c': BPUT(ctx, loc, &fmt, (char) va_arg(arg, int)); break; case 's': case 'S': sValue = va_arg(arg, char*); if (sValue == 0) { sValue = "null"; len = strlen(sValue); } else if (fmt.flags & SPRINTF_ALTERNATE) { sValue++; len = (int) *sValue; } else if (fmt.precision >= 0) { /* * Can't use strlen(), the string may not have a null */ cp = sValue; for (len = 0; len < fmt.precision; len++) { if (*cp++ == '\0') { break; } } } else { len = strlen(sValue); } if (!(fmt.flags & SPRINTF_LEFT)) { for (i = len; i < fmt.width; i++) { BPUT(ctx, loc, &fmt, (char) ' '); } } for (i = 0; i < len && *sValue; i++) { BPUT(ctx, loc, &fmt, *sValue++); } if (fmt.flags & SPRINTF_LEFT) { for (i = len; i < fmt.width; i++) { BPUT(ctx, loc, &fmt, (char) ' '); } } break; case 'i': ; case 'd': fmt.radix = 10; if (fmt.flags & SPRINTF_SHORT) { iValue = (short) va_arg(arg, int); } else if (fmt.flags & SPRINTF_LONG) { iValue = va_arg(arg, long);#if BLD_FEATURE_INT64 } else if (fmt.flags & SPRINTF_LONGLONG) { iValue = va_arg(arg, num);#endif } else { iValue = va_arg(arg, int); } if (iValue >= 0) { if (fmt.flags & SPRINTF_LEAD_SPACE) { outNum(MPR_LOC_PASS(ctx, loc), &fmt, " ", iValue); } else if (fmt.flags & SPRINTF_SIGN) { outNum(MPR_LOC_PASS(ctx, loc), &fmt, "+", iValue); } else { outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, iValue); } } else { outNum(MPR_LOC_PASS(ctx, loc), &fmt, "-", -iValue); } break; case 'X': fmt.flags |= SPRINTF_UPPER_CASE; /* Fall through */ case 'o': case 'x': case 'u': if (fmt.flags & SPRINTF_SHORT) { uValue = (ushort) va_arg(arg, uint); } else if (fmt.flags & SPRINTF_LONG) { uValue = va_arg(arg, ulong);#if BLD_FEATURE_INT64 } else if (fmt.flags & SPRINTF_LONGLONG) { uValue = va_arg(arg, unum);#endif } else { uValue = va_arg(arg, uint); } if (c == 'u') { fmt.radix = 10; outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, uValue); } else if (c == 'o') { fmt.radix = 8; if (fmt.flags & SPRINTF_ALTERNATE && uValue != 0) { outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0", uValue); } else { outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, uValue); } } else { fmt.radix = 16; if (fmt.flags & SPRINTF_ALTERNATE && uValue != 0) { if (c == 'X') { outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0X", uValue); } else { outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0x", uValue); } } else { outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, uValue); } } break; case 'n': /* Count of chars seen thus far */ if (fmt.flags & SPRINTF_SHORT) { short *count = va_arg(arg, short*); *count = fmt.end - fmt.start; } else if (fmt.flags & SPRINTF_LONG) { long *count = va_arg(arg, long*); *count = fmt.end - fmt.start; } else { int *count = va_arg(arg, int *); *count = fmt.end - fmt.start; } break; case 'p': /* Pointer */#if __WORDSIZE == 64 && BLD_FEATURE_INT64 uValue = (unum) va_arg(arg, void*);#else uValue = (uint) (int) va_arg(arg, void*);#endif fmt.radix = 16; outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0x", uValue); break; default: BPUT(ctx, loc, &fmt, c); } } } BPUTNULL(ctx, loc, &fmt); count = fmt.end - fmt.start; if (*bufPtr == 0) { *bufPtr = (char*) fmt.buf; } return count;}/******************************************************************************//* * Output a number according to the given format. If BLD_FEATURE_INT64 is * defined, then uses 64 bits universally. Slower but smaller code. */static void outNum(MPR_LOC_DEC(ctx, loc), Format *fmt, const char *prefix, unum value){ char numBuf[64]; char *cp; char *endp; char c; int letter, len, leadingZeros, i, fill; endp = &numBuf[sizeof(numBuf) - 1]; *endp = '\0'; cp = endp; /* * Convert to ascii */ if (fmt->radix == 16) { do { letter = (int) (value % fmt->radix); if (letter > 9) { if (fmt->flags & SPRINTF_UPPER_CASE) { letter = 'A' + letter - 10; } else { letter = 'a' + letter - 10; } } else { letter += '0'; } *--cp = letter; value /= fmt->radix; } while (value > 0); } else if (fmt->flags & SPRINTF_COMMA) { i = 1; do { *--cp = '0' + (int) (value % fmt->radix); value /= fmt->radix; if ((i++ % 3) == 0 && value > 0) { *--cp = ','; } } while (value > 0); } else { do { *--cp = '0' + (int) (value % fmt->radix); value /= fmt->radix; } while (value > 0); } len = endp - cp; fill = fmt->width - len; if (prefix != 0) { fill -= strlen(prefix); } leadingZeros = (fmt->precision > len) ? fmt->precision - len : 0; fill -= leadingZeros; if (!(fmt->flags & SPRINTF_LEFT)) { c = (fmt->flags & SPRINTF_LEAD_ZERO) ? '0': ' '; for (i = 0; i < fill; i++) { BPUT(ctx, loc, fmt, c); } } if (prefix != 0) { while (*prefix) { BPUT(ctx, loc, fmt, *prefix++); } } for (i = 0; i < leadingZeros; i++) { BPUT(ctx, loc, fmt, '0'); } while (*cp) { BPUT(ctx, loc, fmt, *cp); cp++; } if (fmt->flags & SPRINTF_LEFT) { for (i = 0; i < fill; i++) { BPUT(ctx, loc, fmt, ' '); } }}/******************************************************************************/#if BLD_FEATURE_FLOATING_POINT/* * Output a floating point number */static void outFloat(MPR_LOC_DEC(ctx, loc), Format *fmt, char specChar, double value){ char *cp;#if FUTURE char numBuf[64]; char *endp; char c; int letter, len, leadingZeros, i, fill, width, precision; endp = &numBuf[sizeof(numBuf) - 1]; *endp = '\0'; precision = fmt->precision; if (precision < 0) { precision = 6; } else if (precision > (sizeof(numBuf) - 1)) { precision = (sizeof(numBuf) - 1); } width = min(fmt->width, sizeof(numBuf) - 1); if (__isnanl(value)) { "nan" } else if (__isinfl(value)) { "infinity" } else if (value < 0) { prefix = "-"; } else if (fmt.flags & SPRINTF_LEAD_SPACE) { prefix = " "; } else if (fmt.flags & SPRINTF_SIGN) { prefix = "+"; } /* * Do the exponent part */ cp = &numBuf[sizeof(numBuf) - precision]; for (i = 0; i < precision; i++) { *cp++ = '0' + (int) (value % fmt->radix); value /= fmt->radix; } /* * Do the decimal part */ if (fmt->flags & SPRINTF_COMMA) { i = 1; do { *--cp = '0' + (int) (value % fmt->radix); value /= fmt->radix; if ((i++ % 3) == 0 && value > 0) { *--cp = ','; } } while (value >= 1.0); } else { do { *--cp = '0' + (int) (value % fmt->radix); value /= fmt->radix; } while (value > 1.0); } len = endp - cp; fill = fmt->width - len; if (prefix != 0) { fill -= strlen(prefix); } leadingZeros = (fmt->precision > len) ? fmt->precision - len : 0; fill -= leadingZeros; if (!(fmt->flags & SPRINTF_LEFT)) { c = (fmt->flags & SPRINTF_LEAD_ZERO) ? '0': ' '; for (i = 0; i < fill; i++) { BPUT(ctx, loc, fmt, c); } } if (prefix != 0) { BPUT(ctx, loc, fmt, prefix); } for (i = 0; i < leadingZeros; i++) { BPUT(ctx, loc, fmt, '0'); } BPUT(ctx, loc, fmt, cp); if (fmt->flags & SPRINTF_LEFT) { for (i = 0; i < fill; i++) { BPUT(ctx, loc, fmt, ' '); } }#else char numBuf[64]; if (specChar == 'f') { sprintf(numBuf, "%*.*f", fmt->width, fmt->precision, value); } else if (specChar == 'g') { sprintf(numBuf, "%*.*g", fmt->width, fmt->precision, value); } else if (specChar == 'e') { sprintf(numBuf, "%*.*e", fmt->width, fmt->precision, value); } for (cp = numBuf; *cp; cp++) { BPUT(ctx, loc, fmt, *cp); }#endif}#endif /* BLD_FEATURE_FLOATING_POINT *//******************************************************************************//* * Grow the buffer to fit new data. Return 1 if the buffer can grow. * Grow using the growBy size specified when creating the buffer. */static int growBuf(MPR_LOC_DEC(ctx, loc), Format *fmt){ uchar *newbuf; int buflen; buflen = fmt->endbuf - fmt->buf; if (fmt->maxsize >= 0 && buflen >= fmt->maxsize) { return 0; } if (fmt->growBy < 0) { /* * User supplied buffer */ return 0; } newbuf = (uchar*) mprAlloc(ctx, buflen + fmt->growBy); if (fmt->buf) { memcpy(newbuf, fmt->buf, buflen); mprFree(fmt->buf); } buflen += fmt->growBy; fmt->end = newbuf + (fmt->end - fmt->buf); fmt->start = newbuf + (fmt->start - fmt->buf); fmt->buf = newbuf; fmt->endbuf = &fmt->buf[buflen]; /* * Increase growBy to reduce overhead */ if ((buflen + (fmt->growBy * 2)) < fmt->maxsize) { fmt->growBy *= 2; } return 1;}/******************************************************************************//* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim:tw=78 * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -