PostgreSQL Source Code git master
snprintf.c File Reference
#include "c.h"
#include <math.h>
Include dependency graph for snprintf.c:

Go to the source code of this file.

Data Structures

struct  PrintfTarget
 
union  PrintfArgValue
 

Macros

#define PG_NL_ARGMAX   31
 
#define strchrnul   pg_strchrnul
 

Enumerations

enum  PrintfArgType {
  ATYPE_NONE = 0 , ATYPE_INT , ATYPE_LONG , ATYPE_LONGLONG ,
  ATYPE_DOUBLE , ATYPE_CHARPTR
}
 

Functions

static void flushbuffer (PrintfTarget *target)
 
static void dopr (PrintfTarget *target, const char *format, va_list args)
 
int pg_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
 
int pg_snprintf (char *str, size_t count, const char *fmt,...)
 
int pg_vsprintf (char *str, const char *fmt, va_list args)
 
int pg_sprintf (char *str, const char *fmt,...)
 
int pg_vfprintf (FILE *stream, const char *fmt, va_list args)
 
int pg_fprintf (FILE *stream, const char *fmt,...)
 
int pg_vprintf (const char *fmt, va_list args)
 
int pg_printf (const char *fmt,...)
 
static bool find_arguments (const char *format, va_list args, PrintfArgValue *argvalues)
 
