annotate lib/c-strncasecmp.c @ 7304:1c4ed7637c24

Include <config.h> unconditionally.
author Bruno Haible <bruno@clisp.org>
date Thu, 14 Sep 2006 14:18:36 +0000
parents dbf2af5391cd
children e8d2c6fc33ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6359
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* c-strncasecmp.c -- case insensitive string comparator in C locale
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6359
diff changeset
2 Copyright (C) 1998-1999, 2005-2006 Free Software Foundation, Inc.
6359
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program; if not, write to the Free Software Foundation,
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6359
diff changeset
18 #include <config.h>
6359
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "c-strcase.h"
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <limits.h>
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "c-ctype.h"
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 int
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 c_strncasecmp (const char *s1, const char *s2, size_t n)
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 {
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 register const unsigned char *p1 = (const unsigned char *) s1;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 register const unsigned char *p2 = (const unsigned char *) s2;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 unsigned char c1, c2;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 if (p1 == p2 || n == 0)
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 return 0;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 do
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 {
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 c1 = c_tolower (*p1);
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 c2 = c_tolower (*p2);
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 if (--n == 0 || c1 == '\0')
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 break;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 ++p1;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 ++p2;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 }
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 while (c1 == c2);
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 if (UCHAR_MAX <= INT_MAX)
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 return c1 - c2;
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 else
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 /* On machines where 'char' and 'int' are types of the same size, the
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 difference of two 'unsigned char' values - including the sign bit -
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 doesn't fit in an 'int'. */
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
dbf2af5391cd New module 'c-strcase'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 }