Blame string/strspn.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 strspn
Packit 6c4009
#ifndef STRSPN
Packit 6c4009
# define STRSPN strspn
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Return the length of the maximum initial segment
Packit 6c4009
   of S which contains only characters in ACCEPT.  */
Packit 6c4009
size_t
Packit 6c4009
STRSPN (const char *str, const char *accept)
Packit 6c4009
{
Packit 6c4009
  if (accept[0] == '\0')
Packit 6c4009
    return 0;
Packit 6c4009
  if (__glibc_unlikely (accept[1] == '\0'))
Packit 6c4009
    {
Packit 6c4009
      const char *a = str;
Packit 6c4009
      for (; *str == *accept; str++);
Packit 6c4009
      return str - a;
Packit 6c4009
    }
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*) accept;
Packit 6c4009
  /* Different from strcspn it does not add the NULL on the table
Packit 6c4009
     so can avoid check if str[i] is NULL, since table['\0'] will
Packit 6c4009
     be 0 and thus stopping the loop check.  */
Packit 6c4009
  do
Packit 6c4009
    p[*s++] = 1;
Packit 6c4009
  while (*s);
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
      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
  } 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 : count + c2 + 2;
Packit 6c4009
}
Packit 6c4009
libc_hidden_builtin_def (strspn)