static void fmtstr (const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
 
static void fmtptr (const void *value, PrintfTarget *target)
 
static void fmtint (long long value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
 
static void fmtchar (int value, int leftjust, int minlen, PrintfTarget *target)
 
static void fmtfloat (double value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
 
static void dostr (const char *str, int slen, PrintfTarget *target)
 
static void dopr_outch (int c, PrintfTarget *target)
 
static void dopr_outchmulti (int c, int slen, PrintfTarget *target)
 
static int adjust_sign (int is_negative, int forcesign, int *signvalue)
 
static int compute_padlen (int minlen, int vallen, int leftjust)
 
static void leading_pad (int zpad, int signvalue, int *padlen, PrintfTarget *target)
 
static void trailing_pad (int padlen, PrintfTarget *target)
 
static const char * strchrnul (const char *s, int c)
 
int pg_strfromd (char *str, size_t count, int precision, double value)
 

Macro Definition Documentation

◆ PG_NL_ARGMAX

#define PG_NL_ARGMAX   31

Definition at line 44 of file snprintf.c.

◆ strchrnul

#define strchrnul   pg_strchrnul

Definition at line 356 of file snprintf.c.

Enumeration Type Documentation

◆ PrintfArgType

Enumerator
ATYPE_NONE 
ATYPE_INT 
ATYPE_LONG 
ATYPE_LONGLONG 
ATYPE_DOUBLE 
ATYPE_CHARPTR 

Definition at line 142 of file snprintf.c.

143{
144 ATYPE_NONE = 0,
145 ATYPE_INT,
PrintfArgType
Definition: snprintf.c:143
@ ATYPE_LONGLONG
Definition: snprintf.c:147
@ ATYPE_INT
Definition: snprintf.c:145
@ ATYPE_LONG
Definition: snprintf.c:146
@ ATYPE_CHARPTR
Definition: snprintf.c:149
@ ATYPE_NONE
Definition: snprintf.c:144
@ ATYPE_DOUBLE
Definition: snprintf.c:148

Function Documentation

◆ adjust_sign()

static int adjust_sign ( int  is_negative,
int  forcesign,
int *  signvalue 
)
static

Definition at line 1447 of file snprintf.c.

1448{
1449 if (is_negative)
1450 {
1451 *signvalue = '-';
1452 return true;
1453 }
1454 else if (forcesign)
1455 *signvalue = '+';
1456 return false;
1457}

Referenced by fmtfloat(), and fmtint().

◆ compute_padlen()

static int compute_padlen ( int  minlen,
int  vallen,
int  leftjust 
)
static

Definition at line 1461 of file snprintf.c.

1462{
1463 int padlen;
1464
1465 padlen = minlen - vallen;
1466 if (padlen < 0)
1467 padlen = 0;
1468 if (leftjust)
1469 padlen = -padlen;
1470 return padlen;
1471}

Referenced by fmtchar(), fmtfloat(), fmtint(), and fmtstr().

◆ dopr()

static void dopr ( PrintfTarget target,
const char *  format,
va_list  args 
)
static

Definition at line 373 of file snprintf.c.

374{
375 int save_errno = errno;
376 const char *first_pct = NULL;
377 int ch;
378 bool have_dollar;
379 bool have_star;
380 bool afterstar;
381 int accum;
382 int longlongflag;
383 int longflag;
384 int pointflag;
385 int leftjust;
386 int fieldwidth;
387 int precision;
388 int zpad;
389 int forcesign;
390 int fmtpos;
391 int cvalue;
392 long long numvalue;
393 double fvalue;
394 const char *strvalue;
395 PrintfArgValue argvalues[PG_NL_ARGMAX + 1];
396
397 /*
398 * Initially, we suppose the format string does not use %n$. The first
399 * time we come to a conversion spec that has that, we'll call
400 * find_arguments() to check for consistent use of %n$ and fill the
401 * argvalues array with the argument values in the correct order.
402 */
403 have_dollar = false;
404
405 while (*format != '\0')
406 {
407 /* Locate next conversion specifier */
408 if (*format != '%')
409 {
410 /* Scan to next '%' or end of string */
411 const char *next_pct = strchrnul(format + 1, '%');
412
413 /* Dump literal data we just scanned over */
414 dostr(format, next_pct - format, target);
415 if (target->failed)
416 break;
417
418 if (*next_pct == '\0')
419 break;
420 format = next_pct;
421 }
422
423 /*
424 * Remember start of first conversion spec; if we find %n$, then it's
425 * sufficient for find_arguments() to start here, without rescanning
426 * earlier literal text.
427 */
428 if (first_pct == NULL)
429 first_pct = format;
430
431 /* Process conversion spec starting at *format */
432 format++;
433
434 /* Fast path for conversion spec that is exactly %s */
435 if (*format == 's')
436 {
437 format++;
438 strvalue = va_arg(args, char *);
439 if (strvalue == NULL)
440 strvalue = "(null)";
441 dostr(strvalue, strlen(strvalue), target);
442 if (target->failed)
443 break;
444 continue;
445 }
446
447 fieldwidth = precision = zpad = leftjust = forcesign = 0;
448 longflag = longlongflag = pointflag = 0;
449 fmtpos = accum = 0;
450 have_star = afterstar = false;
451nextch2:
452 ch = *format++;
453 switch (ch)
454 {
455 case '-':
456 leftjust = 1;
457 goto nextch2;
458 case '+':
459 forcesign = 1;
460 goto nextch2;
461 case '0':
462 /* set zero padding if no nonzero digits yet */
463 if (accum == 0 && !pointflag)
464 zpad = '0';
465 /* FALL THRU */
466 case '1':
467 case '2':
468 case '3':
469 case '4':
470 case '5':
471 case '6':
472 case '7':
473 case '8':
474 case '9':
475 accum = accum * 10 + (ch - '0');
476 goto nextch2;
477 case '.':
478 if (have_star)
479 have_star = false;
480 else
481 fieldwidth = accum;
482 pointflag = 1;
483 accum = 0;
484 goto nextch2;
485 case '*':
486 if (have_dollar)
487 {
488 /*
489 * We'll process value after reading n$. Note it's OK to
490 * assume have_dollar is set correctly, because in a valid
491 * format string the initial % must have had n$ if * does.
492 */
493 afterstar = true;
494 }
495 else
496 {
497 /* fetch and process value now */
498 int starval = va_arg(args, int);
499
500 if (pointflag)
501 {
502 precision = starval;
503 if (precision < 0)
504 {
505 precision = 0;
506 pointflag = 0;
507 }
508 }
509 else
510 {
511 fieldwidth = starval;
512 if (fieldwidth < 0)
513 {
514 leftjust = 1;
515 fieldwidth = -fieldwidth;
516 }
517 }
518 }
519 have_star = true;
520 accum = 0;
521 goto nextch2;
522 case '$':
523 /* First dollar sign? */
524 if (!have_dollar)
525 {
526 /* Yup, so examine all conversion specs in format */
527 if (!find_arguments(first_pct, args, argvalues))
528 goto bad_format;
529 have_dollar = true;
530 }
531 if (afterstar)
532 {
533 /* fetch and process star value */
534 int starval = argvalues[accum].i;
535
536 if (pointflag)
537 {
538 precision = starval;
539 if (precision < 0)
540 {
541 precision = 0;
542 pointflag = 0;
543 }
544 }
545 else
546 {
547 fieldwidth = starval;
548 if (fieldwidth < 0)
549 {
550 leftjust = 1;
551 fieldwidth = -fieldwidth;
552 }
553 }
554 afterstar = false;
555 }
556 else
557 fmtpos = accum;
558 accum = 0;
559 goto nextch2;
560 case 'l':
561 if (longflag)
562 longlongflag = 1;
563 else
564 longflag = 1;
565 goto nextch2;
566 case 'j':
567#if SIZEOF_INTMAX_T == SIZEOF_LONG
568 longflag = 1;
569#elif SIZEOF_INTMAX_T == SIZEOF_LONG_LONG
570 longlongflag = 1;
571#else
572#error "cannot find integer type of the same size as intmax_t"
573#endif
574 goto nextch2;
575 case 'z':
576#if SIZEOF_SIZE_T == SIZEOF_LONG
577 longflag = 1;
578#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
579 longlongflag = 1;
580#else
581#error "cannot find integer type of the same size as size_t"
582#endif
583 goto nextch2;
584 case 'h':
585 case '\'':
586 /* ignore these */
587 goto nextch2;
588 case 'd':
589 case 'i':
590 if (!have_star)
591 {
592 if (pointflag)
593 precision = accum;
594 else
595 fieldwidth = accum;
596 }
597 if (have_dollar)
598 {
599 if (longlongflag)
600 numvalue = argvalues[fmtpos].ll;
601 else if (longflag)
602 numvalue = argvalues[fmtpos].l;
603 else
604 numvalue = argvalues[fmtpos].i;
605 }
606 else
607 {
608 if (longlongflag)
609 numvalue = va_arg(args, long long);
610 else if (longflag)
611 numvalue = va_arg(args, long);
612 else
613 numvalue = va_arg(args, int);
614 }
615 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
616 precision, pointflag, target);
617 break;
618 case 'o':
619 case 'u':
620 case 'x':
621 case 'X':
622 if (!have_star)
623 {
624 if (pointflag)
625 precision = accum;
626 else
627 fieldwidth = accum;
628 }
629 if (have_dollar)
630 {
631 if (longlongflag)
632 numvalue = (unsigned long long) argvalues[fmtpos].ll;
633 else if (longflag)
634 numvalue = (unsigned long) argvalues[fmtpos].l;
635 else
636 numvalue = (unsigned int) argvalues[fmtpos].i;
637 }
638 else
639 {
640 if (longlongflag)
641 numvalue = (unsigned long long) va_arg(args, long long);
642 else if (longflag)
643 numvalue = (unsigned long) va_arg(args, long);
644 else
645 numvalue = (unsigned int) va_arg(args, int);
646 }
647 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
648 precision, pointflag, target);
649 break;
650 case 'c':
651 if (!have_star)
652 {
653 if (pointflag)
654 precision = accum;
655 else
656 fieldwidth = accum;
657 }
658 if (have_dollar)
659 cvalue = (unsigned char) argvalues[fmtpos].i;
660 else
661 cvalue = (unsigned char) va_arg(args, int);
662 fmtchar(cvalue, leftjust, fieldwidth, target);
663 break;
664 case 's':
665 if (!have_star)
666 {
667 if (pointflag)
668 precision = accum;
669 else
670 fieldwidth = accum;
671 }
672 if (have_dollar)
673 strvalue = argvalues[fmtpos].cptr;
674 else
675 strvalue = va_arg(args, char *);
676 /* If string is NULL, silently substitute "(null)" */
677 if (strvalue == NULL)
678 strvalue = "(null)";
679 fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
680 target);
681 break;
682 case 'p':
683 /* fieldwidth/leftjust are ignored ... */
684 if (have_dollar)
685 strvalue = argvalues[fmtpos].cptr;
686 else
687 strvalue = va_arg(args, char *);
688 fmtptr((const void *) strvalue, target);
689 break;
690 case 'e':
691 case 'E':
692 case 'f':
693 case 'g':
694 case 'G':
695 if (!have_star)
696 {
697 if (pointflag)
698 precision = accum;
699 else
700 fieldwidth = accum;
701 }
702 if (have_dollar)
703 fvalue = argvalues[fmtpos].d;
704 else
705 fvalue = va_arg(args, double);
706 fmtfloat(fvalue, ch, forcesign, leftjust,
707 fieldwidth, zpad,
708 precision, pointflag,
709 target);
710 break;
711 case 'm':
712 {
713 char errbuf[PG_STRERROR_R_BUFLEN];
714 const char *errm = strerror_r(save_errno,
715 errbuf, sizeof(errbuf));
716
717 dostr(errm, strlen(errm), target);
718 }
719 break;
720 case '%':
721 dopr_outch('%', target);
722 break;
723 default:
724
725 /*
726 * Anything else --- in particular, '\0' indicating end of
727 * format string --- is bogus.
728 */
729 goto bad_format;
730 }
731
732 /* Check for failure after each conversion spec */
733 if (target->failed)
734 break;
735 }
736
737 return;
738
739bad_format:
740 errno = EINVAL;
741 target->failed = true;
742}
int i
Definition: isn.c:77
static char format
#define PG_STRERROR_R_BUFLEN
Definition: port.h:278
#define strerror_r
Definition: port.h:277
static void dostr(const char *str, int slen, PrintfTarget *target)
Definition: snprintf.c:1357
static void dopr_outch(int c, PrintfTarget *target)
Definition: snprintf.c:1394
static void fmtint(long long value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
Definition: snprintf.c:1017
static void fmtptr(const void *value, PrintfTarget *target)
Definition: snprintf.c:1003
static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
Definition: snprintf.c:1128
static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
Definition: snprintf.c:974
#define strchrnul
Definition: snprintf.c:356
#define PG_NL_ARGMAX
Definition: snprintf.c:44
static void fmtfloat(double value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
Definition: snprintf.c:1146
static bool find_arguments(const char *format, va_list args, PrintfArgValue *argvalues)
Definition: snprintf.c:751
bool failed
Definition: snprintf.c:133
long long ll
Definition: snprintf.c:156
char * cptr
Definition: snprintf.c:158

References generate_unaccent_rules::args, PrintfArgValue::cptr, PrintfArgValue::d, dopr_outch(), dostr(), PrintfTarget::failed, find_arguments(), fmtchar(), fmtfloat(), fmtint(), fmtptr(), fmtstr(), format, i, PrintfArgValue::i, PrintfArgValue::l, PrintfArgValue::ll, PG_NL_ARGMAX, PG_STRERROR_R_BUFLEN, strchrnul, and strerror_r.

Referenced by pg_vfprintf(), pg_vsnprintf(), and pg_vsprintf().

◆ dopr_outch()

static void dopr_outch ( int  c,
PrintfTarget target 
)
static

Definition at line 1394 of file snprintf.c.

1395{
1396 if (target->bufend != NULL && target->bufptr >= target->bufend)
1397 {
1398 /* buffer full, can we dump to stream? */
1399 if (target->stream == NULL)
1400 {
1401 target->nchars++; /* no, lose the data */
1402 return;
1403 }
1404 flushbuffer(target);
1405 }
1406 *(target->bufptr++) = c;
1407}
char * c
static void flushbuffer(PrintfTarget *target)
Definition: snprintf.c:298
char * bufend
Definition: snprintf.c:129
char * bufptr
Definition: snprintf.c:127
FILE * stream
Definition: snprintf.c:131

References PrintfTarget::bufend, PrintfTarget::bufptr, flushbuffer(), PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by dopr(), dopr_outchmulti(), dostr(), fmtchar(), leading_pad(), and pg_strfromd().

◆ dopr_outchmulti()

static void dopr_outchmulti ( int  c,
int  slen,
PrintfTarget target 
)
static

Definition at line 1410 of file snprintf.c.

1411{
1412 /* fast path for common case of slen == 1 */
1413 if (slen == 1)
1414 {
1415 dopr_outch(c, target);
1416 return;
1417 }
1418
1419 while (slen > 0)
1420 {
1421 int avail;
1422
1423 if (target->bufend != NULL)
1424 avail = target->bufend - target->bufptr;
1425 else
1426 avail = slen;
1427 if (avail <= 0)
1428 {
1429 /* buffer full, can we dump to stream? */
1430 if (target->stream == NULL)
1431 {
1432 target->nchars += slen; /* no, lose the data */
1433 return;
1434 }
1435 flushbuffer(target);
1436 continue;
1437 }
1438 avail = Min(avail, slen);
1439 memset(target->bufptr, c, avail);
1440 target->bufptr += avail;
1441 slen -= avail;
1442 }
1443}
#define Min(x, y)
Definition: c.h:1016

References PrintfTarget::bufend, PrintfTarget::bufptr, dopr_outch(), flushbuffer(), Min, PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by fmtchar(), fmtfloat(), fmtint(), fmtstr(), leading_pad(), and trailing_pad().

◆ dostr()

static void dostr ( const char *  str,
int  slen,
PrintfTarget target 
)
static

Definition at line 1357 of file snprintf.c.

1358{
1359 /* fast path for common case of slen == 1 */
1360 if (slen == 1)
1361 {
1362 dopr_outch(*str, target);
1363 return;
1364 }
1365
1366 while (slen > 0)
1367 {
1368 int avail;
1369
1370 if (target->bufend != NULL)
1371 avail = target->bufend - target->bufptr;
1372 else
1373 avail = slen;
1374 if (avail <= 0)
1375 {
1376 /* buffer full, can we dump to stream? */
1377 if (target->stream == NULL)
1378 {
1379 target->nchars += slen; /* no, lose the data */
1380 return;
1381 }
1382 flushbuffer(target);
1383 continue;
1384 }
1385 avail = Min(avail, slen);
1386 memmove(target->bufptr, str, avail);
1387 target->bufptr += avail;
1388 str += avail;
1389 slen -= avail;
1390 }
1391}
const char * str

References PrintfTarget::bufend, PrintfTarget::bufptr, dopr_outch(), flushbuffer(), Min, PrintfTarget::nchars, str, and PrintfTarget::stream.

Referenced by dopr(), fmtfloat(), fmtint(), fmtptr(), fmtstr(), and pg_strfromd().

◆ find_arguments()

static bool find_arguments ( const char *  format,
va_list  args,
PrintfArgValue argvalues 
)
static

Definition at line 751 of file snprintf.c.

753{
754 int ch;
755 bool afterstar;
756 int accum;
757 int longlongflag;
758 int longflag;
759 int fmtpos;
760 int i;
761 int last_dollar = 0; /* Init to "no dollar arguments known" */
762 PrintfArgType argtypes[PG_NL_ARGMAX + 1] = {0};
763
764 /*
765 * This loop must accept the same format strings as the one in dopr().
766 * However, we don't need to analyze them to the same level of detail.
767 *
768 * Since we're only called if there's a dollar-type spec somewhere, we can
769 * fail immediately if we find a non-dollar spec. Per the C99 standard,
770 * all argument references in the format string must be one or the other.
771 */
772 while (*format != '\0')
773 {
774 /* Locate next conversion specifier */
775 if (*format != '%')
776 {
777 /* Unlike dopr, we can just quit if there's no more specifiers */
778 format = strchr(format + 1, '%');
779 if (format == NULL)
780 break;
781 }
782
783 /* Process conversion spec starting at *format */
784 format++;
785 longflag = longlongflag = 0;
786 fmtpos = accum = 0;
787 afterstar = false;
788nextch1:
789 ch = *format++;
790 switch (ch)
791 {
792 case '-':
793 case '+':
794 goto nextch1;
795 case '0':
796 case '1':
797 case '2':
798 case '3':
799 case '4':
800 case '5':
801 case '6':
802 case '7':
803 case '8':
804 case '9':
805 accum = accum * 10 + (ch - '0');
806 goto nextch1;
807 case '.':
808 accum = 0;
809 goto nextch1;
810 case '*':
811 if (afterstar)
812 return false; /* previous star missing dollar */
813 afterstar = true;
814 accum = 0;
815 goto nextch1;
816 case '$':
817 if (accum <= 0 || accum > PG_NL_ARGMAX)
818 return false;
819 if (afterstar)
820 {
821 if (argtypes[accum] &&
822 argtypes[accum] != ATYPE_INT)
823 return false;
824 argtypes[accum] = ATYPE_INT;
825 last_dollar = Max(last_dollar, accum);
826 afterstar = false;
827 }
828 else
829 fmtpos = accum;
830 accum = 0;
831 goto nextch1;
832 case 'l':
833 if (longflag)
834 longlongflag = 1;
835 else
836 longflag = 1;
837 goto nextch1;
838 case 'j':
839#if SIZEOF_INTMAX_T == SIZEOF_LONG
840 longflag = 1;
841#elif SIZEOF_INTMAX_T == SIZEOF_LONG_LONG
842 longlongflag = 1;
843#else
844#error "cannot find integer type of the same size as intmax_t"
845#endif
846 goto nextch1;
847 case 'z':
848#if SIZEOF_SIZE_T == SIZEOF_LONG
849 longflag = 1;
850#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
851 longlongflag = 1;
852#else
853#error "cannot find integer type of the same size as size_t"
854#endif
855 goto nextch1;
856 case 'h':
857 case '\'':
858 /* ignore these */
859 goto nextch1;
860 case 'd':
861 case 'i':
862 case 'o':
863 case 'u':
864 case 'x':
865 case 'X':
866 if (fmtpos)
867 {
868 PrintfArgType atype;
869
870 if (longlongflag)
871 atype = ATYPE_LONGLONG;
872 else if (longflag)
873 atype = ATYPE_LONG;
874 else
875 atype = ATYPE_INT;
876 if (argtypes[fmtpos] &&
877 argtypes[fmtpos] != atype)
878 return false;
879 argtypes[fmtpos] = atype;
880 last_dollar = Max(last_dollar, fmtpos);
881 }
882 else
883 return false; /* non-dollar conversion spec */
884 break;
885 case 'c':
886 if (fmtpos)
887 {
888 if (argtypes[fmtpos] &&
889 argtypes[fmtpos] != ATYPE_INT)
890 return false;
891 argtypes[fmtpos] = ATYPE_INT;
892 last_dollar = Max(last_dollar, fmtpos);
893 }
894 else
895 return false; /* non-dollar conversion spec */
896 break;
897 case 's':
898 case 'p':
899 if (fmtpos)
900 {
901 if (argtypes[fmtpos] &&
902 argtypes[fmtpos] != ATYPE_CHARPTR)
903 return false;
904 argtypes[fmtpos] = ATYPE_CHARPTR;
905 last_dollar = Max(last_dollar, fmtpos);
906 }
907 else
908 return false; /* non-dollar conversion spec */
909 break;
910 case 'e':
911 case 'E':
912 case 'f':
913 case 'g':
914 case 'G':
915 if (fmtpos)
916 {
917 if (argtypes[fmtpos] &&
918 argtypes[fmtpos] != ATYPE_DOUBLE)
919 return false;
920 argtypes[fmtpos] = ATYPE_DOUBLE;
921 last_dollar = Max(last_dollar, fmtpos);
922 }
923 else
924 return false; /* non-dollar conversion spec */
925 break;
926 case 'm':
927 case '%':
928 break;
929 default:
930 return false; /* bogus format string */
931 }
932
933 /*
934 * If we finish the spec with afterstar still set, there's a
935 * non-dollar star in there.
936 */
937 if (afterstar)
938 return false; /* non-dollar conversion spec */
939 }
940
941 /*
942 * Format appears valid so far, so collect the arguments in physical
943 * order. (Since we rejected any non-dollar specs that would have
944 * collected arguments, we know that dopr() hasn't collected any yet.)
945 */
946 for (i = 1; i <= last_dollar; i++)
947 {
948 switch (argtypes[i])
949 {
950 case ATYPE_NONE:
951 return false;
952 case ATYPE_INT:
953 argvalues[i].i = va_arg(args, int);
954 break;
955 case ATYPE_LONG:
956 argvalues[i].l = va_arg(args, long);
957 break;
958 case ATYPE_LONGLONG:
959 argvalues[i].ll = va_arg(args, long long);
960 break;
961 case ATYPE_DOUBLE:
962 argvalues[i].d = va_arg(args, double);
963 break;
964 case ATYPE_CHARPTR:
965 argvalues[i].cptr = va_arg(args, char *);
966 break;
967 }
968 }
969
970 return true;
971}
#define Max(x, y)
Definition: c.h:1010

References generate_unaccent_rules::args, ATYPE_CHARPTR, ATYPE_DOUBLE, ATYPE_INT, ATYPE_LONG, ATYPE_LONGLONG, ATYPE_NONE, PrintfArgValue::cptr, PrintfArgValue::d, format, i, PrintfArgValue::i, PrintfArgValue::l, PrintfArgValue::ll, Max, and PG_NL_ARGMAX.

Referenced by dopr().

◆ flushbuffer()

static void flushbuffer ( PrintfTarget target)
static

Definition at line 298 of file snprintf.c.

299{
300 size_t nc = target->bufptr - target->bufstart;
301
302 /*
303 * Don't write anything if we already failed; this is to ensure we
304 * preserve the original failure's errno.
305 */
306 if (!target->failed && nc > 0)
307 {
308 size_t written;
309
310 written = fwrite(target->bufstart, 1, nc, target->stream);
311 target->nchars += written;
312 if (written != nc)
313 target->failed = true;
314 }
315 target->bufptr = target->bufstart;
316}
char * bufstart
Definition: snprintf.c:128

References PrintfTarget::bufptr, PrintfTarget::bufstart, PrintfTarget::failed, PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by dopr_outch(), dopr_outchmulti(), dostr(), and pg_vfprintf().

◆ fmtchar()

static void fmtchar ( int  value,
int  leftjust,
int  minlen,
PrintfTarget target 
)
static

Definition at line 1128 of file snprintf.c.

1129{
1130 int padlen; /* amount to pad */
1131
1132 padlen = compute_padlen(minlen, 1, leftjust);
1133
1134 if (padlen > 0)
1135 {
1136 dopr_outchmulti(' ', padlen, target);
1137 padlen = 0;
1138 }
1139
1140 dopr_outch(value, target);
1141
1142 trailing_pad(padlen, target);
1143}
static struct @171 value
static int compute_padlen(int minlen, int vallen, int leftjust)
Definition: snprintf.c:1461
static void trailing_pad(int padlen, PrintfTarget *target)
Definition: snprintf.c:1511
static void dopr_outchmulti(int c, int slen, PrintfTarget *target)
Definition: snprintf.c:1410

References compute_padlen(), dopr_outch(), dopr_outchmulti(), trailing_pad(), and value.

Referenced by dopr(), and rfmtlong().

◆ fmtfloat()

static void fmtfloat ( double  value,
char  type,
int  forcesign,
int  leftjust,
int  minlen,
int  zpad,
int  precision,
int  pointflag,
PrintfTarget target 
)
static

Definition at line 1146 of file snprintf.c.

1149{
1150 int signvalue = 0;
1151 int prec;
1152 int vallen;
1153 char fmt[8];
1154 char convert[1024];
1155 int zeropadlen = 0; /* amount to pad with zeroes */
1156 int padlen; /* amount to pad with spaces */
1157
1158 /*
1159 * We rely on the regular C library's snprintf to do the basic conversion,
1160 * then handle padding considerations here.
1161 *
1162 * The dynamic range of "double" is about 1E+-308 for IEEE math, and not
1163 * too wildly more than that with other hardware. In "f" format, snprintf
1164 * could therefore generate at most 308 characters to the left of the
1165 * decimal point; while we need to allow the precision to get as high as
1166 * 308+17 to ensure that we don't truncate significant digits from very
1167 * small values. To handle both these extremes, we use a buffer of 1024
1168 * bytes and limit requested precision to 350 digits; this should prevent
1169 * buffer overrun even with non-IEEE math. If the original precision
1170 * request was more than 350, separately pad with zeroes.
1171 *
1172 * We handle infinities and NaNs specially to ensure platform-independent
1173 * output.
1174 */
1175 if (precision < 0) /* cover possible overflow of "accum" */
1176 precision = 0;
1177 prec = Min(precision, 350);
1178
1179 if (isnan(value))
1180 {
1181 strcpy(convert, "NaN");
1182 vallen = 3;
1183 /* no zero padding, regardless of precision spec */
1184 }
1185 else
1186 {
1187 /*
1188 * Handle sign (NaNs have no sign, so we don't do this in the case
1189 * above). "value < 0.0" will not be true for IEEE minus zero, so we
1190 * detect that by looking for the case where value equals 0.0
1191 * according to == but not according to memcmp.
1192 */
1193 static const double dzero = 0.0;
1194
1195 if (adjust_sign((value < 0.0 ||
1196 (value == 0.0 &&
1197 memcmp(&value, &dzero, sizeof(double)) != 0)),
1198 forcesign, &signvalue))
1199 value = -value;
1200
1201 if (isinf(value))
1202 {
1203 strcpy(convert, "Infinity");
1204 vallen = 8;
1205 /* no zero padding, regardless of precision spec */
1206 }
1207 else if (pointflag)
1208 {
1209 zeropadlen = precision - prec;
1210 fmt[0] = '%';
1211 fmt[1] = '.';
1212 fmt[2] = '*';
1213 fmt[3] = type;
1214 fmt[4] = '\0';
1215 vallen = snprintf(convert, sizeof(convert), fmt, prec, value);
1216 }
1217 else
1218 {
1219 fmt[0] = '%';
1220 fmt[1] = type;
1221 fmt[2] = '\0';
1222 vallen = snprintf(convert, sizeof(convert), fmt, value);
1223 }
1224 if (vallen < 0)
1225 goto fail;
1226 }
1227
1228 padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust);
1229
1230 leading_pad(zpad, signvalue, &padlen, target);
1231
1232 if (zeropadlen > 0)
1233 {
1234 /* If 'e' or 'E' format, inject zeroes before the exponent */
1235 char *epos = strrchr(convert, 'e');
1236
1237 if (!epos)
1238 epos = strrchr(convert, 'E');
1239 if (epos)
1240 {
1241 /* pad before exponent */
1242 dostr(convert, epos - convert, target);
1243 dopr_outchmulti('0', zeropadlen, target);
1244 dostr(epos, vallen - (epos - convert), target);
1245 }
1246 else
1247 {
1248 /* no exponent, pad after the digits */
1249 dostr(convert, vallen, target);
1250 dopr_outchmulti('0', zeropadlen, target);
1251 }
1252 }
1253 else
1254 {
1255 /* no zero padding, just emit the number as-is */
1256 dostr(convert, vallen, target);
1257 }
1258
1259 trailing_pad(padlen, target);
1260 return;
1261
1262fail:
1263 target->failed = true;
1264}
#define snprintf
Definition: port.h:260
static void leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
Definition: snprintf.c:1475
static int adjust_sign(int is_negative, int forcesign, int *signvalue)
Definition: snprintf.c:1447
const char * type
static void convert(const int_fast32_t val, char *const buf)
Definition: zic.c:1980

References adjust_sign(), compute_padlen(), convert(), dopr_outchmulti(), dostr(), PrintfTarget::failed, leading_pad(), Min, snprintf, trailing_pad(), type, and value.

Referenced by dopr().

◆ fmtint()

static void fmtint ( long long  value,
char  type,
int  forcesign,
int  leftjust,
int  minlen,
int  zpad,
int  precision,
int  pointflag,
PrintfTarget target 
)
static

Definition at line 1017 of file snprintf.c.

1020{
1021 unsigned long long uvalue;
1022 int base;
1023 int dosign;
1024 const char *cvt = "0123456789abcdef";
1025 int signvalue = 0;
1026 char convert[64];
1027 int vallen = 0;
1028 int padlen; /* amount to pad */
1029 int zeropad; /* extra leading zeroes */
1030
1031 switch (type)
1032 {
1033 case 'd':
1034 case 'i':
1035 base = 10;
1036 dosign = 1;
1037 break;
1038 case 'o':
1039 base = 8;
1040 dosign = 0;
1041 break;
1042 case 'u':
1043 base = 10;
1044 dosign = 0;
1045 break;
1046 case 'x':
1047 base = 16;
1048 dosign = 0;
1049 break;
1050 case 'X':
1051 cvt = "0123456789ABCDEF";
1052 base = 16;
1053 dosign = 0;
1054 break;
1055 default:
1056 return; /* keep compiler quiet */
1057 }
1058
1059 /* disable MSVC warning about applying unary minus to an unsigned value */
1060#ifdef _MSC_VER
1061#pragma warning(push)
1062#pragma warning(disable: 4146)
1063#endif
1064 /* Handle +/- */
1065 if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
1066 uvalue = -(unsigned long long) value;
1067 else
1068 uvalue = (unsigned long long) value;
1069#ifdef _MSC_VER
1070#pragma warning(pop)
1071#endif
1072
1073 /*
1074 * SUS: the result of converting 0 with an explicit precision of 0 is no
1075 * characters
1076 */
1077 if (value == 0 && pointflag && precision == 0)
1078 vallen = 0;
1079 else
1080 {
1081 /*
1082 * Convert integer to string. We special-case each of the possible
1083 * base values so as to avoid general-purpose divisions. On most
1084 * machines, division by a fixed constant can be done much more
1085 * cheaply than a general divide.
1086 */
1087 if (base == 10)
1088 {
1089 do
1090 {
1091 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 10];
1092 uvalue = uvalue / 10;
1093 } while (uvalue);
1094 }
1095 else if (base == 16)
1096 {
1097 do
1098 {
1099 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 16];
1100 uvalue = uvalue / 16;
1101 } while (uvalue);
1102 }
1103 else /* base == 8 */
1104 {
1105 do
1106 {
1107 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 8];
1108 uvalue = uvalue / 8;
1109 } while (uvalue);
1110 }
1111 }
1112
1113 zeropad = Max(0, precision - vallen);
1114
1115 padlen = compute_padlen(minlen, vallen + zeropad, leftjust);
1116
1117 leading_pad(zpad, signvalue, &padlen, target);
1118
1119 if (zeropad > 0)
1120 dopr_outchmulti('0', zeropad, target);
1121
1122 dostr(convert + sizeof(convert) - vallen, vallen, target);
1123
1124 trailing_pad(padlen, target);
1125}

