PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 1500 of file snprintf.c.

1501{
1502 if (is_negative)
1503 {
1504 *signvalue = '-';
1505 return true;
1506 }
1507 else if (forcesign)
1508 *signvalue = '+';
1509 return false;
1510}

Referenced by fmtfloat(), and fmtint().

◆ compute_padlen()

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

Definition at line 1514 of file snprintf.c.

1515{
1516 int padlen;
1517
1518 padlen = minlen - vallen;
1519 if (padlen < 0)
1520 padlen = 0;
1521 if (leftjust)
1522 padlen = -padlen;
1523 return padlen;
1524}

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#ifdef WIN32
561 case 'I':
562 /* Windows PRI*{32,64,PTR} size */
563 if (format[0] == '3' && format[1] == '2')
564 format += 2;
565 else if (format[0] == '6' && format[1] == '4')
566 {
567 format += 2;
568 longlongflag = 1;
569 }
570 else
571 {
572#if SIZEOF_VOID_P == SIZEOF_LONG
573 longflag = 1;
574#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
575 longlongflag = 1;
576#else
577#error "cannot find integer type of the same size as intptr_t"
578#endif
579 }
580 goto nextch2;
581#endif
582 case 'l':
583 if (longflag)
584 longlongflag = 1;
585 else
586 longflag = 1;
587 goto nextch2;
588 case 'z':
589#if SIZEOF_SIZE_T == SIZEOF_LONG
590 longflag = 1;
591#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
592 longlongflag = 1;
593#else
594#error "cannot find integer type of the same size as size_t"
595#endif
596 goto nextch2;
597 case 'h':
598 case '\'':
599 /* ignore these */
600 goto nextch2;
601 case 'd':
602 case 'i':
603 if (!have_star)
604 {
605 if (pointflag)
606 precision = accum;
607 else
608 fieldwidth = accum;
609 }
610 if (have_dollar)
611 {
612 if (longlongflag)
613 numvalue = argvalues[fmtpos].ll;
614 else if (longflag)
615 numvalue = argvalues[fmtpos].l;
616 else
617 numvalue = argvalues[fmtpos].i;
618 }
619 else
620 {
621 if (longlongflag)
622 numvalue = va_arg(args, long long);
623 else if (longflag)
624 numvalue = va_arg(args, long);
625 else
626 numvalue = va_arg(args, int);
627 }
628 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
629 precision, pointflag, target);
630 break;
631 case 'o':
632 case 'u':
633 case 'x':
634 case 'X':
635 if (!have_star)
636 {
637 if (pointflag)
638 precision = accum;
639 else
640 fieldwidth = accum;
641 }
642 if (have_dollar)
643 {
644 if (longlongflag)
645 numvalue = (unsigned long long) argvalues[fmtpos].ll;
646 else if (longflag)
647 numvalue = (unsigned long) argvalues[fmtpos].l;
648 else
649 numvalue = (unsigned int) argvalues[fmtpos].i;
650 }
651 else
652 {
653 if (longlongflag)
654 numvalue = (unsigned long long) va_arg(args, long long);
655 else if (longflag)
656 numvalue = (unsigned long) va_arg(args, long);
657 else
658 numvalue = (unsigned int) va_arg(args, int);
659 }
660 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
661 precision, pointflag, target);
662 break;
663 case 'c':
664 if (!have_star)
665 {
666 if (pointflag)
667 precision = accum;
668 else
669 fieldwidth = accum;
670 }
671 if (have_dollar)
672 cvalue = (unsigned char) argvalues[fmtpos].i;
673 else
674 cvalue = (unsigned char) va_arg(args, int);
675 fmtchar(cvalue, leftjust, fieldwidth, target);
676 break;
677 case 's':
678 if (!have_star)
679 {
680 if (pointflag)
681 precision = accum;
682 else
683 fieldwidth = accum;
684 }
685 if (have_dollar)
686 strvalue = argvalues[fmtpos].cptr;
687 else
688 strvalue = va_arg(args, char *);
689 /* If string is NULL, silently substitute "(null)" */
690 if (strvalue == NULL)
691 strvalue = "(null)";
692 fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
693 target);
694 break;
695 case 'p':
696 /* fieldwidth/leftjust are ignored ... */
697 if (have_dollar)
698 strvalue = argvalues[fmtpos].cptr;
699 else
700 strvalue = va_arg(args, char *);
701 fmtptr((const void *) strvalue, target);
702 break;
703 case 'e':
704 case 'E':
705 case 'f':
706 case 'g':
707 case 'G':
708 if (!have_star)
709 {
710 if (pointflag)
711 precision = accum;
712 else
713 fieldwidth = accum;
714 }
715 if (have_dollar)
716 fvalue = argvalues[fmtpos].d;
717 else
718 fvalue = va_arg(args, double);
719 fmtfloat(fvalue, ch, forcesign, leftjust,
720 fieldwidth, zpad,
721 precision, pointflag,
722 target);
723 break;
724 case 'm':
725 {
726 char errbuf[PG_STRERROR_R_BUFLEN];
727 const char *errm = strerror_r(save_errno,
728 errbuf, sizeof(errbuf));
729
730 dostr(errm, strlen(errm), target);
731 }
732 break;
733 case '%':
734 dopr_outch('%', target);
735 break;
736 default:
737
738 /*
739 * Anything else --- in particular, '\0' indicating end of
740 * format string --- is bogus.
741 */
742 goto bad_format;
743 }
744
745 /* Check for failure after each conversion spec */
746 if (target->failed)
747 break;
748 }
749
750 return;
751
752bad_format:
753 errno = EINVAL;
754 target->failed = true;
755}
int i
Definition: isn.c:77
static char format
#define PG_STRERROR_R_BUFLEN
Definition: port.h:257
#define strerror_r
Definition: port.h:256
static void dostr(const char *str, int slen, PrintfTarget *target)
Definition: snprintf.c:1410
static void dopr_outch(int c, PrintfTarget *target)
Definition: snprintf.c:1447
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:1043
static void fmtptr(const void *value, PrintfTarget *target)
Definition: snprintf.c:1029
static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
Definition: snprintf.c:1154
static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
Definition: snprintf.c:1000
#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:1172
static bool find_arguments(const char *format, va_list args, PrintfArgValue *argvalues)
Definition: snprintf.c:764
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 1447 of file snprintf.c.

