diff options
author | brane <brane@13f79535-47bb-0310-9956-ffa450edef68> | 2001-05-10 18:05:18 +0000 |
---|---|---|
committer | brane <brane@13f79535-47bb-0310-9956-ffa450edef68> | 2001-05-10 18:05:18 +0000 |
commit | 169842167b0ff0a17603fc3c1a4840c34e3870e1 (patch) | |
tree | ee7fc56eb80479a22c443319979a6a31ba45ca37 /strings | |
parent | 954775b1ea405716e493f2dbe3d17ae5ab199abd (diff) | |
download | libapr-169842167b0ff0a17603fc3c1a4840c34e3870e1.tar.gz |
* apr_pstrndup: Check string length with memchr instead of strlen so
that s doesn't have to be null-terminated.
* Provide memchr implementation if !APR_HAS_MEMCHR.
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@61606 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'strings')
-rw-r--r-- | strings/apr_strings.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/strings/apr_strings.c b/strings/apr_strings.c index c2d9c1fb4..ab52478c5 100644 --- a/strings/apr_strings.c +++ b/strings/apr_strings.c @@ -54,6 +54,7 @@ #include "apr.h" #include "apr_strings.h" +#include "apr_general.h" #include "apr_private.h" #include "apr_lib.h" #ifdef HAVE_STDDEF_H @@ -83,14 +84,14 @@ APR_DECLARE(char *) apr_pstrdup(apr_pool_t *a, const char *s) APR_DECLARE(char *) apr_pstrndup(apr_pool_t *a, const char *s, apr_size_t n) { char *res; - apr_size_t len; + const char *end; if (s == NULL) { return NULL; } - len = strlen(s); - if (len < n) - n = len; + end = memchr(s, '\0', n); + if (end != NULL) + n = end - s; res = apr_palloc(a, n + 1); memcpy(res, s, n); res[n] = '\0'; @@ -147,3 +148,16 @@ APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...) return res; } +#if (!APR_HAVE_MEMCHR) +void *memchr(const void *s, int c, size_t n) +{ + const char *cp; + + for (cp = s; n > 0; n--, cp++) { + if (*cp == c) + return (char *) cp; /* Casting away the const here */ + } + + return NULL; +} +#endif |