References adjust_sign(), compute_padlen(), convert(), dopr_outchmulti(), dostr(), leading_pad(), Max, trailing_pad(), type, and value.

Referenced by dopr().

◆ fmtptr()

static void fmtptr ( const void *  value,
PrintfTarget target 
)
static

Definition at line 1003 of file snprintf.c.

1004{
1005 int vallen;
1006 char convert[64];
1007
1008 /* we rely on regular C library's snprintf to do the basic conversion */
1009 vallen = snprintf(convert, sizeof(convert), "%p", value);
1010 if (vallen < 0)
1011 target->failed = true;
1012 else
1013 dostr(convert, vallen, target);
1014}

References convert(), dostr(), PrintfTarget::failed, snprintf, and value.

Referenced by dopr().

◆ fmtstr()

static void fmtstr ( const char *  value,
int  leftjust,
int  minlen,
int  maxwidth,
int  pointflag,
PrintfTarget target 
)
static

Definition at line 974 of file snprintf.c.

976{
977 int padlen,
978 vallen; /* amount to pad */
979
980 /*
981 * If a maxwidth (precision) is specified, we must not fetch more bytes
982 * than that.
983 */
984 if (pointflag)
985 vallen = strnlen(value, maxwidth);
986 else
987 vallen = strlen(value);
988
989 padlen = compute_padlen(minlen, vallen, leftjust);
990
991 if (padlen > 0)
992 {
993 dopr_outchmulti(' ', padlen, target);
994 padlen = 0;
995 }
996
997 dostr(value, vallen, target);
998
999 trailing_pad(padlen, target);
1000}
size_t strnlen(const char *str, size_t maxlen)
Definition: strnlen.c:26

