Blame lib/c-strncasecmp.c

Packit 8f70b4
/* c-strncasecmp.c -- case insensitive string comparator in C locale
Packit 8f70b4
   Copyright (C) 1998-1999, 2005-2006, 2009-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software; you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3, or (at your option)
Packit 8f70b4
   any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include "c-strcase.h"
Packit 8f70b4
Packit 8f70b4
#include <limits.h>
Packit 8f70b4
Packit 8f70b4
#include "c-ctype.h"
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
c_strncasecmp (const char *s1, const char *s2, size_t n)
Packit 8f70b4
{
Packit 8f70b4
  register const unsigned char *p1 = (const unsigned char *) s1;
Packit 8f70b4
  register const unsigned char *p2 = (const unsigned char *) s2;
Packit 8f70b4
  unsigned char c1, c2;
Packit 8f70b4
Packit 8f70b4
  if (p1 == p2 || n == 0)
Packit 8f70b4
    return 0;
Packit 8f70b4
Packit 8f70b4
  do
Packit 8f70b4
    {
Packit 8f70b4
      c1 = c_tolower (*p1);
Packit 8f70b4
      c2 = c_tolower (*p2);
Packit 8f70b4
Packit 8f70b4
      if (--n == 0 || c1 == '\0')
Packit 8f70b4
        break;
Packit 8f70b4
Packit 8f70b4
      ++p1;
Packit 8f70b4
      ++p2;
Packit 8f70b4
    }
Packit 8f70b4
  while (c1 == c2);
Packit 8f70b4
Packit 8f70b4
  if (UCHAR_MAX <= INT_MAX)
Packit 8f70b4
    return c1 - c2;
Packit 8f70b4
  else
Packit 8f70b4
    /* On machines where 'char' and 'int' are types of the same size, the
Packit 8f70b4
       difference of two 'unsigned char' values - including the sign bit -
Packit 8f70b4
       doesn't fit in an 'int'.  */
Packit 8f70b4
    return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
Packit 8f70b4
}