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

1430{
1431 if (is_negative)
1432 {
1433 *signvalue = '-';
1434 return true;
1435 }
1436 else if (forcesign)
1437 *signvalue = '+';
1438 return false;
1439}

Referenced by fmtfloat(), and fmtint().

◆ compute_padlen()

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

Definition at line 1443 of file snprintf.c.

1444{
1445 int padlen;
1446
1447 padlen = minlen - vallen;
1448 if (padlen < 0)
1449 padlen = 0;
1450 if (leftjust)
1451 padlen = -padlen;
1452 return padlen;
1453}

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

1377{
1378 if (target->bufend != NULL && target->bufptr >= target->bufend)
1379 {
1380 /* buffer full, can we dump to stream? */
1381 if (target->stream == NULL)
1382 {
1383 target->nchars++; /* no, lose the data */
1384 return;
1385 }
1386 flushbuffer(target);
1387 }
1388 *(target->bufptr++) = c;
1389}
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 1392 of file snprintf.c.

1393{
1394 /* fast path for common case of slen == 1 */
1395 if (slen == 1)
1396 {
1397 dopr_outch(c, target);
1398 return;
1399 }
1400
1401 while (slen > 0)
1402 {
1403 int avail;
1404
1405 if (target->bufend != NULL)
1406 avail = target->bufend - target->bufptr;
1407 else
1408 avail = slen;
1409 if (avail <= 0)
1410 {
1411 /* buffer full, can we dump to stream? */
1412 if (target->stream == NULL)
1413 {
1414 target->nchars += slen; /* no, lose the data */
1415 return;
1416 }
1417 flushbuffer(target);
1418 continue;
1419 }
1420 avail = Min(avail, slen);
1421 memset(target->bufptr, c, avail);
1422 target->bufptr += avail;
1423 slen -= avail;
1424 }
1425}
#define Min(x, y)
Definition: c.h:1008

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

1340{
1341 /* fast path for common case of slen == 1 */
1342 if (slen == 1)
1343 {
1344 dopr_outch(*str, target);
1345 return;
1346 }
1347
1348 while (slen > 0)
1349 {
1350 int avail;
1351
1352 if (target->bufend != NULL)
1353 avail = target->bufend - target->bufptr;
1354 else
1355 avail = slen;
1356 if (avail <= 0)
1357 {
1358 /* buffer full, can we dump to stream? */
1359 if (target->stream == NULL)
1360 {
1361 target->nchars += slen; /* no, lose the data */
1362 return;
1363 }
1364 flushbuffer(target);
1365 continue;
1366 }
1367 avail = Min(avail, slen);
1368 memmove(target->bufptr, str, avail);
1369 target->bufptr += avail;
1370 str += avail;
1371 slen -= avail;
1372 }
1373}
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 742 of file snprintf.c.

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

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

1111{
1112 int padlen; /* amount to pad */
1113
1114 padlen = compute_padlen(minlen, 1, leftjust);
1115
1116 if (padlen > 0)
1117 {
1118 dopr_outchmulti(' ', padlen, target);
1119 padlen = 0;
1120 }
1121
1122 dopr_outch(value, target);
1123
1124 trailing_pad(padlen, target);
1125}
static struct @171 value
static int compute_padlen(int minlen, int vallen, int leftjust)
Definition: snprintf.c:1443
static void trailing_pad(int padlen, PrintfTarget *target)
Definition: snprintf.c:1493
static void dopr_outchmulti(int c, int slen, PrintfTarget *target)
Definition: snprintf.c:1392

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

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

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

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

986{
987 int vallen;
988 char convert[64];
989
990 /* we rely on regular C library's snprintf to do the basic conversion */
991 vallen = snprintf(convert, sizeof(convert), "%p", value);
992 if (vallen < 0)
993 target->failed = true;
994 else
995 dostr(convert, vallen, target);
996}

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