References compute_padlen(), dopr_outchmulti(), dostr(), strnlen(), trailing_pad(), and value.

Referenced by dopr(), dtcvfmtasc(), dttofmtasc(), dttofmtasc_replace(), and PGTYPEStimestamp_fmt_asc().

◆ leading_pad()

static void leading_pad ( int  zpad,
int  signvalue,
int *  padlen,
PrintfTarget target 
)
static

Definition at line 1475 of file snprintf.c.

1476{
1477 int maxpad;
1478
1479 if (*padlen > 0 && zpad)
1480 {
1481 if (signvalue)
1482 {
1483 dopr_outch(signvalue, target);
1484 --(*padlen);
1485 signvalue = 0;
1486 }
1487 if (*padlen > 0)
1488 {
1489 dopr_outchmulti(zpad, *padlen, target);
1490 *padlen = 0;
1491 }
1492 }
1493 maxpad = (signvalue != 0);
1494 if (*padlen > maxpad)
1495 {
1496 dopr_outchmulti(' ', *padlen - maxpad, target);
1497 *padlen = maxpad;
1498 }
1499 if (signvalue)
1500 {
1501 dopr_outch(signvalue, target);
1502 if (*padlen > 0)
1503 --(*padlen);
1504 else if (*padlen < 0)
1505 ++(*padlen);
1506 }
1507}

