hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame string/strcspn.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <libc-pointer-arith.h>
Packit 6c4009
Packit 6c4009
#undef strcspn
Packit 6c4009
Packit 6c4009
#ifndef STRCSPN
Packit 6c4009
# define STRCSPN strcspn
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Return the length of the maximum initial segment of S
Packit 6c4009
   which contains no characters from REJECT.  */
Packit 6c4009
size_t
Packit 6c4009
STRCSPN (const char *str, const char *reject)
Packit 6c4009
{
Packit 6c4009
  if (__glibc_unlikely (reject[0] == '\0') ||
Packit 6c4009
      __glibc_unlikely (reject[1] == '\0'))
Packit 6c4009
    return __strchrnul (str, reject [0]) - str;
Packit 6c4009
Packit 6c4009
  /* Use multiple small memsets to enable inlining on most targets.  */
Packit 6c4009
  unsigned char table[256];
Packit 6c4009
  unsigned char *p = memset (table, 0, 64);
Packit 6c4009
  memset (p + 64, 0, 64);
Packit 6c4009
  memset (p + 128, 0, 64);
Packit 6c4009
  memset (p + 192, 0, 64);
Packit 6c4009
Packit 6c4009
  unsigned char *s = (unsigned char*) reject;
Packit 6c4009
  unsigned char tmp;
Packit 6c4009
  do
Packit 6c4009
    p[tmp = *s++] = 1;
Packit 6c4009
  while (tmp);
Packit 6c4009
Packit 6c4009
  s = (unsigned char*) str;
Packit 6c4009
  if (p[s[0]]) return 0;
Packit 6c4009
  if (p[s[1]]) return 1;
Packit 6c4009
  if (p[s[2]]) return 2;
Packit 6c4009
  if (p[s[3]]) return 3;
Packit 6c4009
Packit 6c4009
  s = (unsigned char *) PTR_ALIGN_DOWN (s, 4);
Packit 6c4009
Packit 6c4009
  unsigned int c0, c1, c2, c3;
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      s += 4;
Packit 6c4009
      c0 = p[s[0]];
Packit 6c4009
      c1 = p[s[1]];
Packit 6c4009
      c2 = p[s[2]];
Packit 6c4009
      c3 = p[s[3]];
Packit 6c4009
    }
Packit 6c4009
  while ((c0 | c1 | c2 | c3) == 0);
Packit 6c4009
Packit 6c4009
  size_t count = s - (unsigned char *) str;
Packit 6c4009
  return (c0 | c1) != 0 ? count - c0 + 1 : count - c2 + 3;
Packit 6c4009
}
Packit 6c4009
libc_hidden_builtin_def (strcspn)