Blame lib/memcasecmp.c

Packit 8f70b4
/* Case-insensitive buffer comparator.
Packit 8f70b4
   Copyright (C) 1996-1997, 2000, 2003, 2006, 2009-2018 Free Software
Packit 8f70b4
   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 of the License, or
Packit 8f70b4
   (at your option) 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
/* Written by Jim Meyering.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
#include "memcasecmp.h"
Packit 8f70b4
Packit 8f70b4
#include <ctype.h>
Packit 8f70b4
#include <limits.h>
Packit 8f70b4
Packit 8f70b4
/* Like memcmp, but ignore differences in case.
Packit 8f70b4
   Convert to upper case (not lower) before comparing so that
Packit 8f70b4
   join -i works with sort -f.  */
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
memcasecmp (const void *vs1, const void *vs2, size_t n)
Packit 8f70b4
{
Packit 8f70b4
  size_t i;
Packit 8f70b4
  char const *s1 = vs1;
Packit 8f70b4
  char const *s2 = vs2;
Packit 8f70b4
  for (i = 0; i < n; i++)
Packit 8f70b4
    {
Packit 8f70b4
      unsigned char u1 = s1[i];
Packit 8f70b4
      unsigned char u2 = s2[i];
Packit 8f70b4
      int U1 = toupper (u1);
Packit 8f70b4
      int U2 = toupper (u2);
Packit 8f70b4
      int diff = (UCHAR_MAX <= INT_MAX ? U1 - U2
Packit 8f70b4
                  : U1 < U2 ? -1 : U2 < U1);
Packit 8f70b4
      if (diff)
Packit 8f70b4
        return diff;
Packit 8f70b4
    }
Packit 8f70b4
  return 0;
Packit 8f70b4
}