diff options
-rw-r--r-- | ChangeLog | 16 | ||||
-rwxr-xr-x | MODULES.html.sh | 1 | ||||
-rw-r--r-- | lib/mbstok_r.c | 71 | ||||
-rw-r--r-- | lib/string_.h | 28 | ||||
-rw-r--r-- | m4/mbstok_r.m4 | 16 | ||||
-rw-r--r-- | m4/string_h.m4 | 1 | ||||
-rw-r--r-- | modules/mbstok_r | 31 | ||||
-rw-r--r-- | modules/string | 1 |
8 files changed, 162 insertions, 3 deletions
@@ -1,5 +1,19 @@ 2007-02-04 Bruno Haible <bruno@clisp.org> + New module mbstok_r. + * modules/mbstok_r: New file. + * lib/mbstok_r.c: New file. + * lib/string_.h (strtok_r): Change argument names to match the + comments. Add a conditional link warning. + (mbstok_r): New declaration. + * m4/mbstok_r.m4: New file. + * m4/string_h.m4 (gl_STRING_MODULE_INDICATOR_DEFAULTS): Initialize + GNULIB_MBSTOK_R. + * modules/string (string.h): Also substitute GNULIB_MBSTOK_R. + * MODULES.html.sh (Internationalization functions): Add mbstok_r. + +2007-02-04 Bruno Haible <bruno@clisp.org> + New module mbsspn. * modules/mbsspn: New file. * lib/mbsspn.c: New file. @@ -7,7 +21,7 @@ (mbsspn): New declaration. * m4/mbsspn.m4: New file. * m4/string_h.m4 (gl_STRING_MODULE_INDICATOR_DEFAULTS): Initialize - GNULIB_MBSCSPN. + GNULIB_MBSSPN. * modules/string (string.h): Also substitute GNULIB_MBSSPN. * MODULES.html.sh (Internationalization functions): Add mbsspn. diff --git a/MODULES.html.sh b/MODULES.html.sh index aa40216c34..22779e2c21 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2168,6 +2168,7 @@ func_all_modules () func_module mbscspn func_module mbspbrk func_module mbsspn + func_module mbstok_r func_module mbswidth func_module memcasecmp func_module memcoll diff --git a/lib/mbstok_r.c b/lib/mbstok_r.c new file mode 100644 index 0000000000..fb6bdef396 --- /dev/null +++ b/lib/mbstok_r.c @@ -0,0 +1,71 @@ +/* Tokenizing a string. + Copyright (C) 1999, 2002, 2006-2007 Free Software Foundation, Inc. + Written by Bruno Haible <bruno@clisp.org>, 2007. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include <config.h> + +/* Specification. */ +#include <string.h> + +#if HAVE_MBRTOWC +# include "mbuiter.h" +#endif + +char * +mbstok_r (char *string, const char *delim, char **save_ptr) +{ +#if HAVE_MBRTOWC + if (MB_CUR_MAX > 1) + { + if (string == NULL) + { + string = *save_ptr; + if (string == NULL) + return NULL; /* reminder that end of token sequence has been + reached */ + } + + /* Skip leading delimiters. */ + string += mbsspn (string, delim); + + /* Found a token? */ + if (*string == '\0') + { + *save_ptr = NULL; + return NULL; + } + + /* Move past the token. */ + { + char *token_end = mbspbrk (string, delim); + + if (token_end != NULL) + { + /* NUL-terminate the token. */ + *token_end = '\0'; + *save_ptr = token_end + 1; + } + else + *save_ptr = NULL; + } + + return string; + } + else +#endif + return strtok_r (string, delim, save_ptr); +} diff --git a/lib/string_.h b/lib/string_.h index 78b27647fe..2aaf067bdc 100644 --- a/lib/string_.h +++ b/lib/string_.h @@ -325,8 +325,14 @@ extern char *strcasestr (const char *haystack, const char *needle); See also strsep(). */ #if @GNULIB_STRTOK_R@ # if ! @HAVE_DECL_STRTOK_R@ -extern char *strtok_r (char *restrict __s, char const *restrict __sep, - char **restrict __lasts); +extern char *strtok_r (char *restrict s, char const *restrict delim, + char **restrict save_ptr); +# endif +# if defined GNULIB_POSIXCHECK +# undef strtok_r +# define strtok_r(s,d,p) \ + (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings in multibyte locales - use mbstok_r if you care about internationalization"), \ + strtok_r (s, d, p)) # endif #elif defined GNULIB_POSIXCHECK # undef strtok_r @@ -409,6 +415,24 @@ extern char * mbspbrk (const char *string, const char *accept); extern size_t mbsspn (const char *string, const char *reject); #endif +#if @GNULIB_MBSTOK_R@ +/* Parse the character string STRING into tokens separated by characters in + the character string DELIM. + If STRING is NULL, the saved pointer in SAVE_PTR is used as + the next starting point. For example: + char s[] = "-abc-=-def"; + char *sp; + x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def" + x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL + x = mbstok_r(NULL, "=", &sp); // x = NULL + // s = "abc\0-def\0" + + Caveat: It modifies the original string. + Caveat: These functions cannot be used on constant strings. + Caveat: The identity of the delimiting character is lost. */ +extern char * mbstok_r (char *string, const char *delim, char **save_ptr); +#endif + #ifdef __cplusplus } diff --git a/m4/mbstok_r.m4 b/m4/mbstok_r.m4 new file mode 100644 index 0000000000..b1a03da17e --- /dev/null +++ b/m4/mbstok_r.m4 @@ -0,0 +1,16 @@ +# mbstok_r.m4 serial 1 +dnl Copyright (C) 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_MBSTOK_R], +[ + gl_PREREQ_MBSTOK_R +]) + +# Prerequisites of lib/mbstok_r.c. +AC_DEFUN([gl_PREREQ_MBSTOK_R], [ + AC_REQUIRE([gl_FUNC_MBRTOWC]) + : +]) diff --git a/m4/string_h.m4 b/m4/string_h.m4 index bcb99e7066..14ed54ad54 100644 --- a/m4/string_h.m4 +++ b/m4/string_h.m4 @@ -75,4 +75,5 @@ AC_DEFUN([gl_STRING_MODULE_INDICATOR_DEFAULTS], GNULIB_MBSCSPN=0; AC_SUBST([GNULIB_MBSCSPN]) GNULIB_MBSPBRK=0; AC_SUBST([GNULIB_MBSPBRK]) GNULIB_MBSSPN=0; AC_SUBST([GNULIB_MBSSPN]) + GNULIB_MBSTOK_R=0; AC_SUBST([GNULIB_MBSTOK_R]) ]) diff --git a/modules/mbstok_r b/modules/mbstok_r new file mode 100644 index 0000000000..2aab0848bd --- /dev/null +++ b/modules/mbstok_r @@ -0,0 +1,31 @@ +Description: +mbstok_r() function: split string into tokens, thread safe. + +Files: +lib/mbstok_r.c +m4/mbstok_r.m4 +m4/mbrtowc.m4 + +Depends-on: +mbuiter +string +mbsspn +mbspbrk +strtok_r + +configure.ac: +gl_FUNC_MBSTOK_R +gl_STRING_MODULE_INDICATOR([mbstok_r]) + +Makefile.am: +lib_SOURCES += mbstok_r.c + +Include: +<string.h> + +License: +LGPL + +Maintainer: +Bruno Haible + diff --git a/modules/string b/modules/string index b4acff4290..4e1229c8e9 100644 --- a/modules/string +++ b/modules/string @@ -29,6 +29,7 @@ string.h: string_.h -e 's|@''GNULIB_MBSCSPN''@|$(GNULIB_MBSCSPN)|g' \ -e 's|@''GNULIB_MBSPBRK''@|$(GNULIB_MBSPBRK)|g' \ -e 's|@''GNULIB_MBSSPN''@|$(GNULIB_MBSSPN)|g' \ + -e 's|@''GNULIB_MBSTOK_R''@|$(GNULIB_MBSTOK_R)|g' \ -e 's|@''GNULIB_MEMMEM''@|$(GNULIB_MEMMEM)|g' \ -e 's|@''GNULIB_MEMPCPY''@|$(GNULIB_MEMPCPY)|g' \ -e 's|@''GNULIB_MEMRCHR''@|$(GNULIB_MEMRCHR)|g' \ |