xref: /dflybsd-src/contrib/grep/lib/mbslen.c (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
195b7b453SJohn Marino /* Counting the multibyte characters in a string.
2*09d4459fSDaniel Fojt    Copyright (C) 2007-2020 Free Software Foundation, Inc.
395b7b453SJohn Marino    Written by Bruno Haible <bruno@clisp.org>, 2007.
495b7b453SJohn Marino 
595b7b453SJohn Marino    This program is free software: you can redistribute it and/or modify
695b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
795b7b453SJohn Marino    the Free Software Foundation; either version 3 of the License, or
895b7b453SJohn Marino    (at your option) any later version.
995b7b453SJohn Marino 
1095b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
1195b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1295b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1395b7b453SJohn Marino    GNU General Public License for more details.
1495b7b453SJohn Marino 
1595b7b453SJohn Marino    You should have received a copy of the GNU General Public License
16*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
1795b7b453SJohn Marino 
1895b7b453SJohn Marino #include <config.h>
1995b7b453SJohn Marino 
2095b7b453SJohn Marino /* Specification.  */
2195b7b453SJohn Marino #include <string.h>
2295b7b453SJohn Marino 
2395b7b453SJohn Marino #include <stdlib.h>
2495b7b453SJohn Marino 
2595b7b453SJohn Marino #include "mbuiter.h"
2695b7b453SJohn Marino 
2795b7b453SJohn Marino /* Return the number of multibyte characters in the character string STRING.  */
2895b7b453SJohn Marino size_t
mbslen(const char * string)2995b7b453SJohn Marino mbslen (const char *string)
3095b7b453SJohn Marino {
3195b7b453SJohn Marino   if (MB_CUR_MAX > 1)
3295b7b453SJohn Marino     {
3395b7b453SJohn Marino       size_t count;
3495b7b453SJohn Marino       mbui_iterator_t iter;
3595b7b453SJohn Marino 
3695b7b453SJohn Marino       count = 0;
3795b7b453SJohn Marino       for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
3895b7b453SJohn Marino         count++;
3995b7b453SJohn Marino 
4095b7b453SJohn Marino       return count;
4195b7b453SJohn Marino     }
4295b7b453SJohn Marino   else
4395b7b453SJohn Marino     return strlen (string);
4495b7b453SJohn Marino }
45