Blame lib/c-strncasecmp.c

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