Blame gl/strncasecmp.c

Packit 549fdc
/* strncasecmp.c -- case insensitive string comparator
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 no more than N bytes of strings S1 and S2, ignoring case,
Packit 549fdc
   returning less than, equal to or greater than zero if S1 is
Packit 549fdc
   lexicographically less than, equal to or greater than S2.
Packit 549fdc
   Note: This function cannot work correctly in multibyte locales.  */
Packit 549fdc
Packit 549fdc
int
Packit 549fdc
strncasecmp (const char *s1, const char *s2, size_t n)
Packit 549fdc
{
Packit 549fdc
  register const unsigned char *p1 = (const unsigned char *) s1;
Packit 549fdc
  register const unsigned char *p2 = (const unsigned char *) s2;
Packit 549fdc
  unsigned char c1, c2;
Packit 549fdc
Packit 549fdc
  if (p1 == p2 || n == 0)
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 (--n == 0 || 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
}