1 /* Compare two memory areas with possibly different lengths, case-insensitive.
2 Copyright (C) 1998-1999, 2005-2016 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009,
4 based on earlier glibc code.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "mbmemcasecmp.h"
30 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
33 mbmemcasecmp (const char *s1, size_t n1, const char *s2, size_t n2)
36 return (n1 < n2 ? -1 : n1 > n2 ? 1 : 0);
43 mbi_init (iter1, s1, n1);
44 mbi_init (iter2, s2, n2);
46 while (mbi_avail (iter1) && mbi_avail (iter2))
48 int cmp = mb_casecmp (mbi_cur (iter1), mbi_cur (iter2));
56 if (mbi_avail (iter1))
57 /* s2 terminated before s1. */
59 if (mbi_avail (iter2))
60 /* s1 terminated before s2. */
66 const unsigned char *s1_end = (const unsigned char *) (s1 + n1);
67 const unsigned char *s2_end = (const unsigned char *) (s2 + n2);
68 const unsigned char *p1 = (const unsigned char *) s1;
69 const unsigned char *p2 = (const unsigned char *) s2;
71 while (p1 < s1_end && p2 < s2_end)
73 unsigned char c1 = TOLOWER (*p1);
74 unsigned char c2 = TOLOWER (*p2);
77 if (UCHAR_MAX <= INT_MAX)
80 /* On machines where 'char' and 'int' are types of the same
81 size, the difference of two 'unsigned char' values
82 - including the sign bit - doesn't fit in an 'int'. */
83 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
89 /* s2 terminated before s1. */
92 /* s1 terminated before s2. */