Add pg_string_endswith as the start of a string helper library in src/common.

Backpatch to 9.3 where src/common was introduce, because a bugfix that
needs to be backpatched, requires the function. Earlier branches will
have to duplicate the code.
This commit is contained in:
Andres Freund 2015-01-03 20:51:52 +01:00
parent 15331540cd
commit bb2e2ce6e2
4 changed files with 60 additions and 2 deletions

View File

@ -23,7 +23,7 @@ include $(top_builddir)/src/Makefile.global
override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
LIBS += $(PTHREAD_LIBS)
OBJS_COMMON = relpath.o
OBJS_COMMON = relpath.o string.o
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o

43
src/common/string.c Normal file
View File

@ -0,0 +1,43 @@
/*-------------------------------------------------------------------------
*
* string.c
* string handling helpers
*
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/common/string.c
*
*-------------------------------------------------------------------------
*/
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
#include "common/string.h"
/*
* Returns whether the string `str' has the postfix `end'.
*/
bool
pg_str_endswith(const char *str, const char *end)
{
size_t slen = strlen(str);
size_t elen = strlen(end);
/* can't be a postfix if longer */
if (elen > slen)
return false;
/* compare the end of the strings */
str += slen - elen;
return strcmp(str, end) == 0;
}

View File

@ -0,0 +1,15 @@
/*
* string.h
* string handling helpers
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/include/common/string.h
*/
#ifndef COMMON_STRING_H
#define COMMON_STRING_H
extern bool pg_str_endswith(const char *str, const char *end);
#endif /* COMMON_STRING_H */

View File

@ -76,7 +76,7 @@ sub mkvcbuild
push(@pgportfiles, 'rint.c') if ($vsVersion < '12.00');
our @pgcommonallfiles = qw(
relpath.c);
relpath.c string.c);
our @pgcommonfrontendfiles = (@pgcommonallfiles, qw(fe_memutils.c));