aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2017-10-09 15:20:42 -0700
committerAndres Freund <andres@anarazel.de>2017-10-09 15:20:42 -0700
commit8a241792f968ed5be6cf4d41e32c0d264f6c0c65 (patch)
tree80391a1d37c432548e50d07a58a484278c202f74 /src
parent71c75ddfbb277362bf62dc5b1645c3903e16bc34 (diff)
downloadpostgresql-8a241792f968ed5be6cf4d41e32c0d264f6c0c65.tar.gz
postgresql-8a241792f968ed5be6cf4d41e32c0d264f6c0c65.zip
Add pg_strnlen() a portable implementation of strlen.
As the OS version is likely going to be more optimized, fall back to it if available, as detected by configure.
Diffstat (limited to 'src')
-rw-r--r--src/common/string.c20
-rw-r--r--src/include/common/string.h15
-rw-r--r--src/include/pg_config.h.in3
-rw-r--r--src/include/pg_config.h.win323
-rw-r--r--src/port/snprintf.c12
5 files changed, 43 insertions, 10 deletions
diff --git a/src/common/string.c b/src/common/string.c
index 159d9ea7b62..901821f3d87 100644
--- a/src/common/string.c
+++ b/src/common/string.c
@@ -41,3 +41,23 @@ pg_str_endswith(const char *str, const char *end)
str += slen - elen;
return strcmp(str, end) == 0;
}
+
+
+/*
+ * Portable version of posix' strnlen.
+ *
+ * Returns the number of characters before a null-byte in the string pointed
+ * to by str, unless there's no null-byte before maxlen. In the latter case
+ * maxlen is returned.
+ */
+#ifndef HAVE_STRNLEN
+size_t
+pg_strnlen(const char *str, size_t maxlen)
+{
+ const char *p = str;
+
+ while (maxlen-- > 0 && *p)
+ p++;
+ return p - str;
+}
+#endif
diff --git a/src/include/common/string.h b/src/include/common/string.h
index 5f3ea71d613..3d46b80918c 100644
--- a/src/include/common/string.h
+++ b/src/include/common/string.h
@@ -12,4 +12,19 @@
extern bool pg_str_endswith(const char *str, const char *end);
+/*
+ * Portable version of posix' strnlen.
+ *
+ * Returns the number of characters before a null-byte in the string pointed
+ * to by str, unless there's no null-byte before maxlen. In the latter case
+ * maxlen is returned.
+ *
+ * Use the system strnlen if provided, it's likely to be faster.
+ */
+#ifdef HAVE_STRNLEN
+#define pg_strnlen(str, maxlen) strnlen(str, maxlen)
+#else
+extern size_t pg_strnlen(const char *str, size_t maxlen);
+#endif
+
#endif /* COMMON_STRING_H */
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 368a297e6df..d20cc47fde0 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -496,6 +496,9 @@
/* Define to 1 if you have the `strlcpy' function. */
#undef HAVE_STRLCPY
+/* Define to 1 if you have the `strnlen' function. */
+#undef HAVE_STRNLEN
+
/* Define to use have a strong random number source */
#undef HAVE_STRONG_RANDOM
diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32
index 3537b6f7045..58eef0a538e 100644
--- a/src/include/pg_config.h.win32
+++ b/src/include/pg_config.h.win32
@@ -345,6 +345,9 @@
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
+/* Define to 1 if you have the `strnlen' function. */
+#define HAVE_STRNLEN
+
/* Define to use have a strong random number source */
#define HAVE_STRONG_RANDOM 1
diff --git a/src/port/snprintf.c b/src/port/snprintf.c
index 231e5d6bdb4..531d2c5ee35 100644
--- a/src/port/snprintf.c
+++ b/src/port/snprintf.c
@@ -43,6 +43,8 @@
#endif
#include <sys/param.h>
+#include "common/string.h"
+
#ifndef NL_ARGMAX
#define NL_ARGMAX 16
#endif
@@ -790,16 +792,6 @@ bad_format:
target->failed = true;
}
-static size_t
-pg_strnlen(const char *str, size_t maxlen)
-{
- const char *p = str;
-
- while (maxlen-- > 0 && *p)
- p++;
- return p - str;
-}
-
static void
fmtstr(char *value, int leftjust, int minlen, int maxwidth,
int pointflag, PrintfTarget *target)