References dopr_outch(), and dopr_outchmulti().

Referenced by fmtfloat(), and fmtint().

◆ pg_fprintf()

int pg_fprintf ( FILE *  stream,
const char *  fmt,
  ... 
)

Definition at line 264 of file snprintf.c.

265{
266 int len;
267 va_list args;
268
269 va_start(args, fmt);
270 len = pg_vfprintf(stream, fmt, args);
271 va_end(args);
272 return len;
273}
const void size_t len
int pg_vfprintf(FILE *stream, const char *fmt, va_list args)
Definition: snprintf.c:242

References generate_unaccent_rules::args, len, and pg_vfprintf().

◆ pg_printf()

int pg_printf ( const char *  fmt,
  ... 
)

Definition at line 282 of file snprintf.c.

283{
284 int len;
285 va_list args;
286
287 va_start(args, fmt);
288 len = pg_vfprintf(stdout, fmt, args);
289 va_end(args);
290 return len;
291}

References generate_unaccent_rules::args, len, pg_vfprintf(), and generate_unaccent_rules::stdout.

◆ pg_snprintf()

int pg_snprintf ( char *  str,
size_t  count,
const char *  fmt,
  ... 
)

Definition at line 202 of file snprintf.c.

203{
204 int len;
205 va_list args;
206
207 va_start(args, fmt);
208 len = pg_vsnprintf(str, count, fmt, args);
209 va_end(args);
210 return len;
211}
int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
Definition: snprintf.c:174

