annotate lib/mbsnlen.c @ 10780:5c7a68d31801

Add support for Haiku.
author Bruno Haible <bruno@clisp.org>
date Mon, 10 Nov 2008 12:37:32 +0100
parents bbbbbf4cd1c5
children a0bbe1a6f787
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
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8968
diff changeset
5 This program is free software: you can redistribute it and/or modify
8968
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
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8968
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8968
diff changeset
8 (at your option) any later version.
8968
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
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8968
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8968
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <string.h>
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stdlib.h>
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #if HAVE_MBRTOWC
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 # include "mbiter.h"
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #endif
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 /* Return the number of multibyte characters in the character string starting
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 at STRING and ending at STRING + LEN. */
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 size_t
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 mbsnlen (const char *string, size_t len)
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 {
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #if HAVE_MBRTOWC
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 if (MB_CUR_MAX > 1)
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 size_t count;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 mbi_iterator_t iter;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 count = 0;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 for (mbi_init (iter, string, len); mbi_avail (iter); mbi_advance (iter))
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 count++;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 return count;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 }
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 else
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #endif
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 return len;
236e8277b9b1 New module 'mbsnlen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 }