1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-07 00:36:50 +03:00

Move strtoint() to common

Several places used similar code to convert a string to an int, so take
the function that we already had and make it globally available.

Reviewed-by: Michael Paquier <michael@paquier.xyz>
This commit is contained in:
Peter Eisentraut
2018-03-13 10:21:09 -04:00
parent 6cf86f4354
commit 17bb625017
9 changed files with 39 additions and 49 deletions

View File

@ -4,4 +4,5 @@
/pgstrcasecmp.c
/rint.c
/snprintf.c
/string.c
/strnlen.c

View File

@ -32,6 +32,7 @@ SHLIB_EXPORTS = exports.txt
OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \
pgstrcasecmp.o \
$(filter rint.o snprintf.o strnlen.o, $(LIBOBJS)) \
string.o \
$(WIN32RES)
all: all-lib
@ -47,6 +48,9 @@ include $(top_srcdir)/src/Makefile.shlib
pgstrcasecmp.c rint.c snprintf.c strnlen.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
string.c: % : $(top_srcdir)/src/common/%
rm -f $@ && $(LN_S) $< .
install: all installdirs install-lib
installdirs: installdirs-lib
@ -54,6 +58,6 @@ installdirs: installdirs-lib
uninstall: uninstall-lib
clean distclean: clean-lib
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c string.c
maintainer-clean: distclean maintainer-clean-lib

View File

@ -9,25 +9,13 @@
#error -ffast-math is known to break this code
#endif
#include "common/string.h"
#include "extern.h"
#include "dt.h"
#include "pgtypes_error.h"
#include "pgtypes_interval.h"
/* copy&pasted from .../src/backend/utils/adt/datetime.c */
static int
strtoint(const char *nptr, char **endptr, int base)
{
long val;
val = strtol(nptr, endptr, base);
#ifdef HAVE_LONG_INT_64
if (val != (long) ((int32) val))
errno = ERANGE;
#endif
return (int) val;
}
/* copy&pasted from .../src/backend/utils/adt/datetime.c
* and changesd struct pg_tm to struct tm
*/

View File

@ -21,6 +21,8 @@
#include <ctype.h>
#include <limits.h>
#include "common/string.h"
#include "extern.h"
#include "preproc.h"
}
@ -727,14 +729,12 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
return PARAM;
}
<C,SQL>{integer} {
long val;
int val;
char* endptr;
errno = 0;
val = strtol((char *)yytext, &endptr,10);
if (*endptr != '\0' || errno == ERANGE ||
/* check for overflow of int */
val != (int) val)
val = strtoint(yytext, &endptr, 10);
if (*endptr != '\0' || errno == ERANGE)
{
errno = 0;
base_yylval.str = mm_strdup(yytext);