References generate_unaccent_rules::args, len, pg_vsnprintf(), and str.

◆ pg_sprintf()

int pg_sprintf ( char *  str,
const char *  fmt,
  ... 
)

Definition at line 230 of file snprintf.c.

231{
232 int len;
233 va_list args;
234
235 va_start(args, fmt);
236 len = pg_vsprintf(str, fmt, args);
237 va_end(args);
238 return len;
239}
int pg_vsprintf(char *str, const char *fmt, va_list args)
Definition: snprintf.c:214

References generate_unaccent_rules::args, len, pg_vsprintf(), and str.

◆ pg_strfromd()

int pg_strfromd ( char *  str,
size_t  count,
int  precision,
double  value 
)

Definition at line 1276 of file snprintf.c.

1277{
1278 PrintfTarget target;
1279 int signvalue = 0;
1280 int vallen;
1281 char fmt[8];
1282 char convert[64];
1283
1284 /* Set up the target like pg_snprintf, but require nonempty buffer */
1285 Assert(count > 0);
1286 target.bufstart = target.bufptr = str;
1287 target.bufend = str + count - 1;
1288 target.stream = NULL;
1289 target.nchars = 0;
1290 target.failed = false;
1291
1292 /*
1293 * We bound precision to a reasonable range; the combination of this and
1294 * the knowledge that we're using "g" format without padding allows the
1295 * convert[] buffer to be reasonably small.
1296 */
1297 if (precision < 1)
1298 precision = 1;
1299 else if (precision > 32)
1300 precision = 32;
1301
1302 /*
1303 * The rest is just an inlined version of the fmtfloat() logic above,
1304 * simplified using the knowledge that no padding is wanted.
1305 */
1306 if (isnan(value))
1307 {
1308 strcpy(convert, "NaN");
1309 vallen = 3;
1310 }
1311 else
1312 {
1313 static const double dzero = 0.0;
1314
1315 if (value < 0.0 ||
1316 (value == 0.0 &&
1317 memcmp(&value, &dzero, sizeof(double)) != 0))
1318 {
1319 signvalue = '-';
1320 value = -value;
1321 }
1322
1323 if (isinf(value))
1324 {
1325 strcpy(convert, "Infinity");
1326 vallen = 8;
1327 }
1328 else
1329 {
1330 fmt[0] = '%';
1331 fmt[1] = '.';
1332 fmt[2] = '*';
1333 fmt[3] = 'g';
1334 fmt[4] = '\0';
1335 vallen = snprintf(convert, sizeof(convert), fmt, precision, value);
1336 if (vallen < 0)
1337 {
1338 target.failed = true;
1339 goto fail;
1340 }
1341 }
1342 }
1343
1344 if (signvalue)
1345 dopr_outch(signvalue, &target);
1346
1347 dostr(convert, vallen, &target);
1348
1349fail:
1350 *(target.bufptr) = '\0';
1351 return target.failed ? -1 : (target.bufptr - target.bufstart
1352 + target.nchars);
1353}
Assert(PointerIsAligned(start, uint64))