1448{
1449 if (target->bufend != NULL && target->bufptr >= target->bufend)
1450 {
1451 /* buffer full, can we dump to stream? */
1452 if (target->stream == NULL)
1453 {
1454 target->nchars++; /* no, lose the data */
1455 return;
1456 }
1457 flushbuffer(target);
1458 }
1459 *(target->bufptr++) = c;
1460}
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 1463 of file snprintf.c.

1464{
1465 /* fast path for common case of slen == 1 */
1466 if (slen == 1)
1467 {
1468 dopr_outch(c, target);
1469 return;
1470 }
1471
1472 while (slen > 0)
1473 {
1474 int avail;
1475
1476 if (target->bufend != NULL)
1477 avail = target->bufend - target->bufptr;
1478 else
1479 avail = slen;
1480 if (avail <= 0)
1481 {
1482 /* buffer full, can we dump to stream? */
1483 if (target->stream == NULL)
1484 {
1485 target->nchars += slen; /* no, lose the data */
1486 return;
1487 }
1488 flushbuffer(target);
1489 continue;
1490 }
1491 avail = Min(avail, slen);
1492 memset(target->bufptr, c, avail);
1493 target->bufptr += avail;
1494 slen -= avail;
1495 }
1496}
#define Min(x, y)
Definition: c.h:975

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 1410 of file snprintf.c.

