Blame gl/strcasecmp.c

Packit 549fdc
/* Case-insensitive string comparison function.
Packit 549fdc
   Copyright (C) 1998-1999, 2005-2007, 2009-2016 Free Software Foundation, Inc.
Packit 549fdc
Packit 549fdc
   This program is free software; you can redistribute it and/or modify
Packit 549fdc
   it under the terms of the GNU Lesser General Public License as published by
Packit 549fdc
   the Free Software Foundation; either version 2.1, or (at your option)
Packit 549fdc
   any later version.
Packit 549fdc
Packit 549fdc
   This program is distributed in the hope that it will be useful,
Packit 549fdc
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 549fdc
   GNU Lesser General Public License for more details.
Packit 549fdc
Packit 549fdc
   You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 549fdc
Packit 549fdc
#include <config.h>
Packit 549fdc
Packit 549fdc
/* Specification.  */
Packit 549fdc
#include <string.h>
Packit 549fdc
Packit 549fdc
#include <ctype.h>
Packit 549fdc
#include <limits.h>
Packit 549fdc
Packit 549fdc
#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
Packit 549fdc
Packit 549fdc
/* Compare strings S1 and S2, ignoring case, returning less than, equal to or
Packit 549fdc
   greater than zero if S1 is lexicographically less than, equal to or greater
Packit 549fdc
   than S2.
Packit 549fdc
   Note: This function does not work with multibyte strings!  */
Packit 549fdc
Packit 549fdc
int
Packit 549fdc
strcasecmp (const char *s1, const char *s2)
Packit 549fdc
{
Packit 549fdc
  const unsigned char *p1 = (const unsigned char *) s1;
Packit 549fdc
  const unsigned char *p2 = (const unsigned char *) s2;
Packit 549fdc
  unsigned char c1, c2;
Packit 549fdc
Packit 549fdc
  if (p1 == p2)
Packit 549fdc
    return 0;
Packit 549fdc
Packit 549fdc
  do
Packit 549fdc
    {
Packit 549fdc
      c1 = TOLOWER (*p1);
Packit 549fdc
      c2 = TOLOWER (*p2);
Packit 549fdc
Packit 549fdc
      if (c1 == '\0')
Packit 549fdc
        break;
Packit 549fdc
Packit 549fdc
      ++p1;
Packit 549fdc
      ++p2;
Packit 549fdc
    }
Packit 549fdc
  while (c1 == c2);
Packit 549fdc
Packit 549fdc
  if (UCHAR_MAX <= INT_MAX)
Packit 549fdc
    return c1 - c2;
Packit 549fdc
  else
Packit 549fdc
    /* On machines where 'char' and 'int' are types of the same size, the
Packit 549fdc
       difference of two 'unsigned char' values - including the sign bit -
Packit 549fdc
       doesn't fit in an 'int'.  */
Packit 549fdc
    return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
Packit 549fdc
}