958{
959 int padlen,
960 vallen; /* amount to pad */
961
962 /*
963 * If a maxwidth (precision) is specified, we must not fetch more bytes
964 * than that.
965 */
966 if (pointflag)
967 vallen = strnlen(value, maxwidth);
968 else
969 vallen = strlen(value);
970
971 padlen = compute_padlen(minlen, vallen, leftjust);
972
973 if (padlen > 0)
974 {
975 dopr_outchmulti(' ', padlen, target);
976 padlen = 0;
977 }
978
979 dostr(value, vallen, target);
980
981 trailing_pad(padlen, target);
982}
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 1457 of file snprintf.c.

1458{
1459 int maxpad;
1460
1461 if (*padlen > 0 && zpad)
1462 {
1463 if (signvalue)
1464 {
1465 dopr_outch(signvalue, target);
1466 --(*padlen);
1467 signvalue = 0;
1468 }
1469 if (*padlen > 0)
1470 {
1471 dopr_outchmulti(zpad, *padlen, target);
1472 *padlen = 0;
1473 }
1474 }
1475 maxpad = (signvalue != 0);
1476 if (*padlen > maxpad)
1477 {
1478 dopr_outchmulti(' ', *padlen - maxpad, target);
1479 *padlen = maxpad;
1480 }
1481 if (signvalue)
1482 {
1483 dopr_outch(signvalue, target);
1484 if (*padlen > 0)
1485 --(*padlen);
1486 else if (*padlen < 0)
1487 ++(*padlen);
1488 }
1489}

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

1259{
1260 PrintfTarget target;
1261 int signvalue = 0;
1262 int vallen;
1263 char fmt[8];
1264 char convert[64];
1265
1266 /* Set up the target like pg_snprintf, but require nonempty buffer */
1267 Assert(count > 0);
1268 target.bufstart = target.bufptr = str;
1269 target.bufend = str + count - 1;
1270 target.stream = NULL;
1271 target.nchars = 0;
1272 target.failed = false;
1273
1274 /*
1275 * We bound precision to a reasonable range; the combination of this and
1276 * the knowledge that we're using "g" format without padding allows the
1277 * convert[] buffer to be reasonably small.
1278 */
1279 if (precision < 1)
1280 precision = 1;
1281 else if (precision > 32)
1282 precision = 32;
1283
1284 /*
1285 * The rest is just an inlined version of the fmtfloat() logic above,
1286 * simplified using the knowledge that no padding is wanted.
1287 */
1288 if (isnan(value))
1289 {
1290 strcpy(convert, "NaN");
1291 vallen = 3;
1292 }
1293 else
1294 {
1295 static const double dzero = 0.0;
1296
1297 if (value < 0.0 ||
1298 (value == 0.0 &&
1299 memcmp(&value, &dzero, sizeof(double)) != 0))
1300 {
1301 signvalue = '-';
1302 value = -value;
1303 }
1304
1305 if (isinf(value))
1306 {
1307 strcpy(convert, "Infinity");
1308 vallen = 8;
1309 }
1310 else
1311 {
1312 fmt[0] = '%';
1313 fmt[1] = '.';
1314 fmt[2] = '*';
1315 fmt[3] = 'g';
1316 fmt[4] = '\0';
1317 vallen = snprintf(convert, sizeof(convert), fmt, precision, value);
1318 if (vallen < 0)
1319 {
1320 target.failed = true;
1321 goto fail;
1322 }
1323 }
1324 }
1325
1326 if (signvalue)
1327 dopr_outch(signvalue, &target);
1328
1329 dostr(convert, vallen, &target);
1330
1331fail:
1332 *(target.bufptr) = '\0';
1333 return target.failed ? -1 : (target.bufptr - target.bufstart
1334 + target.nchars);
1335}
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 1493 of file snprintf.c.

1494{
1495 if (padlen < 0)
1496 dopr_outchmulti(' ', -padlen, target);
1497}

References dopr_outchmulti().

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