1411{
1412 /* fast path for common case of slen == 1 */
1413 if (slen == 1)
1414 {
1415 dopr_outch(*str, 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 memmove(target->bufptr, str, avail);
1440 target->bufptr += avail;
1441 str += avail;
1442 slen -= avail;
1443 }
1444}
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 764 of file snprintf.c.

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

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 1154 of file snprintf.c.

1155{
1156 int padlen; /* amount to pad */
1157
1158 padlen = compute_padlen(minlen, 1, leftjust);
1159
1160 if (padlen > 0)
1161 {
1162 dopr_outchmulti(' ', padlen, target);
1163 padlen = 0;
1164 }
1165
1166 dopr_outch(value, target);
1167
1168 trailing_pad(padlen, target);
1169}
static struct @165 value
static int compute_padlen(int minlen, int vallen, int leftjust)
Definition: snprintf.c:1514
static void trailing_pad(int padlen, PrintfTarget *target)
Definition: snprintf.c:1564
static void dopr_outchmulti(int c, int slen, PrintfTarget *target)
Definition: snprintf.c:1463

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 1172 of file snprintf.c.

1175{
1176 int signvalue = 0;
1177 int prec;
1178 int vallen;
1179 char fmt[8];
1180 char convert[1024];
1181 int zeropadlen = 0; /* amount to pad with zeroes */
1182 int padlen; /* amount to pad with spaces */
1183
1184 /*
1185 * We rely on the regular C library's snprintf to do the basic conversion,
1186 * then handle padding considerations here.
1187 *
1188 * The dynamic range of "double" is about 1E+-308 for IEEE math, and not
1189 * too wildly more than that with other hardware. In "f" format, snprintf
1190 * could therefore generate at most 308 characters to the left of the
1191 * decimal point; while we need to allow the precision to get as high as
1192 * 308+17 to ensure that we don't truncate significant digits from very
1193 * small values. To handle both these extremes, we use a buffer of 1024
1194 * bytes and limit requested precision to 350 digits; this should prevent
1195 * buffer overrun even with non-IEEE math. If the original precision
1196 * request was more than 350, separately pad with zeroes.
1197 *
1198 * We handle infinities and NaNs specially to ensure platform-independent
1199 * output.
1200 */
1201 if (precision < 0) /* cover possible overflow of "accum" */
1202 precision = 0;
1203 prec = Min(precision, 350);
1204
1205 if (isnan(value))
1206 {
1207 strcpy(convert, "NaN");
1208 vallen = 3;
1209 /* no zero padding, regardless of precision spec */
1210 }
1211 else
1212 {
1213 /*
1214 * Handle sign (NaNs have no sign, so we don't do this in the case
1215 * above). "value < 0.0" will not be true for IEEE minus zero, so we
1216 * detect that by looking for the case where value equals 0.0
1217 * according to == but not according to memcmp.
1218 */
1219 static const double dzero = 0.0;
1220
1221 if (adjust_sign((value < 0.0 ||
1222 (value == 0.0 &&
1223 memcmp(&value, &dzero, sizeof(double)) != 0)),
1224 forcesign, &signvalue))
1225 value = -value;
1226
1227 if (isinf(value))
1228 {
1229 strcpy(convert, "Infinity");
1230 vallen = 8;
1231 /* no zero padding, regardless of precision spec */
1232 }
1233 else if (pointflag)
1234 {
1235 zeropadlen = precision - prec;
1236 fmt[0] = '%';
1237 fmt[1] = '.';
1238 fmt[2] = '*';
1239 fmt[3] = type;
1240 fmt[4] = '\0';
1241 vallen = snprintf(convert, sizeof(convert), fmt, prec, value);
1242 }
1243 else
1244 {
1245 fmt[0] = '%';
1246 fmt[1] = type;
1247 fmt[2] = '\0';
1248 vallen = snprintf(convert, sizeof(convert), fmt, value);
1249 }
1250 if (vallen < 0)
1251 goto fail;
1252
1253 /*
1254 * Windows, alone among our supported platforms, likes to emit
1255 * three-digit exponent fields even when two digits would do. Hack
1256 * such results to look like the way everyone else does it.
1257 */
1258#ifdef WIN32
1259 if (vallen >= 6 &&
1260 convert[vallen - 5] == 'e' &&
1261 convert[vallen - 3] == '0')
1262 {
1263 convert[vallen - 3] = convert[vallen - 2];
1264 convert[vallen - 2] = convert[vallen - 1];
1265 vallen--;
1266 }
1267#endif
1268 }
1269
1270 padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust);
1271
1272 leading_pad(zpad, signvalue, &padlen, target);
1273
1274 if (zeropadlen > 0)
1275 {
1276 /* If 'e' or 'E' format, inject zeroes before the exponent */
1277 char *epos = strrchr(convert, 'e');
1278
1279 if (!epos)
1280 epos = strrchr(convert, 'E');
1281 if (epos)
1282 {
1283 /* pad before exponent */
1284 dostr(convert, epos - convert, target);
1285 dopr_outchmulti('0', zeropadlen, target);
1286 dostr(epos, vallen - (epos - convert), target);
1287 }
1288 else
1289 {
1290 /* no exponent, pad after the digits */
1291 dostr(convert, vallen, target);
1292 dopr_outchmulti('0', zeropadlen, target);
1293 }
1294 }
1295 else
1296 {
1297 /* no zero padding, just emit the number as-is */
1298 dostr(convert, vallen, target);
1299 }
1300
1301 trailing_pad(padlen, target);
1302 return;
1303
1304fail:
1305 target->failed = true;
1306}
#define snprintf
Definition: port.h:239
static void leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
Definition: snprintf.c:1528
static int adjust_sign(int is_negative, int forcesign, int *signvalue)
Definition: snprintf.c:1500
const char * type
static void convert(const int32 val, char *const buf)
Definition: zic.c:1992

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 1043 of file snprintf.c.

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

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 1029 of file snprintf.c.