References Assert(), PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, convert(), dopr_outch(), dostr(), PrintfTarget::failed, PrintfTarget::nchars, snprintf, str, PrintfTarget::stream, and value.

Referenced by float4out(), and float8out_internal().

◆ pg_vfprintf()

int pg_vfprintf ( FILE *  stream,
const char *  fmt,
va_list  args 
)

Definition at line 242 of file snprintf.c.

243{
244 PrintfTarget target;
245 char buffer[1024]; /* size is arbitrary */
246
247 if (stream == NULL)
248 {
249 errno = EINVAL;
250 return -1;
251 }
252 target.bufstart = target.bufptr = buffer;
253 target.bufend = buffer + sizeof(buffer); /* use the whole buffer */
254 target.stream = stream;
255 target.nchars = 0;
256 target.failed = false;
257 dopr(&target, fmt, args);
258 /* dump any remaining buffer contents */
259 flushbuffer(&target);
260 return target.failed ? -1 : target.nchars;
261}
static void dopr(PrintfTarget *target, const char *format, va_list args)
Definition: snprintf.c:373

References generate_unaccent_rules::args, PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, dopr(), PrintfTarget::failed, flushbuffer(), PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by pg_fprintf(), pg_printf(), and pg_vprintf().

◆ pg_vprintf()

