aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert2019-06-25 14:53:39 -0700
committerPaul Eggert2019-06-25 14:55:08 -0700
commitd7c6836288c91bb639956cb8c748dd6597c55cd4 (patch)
tree687f6f257d3cb5f4bd54eb634aef2737eb9c0856 /lib
parent349b778dde7f583a92dd1531aef3ff5c336e9aee (diff)
downloademacs-d7c6836288c91bb639956cb8c748dd6597c55cd4.tar.gz
emacs-d7c6836288c91bb639956cb8c748dd6597c55cd4.zip
Avoid some strlen work, primarily via strnlen
* admin/merge-gnulib (GNULIB_MODULES): Add strnlen. * lib-src/etags.c (find_entries): * src/emacs.c (main): * src/nsmenu.m (parseKeyEquiv:): * src/nsterm.m (ns_xlfd_to_fontname): * src/term.c (vfatal): Prefer !*X to !strlen (X). * lib-src/etags.c (pfnote, add_regex): * lib-src/pop.c (pop_open): * lib-src/update-game-score.c (main): * lwlib/lwlib.c (lw_separator_p): * src/doprnt.c (doprnt): * src/emacs.c (main): * src/inotify.c (inotifyevent_to_event): * src/keyboard.c (menu_separator_name_p, parse_tool_bar_item): * src/sysdep.c (get_current_dir_name_or_unreachable): * src/xdisp.c (store_mode_line_string): Use strnlen to avoid unnecessary work with strlen. * lib-src/etags.c (Prolog_functions, prolog_pr) (Erlang_functions, erlang_func): Prefer ptrdiff_t to size_t when either will do. (prolog_pr, erlang_func): New arg LASTLEN, to avoid unnecessary strlen call. All callers changed. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate. * lib/strnlen.c, m4/strnlen.m4: New files, copied from Gnulib. * lwlib/lwlib.c (lw_separator_p): * src/json.c (json_has_prefix): Use strncmp to avoid unecessary work with strlen + memcmp. * src/process.c (set_socket_option): Use SBYTES instead of strlen.
Diffstat (limited to 'lib')
-rw-r--r--lib/gnulib.mk.in14
-rw-r--r--lib/strnlen.c30
2 files changed, 42 insertions, 2 deletions
diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in
index b9a32d9dbc1..b4d510bd62e 100644
--- a/lib/gnulib.mk.in
+++ b/lib/gnulib.mk.in
@@ -136,6 +136,7 @@
136# stddef \ 136# stddef \
137# stdio \ 137# stdio \
138# stpcpy \ 138# stpcpy \
139# strnlen \
139# strtoimax \ 140# strtoimax \
140# symlink \ 141# symlink \
141# sys_stat \ 142# sys_stat \
@@ -1075,7 +1076,6 @@ gl_GNULIB_ENABLED_getdtablesize = @gl_GNULIB_ENABLED_getdtablesize@
1075gl_GNULIB_ENABLED_getgroups = @gl_GNULIB_ENABLED_getgroups@ 1076gl_GNULIB_ENABLED_getgroups = @gl_GNULIB_ENABLED_getgroups@
1076gl_GNULIB_ENABLED_malloca = @gl_GNULIB_ENABLED_malloca@ 1077gl_GNULIB_ENABLED_malloca = @gl_GNULIB_ENABLED_malloca@
1077gl_GNULIB_ENABLED_open = @gl_GNULIB_ENABLED_open@ 1078gl_GNULIB_ENABLED_open = @gl_GNULIB_ENABLED_open@
1078gl_GNULIB_ENABLED_pathmax = @gl_GNULIB_ENABLED_pathmax@
1079gl_GNULIB_ENABLED_strtoll = @gl_GNULIB_ENABLED_strtoll@ 1079gl_GNULIB_ENABLED_strtoll = @gl_GNULIB_ENABLED_strtoll@
1080gl_LIBOBJS = @gl_LIBOBJS@ 1080gl_LIBOBJS = @gl_LIBOBJS@
1081gl_LTLIBOBJS = @gl_LTLIBOBJS@ 1081gl_LTLIBOBJS = @gl_LTLIBOBJS@
@@ -1110,7 +1110,6 @@ pdfdir = @pdfdir@
1110prefix = @prefix@ 1110prefix = @prefix@
1111program_transform_name = @program_transform_name@ 1111program_transform_name = @program_transform_name@
1112psdir = @psdir@ 1112psdir = @psdir@
1113runstatedir = @runstatedir@
1114sbindir = @sbindir@ 1113sbindir = @sbindir@
1115sharedstatedir = @sharedstatedir@ 1114sharedstatedir = @sharedstatedir@
1116srcdir = @srcdir@ 1115srcdir = @srcdir@
@@ -2811,6 +2810,17 @@ EXTRA_DIST += string.in.h
2811endif 2810endif
2812## end gnulib module string 2811## end gnulib module string
2813 2812
2813## begin gnulib module strnlen
2814ifeq (,$(OMIT_GNULIB_MODULE_strnlen))
2815
2816
2817EXTRA_DIST += strnlen.c
2818
2819EXTRA_libgnu_a_SOURCES += strnlen.c
2820
2821endif
2822## end gnulib module strnlen
2823
2814## begin gnulib module strtoimax 2824## begin gnulib module strtoimax
2815ifeq (,$(OMIT_GNULIB_MODULE_strtoimax)) 2825ifeq (,$(OMIT_GNULIB_MODULE_strtoimax))
2816 2826
diff --git a/lib/strnlen.c b/lib/strnlen.c
new file mode 100644
index 00000000000..9fb663509a0
--- /dev/null
+++ b/lib/strnlen.c
@@ -0,0 +1,30 @@
1/* Find the length of STRING, but scan at most MAXLEN characters.
2 Copyright (C) 2005-2007, 2009-2019 Free Software Foundation, Inc.
3 Written by Simon Josefsson.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17
18#include <config.h>
19
20#include <string.h>
21
22/* Find the length of STRING, but scan at most MAXLEN characters.
23 If no '\0' terminator is found in that many characters, return MAXLEN. */
24
25size_t
26strnlen (const char *string, size_t maxlen)
27{
28 const char *end = memchr (string, '\0', maxlen);
29 return end ? (size_t) (end - string) : maxlen;
30}