1030{
1031 int vallen;
1032 char convert[64];
1033
1034 /* we rely on regular C library's snprintf to do the basic conversion */
1035 vallen = snprintf(convert, sizeof(convert), "%p", value);
1036 if (vallen < 0)
1037 target->failed = true;
1038 else
1039 dostr(convert, vallen, target);
1040}

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 1000 of file snprintf.c.

1002{
1003 int padlen,
1004 vallen; /* amount to pad */
1005
1006 /*
1007 * If a maxwidth (precision) is specified, we must not fetch more bytes
1008 * than that.
1009 */
1010 if (pointflag)
1011 vallen = strnlen(value, maxwidth);
1012 else
1013 vallen = strlen(value);
1014
1015 padlen = compute_padlen(minlen, vallen, leftjust);
1016
1017 if (padlen > 0)
1018 {
1019 dopr_outchmulti(' ', padlen, target);
1020 padlen = 0;
1021 }
1022
1023 dostr(value, vallen, target);
1024
1025 trailing_pad(padlen, target);
1026}
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 1528 of file snprintf.c.

1529{
1530 int maxpad;
1531
1532 if (*padlen > 0 && zpad)
1533 {
1534 if (signvalue)
1535 {
1536 dopr_outch(signvalue, target);
1537 --(*padlen);
1538 signvalue = 0;
1539 }
1540 if (*padlen > 0)
1541 {
1542 dopr_outchmulti(zpad, *padlen, target);
1543 *padlen = 0;
1544 }
1545 }
1546 maxpad = (signvalue != 0);
1547 if (*padlen > maxpad)
1548 {
1549 dopr_outchmulti(' ', *padlen - maxpad, target);
1550 *padlen = maxpad;
1551 }
1552 if (signvalue)
1553 {
1554 dopr_outch(signvalue, target);
1555 if (*padlen > 0)
1556 --(*padlen);
1557 else if (*padlen < 0)
1558 ++(*padlen);
1559 }
1560}

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 1318 of file snprintf.c.

1319{
1320 PrintfTarget target;
1321 int signvalue = 0;
1322 int vallen;
1323 char fmt[8];
1324 char convert[64];
1325
1326 /* Set up the target like pg_snprintf, but require nonempty buffer */
1327 Assert(count > 0);
1328 target.bufstart = target.bufptr = str;
1329 target.bufend = str + count - 1;
1330 target.stream = NULL;
1331 target.nchars = 0;
1332 target.failed = false;
1333
1334 /*
1335 * We bound precision to a reasonable range; the combination of this and
1336 * the knowledge that we're using "g" format without padding allows the
1337 * convert[] buffer to be reasonably small.
1338 */
1339 if (precision < 1)
1340 precision = 1;
1341 else if (precision > 32)
1342 precision = 32;
1343
1344 /*
1345 * The rest is just an inlined version of the fmtfloat() logic above,
1346 * simplified using the knowledge that no padding is wanted.
1347 */
1348 if (isnan(value))
1349 {
1350 strcpy(convert, "NaN");
1351 vallen = 3;
1352 }
1353 else
1354 {
1355 static const double dzero = 0.0;
1356
1357 if (value < 0.0 ||
1358 (value == 0.0 &&
1359 memcmp(&value, &dzero, sizeof(double)) != 0))
1360 {
1361 signvalue = '-';
1362 value = -value;
1363 }
1364
1365 if (isinf(value))
1366 {
1367 strcpy(convert, "Infinity");
1368 vallen = 8;
1369 }
1370 else
1371 {
1372 fmt[0] = '%';
1373 fmt[1] = '.';
1374 fmt[2] = '*';
1375 fmt[3] = 'g';
1376 fmt[4] = '\0';
1377 vallen = snprintf(convert, sizeof(convert), fmt, precision, value);
1378 if (vallen < 0)
1379 {
1380 target.failed = true;
1381 goto fail;
1382 }
1383
1384#ifdef WIN32
1385 if (vallen >= 6 &&
1386 convert[vallen - 5] == 'e' &&
1387 convert[vallen - 3] == '0')
1388 {
1389 convert[vallen - 3] = convert[vallen - 2];
1390 convert[vallen - 2] = convert[vallen - 1];
1391 vallen--;
1392 }
1393#endif
1394 }
1395 }
1396
1397 if (signvalue)
1398 dopr_outch(signvalue, &target);
1399
1400 dostr(convert, vallen, &target);
1401
1402fail:
1403 *(target.bufptr) = '\0';
1404 return target.failed ? -1 : (target.bufptr - target.bufstart
1405 + target.nchars);
1406}
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 1564 of file snprintf.c.

1565{
1566 if (padlen < 0)
1567 dopr_outchmulti(' ', -padlen, target);
1568}

References dopr_outchmulti().

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