Fix bug. Remove unnecessary strlen call.
[gnulib.git] / lib / mbsspn.c
1 /* Searching a string for a character outside a given set of characters.
2    Copyright (C) 1999, 2002, 2006-2007 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2007.
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 2, 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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <string.h>
23
24 #if HAVE_MBRTOWC
25 # include "mbuiter.h"
26 #endif
27
28 /* Find the first occurrence in the character string STRING of any character
29    not in the character string REJECT.  Return the number of bytes from the
30    beginning of the string to this occurrence, or to the end of the string
31    if none exists.  */
32 size_t
33 mbsspn (const char *string, const char *reject)
34 {
35   /* Optimize two cases.  */
36   if (reject[0] == '\0')
37     return 0;
38   if (reject[1] == '\0')
39     {
40       unsigned char uc = (unsigned char) reject[0];
41
42 #if HAVE_MBRTOWC
43       if (MB_CUR_MAX > 1)
44         {
45           mbui_iterator_t iter;
46
47           for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
48             if (!(mb_len (mbui_cur (iter)) == 1
49                   && (unsigned char) * mbui_cur_ptr (iter) == uc))
50               break;
51           return mbui_cur_ptr (iter) - string;
52         }
53       else
54 #endif
55         {
56           const char *ptr;
57
58           for (ptr = string; *ptr != '\0'; ptr++)
59             if ((unsigned char) *ptr != uc)
60               break;
61           return ptr - string;
62         }
63     }
64   /* General case.  */
65 #if HAVE_MBRTOWC
66   if (MB_CUR_MAX > 1)
67     {
68       mbui_iterator_t iter;
69
70       for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
71         {
72           if (mb_len (mbui_cur (iter)) == 1)
73             {
74               if (mbschr (reject, * mbui_cur_ptr (iter)) == NULL)
75                 goto found;
76             }
77           else
78             {
79               mbui_iterator_t aiter;
80
81               for (mbui_init (aiter, reject);; mbui_advance (aiter))
82                 {
83                   if (!mbui_avail (aiter))
84                     goto found;
85                   if (mb_equal (mbui_cur (aiter), mbui_cur (iter)))
86                     break;
87                 }
88             }
89         }
90      found:
91       return mbui_cur_ptr (iter) - string;
92     }
93   else
94 #endif
95     return strspn (string, reject);
96 }