On 14/07/2023 07:54, Jim Meyering wrote:
Small suggested improvement:

Looks good thanks.
Testing with the attached shows a 23% improvement on average
with using strcspn() over strchr()x3.

thanks,
Pádraig.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

__attribute__ ((__pure__))
static bool
problematic_chars (char const *s)
{
#if 0
   return strchr (s, '\\') || strchr (s, '\n') || strchr (s, '\r');
#else
  size_t length = strcspn (s, "\\\n\r");
  return s[length] != '\0';
#endif
}

int main(void)
{
  int ret=0;
  char str[10] = ".........";
  int len = strlen(str);
  for (int i=0; i<50000000; i++)
    {
      *str='_'; ret |= problematic_chars(str);
      *str='\\'; ret |= problematic_chars(str);
      *str='_'; str[len-1]='\\'; ret |= problematic_chars(str);
      *str='_'; str[len-1]='\r'; ret |= problematic_chars(str);
      *str='\n'; str[len-1]='.'; ret |= problematic_chars(str);
    }
  return ret;
}

Reply via email to