int pg_vprintf ( const char *  fmt,
va_list  args 
)

Definition at line 276 of file snprintf.c.

277{
278 return pg_vfprintf(stdout, fmt, args);
279}

References generate_unaccent_rules::args, pg_vfprintf(), and generate_unaccent_rules::stdout.

◆ pg_vsnprintf()

int pg_vsnprintf ( char *  str,
size_t  count,
const char *  fmt,
va_list  args 
)

Definition at line 174 of file snprintf.c.

175{
176 PrintfTarget target;
177 char onebyte[1];
178
179 /*
180 * C99 allows the case str == NULL when count == 0. Rather than
181 * special-casing this situation further down, we substitute a one-byte
182 * local buffer. Callers cannot tell, since the function result doesn't
183 * depend on count.
184 */
185 if (count == 0)
186 {
187 str = onebyte;
188 count = 1;
189 }
190 target.bufstart = target.bufptr = str;
191 target.bufend = str + count - 1;
192 target.stream = NULL;
193 target.nchars = 0;
194 target.failed = false;
195 dopr(&target, fmt, args);
196 *(target.bufptr) = '\0';
197 return target.failed ? -1 : (target.bufptr - target.bufstart
198 + target.nchars);
199}

References generate_unaccent_rules::args, PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, dopr(), PrintfTarget::failed, PrintfTarget::nchars, str, and PrintfTarget::stream.

Referenced by pg_snprintf().

◆ pg_vsprintf()

int pg_vsprintf ( char *  str,
const char *  fmt,
va_list  args 
)

Definition at line 214 of file snprintf.c.

215{
216 PrintfTarget target;
217
218 target.bufstart = target.bufptr = str;
219 target.bufend = NULL;
220 target.stream = NULL;
221 target.nchars = 0; /* not really used in this case */
222 target.failed = false;
223 dopr(&target, fmt, args);
224 *(target.bufptr) = '\0';
225 return target.failed ? -1 : (target.bufptr - target.bufstart
226 + target.nchars);
227}

References generate_unaccent_rules::args, PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, dopr(), PrintfTarget::failed, PrintfTarget::nchars, str, and PrintfTarget::stream.

Referenced by pg_sprintf().

◆ strchrnul()

static const char * strchrnul ( const char *  s,
int  c 
)
inlinestatic

Definition at line 359 of file snprintf.c.

360{
361 while (*s != '\0' && *s != c)
362 s++;
363 return s;
364}

◆ trailing_pad()

static void trailing_pad ( int  padlen,
PrintfTarget target 
)
static

Definition at line 1511 of file snprintf.c.

1512{
1513 if (padlen < 0)
1514 dopr_outchmulti(' ', -padlen, target);
1515}

References dopr_outchmulti().

Referenced by fmtchar(), fmtfloat(), fmtint(), and fmtstr().