Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8a24179

Browse files
committed
Add pg_strnlen() a portable implementation of strlen.
As the OS version is likely going to be more optimized, fall back toit if available, as detected by configure.
1 parent71c75dd commit8a24179

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

‎configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8777,7 +8777,7 @@ fi
87778777

87788778

87798779

8780-
forac_funcin strerror_r getpwuid_r gethostbyname_r
8780+
forac_funcin strerror_r getpwuid_r gethostbyname_r strnlen
87818781
do:
87828782
as_ac_var=`$as_echo"ac_cv_func_$ac_func"|$as_tr_sh`
87838783
ac_fn_c_check_func"$LINENO""$ac_func""$as_ac_var"

‎configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ LIBS="$LIBS $PTHREAD_LIBS"
961961
AC_CHECK_HEADER(pthread.h, [], [AC_MSG_ERROR([
962962
pthread.h not found; use --disable-thread-safety to disable thread safety])])
963963

964-
AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r])
964+
AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r strnlen])
965965

966966
# Do test here with the proper thread flags
967967
PGAC_FUNC_STRERROR_R_INT

‎src/common/string.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ pg_str_endswith(const char *str, const char *end)
4141
str+=slen-elen;
4242
returnstrcmp(str,end)==0;
4343
}
44+
45+
46+
/*
47+
* Portable version of posix' strnlen.
48+
*
49+
* Returns the number of characters before a null-byte in the string pointed
50+
* to by str, unless there's no null-byte before maxlen. In the latter case
51+
* maxlen is returned.
52+
*/
53+
#ifndefHAVE_STRNLEN
54+
size_t
55+
pg_strnlen(constchar*str,size_tmaxlen)
56+
{
57+
constchar*p=str;
58+
59+
while (maxlen-->0&&*p)
60+
p++;
61+
returnp-str;
62+
}
63+
#endif

‎src/include/common/string.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@
1212

1313
externboolpg_str_endswith(constchar*str,constchar*end);
1414

15+
/*
16+
* Portable version of posix' strnlen.
17+
*
18+
* Returns the number of characters before a null-byte in the string pointed
19+
* to by str, unless there's no null-byte before maxlen. In the latter case
20+
* maxlen is returned.
21+
*
22+
* Use the system strnlen if provided, it's likely to be faster.
23+
*/
24+
#ifdefHAVE_STRNLEN
25+
#definepg_strnlen(str,maxlen) strnlen(str, maxlen)
26+
#else
27+
externsize_tpg_strnlen(constchar*str,size_tmaxlen);
28+
#endif
29+
1530
#endif/* COMMON_STRING_H */

‎src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@
496496
/* Define to 1 if you have the `strlcpy' function. */
497497
#undef HAVE_STRLCPY
498498

499+
/* Define to 1 if you have the `strnlen' function. */
500+
#undef HAVE_STRNLEN
501+
499502
/* Define to use have a strong random number source */
500503
#undef HAVE_STRONG_RANDOM
501504

‎src/include/pg_config.h.win32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@
345345
/* Define to 1 if you have the <string.h> header file. */
346346
#define HAVE_STRING_H 1
347347

348+
/* Define to 1 if you have the `strnlen' function. */
349+
#define HAVE_STRNLEN
350+
348351
/* Define to use have a strong random number source */
349352
#define HAVE_STRONG_RANDOM 1
350353

‎src/port/snprintf.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#endif
4444
#include<sys/param.h>
4545

46+
#include"common/string.h"
47+
4648
#ifndefNL_ARGMAX
4749
#defineNL_ARGMAX 16
4850
#endif
@@ -790,16 +792,6 @@ dopr(PrintfTarget *target, const char *format, va_list args)
790792
target->failed= true;
791793
}
792794

793-
staticsize_t
794-
pg_strnlen(constchar*str,size_tmaxlen)
795-
{
796-
constchar*p=str;
797-
798-
while (maxlen-->0&&*p)
799-
p++;
800-
returnp-str;
801-
}
802-
803795
staticvoid
804796
fmtstr(char*value,intleftjust,intminlen,intmaxwidth,
805797
intpointflag,PrintfTarget*target)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp