annotate lib/mbsnlen.c @ 8968:236e8277b9b1

New module 'mbsnlen'.
author Bruno Haible <bruno@clisp.org>
date Mon, 11 Jun 2007 00:29:30 +0000
parents
children bbbbbf4cd1c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8968
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Counting the multibyte characters in a string.
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2007 Free Software Foundation, Inc.
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <string.h>
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdlib.h>
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #if HAVE_MBRTOWC
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 # include "mbiter.h"
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #endif
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 /* Return the number of multibyte characters in the character string starting
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 at STRING and ending at STRING + LEN. */
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 size_t
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 mbsnlen (const char *string, size_t len)
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #if HAVE_MBRTOWC
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 if (MB_CUR_MAX > 1)
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 {
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 size_t count;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 mbi_iterator_t iter;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 count = 0;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 for (mbi_init (iter, string, len); mbi_avail (iter); mbi_advance (iter))
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 count++;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 return count;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 }
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 else
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #endif
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 return len;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 }